Please write in java; thanks. You have been hired by Millennial Mart to write a
ID: 3707696 • Letter: P
Question
Please write in java; thanks.
You have been hired by Millennial Mart to write a Java console application that processes customers through their store checkout line. The application has the following two classes: Customer.java Each object created from t following fields and methods: his class represents one customer and includes the Fields (static) totalCustomers-count of all distinct customers; initialize to 0 in declaration. (static) totalltems- total items purchased by all customers; initialize to 0 in declaration. 9 (static) totalCost-total cost of all items purchased by all customers; initialize to 0 in declaration. . ID- unique number assigned to each customer. e items-number of items in customer shopping cart. cost-cost of all items in customer shopping cart o next- pointer to next customer Methods . A constructor with no parameters that sets the fields, respectively, to these values: totalCustomers ID =-1 items =-1 cost-1 next null totalCustomers+1 . A constructor with two parameters that sets the fields, respectively, to these values total Customers- totalCustomers + 1 totalltems totaliltems + items totalCost totalCost + cost ID - set to totalCustomers items set from parameter cost set from parameter next null . Getter methods for each field (declare the getters for totalCustomers, totalltems, and totalCost static). Setter methods for each field (declare the setters for totalCustomers, totalltems, and totalCost static). equals method that compares ID for equality. . to String method for returning ID, items, and cost values only.Explanation / Answer
CODING:
/*
* This is an open source project
* Anybody can view, download this project
* Authors : Ram Prasad Gudiwada
*/
package Chegg196;
// you can this class to HW6
import java.util.Random;
// this class has the main method
public class Chegg198 {
Customer customers = null;
/**
* this method adds a customer to the queue.
* @param ptr
*/
void insertCustomer(Customer ptr) {
if(customers == null) { // if queue is null then add the customer at the beginning
customers = ptr;
} else { // else add at the end of the queue
Customer t = customers;
while(t.getNext() != null) {
t = t.getNext();
}
t.setNext(ptr);
}
}
/**
* this method removes a customer from the queue.
* @return The first Customer in the Queue
*/
Customer removeCustomer() {
if(customers == null) {
System.out.println("No customers in the Queue");
return null;
}
Customer c = customers;
customers = customers.getNext();
Customer.setTotalCost(Customer.getTotalCost()-c.getCost());
Customer.setTotalCustomers(Customer.getTotalCustomers()-1);
Customer.setTotalItems(Customer.getTotalItems()-c.getItems());
c.setNext(null);
return c;
}
/**
* this method prints any customers in the queue.
* It prints the ID, items, and cost for each customer
* formatted in three columns.
*/
void printCustomers() {
if(customers == null) {
System.out.println("No customers in the Queue");
System.out.println();
return;
}
System.out.println("Customers in the Queue Are :");
System.out.println(" ID Items Cost");
System.out.println("==== ======= ======");
Customer t = customers;
while(t != null) {
System.out.println(t);
t = t.getNext();
}
System.out.println();
}
// main method
public static void main(String... args ) throws InterruptedException{
Chegg198 hw6 = new Chegg198();
int itemsPurchased = 0;
int itemsCost = 0;
for(int i = 0; i < 20; i++) { // run the loop 20 times
Random rand = new Random();
boolean choice = rand.nextBoolean();
if(choice) {
int items = rand.nextInt(40)+1;
int cost = rand.nextInt(200)+1;
Customer c = new Customer(i+1, items, cost);
hw6.insertCustomer(c);
System.out.println("Added New Customer : "+c);
} else {
Customer c = hw6.removeCustomer();
System.out.println("Removed Customer : "+c);
if(c != null) {
itemsPurchased += c.getItems();
itemsCost += c.getCost();
}
}
Thread.sleep(1000); // sleep for one second
System.out.println("The number of customers that have left the queue : "+(i-Customer.getTotalCustomers()));
System.out.println("The number still in the queue : "+ Customer.getTotalCustomers());
System.out.println("The total number of customers handled : " + i);
System.out.println("The total items purchased by all customers : "+itemsPurchased);
System.out.println("The total cost of all items purchased by all customers : "+itemsCost);
hw6.printCustomers();
}
}
}
class Customer {
private static int totalCustomers;
private static int totalItems ;
private static int totalCost;
private int ID;
private int items;
private int cost;
private Customer next;
// default constructor
public Customer() {
totalCustomers = totalCustomers + 1;
ID = -1;
items = -1;
cost = -1;
next = null;
}
// parametered constructor
public Customer(int ID, int items, int cost) {
totalCustomers = totalCustomers + 1;
totalItems = totalItems + items;
totalCost = totalCost + cost;
this.ID = ID;
this.items = items;
this.cost = cost;
next = null;
}
// getters and setters
public static int getTotalCustomers() {
return totalCustomers;
}
public static void setTotalCustomers(int totalCustomers) {
Customer.totalCustomers = totalCustomers;
}
public static int getTotalItems() {
return totalItems;
}
public static void setTotalItems(int totalItems) {
Customer.totalItems = totalItems;
}
public static int getTotalCost() {
return totalCost;
}
public static void setTotalCost(int totalCost) {
Customer.totalCost = totalCost;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getItems() {
return items;
}
public void setItems(int items) {
this.items = items;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
/**
* This method returns the next Customer in the queue.
* @return The Object in the queue next to it.
*/
public Customer getNext() {
return next;
}
public void setNext(Customer next) {
this.next = next;
}
/**
* To string method
* @return
*/
@Override
public String toString() {
return String.format(" %2d ", ID) +' '+ String.format(" %2d ", items) + ' '+ String.format(" %2d", cost);
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + this.ID;
return hash;
}
/**
* equals method compares ID for equality.
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Customer other = (Customer) obj;
return this.ID == other.ID;
}
}