JAVA Write a Customer class that contains the following fields: - Name - Custome
ID: 3757142 • Letter: J
Question
JAVA
Write a Customer class that contains the following fields: - Name - Customer ID - Address - Phone Number The class should have two constructors: -
A no-arg constructor that sets the fields to empty strings ("") -
A constructor that accepts the four fields as arguments and assigns them to their appropriate fields in the class Write appropriate mutator methods to store values in the fields and accessor methods to return the values in the fields.
Write a separate program that creates three Customer objects by entering the fields for each object from the keyboard.
Explanation / Answer
import java.util.Scanner; class Customer { private String name; private String customerId; private String address; private String phoneNumber; public Customer() { } public Customer(String name, String customerId, String address, String phoneNumber) { this.name = name; this.customerId = customerId; this.address = address; this.phoneNumber = phoneNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } } class CustomerTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); Customer customer1 = new Customer(); System.out.println("Enter customer 1 details"); System.out.print("Name: "); customer1.setName(in.nextLine()); System.out.print("ID: "); customer1.setCustomerId(in.nextLine()); System.out.print("Address: "); customer1.setAddress(in.nextLine()); System.out.print("Phone number: "); customer1.setPhoneNumber(in.nextLine()); Customer customer2 = new Customer(); System.out.println("Enter customer 2 details"); System.out.print("Name: "); customer2.setName(in.nextLine()); System.out.print("ID: "); customer2.setCustomerId(in.nextLine()); System.out.print("Address: "); customer2.setAddress(in.nextLine()); System.out.print("Phone number: "); customer2.setPhoneNumber(in.nextLine()); String name, id, phone, address; System.out.println("Enter customer 3 details"); System.out.print("Name: "); name = in.nextLine(); System.out.print("ID: "); id = in.nextLine(); System.out.print("Address: "); address = in.nextLine(); System.out.print("Phone number: "); phone = in.nextLine(); Customer customer3 = new Customer(name, id, address, phone); System.out.printf("Customer 1: Name: %s, Id: %s, Address: %s, Phone number: %s ", customer1.getName(), customer1.getCustomerId(), customer1.getAddress(), customer1.getPhoneNumber()); System.out.printf("Customer 2: Name: %s, Id: %s, Address: %s, Phone number: %s ", customer2.getName(), customer2.getCustomerId(), customer2.getAddress(), customer2.getPhoneNumber()); System.out.printf("Customer 3: Name: %s, Id: %s, Address: %s, Phone number: %s ", customer3.getName(), customer3.getCustomerId(), customer3.getAddress(), customer3.getPhoneNumber()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.