Hello expert, I have solved the following java project: A supermarket wants to r
ID: 3764364 • Letter: H
Question
Hello expert, I have solved the following java project:
A supermarket wants to reward its best customer of each day, showing the customer’s name on a screen in the supermarket. For that purpose, the store keeps an ArrayList<Customer>. In the Store class, implement methods public void addSale(String customerName, double amount) public String nameOfBestCustomer() to record the sale and return the name of the customer with the largest sale. Write a program that prompts the cashier to enter all prices and names, adds them to a Store object, and displays the best customer’s name. Use a price of 0 as a sentinel.
In my solution, I have implemented 3 classes:
1.)Customer, which contains the code:
public class Customer {
private String name;
private double sale;
public Customer (String customerName, double amount){
name = customerName;
sale = amount;
}
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
public double getSale(){
return sale;
}
public void setSale(double sale){
this.sale=sale;
}
}
2.)Store, which contains:
import java.util.ArrayList;
public class Store {
private ArrayList<Customer> customer;
public Store(){
customer= new ArrayList<Customer>();
}
public void addSale(String customerName, double amount){
customer.add(new Customer (customerName, amount));
}
public ArrayList getCustomer() {
return customer;
}
public String nameOfBestCustomer(){
Customer largest = customer.get(0);
for (int i = 0; i<customer.size(); i++){
if (customer.get(i).getSale() > largest.getSale()){
largest = customer.get(i);
}
}
return largest.getName();
}
}
3.) a tester class that contains:
import java.util.ArrayList;
import java.util.Scanner;
public class storeTester {
ArrayList<Customer> store = new ArrayList<Customer>();
public static void main(String[] args) {
Store store = new Store();
double purchase = 1.0;
Scanner input = new Scanner(System.in);
System.out.println("To exit, type 0 when prompted, otherwise give sales amount");
while(true){
System.out.println("Please enter the customer's name");
String customerName = input.nextLine();
if(customerName == "0")
break;
System.out.println("Please enter the amount of the sale");
double amount = Double.parseDouble(input.nextLine());
if(amount == 0)
break;
store.addSale(customerName, amount);
}
System.out.println(store.nameOfBestCustomer());
}
}
Now in the subsequent question of my assignment, I need to improve the program so that it displays the top customers, that is, the topN customers with the largest sales, where topN is a value that the user of the program supplies. Implement a method public ArrayList<String> nameOfBestCustomers(int topN) If there were fewer than topN customers, include all of them.
Explanation / Answer
Customer.java
public class Customer implements Comparable<Customer> {
private String name;
private double sale;
public Customer(String customerName, double amount) {
name = customerName;
sale = amount;
}
public int compareTo(Customer customer) {
double customerSale = customer.getSale();
/* For Ascending order */
double diff = customerSale - this.sale;
return (int) diff;
/* For Descending order do like this */
// return compareage-this.studentage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSale() {
return sale;
}
public void setSale(double sale) {
this.sale = sale;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Customer [name=" + name + ", sale=" + sale + "]";
}
}
Store.java
import java.util.ArrayList;
import java.util.Collections;
public class Store {
private ArrayList<Customer> customer;
public Store() {
customer = new ArrayList<Customer>();
}
public void addSale(String customerName, double amount) {
customer.add(new Customer(customerName, amount));
}
public ArrayList getCustomer() {
return customer;
}
public String nameOfBestCustomer() {
Customer largest = customer.get(0);
for (int i = 0; i < customer.size(); i++) {
if (customer.get(i).getSale() > largest.getSale()) {
largest = customer.get(i);
}
}
return largest.getName();
}
public ArrayList<String> nameOfBestCustomers(int topN) {
ArrayList<String> bestCustomers = new ArrayList<String>();
Collections.sort(customer);
for (int i = 0; i < customer.size(); i++) {
String custName = customer.get(i).getName();
double custSale = customer.get(i).getSale();
if (i == topN) {
break;
}
bestCustomers.add(custName);
// System.out.println(custName);
}
return bestCustomers;
}
}
storeTester.java
import java.util.ArrayList;
import java.util.Scanner;
public class storeTester {
ArrayList<Customer> store = new ArrayList<Customer>();
public static void main(String[] args) {
Store store = new Store();
double purchase = 1.0;
Scanner input = new Scanner(System.in);
System.out
.println("To exit, type 0 when prompted, otherwise give sales amount");
while (true) {
System.out.print("Please enter the customer's name");
String customerName = input.nextLine();
if (customerName == "0")
break;
System.out.print("Please enter the amount of the sale");
double amount = Double.parseDouble(input.nextLine());
if (amount == 0)
break;
store.addSale(customerName, amount);
}
System.out.println(store.nameOfBestCustomer());
System.out.println("name 3 best customers");
System.out.println(store.nameOfBestCustomers(3));
}
}
OUTPUT:
To exit, type 0 when prompted, otherwise give sales amount
Please enter the customer's namesrinivas1
Please enter the amount of the sale342
Please enter the customer's namesrinivas2
Please enter the amount of the sale32
Please enter the customer's namesrinivas3
Please enter the amount of the sale345
Please enter the customer's namesrinivas4
Please enter the amount of the sale322
Please enter the customer's namesrinivas5
Please enter the amount of the sale768
Please enter the customer's name0
Please enter the amount of the sale0
srinivas5
name 3 best customers
[srinivas5, srinivas3, srinivas1]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.