Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this java question. Please need it done asap. Thanks so much. Package murach.business Customer.java package murach.business; public class Customer { private String

Need help with this java question. Please need it done asap. Thanks so much.

image text in transcribed

image text in transcribed

Package murach.business

Customer.java

package murach.business;

public class Customer {

private String firstName; private String lastName; private String email;

public Customer() { this("", "", ""); }

public Customer(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; }

public void setFirstName(String firstName) { this.firstName = firstName; }

public String getFirstName() { return firstName; }

public void setLastName(String lastName) { this.lastName = lastName; }

public String getLastName() { return lastName; }

public void setEmail(String email) { this.email = email; }

public String getEmail() { return email; }

public String getName() { return firstName + " " + lastName; } }

Package murach.io

CustomerTextFile.java

package murach.io;

import java.util.*; import java.io.*; import java.nio.file.*; import murach.business.Customer;

public class CustomerTextFile { private static final String FIELD_SEP = "\t"; private static final Path customersPath = Paths.get("customers.txt"); private static final File customersFile = customersPath.toFile(); private static List customers;

// prevent instantiation of the class private CustomerTextFile() {}

public static List getCustomers() { // if the customers file has already been read, don't read it again if (customers != null) { return customers; }

try (BufferedReader in = new BufferedReader( new FileReader(customersFile))) { // read all customers stored in the file // into the array list customers = new ArrayList(); String line; while ((line = in.readLine()) != null) { String[] columns = line.split(FIELD_SEP); String firstName = columns[0]; String lastName = columns[1]; String email = columns[2];

Customer p = new Customer( firstName, lastName, email);

customers.add(p); } return customers; } catch (IOException e) { System.out.println(e); return null; } }

public static Customer getCustomer(String email) { customers = getCustomers(); for (Customer c : customers) { if (c.getEmail().equals(email)) { return c; } } return null; }

public static boolean addCustomer(Customer c) { customers = getCustomers(); customers.add(c); return saveCustomers(); }

public static boolean deleteCustomer(Customer c) { customers = getCustomers(); customers.remove(c); return saveCustomers(); }

private static boolean saveCustomers() { try (PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter(customersFile)))) {

// write all customers in the array list // to the file for (Customer c : customers) { out.print(c.getFirstName() + FIELD_SEP); out.print(c.getLastName() + FIELD_SEP); out.println(c.getEmail()); } } catch (IOException e) { System.out.println(e); return false; }

return true; } }

murach.ui

Main.java

package murach.ui;

import java.util.List;

import murach.business.Customer; import murach.io.CustomerTextFile;

public class Main {

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

// display the command menu displayMenu();

// perform 1 or more actions String action; while (true) { // get the input from the user action = Console.getString("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")) { quit(); } 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");

List customers = CustomerTextFile.getCustomers(); Customer c; final int NAME_SIZE = 25; StringBuilder sb = new StringBuilder(); for (Customer customer : customers) { c = customer; 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 = Console.getString("Enter first name: "); String lastName = Console.getString("Enter last name: "); String email = Console.getString("Enter customer email: ");

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

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

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

Customer c = CustomerTextFile.getCustomer(email);

System.out.println(); if (c != null) { CustomerTextFile.deleteCustomer(c); System.out.println(c.getName() + " has been deleted. "); } else { System.out.println("No customer matches that email. "); } } public static void quit() { System.out.println("Bye. "); System.exit(0); } }

package murach.ui

console.java

package murach.ui;

import java.util.Scanner;

public class Console {

private static Scanner sc = new Scanner(System.in); public static void println() { System.out.println(); }

public static void println(String string) { System.out.println(string); }

public static String getString(String prompt) { System.out.print(prompt); String s = sc.nextLine(); // read the whole line return s; }

public static int getInt(String prompt) { boolean isValid = false; int i = 0; while (isValid == false) { System.out.print(prompt); try { i = Integer.parseInt(sc.nextLine()); isValid = true; } catch (NumberFormatException e) { System.out.println("Error! Invalid integer value. Try again."); } } return i; }

public static int getInt(String prompt, int min, int max) { int i = 0; boolean isValid = false; while (isValid == false) { i = getInt(prompt); if (i = max) { System.out.println( "Error! Number must be less than " + max); } else { isValid = true; } } return i; }

public static double getDouble(String prompt) { boolean isValid = false; double d = 0; while (isValid == false) { System.out.print(prompt); try { d = Double.parseDouble(sc.nextLine()); isValid = true; } catch (NumberFormatException e) { System.out.println("Error! Invalid decimal value. Try again."); } } return d; }

public static double getDouble(String prompt, double min, double max) { double d = 0; boolean isValid = false; while (isValid == false) { d = getDouble(prompt); if (d = max) { System.out.println( "Error! Number must be less than " + max); } else { isValid = true; } } return d; } }

In this exercise, you'll modify the Customer Manager application so it handles exceptions differently. This application is similar to the Product Manager application described in chapter 17 Review the application 1. Open the project named ch16 ex4_CustomerManager in the extraes starts, directory 2. Open the CustomerTextFile class and the Main class and review the code. Note that the CustomerTextFile class handles all I'O exceptions, and the Main class doesn't catch any exceptions. 3. Run the application to make sure it works correctly Cause an exception to be thrown 4. In the CustomerTextFile class, add the following code to the beginning of the try block that's in the getCustomers method: if (true) throw new IOException ("Test") 5. Run the application and test the exception handling. At this point, you should be able to test the exception handling for the list, add, and del commands. For these commands, the application should throw a NullPointerException and display a message like this java.io.IOException: Test Create a custom exception and throw it 6. In the murach io package, add a class that defines a custom exception named DBException. This exception should allow for exception chaining. In the QustomerTextFile class, modify the getCustomers method so that the catch block creates a DBException object that stores the IQException object and throws the 7. object. Add a throws clause that throws a DBException to all of the methods in the CustomerTextFile that need it. 8. Catch the custom exceptiorn 9. In the Main class, add a try/catch statement that catches any DBExceptions that are thrown by the QustomerTextFile class. In the catch clauses, display a user-friendly message In this exercise, you'll modify the Customer Manager application so it handles exceptions differently. This application is similar to the Product Manager application described in chapter 17 Review the application 1. Open the project named ch16 ex4_CustomerManager in the extraes starts, directory 2. Open the CustomerTextFile class and the Main class and review the code. Note that the CustomerTextFile class handles all I'O exceptions, and the Main class doesn't catch any exceptions. 3. Run the application to make sure it works correctly Cause an exception to be thrown 4. In the CustomerTextFile class, add the following code to the beginning of the try block that's in the getCustomers method: if (true) throw new IOException ("Test") 5. Run the application and test the exception handling. At this point, you should be able to test the exception handling for the list, add, and del commands. For these commands, the application should throw a NullPointerException and display a message like this java.io.IOException: Test Create a custom exception and throw it 6. In the murach io package, add a class that defines a custom exception named DBException. This exception should allow for exception chaining. In the QustomerTextFile class, modify the getCustomers method so that the catch block creates a DBException object that stores the IQException object and throws the 7. object. Add a throws clause that throws a DBException to all of the methods in the CustomerTextFile that need it. 8. Catch the custom exceptiorn 9. In the Main class, add a try/catch statement that catches any DBExceptions that are thrown by the QustomerTextFile class. In the catch clauses, display a user-friendly message

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions

Question

6. Identify characteristics of whiteness.

Answered: 1 week ago

Question

e. What are notable achievements of the group?

Answered: 1 week ago