Answer to Question #294491 in Java | JSP | JSF for Mayu

Question #294491

Q1) Create a class with Customer with the following properties CustomerID and CustomerName and in that class only write functions:




a. List<Customer> retriveCustomers() to fill the customer class with objects and retrieve filled details of the customer .




b. FindCustomer (List<Customer> clist1,int custid1) find the customer from the above list of customers by fetching id from the user.




c. Updatecustomer(List<Customer> clist1, int custid1) to update a particular customer when id is given




d. Deletecustomer (List<Customer> clist1,int custid1) to delete the particular customer when id is given .

1
Expert's answer
2022-02-06T12:30:56-0500
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Customer {
    private int customerID;
    private String customerName;

    static List<Customer> retriveCustomers() {
        List<Customer> customers = new LinkedList<>();
        Scanner in = new Scanner(System.in);
        while (true) {
            Customer customer = new Customer();
            System.out.println("ID(-1 to stop)");
            customer.customerID = Integer.parseInt(in.nextLine());
            if (customer.customerID == -1) {
                break;
            }
            System.out.println("NAME");
            customer.customerName = in.nextLine();
            customers.add(customer);

        }
        return customers;
    }

    static Customer findCustomer(List<Customer> clist1, int custid1) {
        for (Customer customer : clist1) {
            if (customer.customerID == custid1) {
                return customer;
            }
        }
        return null;
    }

    static void updateCustomer(List<Customer> clist1, int custid1) {
        Customer customer = findCustomer(clist1, custid1);
        if (customer != null) {
            Scanner in = new Scanner(System.in);
            System.out.println("ID");
            customer.customerID = Integer.parseInt(in.nextLine());
            System.out.println("NAME");
            customer.customerName = in.nextLine();
        }
    }

    static void deleteCustomer(List<Customer> clist1, int custid1) {
        Customer customer = findCustomer(clist1, custid1);
        if (customer != null) {
            clist1.remove(customer);
        }
    }

}

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