(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
public class HelloWorld{
public class Customer
{
private String firstName;
private String lastName;
private int customerNumber;
private String emailAddress;
/* default constructor
public Customer()
{
firstName = "";
lastName = "";
customerNumber = -1;
emailAddress = "";
}*/
//constructor with parameter
public Customer(String firstName, String lastName, int
customerNumber, String emailAddress)
{
this.firstName = firstName;
this.lastName = lastName;
this.customerNumber = customerNumber;
this.emailAddress = emailAddress;
}
//accessor for firstName
public String getfirstName()
{
return firstName;
}
//Mutator for firstName
public void setfirstName( String fname )
{
firstName = fname;
}
//accessor for lastName
public String getlastName()
{
return lastName;
}
//Mutator for lastName
public void setlastName( String lname )
{
firstName = lname;
}
//accessor for customerNumber
public int getcustomerNumber()
{
return customerNumber;
}
//Mutator for customerNumber
public void setcustomerNumber( int num )
{
customerNumber = num;
}
//accessor for emailAddress
public String getemailAddress()
{
return emailAddress;
}
//Mutator for emailAddress
public void setemailAddress( String add )
{
emailAddress = add;
}
@Override
public boolean equals(Object c){
if(c == null) return false;
if(!(c instanceof Customer)) return false;
Customer other = (Customer) c;
if(this.customerNumber != other.customerNumber) return false;
if(!this.firstName.equals(other.firstName)) return false;
if(! this.lastName.equals(other.lastName)) return false;
if(! this.emailAddress.equals(other.emailAddress)) return
false;
return true;
}
@Override
public String toString(){
String r = getcustomerNumber() + " " + getfirstName() + " " +
getlastName()+ " " + getemailAddress();
return r;
}
}
public static void main(String []args){
Customer cus = new Customer("A","B",123,"[email protected]");
//cus.setfirstName("A");
//cus.setlastName("B");
//cus.setcustomerNumber(125331);
//cus.emailAddress("[email protected]");
System.out.println(cus.toString(cus));
}
}
Get Answers For Free
Most questions answered within 1 hours.