Question: The question is found in attachment, Please answer the 7 part question in jave using the code provided. CustomerTextFile.java import java.util.*; import java.io.*; import java.nio.file.*;

The question is found in attachment, Please answer the 7 part question in jave using the code provided.

CustomerTextFile.java

import java.util.*; import java.io.*; import java.nio.file.*;

public final class CustomerTextFile implements DAO {

private List customers = null; private Path customersPath = null; private File customersFile = null;

private final String FIELD_SEP = "\t";

public CustomerTextFile() { // initialize the class variables }

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

customers = new ArrayList();

// load the array list with Customer objects created from // the data in the file return customers; }

@Override public Customer get(String email) { for (Customer c : customers) { if (c.getEmail().equals(email)) { return c; } } return null; }

@Override public boolean add(Customer c) { customers.add(c); return this.saveAll(); }

@Override public boolean delete(Customer c) { customers.remove(c); return this.saveAll(); }

@Override public boolean update(Customer newCustomer) { // get the old customer and remove it Customer oldCustomer = this.get(newCustomer.getEmail()); int i = customers.indexOf(oldCustomer); customers.remove(i);

// add the updated customer customers.add(i, newCustomer);

return this.saveAll(); }

private boolean saveAll() { // save the Customer objects in the array list to the file

return true; } }

Customer.java

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; } }

CustomerManngerApp.java

import java.util.List;

public class CustomerManagerApp {

// declare class variables private static DAO customerDAO = null;

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

// set the class variables customerDAO = new CustomerTextFile();

// display the command menu displayMenu();

// perform 1 or more actions String action = ""; while (!action.equalsIgnoreCase("exit")) { // 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")) { 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");

List customers = customerDAO.getAll(); Customer c; StringBuilder sb = new StringBuilder(); for (int i = 0; i

public static void addCustomer() { String firstName = Console.getLine("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); customerDAO.add(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 = customerDAO.get(email);

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

DAO.jave

import java.util.List;

public interface DAO { T get(String code); List getAll(); boolean add(T t); boolean update(T t); boolean delete(T t); }

StringUtils.java

public class StringUtils { public static String padWithSpaces(String s, int length) { if (s.length()

The question is found in attachment, Please answer the 7 part question

Chapter 15 In this exercise, you'll add code to a Customer Manager application for reading Exercise 15-1 How to work with file I/O 4 Work with a text file line character. 1. and writing data from a text file named customers.txt. Each record in this file contains three fields with the customer's first name, last name, and email. The fields are separated by a tab character, and the records are separated by a new Open the project named ch15_exl_CustomerManager that's in the ex_starts directory 2. Open the Customer class and review its code. 3. Open the CustomerTextFile class, and notice the three class variables that declare a list of Customer objects, a Path object for the file, and a File object for the file. Add code to the constructor that initializes these variables. 4. Add code to the getAll(method that loads the customer list variable with the Customer objects that are created from the data in the customers.txt file. Be sure to check that this file exists, and if it does, use a try-with-resources statement to open the input stream. If an IOException occurs when the input stream is opened, print the exception to the console and return a null to the calling method. 5. Add code to the saveAll() method that writes the data in each Customer object in the customer list to a record in the customer file. Be sure to delimit the fields and records with the appropriate character. If an IOException occurs when the output stream is opened, print the exception to the console and return false to the calling method. 6. Run the application. Then, test the list, add, and delete commands to be sure they work correctly. 7. Find the customers.txt file and open it in a text editor. The text editor should be able to display the data in this file in a way that you can easily read

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!