Question: Hi there! I really need help with my Java assigment. The main class is posted at the bottom and I have to write the Customer
Hi there! I really need help with my Java assigment.
The main class is posted at the bottom and I have to write the Customer class to work with the included code.
I'm not getting the second customer's input to pop up, my total cost output is 0.0, customer with higher cost isnt working but that might be because I cant enter info for the first customer, and I don't know how to use the formatter class. Here is all the info! -Thank you in advance.
Your assignment is to create a file called Customer.java containing a class Customer (there is no main method in this class). A Customer has a first name (a String), last name (String), ID number (integer), number of larger drinks (integer), number of small drinks (integer), and total cost (double). Each large drink costs $8.50 and each small drink costs $4.50. You need to compute the total cost based on the number of large drinks and small drinks.
The class Customer must include the following constructors and methods. (If your class does not contain any of the following methods, points will be deducted.)
| Method | Description of the Method |
| public Customer( ) | Default constructor sets the first name and last name to "???", customer ID, the number of large drinks, and the number of small drinks to 0, and the total cost to 0.0. |
| public Customer (String lname, String fname, int id, int largeDrinks, intsmallDrinks) | Constructs a Customer object given the last name, first name, customer id, the number of large drinks, the number of small drinks. |
| public String getFirstName( ) | Returns the first name. |
| public String getLastName() | Returns the last name. |
| public int getCustomerID() | Return the customer ID. |
| public int getLargeDrinks( ) | Returns the number of large drinks |
| public int getSmallDrinks( ) | Returns the number of small drinks. |
| public double getTotalCost() | Returns the total cost. |
| public void setFirstName (String fname) | Sets the first name. |
| public void setLastName (String lname) | Sets the last name. |
| public void setCustomerID (int id) | Sets the id. |
| public void setLargeDrinks (intlargeNum) | Sets the number of large drinks. It should also call computeTotalCost() method to update its total cost. |
| public void setSmallDrinks (intsmallNum) | Sets the number of small drinks. It should also call computeTotalCost() method to update its total cost. |
| public boolean equals (Customer other) | Returns true if the ids and names are the same, return false otherwise. |
| private void computeTotalCost( ) | This is a helper (service) method that computes the total cost based on the number of large drinks and small drinks (Each large drink costs $8.50 and each small drink costs $4.50). This method will be called by other methods inside of the class Customer anytime the number of large drinks or small drinks is updated. |
| public Customer hasMore (Customer other) | Compares the total costs of two customers and returns the Customer who has a higher total cost. If the total costs are same, the first customer (itself, i.e., thisreference) is returned. |
| public String toString( ) | Returns a String with information on each Customer using the following format: First name: Bill Last name: Gates Customer ID: 888777888 Number of Large Drink(s): 0 Number of Small Drink(s): 50 Total cost: $375.00 Make use of the NumberFormat class to format the total cost. |
Save the Customer class in a file called Customer.java and use the following program, which has the main method to create new Customer objects and to test your class. A sample output is shown below also.
Important Notes: Your class should have exactly the method headers that are described or otherwise your class will not work with the test driver program (Assignment5.java) that is provided. You should never change the test driver program if the test driver is provided but instead make changes to Customer class to make it work.
Helpful hints for doing this assignment:
work on it in steps. Write one method, test it with a test driver and make sure it works before going on to the next method
always make sure your code compiles before you add another method
your methods should be able to be called in any order
After compiling Customer.java file and Assignment5.java file, you need to execute Assignment5 class.
Grading Criteria
05 pts: Each file (Customer.java file and Assignment5.java file) contains header information (the assignment number, your name, a description of each file.
10 pts: adequate comment to explain every method
05 pts: consistent indentation and spacing
10 pts: it compiles
15 pts total The instance variables are declared correctly (lastName, firstName, customerID,
largeDrinks, smallDrinks, totalCost)
05 pt: The Constructor, Customer( ) is correct
05 pt: The Constructor, Customer(String, String, int, int, int) is correct
12.5 pts total (0.5 pt each): The modifier methods
(setLastName, setFirstName, setCustomerID, setLargeDrinks, setSmallDrinks) are correct
12.5 pts total (0.5 pt each): The accessor methods
(getLastName, getFirstName, getCustomerID, getLargeDrinks, getSmallDrinks) are correct
05 pt: The method computeTotalCost( ) is correct
05 pt: The method equals(Customer) is correct
05 pt: The method hasMore(Customer) is correct
05 pt: The method toString() is correct including the dollar formatting
Sample Output: (the inputs entered by a user are shown in bold)
******** Print the default value of customer's info: **********
First name: ??? Last name: ??? Customer ID: 0 Number of Large Drink(s): 0 Number of Small Drink(s): 0 Total cost: $0.00
******** Print the first Customer's info: **********
First name: Bill Last name: Gates Customer ID: 1234567890 Number of Large Drink(s): 50 Number of Small Drink(s): 0 Total cost: $425.00
Enter customer's info: First Name: Steve Last Name:
Jobs
ID #:
100000000
Number of Large Drinks:
234
Number of Small Drinks:
4
******** Print the second Customer's info using get methods:********
customer2's First Name: Steve customer2's Last Name: Jobs customer2's Customer ID: 100000000 customer2's Large Drink(s): 234 customer2's Small Drink(s): 4 customer2's Total Cost: 2007.0 ******** Print the second Customer's info: **********
First name: Steve Last name: Jobs Customer ID: 100000000 Number of Large Drink(s): 234 Number of Small Drink(s): 4 Total cost: $2,007.00
********Print the customer who has higher total cost:*****
First name: Steve Last name: Jobs
Customer ID: 100000000 Number of Large Drink(s): 234 Number of Small Drink(s): 4 Total cost: $2,007.00
********Customer comparison****** customer1 and customer2 are different.
Enter customer's info: First Name: Bill Last Name:
Gates
ID #:
1234567890
Number of Large Drinks:
0
Number of Small Drinks:
50
******** Print the second Customer's info using get methods:********
customer2's First Name: Bill customer2's Last Name: Gates customer2's Customer ID: 1234567890 customer2's Large Drink(s): 0 customer2's Small Drink(s): 50 customer2's Total Cost: 225.0 ******** Print the second Customer's info: **********
First name: Bill Last name: Gates Customer ID: 1234567890 Number of Large Drink(s): 0 Number of Small Drink(s): 50 Total cost: $225.00
********Print the customer who has higher total cost:*****
First name: Bill Last name: Gates Customer ID: 1234567890 Number of Large Drink(s): 50 Number of Small Drink(s): 0 Total cost: $425.00
********Customer comparison******
customer1 and customer2 are the same customer.
import javax.swing.*;
public class Assignment5 { public static void main(String[] args){
// instantiate first customer using the first constructor Customer customer1 = new Customer(); System.out.println(** Print the default value of customer's info: ****"); System.out.println(customer1.toString());
// call the mutator methods to change the state of the object customer1.setFirstName("Bill"); customer1.setLastName("Gates"); customer1.setCustomerID(1234567890); customer1.setLargeDrinks(50);
System.out.println(" ******** Print the first Customer's info: **********"); System.out.println(customer1);
//test with two customers for (int i=1; i<=2; i++) {
// instantiate another customer by entering info from the keyboard System.out.println("Enter customer's info: "); String fname = JOptionPane.showInputDialog("First Name: "); String lname = JOptionPane.showInputDialog("Last Name: ");
int id = Integer.parseInt(JOptionPane.showInputDialog("ID#: ")); int largerDrinkNum = Integer.parseInt(
JOptionPane.showInputDialog("Number of Large Drinks: ")); int smallDrinkNum = Integer.parseInt(
JOptionPane.showInputDialog("Number of Small Drinks: "));
Customer customer2 = new Customer(lname, fname, id, largerDrinkNum, smallDrinkNum);
System.out.println(" ******** Print the second Customer's info using get methods:******** "); // call the accesor methods System.out.println("customer2's First Name: " + customer2.getFirstName()); System.out.println("customer2's Last Name: " + customer2.getLastName()); System.out.println("customer2's Customer ID: " + customer2.getCustomerID()); System.out.println("customer2's Large Drink(s): " + customer2.getLargeDrinks()); System.out.println("customer2's Small Drink(s): " + customer2.getSmallDrinks()); System.out.println("customer2's Total Cost: " + customer2.getTotalCost()); System.out.println(" ******** Print the second Customer's info: **********"); System.out.println(customer2.toString());
// who has more total cost System.out.println(" ********Print the customer who has higher total cost:*****"); System.out.println(customer1.hasMore(customer2));
System.out.println(" ********Customer comparison******"); // compare customer1 and customer 2 if (customer1.equals(customer2))
System.out.println("customer1 and customer2 are the same customer."); else
System.out.println("customer1 and customer2 are different.");
System.out.println();
} } // end of main
}// end of class Assignment5
This is what I have so far:
import java.text.NumberFormat;
public class Customer {
private String firstName, lastName;
private int customerID, largerDrinkNum, smallDrinkNum;
private double totalCost;
private double LARGECOST=8.5;
private double SMALLCOST=4.5;
public Customer()
{
firstName="???";
lastName="???";
customerID=0;
largerDrinkNum=0;
smallDrinkNum=0;
totalCost=0.0;
}
public Customer(String fname, String lname, int id, int largeDrinks, int smallDrinks)
{
firstName=fname;
lastName=lname;
customerID=id;
largerDrinkNum=largeDrinks;
smallDrinkNum=smallDrinks;
}
public void setFirstName(String fname)
{
firstName=fname;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lname)
{
lastName=lname;
}
public String getLastName()
{
return lastName;
}
public void setCustomerID(int id)
{
customerID=id;
}
public int getCustomerID()
{
return customerID;
}
public void setLargeDrinks(int largeNum)
{
largerDrinkNum=largeNum;
computeTotalCost();
}
public int getLargeDrinks()
{
return largerDrinkNum;
}
public void setSmallDrinks(int smallNum)
{
smallDrinkNum=smallDrinkNum;
computeTotalCost();
}
public int getSmallDrinks()
{
return smallDrinkNum;
}
public boolean equals(Customer other)
{
if(this.customerID==other.customerID && this.firstName.equals(other.firstName)==true
&& this.lastName.equals(other.lastName)==true)
return true;
else
return false;
}
private void computeTotalCost()
{
totalCost= largerDrinkNum*8.50+smallDrinkNum*4.50;
}
public double getTotalCost()
{
return totalCost;
}
public Customer hasMore (Customer other)
{
if(this.totalCost>=other.totalCost)
return this;
else
return other;
}
public String toString()
{
NumberFormat formatter;
formatter=NumberFormat.getInstance();
return " First Name: "+ this.getFirstName()+ " Last Name: "+this.getLastName()+" Customer ID: "
+ this.getCustomerID()+ " Number of Large Drinks: " + this.getLargeDrinks()
+ " Number of Small Drinks: " + this.getSmallDrinks()
+ " Total Cost: " + this.totalCost;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
