In: Computer Science
in java
Create a class called Customer in three steps: • (Step-1): • Add a constructor of the class Customer that takes the name and purchase amount of the customer as inputs. • Write getter methods getName and getPrice to return the name and price of the customer. You can write a toString() method which returns printed string of Customer name and its purchase. • (Step-2): Create a class called Restaurant. • Write a method addsale that takes customer name and purchase amount of the customer in the class restaurant and stores it into an array list. • Write a method nameBestCustomer that returns customer name who bought highest sale in the restaurant. • (Step-3): Write a tester class that prompts the user to enter name and purchase of the customers and display the name of the customer who bought highest sales in the restaurant.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
import java.util.*;
class customer{
private String name;
private int purchaseAmt;
Scanner sc=new Scanner(System.in);
public customer() {
System.out.println("\nEnter the Name and purchase amount: \n");
this.name=sc.nextLine();
this.purchaseAmt=sc.nextInt();
}
public String getName() {
return this.name;
}
public Integer getPurchaseAmt() {
return this.purchaseAmt;
}
public String toString(){//overriding the toString() method
return "Name: "+this.getName()+" Purchase Amount : "+this.getPurchaseAmt()+"\n";
}
}
class Comp implements Comparator<customer>{
@Override
public int compare(customer e1, customer e2) {
return e1.getPurchaseAmt().compareTo(e2.getPurchaseAmt());
}
}
class Restaurant {
ArrayList<customer> list = new ArrayList<>();
int index;
void addSale(int n){
//ArrayList<customer> list = new ArrayList<>();
for (int i=0;i<n;i++)
list.add( new customer() );
System.out.println(list);
highestSales();
}
void highestSales()
{
customer maxSal = Collections.max(list, new Comp());
System.out.println("\n\nHighest sales :\n"+maxSal);
}
}
public class tester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("\nHow many customers\n");
int n=sc.nextInt();
Restaurant restObj=new Restaurant();
restObj.addSale( n);
}
}
Hope this helps..cheers!!