Answer to Question #263529 in Java | JSP | JSF for Laiba Shahid

Question #263529



In the Store class, make an ArrayList of customers, store name, and address implement methods

public void addSale(Customer c) that will add customers to the arraylist.

public void RemoveCustomer(int id);

public void UpdateCustomerRecord(int Id);

public displayAll();

public String nameOfBestCustomer() to record the sale and return the name of the customer with the largest sale.

public ArrayList nameOfBestCustomers(int topN)

 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.

 

Write a program that prompts following menu and

 

 


                                            Menu


============================================

1. Add Customer record

2. Delete Customer record

3. Update Customer record

4. View all Customers

5. View Best Customer.

6. Find Customer by ID.

1.     Exit.

Ask the user to enter his/her choice then perform the desired function as long as he/she wants.



1
Expert's answer
2021-11-10T08:27:49-0500
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Store {
    private String name;
    private String address;
    private ArrayList<Customer> customers;

    public Store(String name, String address) {
        this.name = name;
        this.address = address;
        customers = new ArrayList<>();
    }

    public void addSale(Customer c) {
        customers.add(c);
    }

    public void removeCustomer(int id) {
        int index = -1;
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getId() == id) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            customers.remove(index);
        }
    }

    public void updateCustomerRecord(int id) {
        int index = -1;
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getId() == id) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            Scanner in = new Scanner(System.in);
            System.out.println("Name:");
            customers.get(index).setName(in.nextLine());
            System.out.println("Sale:");
            customers.get(index).setSale(Double.parseDouble(in.nextLine()));
            System.out.println("ID:");
            customers.get(index).setId(Integer.parseInt(in.nextLine()));
        }
    }

    public void displayAll() {
        for (Customer customer : customers) {
            System.out.println(customer);
        }
    }

    public String nameOfBestCustomer() {
        int index = 0;
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getSale() > customers.get(index).getSale()) {
                index = i;
            }
        }
        return customers.get(index).getName();
    }

    public ArrayList<String> nameOfBestCustomers(int topN) {
        customers.sort(Comparator.comparing(Customer::getSale));
        ArrayList<String> names = new ArrayList<>();
        for (int i = customers.size() - 1; i >= customers.size() - topN; i--) {
            names.add(customers.get(i).getName());
        }
        return names;
    }

    public void menu() {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("                                            Menu" +
                    "\n\n============================================\n" +
                    "1. Add Customer record\n" +
                    "2. Delete Customer record\n" +
                    "3. Update Customer record\n" +
                    "4. View all Customers\n" +
                    "5. View Best Customer.\n" +
                    "6. Find Customer by ID.\n" +
                    "7. Exit.\n");
            switch (in.nextLine()) {
                case "1":
                    Customer customer = new Customer();
                    System.out.println("Name:");
                    customer.setName(in.nextLine());
                    System.out.println("Sale:");
                    customer.setSale(Double.parseDouble(in.nextLine()));
                    System.out.println("ID:");
                    customer.setId(Integer.parseInt(in.nextLine()));
                    addSale(customer);
                    break;
                case "2":
                    System.out.println("ID:");
                    removeCustomer(Integer.parseInt(in.nextLine()));
                    break;
                case "3":
                    System.out.println("ID:");
                    updateCustomerRecord(Integer.parseInt(in.nextLine()));
                    break;
                case "4":
                    displayAll();
                    break;
                case "5":
                    System.out.println(nameOfBestCustomer());
                    break;
                case "6":
                    System.out.println("ID:");
                    int id = Integer.parseInt(in.nextLine());
                    for (Customer value : customers) {
                        if (value.getId() == id) {
                            System.out.println(value);
                            break;
                        }
                    }
                    break;
                case "7":
                    System.exit(0);
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS