Your assignment is to create a file called Customer.java containing a class Cust
ID: 3631179 • Letter: Y
Question
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 matinee tickets (integer), number of normal tickets (integer), and total cost (double). Each matinee ticket costs $5.00 and each normal ticket costs $7.50. You need to compute the total cost based on the number of matinee tickets and normal tickets.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.)
public Customer( )
Default constructor sets the first name and last name to "???", customer ID, the number of matinee tickets, and the number of normal tickets to 0, and the total cost to 0.0.
public Customer (String lname, String fname, int id, int matineeTickets, int normalTickets):Constructs a Customer object given the last name, first name, customer id, the number of matinee tickets, the number of normal tickets. Its total cost should be updated by calling computeTotalCost( ) method.
public String getFirstName( )
Returns the first name.
public String getLastName()
Returns the last name.
public int getCustomerID()
Return the customer ID.
public int getMatineeTickets( )
Returns the number of matinee tickets
public int getNormalTickets( )
Returns the number of normal tickets.
public double getTotalCost()
Returns the total cost.
public void setFirstName (String fname)
Sets the first name.
public void setLastName (String fname)
Sets the last name.
public void setCustomerID(int id)
Sets the id.
public void setMatineeTickets(int matineeNum)
Sets the number of matinee tickets. Its total cost should be updated by calling computTotalCost( ) method.
public void setNormalTickets(int normalNum)
Sets the number of normal tickets. Its total cost should be updated by calling computeTotalCost( ) method.
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 matinee tickets and normal tickets (Each matinee ticket costs $5.00 and each normal ticket costs $7.50). This method will be called by other methods inside of the class Customer anytime the number of matinee tickets or normal tickets 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) 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 Matinee Ticket(s): 0
Number of Normal Ticket(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 stored in Assignment5.java, which has the main method to create new Customer objects and to test your class.
/********************************************************************
// Title: Assignment5.java
// Author: CSE110 instructor
// Description: a program to test the class Customer
//********************************************************************/
import java.util.Scanner;
public class Assignment5
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
// 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(888777888);
customer1.setNormalTickets(50);
System.out.println(" ******** Print the first Customer's info: **********");
System.out.println(customer1);
//test with three customers
for (int i=1; i<=3; i++)
{
// instantiate another customer by entering info from the keyboard
System.out.println("Enter customer's info: ");
System.out.println("First Name: ");
String fname = scan.next();
System.out.println("Last Name: ");
String lname = scan.next();
System.out.println("ID #: ");
int id = scan.nextInt();
System.out.println("Number of Matinee Tickets: ");
int matineeTicketNum = scan.nextInt();
System.out.println("Number of Normal Tickets: ");
int normalTicketNum = scan.nextInt();
Customer customer2 = new Customer(lname, fname, id, matineeTicketNum, normalTicketNum);
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 matinee ticket(s): " + customer2.getMatineeTickets());
System.out.println("customer2's normal ticket(s): " + customer2.getNormalTickets());
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
Explanation / Answer
package com.wrox.p2p; import java.text.NumberFormat; import java.util.Locale; import java.util.StringTokenizer; /** * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class Customer { private String firstName; private String lastName; private int idNumber; private int matineeTickets; private int normalTickets; private double totalCost; private static final int MATINEE_TICKET_PRICE = 5; private static final double NORMAL_TICKET_PRICE = 7.5; public Customer() { firstName = "???"; lastName = "???"; idNumber = 0; matineeTickets = 0; normalTickets = 0; totalCost = 0.0; } public Customer( String aFirstName, String aLastName, int anIdNumber, int aMatineeTicket, int aNormalTicket) { } public Customer(String aTokenizedCustomer) { StringTokenizer st = new StringTokenizer(aTokenizedCustomer, "/"); String firstLastName = st.nextToken(); Integer idNumberObject = new Integer(st.nextToken()); this.idNumber = idNumberObject.intValue(); Integer matineeTicketInteger = new Integer(st.nextToken()); this.matineeTickets = matineeTicketInteger.intValue(); Integer normalTicketsInteger = new Integer(st.nextToken()); this.normalTickets = normalTicketsInteger.intValue(); StringTokenizer st2 = new StringTokenizer(firstLastName); this.firstName = st2.nextToken(); this.lastName = st2.nextToken(); } public String getFirstName() { return firstName; } public void setFirstName(String aFirstName) { firstName = aFirstName; } public String getLastName() { return lastName; } public void setLastName(String aLastName) { lastName = aLastName; } public int getIdNumber() { return idNumber; } public void setIdNumber(int anIdNumber) { idNumber = anIdNumber; } public int getMatineeTickets() { return matineeTickets; } public void setMatineeTickets(int aMatineeTickets) { matineeTickets = aMatineeTickets; } public int getNormalTickets() { return normalTickets; } public void setNormalTickets(int aNormalTickets) { normalTickets = aNormalTickets; } public double getTotalCost() { double yourTotalCost = 0.0; double tempWorkingArea = 0.0; if (!(matineeTickets == 0)) { yourTotalCost += (matineeTickets * MATINEE_TICKET_PRICE); // yourTotalCost = tempWorkingArea; } if (!(normalTickets == 0)) { yourTotalCost += (normalTickets * NORMAL_TICKET_PRICE); // yourTotalCost += tempWorkingArea; } NumberFormat formatTotalCost = NumberFormat.getInstance(Locale.US); formatTotalCost.format(yourTotalCost); return yourTotalCost; } public boolean equals(Customer aCustomer) { boolean isEqual = true; if (!(this.firstName.equalsIgnoreCase(aCustomer.firstName))) { isEqual = false; } else if (!(this.lastName.equalsIgnoreCase(aCustomer.lastName))) { isEqual = false; } else if (!(this.idNumber == aCustomer.idNumber)) { isEqual = false; } return isEqual; } public String toString() { String customerString = null; String myFirstName = "First Name: " + this.getFirstName() + " "; String myLastName = "Last Name: " + this.getLastName() + " "; String myCustomerId = "Customer ID: " + this.getIdNumber() + " "; String myMatineeTickets = "Matinee Ticket(s): " + this.getMatineeTickets() + " "; String myNormalTickets = "Normal Ticket(s)" + this.getNormalTickets() + " "; Locale defaultLocale = new Locale("us", "US"); NumberFormat formatTotalCost = NumberFormat.getNumberInstance(Locale.US); NumberFormat nf = NumberFormat.getCurrencyInstance(defaultLocale); String formattedValue = nf.format(totalCost); String myTotalCost = "Total Cost: " + formattedValue + " "; customerString = myFirstName + myLastName+ myCustomerId+ myMatineeTickets + myNormalTickets+ myTotalCost; return customerString; } Hope this helps.... :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.