Customer.java: // Customer class - allows objects from this class to compare bet
ID: 3878543 • Letter: C
Question
Customer.java:
// Customer class - allows objects from this class to compare between
// themselves to see which Customer object has higher networth. Also
// allow to evaluate each customer's credit rating.
//
import java.text.DecimalFormat;
public class Customer implements Comparable, NetWorthEvaluator
{
//class constants
public static final int UNKNOWN = 99;
public static final int MALE = 1;
public static final int FEMALE = 2;
//member data
private String firstName;
private String lastName;
private int gender;
private double networth;
//default constructor
public Customer ()
{
firstName = "";
lastName ="";
gender =UNKNOWN;
networth=0.0;
}
//another constructor
public Customer (String fname, String lname,int gender, double networth)
{
firstName = fname;
lastName = lname;
this.gender = gender;
this.networth = networth;
}
//Accessors (Getter methods)
public String getFirstName ()
{
return firstName;
}
public String getLastName ()
{
return lastName;
}
public int getGender ()
{
return gender;
}
public double getNetWorth()
{
return networth;
}
//Mutators (Setter methods)
public void setFirstName (String fname)
{
if (fname.length() < 1)
{
System.out.println("Bad first name found in 'setFirstName' method");
System.out.println("No change to the firstName field of the object!");
}
else
firstName =fname;
}
public void setLastName (String lname)
{
if (lname.length() < 1)
{
System.out.println("Bad last name found in 'setLastName' method");
System.out.println("No change to the lastName field of the object!");
}
else
lastName =lname;
}
public void setGender (int gender)
{
if ((gender != MALE) && (gender != FEMALE) && (gender != UNKNOWN))
{
System.out.println("Invalid gender value found in 'setGender' method");
System.out.println("No change to the gender field of the object!");
}
else
this.gender = gender;
}
public void setNetWorth(double asset)
{
networth = asset;
}
//ToString method
public String toString()
{
String sex;
DecimalFormat fmt = new DecimalFormat("$#,###.00");
if (gender == MALE)
sex ="Male";
else
if (gender == FEMALE)
sex ="Female";
else
sex = "Unknown";
// modify this statement below to include the credit rating
return "Name: " + firstName + " " + lastName + " "+
"Gender: " + sex + " " + "Net Worth: " + fmt.format(networth) + " ";
}
//Implement compareTo method from Comparable interface here
-
-
// Implement checkNetWorth method from NetWorthEvaluator interface here
-
-
}
CustomerTester.java (test the program)
// File: CustomerTester.java
// Purpose: A test harness program, to instantiate and use objects of
// the Customer class with both Comparable and NetWorthEvaluator
// interfaces implemented.
//
import java.util.Scanner;
class CustomerTester
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the net worth for the first client (John): ");
double networth1 = scan.nextDouble();
System.out.print("Please enter the net worth for the second client (Peter): ");
double networth2 = scan.nextDouble();
Customer client1 = new Customer ("John", "Smith", Customer.MALE, networth1);
Customer client2 = new Customer ("Peter", "McMann", Customer.MALE, networth2);
if (client1.compareTo (client2) > 0)
System.out.println (client1.getFirstName() + " " + client1.getLastName() +
" has higher net worth than " + client2.getFirstName() + " " +
client2.getLastName());
else if (client1.compareTo (client2) < 0)
System.out.println (client2.getFirstName() + " " + client2.getLastName() +
" has higher net worth than " + client1.getFirstName() + " " +
client1.getLastName());
else
System.out.println (client2.getFirstName() + " " + client2.getLastName() +
" has the same amount of net worth as " + client1.getFirstName() + " " +
client1.getLastName());
System.out.println(" ");
System.out.println("Credit ratings of both customers are as follow:");
System.out.println(client1 + " " + client2);
}
}
1, 185%) You will be given a class file named Customer (available from our Moodle course page), and you need to implement two interfaces in the Customer class The first interface is called Comparable, which is already defined in Java's API. The only abstract method you need to implement from the Comparable interface is the compareTo) method. Please check the header definition of the compareTo0 from Java's API first before you implement it. The second interface is what you need to create for yourself, and let's call it NetWorthEvaluator. This interface contains only one public abstract method named checkNetWorth0 which takes no parameters but will return a String when implemented. Create the interface, save it and compile it. This interface is to be implemented by your Customer class. Now modify the Customer class to implement both Comparable and NetWorthEvaluator interfaces. When you implement the compareTo0 method from the Comparable interface, compare the networth instance variable of the two Customer objects to determine which Customer object is greater than, less than or equal to the other Customer object. When you implement the checkNetWorth) method from the NetWorthEvaluator interface, you check the networth instance variable to determine what string message it should return (see below) networthExplanation / Answer
//Test java program that prompts user to enter net worth of two customers
//and print the result of credit score.
//CustomerTester.java
import java.util.Scanner;
class CustomerTester
{
public static void main (String args[])
{
//create an instance of Scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the net worth for the first client (John): ");
double networth1 = scan.nextDouble();
System.out.print("Please enter the net worth for the second client (Peter): ");
double networth2 = scan.nextDouble();
//create an instance of Customer constructor with networth1
Customer client1 = new Customer ("John", "Smith", Customer.MALE, networth1);
//create an instance of Customer constructor with networth2
Customer client2 = new Customer ("Peter", "McMann", Customer.MALE, networth2);
//calling compareTo method on client1 and client2
if (client1.compareTo (client2) > 0)
System.out.println (client1.getFirstName() + " " + client1.getLastName() +
" has higher net worth than " + client2.getFirstName() + " "
+client2.getLastName());
else if (client1.compareTo (client2) < 0)
System.out.println (client2.getFirstName() + " " + client2.getLastName() +
" has higher net worth than " + client1.getFirstName() + " "
+client1.getLastName());
else
System.out.println (client2.getFirstName() + " " + client2.getLastName() +
" has the same amount of net worth as " + client1.getFirstName() + " " +
client1.getLastName());
System.out.println(" ");
System.out.println("Credit ratings of both customers are as follow:");
System.out.println(client1 + " " + client2);
}
}
---------------------------------------------------------------------------------
//NetWorthEvaluator.java
public interface NetWorthEvaluator {
//method to implenet checkNetWorth
public String checkNetWorth();
}
---------------------------------------------------------------------------------
import java.text.DecimalFormat;
public class Customer implements Comparable, NetWorthEvaluator
{
//class constants
public static final int UNKNOWN = 99;
public static final int MALE = 1;
public static final int FEMALE = 2;
//member data
private String firstName;
private String lastName;
private int gender;
private double networth;
//default constructor
public Customer ()
{
firstName = "";
lastName ="";
gender =UNKNOWN;
networth=0.0;
}
//another constructor
public Customer (String fname, String lname,int gender, double networth)
{
firstName = fname;
lastName = lname;
this.gender = gender;
this.networth = networth;
}
//Accessors (Getter methods)
public String getFirstName ()
{
return firstName;
}
public String getLastName ()
{
return lastName;
}
public int getGender ()
{
return gender;
}
public double getNetWorth()
{
return networth;
}
//Mutators (Setter methods)
public void setFirstName (String fname)
{
if (fname.length() < 1)
{
System.out.println("Bad first name found in 'setFirstName' method");
System.out.println("No change to the firstName field of the object!");
}
else
firstName =fname;
}
public void setLastName (String lname)
{
if (lname.length() < 1)
{
System.out.println("Bad last name found in 'setLastName' method");
System.out.println("No change to the lastName field of the object!");
}
else
lastName =lname;
}
public void setGender (int gender)
{
if ((gender != MALE) && (gender != FEMALE) && (gender != UNKNOWN))
{
System.out.println("Invalid gender value found in 'setGender' method");
System.out.println("No change to the gender field of the object!");
}
else
this.gender = gender;
}
public void setNetWorth(double asset)
{
networth = asset;
}
//ToString method
public String toString()
{
String sex;
DecimalFormat fmt = new DecimalFormat("$#,###.00");
if (gender == MALE)
sex ="Male";
else
if (gender == FEMALE)
sex ="Female";
else
sex = "Unknown";
// modify this statement below to include the credit rating
return "Name: " + firstName + " " + lastName + " "+
"Gender: " + sex + " " + "Net Worth: " +
fmt.format(networth) +" "+ checkNetWorth()+" ";
}
//Override the compareTo method
public int compareTo(Object o)
{
Customer other=(Customer)o;
if(networth<other.getNetWorth())
return -1;
else if(networth>other.getNetWorth())
return 1;
else
return 0;
}
// Implement checkNetWorth method from NetWorthEvaluator interface here
//Override the checkNetWorth method
public String checkNetWorth()
{
if(networth<0)
return "Negative Net Worth-Extermely Poor Credit Rating.";
else if(networth>=0 && networth<10000)
return "Net Worth Is Well Below Average - Very Poor Credit Rating";
else if(networth>=10000 && networth<50000)
return "Net Worth Is Below Average- Poor Credit Rating";
else if(networth>=50000 && networth<100000)
return "Average Net Worth- Acceptable Credit Rating";
else if(networth>=100000 && networth<200000)
return "Above Average Net Worth-Good Credit Rating";
else if(networth>=200000 && networth<1000000)
return "Well Above Average Net Worth-Very Good Credit Rating";
else
return "Net Worth Is Over One Million Dollars-Excellent Credit Rating.";
}
}//end of the class
---------------------------------------------------------------------------------
Sample Output:
Please enter the net worth for the first client (John): -15000
Please enter the net worth for the second client (Peter): 10000
Peter McMann has higher net worth than John Smith
Credit ratings of both customers are as follow:
Name: John Smith
Gender: Male
Net Worth: -$15,000.00
Negative Net Worth-Extermely Poor Credit Rating.
Name: Peter McMann
Gender: Male
Net Worth: $10,000.00
Net Worth Is Below Average- Poor Credit Rating
sample output2:
Please enter the net worth for the first client (John): 66000
Please enter the net worth for the second client (Peter): 66000
Peter McMann has the same amount of net worth as John Smith
Credit ratings of both customers are as follow:
Name: John Smith
Gender: Male
Net Worth: $66,000.00
Average Net Worth- Acceptable Credit Rating
Name: Peter McMann
Gender: Male
Net Worth: $66,000.00
Average Net Worth- Acceptable Credit Rating
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.