I need solutions to murach\'s java programming 4th Edition. Specifically Exercis
ID: 3858473 • Letter: I
Question
I need solutions to murach's java programming 4th Edition. Specifically Exercise 18-1
Exercise 18-1 Work with a text file In this exercise, you'll add code to a Customer Maintenance application for reading 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 line character. Open the application and review its code 1. Open the project named ch18_exl_CustomerMaint 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 will store the array 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. Add code to the getCustomers method that loads the array 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. Add code to the saveCustomers method that writes the data in each Customer object in the array list to a record in the customers file. Be sure to delimit the fields and records with the appropriate character. If an IOException is thrown 4. 5.Explanation / Answer
Hi, I was not having access of the complete code, but I had some code from similar kind of application, and I tested my implementation. It is working... Please find the code below of CustomTextFile.java. If you find any errors while integrating this with your code, it may be you need to change some getters of Customer class. If you find any problem, please comment, I'll resolve it.
import java.util.*;
import java.io.*;
import java.nio.file.*;
public final class CustomerTextFile implements CustomerDAO {
private ArrayList<Customer> customers = null;
private Path customersPath = null;
private File customersFile = null;
private final String FIELD_SEP = " ";
public CustomerTextFile() {
// initialize the class variables
// 3 add code to initialize variables
customersPath = Paths.get("customers.txt"); // 5 use customers.txt file
customersFile = customersPath.toFile();
customers = this.getCustomers();
}
public ArrayList<Customer> getCustomers() {
// if the customers file has already been read, don't read it again
if (customers != null)
return customers;
customers = new ArrayList<>();
// 4a check if file exists
if (Files.exists(customersPath)) {
try (BufferedReader in = // 4b try-with-resources to open input
// stream
new BufferedReader(new FileReader(customersFile))) {
String line = in.readLine();
while (line != null) {
String[] columns = line.split(FIELD_SEP);
String firstname = columns[0];
String lastname = columns[1];
String email = columns[2];
Customer c = new Customer(firstname, lastname, email);
customers.add(c);
line = in.readLine();
}
} catch (IOException e) {
System.out.println(e); // 4c print error if exception occurs
return null; // return null if exception occurs
}
}
// load the array list with Customer objects created from
// the data in the file
return customers;
}
public Customer getCustomer(String email) {
for (Customer c : customers) {
if (c.getEmailAddr().equals(email))
return c;
}
return null;
}
public boolean addCustomer(Customer c) {
customers.add(c);
return this.saveCustomers();
}
public boolean deleteCustomer(Customer c) {
customers.remove(c);
return this.saveCustomers();
}
public boolean updateCustomer(Customer newCustomer) {
// get the old customer and remove it
Customer oldCustomer = this.getCustomer(newCustomer.getEmailAddr());
int i = customers.indexOf(oldCustomer);
customers.remove(i);
// add the updated customer
customers.add(i, newCustomer);
return this.saveCustomers();
}
private boolean saveCustomers() {
// save the Customer objects in the array list to the file
// 5 code to write to file
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(this.customersFile)))) {
for (Customer c : customers) {
out.print(c.getFirstName() + this.FIELD_SEP); // 5a use
// appropriate
// delimiter
// (FIELD_SEP)
out.print(c.getLastName() + this.FIELD_SEP);
out.println(c.getEmailAddr());
}
} catch (IOException e) {
System.out.println(e); // 5b print error if exception occurs
return false; // 5c return false if exception occurs
}
return true;
}
}