Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project A: Maintain Customer Data (Text or XML file) Console Welcome to the Cust

ID: 3558445 • Letter: P

Question

Project A: Maintain Customer Data (Text or XML file)

Console

Welcome to the Customer Maintenance application

COMMAND MENU

list    - List all customers

add     - Add a customer

del     - Delete a customer

help    - Show this menu

exit    - Exit this application

Enter a command: list

CUSTOMER LIST

frank46@hotmail.com               Frank              Jones

sarah_smith@yahoo.com             Sarah              Smith

Enter a command: add

Enter customer email address: test@gmail.com

Enter first name: text

Enter last name: test

text test was added to the database.

Enter a command: list

CUSTOMER LIST

frank46@hotmail.com               Frank              Jones

sarah_smith@yahoo.com             Sarah              Smith

test@gmail.com                    text               test

Enter a command: del

Enter customer email to delete: test@gmail.com

text test was deleted from the database.

Enter a command: list

CUSTOMER LIST

frank46@hotmail.com               Frank              Jones

sarah_smith@yahoo.com             Sarah              Smith

Enter a command: exit

Bye.

Operation

Explanation / Answer

Here is the code


import java.util.Scanner;
import java.util.ArrayList;

public class CustomerMaintApp implements CustomerConstants
{
// declare class variables
private static CustomerDAO customerDAO = null;
private static Scanner sc = null;

public static void main(String[] args)
{
// display a welcome message
System.out.println("Welcome to the Customer Maintenance application ");

// set the class variables
customerDAO = DAOFactory.getCustomerDAO();
sc = new Scanner(System.in);

// display the command menu
displayMenu();

// perform 1 or more actions
String action = "";
while (!action.equalsIgnoreCase("exit"))
{
// get the input from the user
action = Validator.getString(sc,
"Enter a command: ");
System.out.println();

if (action.equalsIgnoreCase("list"))
displayAllCustomers();
else if (action.equalsIgnoreCase("add"))
addCustomer();
else if (action.equalsIgnoreCase("del") || action.equalsIgnoreCase("delete"))
deleteCustomer();
else if (action.equalsIgnoreCase("help") || action.equalsIgnoreCase("menu"))
displayMenu();
else if (action.equalsIgnoreCase("exit") || action.equalsIgnoreCase("quit"))
System.out.println("Bye. ");
else
System.out.println("Error! Not a valid command. ");
}
}
  
public static void displayMenu()
{
System.out.println("COMMAND MENU");
System.out.println("list - List all customers");
System.out.println("add - Add a customer");
System.out.println("del - Delete a customer");
System.out.println("help - Show this menu");
System.out.println("exit - Exit this application ");
}

public static void displayAllCustomers()
{
System.out.println("CUSTOMER LIST");

ArrayList<Customer> customers = customerDAO.getCustomers();
Customer c = null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < customers.size(); i++)
{
c = customers.get(i);
sb.append(StringUtils.padWithSpaces(
c.getName(), NAME_SIZE + 2));
sb.append(c.getEmail());
sb.append(" ");
}
System.out.println(sb.toString());
}

public static void addCustomer()
{
String firstName = Validator.getLine(
sc, "Enter first name: ");
String lastName = Validator.getString(
sc, "Enter last name: ");
String email = Validator.getString(
sc, "Enter customer email: ");

Customer customer = new Customer();
customer.setFirstName(firstName);
customer.setLastName(lastName);
customer.setEmail(email);
customerDAO.addCustomer(customer);

System.out.println();
System.out.println(firstName + " " + lastName
+ " has been added. ");
}

public static void deleteCustomer()
{
String email = Validator.getString(sc,
"Enter email to delete: ");

Customer c = customerDAO.getCustomer(email);

System.out.println();
if (c != null)
{
customerDAO.deleteCustomer(c);
System.out.println(c.getName()
+ " has been deleted. ");
}
else
{
System.out.println("No customer matches that email. ");
}
}
}