What This Assignment Is About: Implementing classes Understanding and accessing
ID: 3804054 • Letter: W
Question
What This Assignment Is About: Implementing classes Understanding and accessing instance variables Implementing methods Object construction Constructors Encapsulation Use the following Coding Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, gross Pay, etc). User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent Use white space to make your program more readable. Part 2: (There is no part in this assignment Programming 20 points) Your assignment is to create a file called Customerjava containing a class Customer (there is no main method in this class). ACustomer 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 S8.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. (fyour class does not contain any of the following methods, points will be deducted.)Explanation / Answer
As Assignment5.java is not shared I cannot test the driver. BUt this code will work.
import java.text.NumberFormat;
public class Customer {
public Customer(String lname, String fname, int id, int largeDrinks, int smallDrinks) {
this.firstName = fname;
this.lastName = lname;
this.customerId = id;
this.largeDrinks = largeDrinks;
this.smallDrinks = smallDrinks;
computeTotalCost();
}
public Customer() {
this.firstName = "???";
this.lastName = "???";
this.customerId = 0;
this.largeDrinks = 0;
this.smallDrinks = 0;
computeTotalCost();
}
// fields for storing customer info.
private String firstName;
private String lastName;
private int customerId;
private int largeDrinks;
private int smallDrinks;
private double totalCost;
// constants for cost of drinks
private double LARGE_DRINK_COST = 8.5;
private double SMALL_DRINK_COST = 4.5;
/**
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
*
* @param firstName first name to be set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
*
* @return last name
*/
public String getLastName() {
return lastName;
}
/**
*
* @param lastName last name to be set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
*
* @return customer id
*/
public int getCustomerId() {
return customerId;
}
/**
*
* @param id customer id to be set
*/
public void setCustomerId(int id) {
this.customerId = id;
}
/**
*
* @return number of large drinks
*/
public int getLargeDrinks() {
return largeDrinks;
}
/**
*
* @param largeDrinks number of large drinks to be set
*/
public void setLargeDrinks(int largeDrinks) {
this.largeDrinks = largeDrinks;
computeTotalCost();
}
/**
*
* @return number of small drinks
*/
public int getSmallDrinks() {
return smallDrinks;
}
/**
*
* @param smallDrinks number of small drinks to be set
*/
public void setSmallDrinks(int smallDrinks) {
this.smallDrinks = smallDrinks;
computeTotalCost();
}
/**
*
* @return total cost of the drinks
*/
public double getTotalCost() {
return totalCost;
}
/**
* Computes total cost and set it in total cost.
*/
private void computeTotalCost()
{
this.totalCost = getLargeDrinks()*LARGE_DRINK_COST + getSmallDrinks()*SMALL_DRINK_COST;
}
public Customer hasMore(Customer other)
{
if (this.equals(other))
{
return this;
}
if (this.getTotalCost() < other.getTotalCost())
{
return other;
}
return this;
}
@Override
public String toString() {
return "First name: " + firstName + " " +
"Last name: " + lastName + " " +
"Customer ID: " + customerId + " " +
"Number of Large Drink(s): " + largeDrinks + " "+
"Number of Small Drink(s): " + smallDrinks + " " +
"Total cost: " + NumberFormat.getCurrencyInstance().format(totalCost);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + customerId;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (customerId != other.customerId)
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.