Question

In: Computer Science

in java Create a class called Customer in three steps: • (Step-1): • Add a constructor...

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.

Solutions

Expert Solution

/*
 * 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!!


Related Solutions

Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class to your project. It should contain:             - The date of the party             - A list of people attending             - A list of containers at the party             - The address of the party (this can be either a String or a Class) --------- Step 3: Add a Container class in your project. It should be an abstract class. Your Container class...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like: dispenserType sanitizer("hand sanitizer", 50, 100); Add the declaration and definition of this parameterized constructor to .h and .cpp files. Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT