A link list system that manages a list of customer
Use a linked list to create a system for managing a list of customers waiting to buy the latest game console.Each customer has a unique name.The system should fulfill the following operations:
• Adding a customer in the start of the list
• Adding a customer in the end of the list
• Removing a customer from the start of the list
• Removing a customer from the end of the list
• Removing a customer by name
• Printing the current list
• Create attest program that allows the user to perform all operations with their own data.
Java Code
importjava.util.LinkedList;
importjava.util.Scanner;
public class CustomersListManagement {
// Entry point of the program
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LinkedListcustomersList = new LinkedList<>();
while (true) {
System.out.println("Menu");
System.out.println("a. Add a customer in the start of the list");
System.out.println("b. Add a customer in the end of the list");
System.out.println("c. Remove a customer from the start of the list");
System.out.println("d. Remove a customer from the end of the list");
System.out.println("e. Remove a customer by name");
System.out.println("f. Print the current list");
System.out.println("x. Exit");
System.out.print("Option: ");
String option = in.nextLine();
System.out.println();
if (option.equalsIgnoreCase("x")) {
break;
}
if (option.equalsIgnoreCase("a")) {
// Add a customer to front
System.out.print("Enter customer name: ");
String name = in.nextLine().trim().toUpperCase();
if (name.isEmpty()) {
System.out.println("Error: A name is required.");
} else if (customersList.contains(name)) {
System.out.println("Error: Name is already used.");
} else {
customersList.addFirst(name);
System.out.println("Ok: '" + name + "' added.");
}
} else if (option.equalsIgnoreCase("b")) {
// Add a customer to end
System.out.print("Enter customer name: ");
String name = in.nextLine().trim().toUpperCase();
if (name.isEmpty()) {
System.out.println("Error: A name is required.");
} else if (customersList.contains(name)) {
System.out.println("Error: Name is already used.");
} else {
customersList.addLast(name);
System.out.println("Ok: '" + name + "' added.");
}
} else if(option.equalsIgnoreCase("c")) {
// Remove front
if(customersList.isEmpty()) {
System.out.println("Error: The list is empty.");
} else {
String name = customersList.removeFirst();
System.out.println("Ok: '" + name + "' removed.");
}
} else if(option.equalsIgnoreCase("d")) {
// Remove back
if(customersList.isEmpty()) {
System.out.println("Error: The list is empty.");
} else {
String name = customersList.removeLast();
System.out.println("Ok: '" + name + "' removed.");
}
} else if(option.equalsIgnoreCase("e")) {
// Remove by name
System.out.print("Enter customer name: ");
String name = in.nextLine().trim().toUpperCase();
if (name.isEmpty()) {
System.out.println("Error: A name is required.");
} else if (!customersList.contains(name)) {
System.out.println("Error: Name is not in the list.");
} else {
customersList.remove(name);
System.out.println("Ok: '" + name + "' removed.");
}
} else if(option.equalsIgnoreCase("f")) {
// Print everything on list
for(String name : customersList) {
System.out.println(name);
}
} else if(option.equalsIgnoreCase("x")) {
break;
}
System.out.println();
}
}
}