In: Computer Science
We plan to develop customer’s database that stores customer’s number (ID), first name, last name and balance. The program will support three operations: (a) reading and loading customer’s info from the text file, (b) entering new customer’s info, (c) looking up existing customer’s info using customer’s number(ID), (d) deleting the customer’s info and (e) the updated database will be stored in the text file after the program terminate.
Customer’s database is an example of a menu-driven program. When the program begins to execute, it first read the text file and upload customer’s info. Then it presents the user with a list of commands. The user can enter as many commands as desired, in any order. The a command prompts the user to enter customer’s number (ID), first name, last name and balance, which are then stored in the program’s database. The f command prompts the user to enter a customer number and then display the corresponding record on the screen. The q command saves the information in the database to the file specified by the user and terminate program.
Based on the project description, please design classes and their corresponding methods. You need describe each method and write API for each method (Leave method body blank) (Hint: you need design one driver class, one database class and one customer record class). Based on your design, please draw the UML diagram as well.
database.txt
12345 Sebastian vanDelden 123.22
11111 Sarah Smith 45.89
22222 Sue Johnson 7765.98
33333 Billy Hunts 374.99
Customer.java
public class Customer {
private String id, firstName, lastName;
private double balance;
public Customer()
{
this.id = "";
this.firstName = "";
this.lastName = "";
this.balance = 0;
}
public Customer(String id, String firstName, String lastName,
double balance)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString()
{
return(this.id + ", " + this.firstName + " " + this.lastName + ",
$" + String.format("%.2f", this.balance));
}
}
Database.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Database {
private ArrayList<Customer> customers;
public Database()
{
this.customers = new ArrayList<>();
}
public void readData(String fileName)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(" ");
String id = data[0];
String firstName = data[1];
String lastName = data[2];
double balance = Double.parseDouble(data[3]);
this.customers.add(new Customer(id, firstName, lastName,
balance));
}
fileReader.close();
System.out.println("File read complete.\nCustomer data uploaded
successfully.\n");
}catch(FileNotFoundException fnfe){
System.out.println("Could not locate file " + fileName +
".\nExiting program...\n");
System.exit(0);
}
}
public void addCustomer()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nAdd New Customer:\n-----------------\n"
+ "Enter customer id: ");
String id = sc.nextLine().trim();
System.out.print("Enter customer first name: ");
String firstName = sc.nextLine().trim();
System.out.print("Enter customer last name: ");
String lastName = sc.nextLine().trim();
System.out.print("Enter customer balance: $");
double balance = Double.parseDouble(sc.nextLine().trim());
this.customers.add(new Customer(id, firstName, lastName,
balance));
System.out.println(firstName + " " + lastName + " is added
successfully.\n");
}
public void searchCustomer()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nSearch for a
Customer:\n----------------------\n"
+ "Enter the customer id: ");
String id = sc.nextLine().trim();
boolean found = false;
int index = 0;
for(int i = 0; i < this.customers.size(); i++)
{
if(this.customers.get(i).getId().equals(id))
{
found = true;
index = i;
break;
}
}
if(!found)
System.out.println("No customer with id " + id + " was
found.\n");
else
System.out.println("Match found!\n" +
this.customers.get(index).toString() + "\n");
}
public void saveData(String fileName)
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(fileName));
pw = new PrintWriter(fw);
for(Customer customer : this.customers)
{
pw.write(customer.toString() + System.lineSeparator());
}
pw.flush();
fw.close();
pw.close();
System.out.println("\nData saved successfully to file " +
fileName
+ "\nGood Bye!\n");
} catch (IOException ex) {
System.out.println("Error in opening file " + fileName + ": " +
ex.getMessage());
}
}
}
CustomerDBApp.java (Main class)
import java.util.Scanner;
public class CustomerDBApp {
private static final String INPUT_FILENAME = "database.txt";
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
String command = "";
Database db = new Database();
db.readData(INPUT_FILENAME);
do
{
printMenu();
System.out.print("Command:> ");
command = sc.nextLine().trim();
switch(command)
{
case "a":
{
db.addCustomer();
break;
}
case "f":
{
db.searchCustomer();
break;
}
case "q":
{
System.out.print("\nEnter the output file name (Eg: out.txt):
");
String outFileName = sc.nextLine().trim();
db.saveData(outFileName);
break;
}
default:
System.out.println("\nInvalid choice\n");
}
}while(!command.equals("q"));
}
private static void printMenu()
{
System.out.println("(a) Add a customer (f) Search for a customer
details "
+ "(q) Save customer data to file and exit");
}
}
********************************************************************** SCREENSHOT *****************************************************
OUTPUT FILE SAMPLE (dbOut.txt as Output file name, can give any name):
CONSOLE OUTPUT: