Question

In: Computer Science

We plan to develop customer’s database that stores customer’s number (ID), first name, last name and...

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

Solutions

Expert Solution

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:


Related Solutions

Employee ID First Name Last Name email Title Address Extension Department Department ID Hiring Date Department...
Employee ID First Name Last Name email Title Address Extension Department Department ID Hiring Date Department Phone # 0001 John Smith jsmith Accountant 1300 West st 5775 Accounting 2100 8/1998 407-366-5700 0002 Brian Miller badams Admin Assistant 1552 Palm dr 5367 Human resource 2300 4/1995 407-366-5300 0003 James Miller miller Inventory Manager 2713 Buck rd 5432 Production 2520 8/1998 407-366-5400 0004 John Jackson jackson_sam Sales Person 433 tree dr 5568 Sales 2102 6/1997 407-366-5500 0005 Robert Davis Davis Manager 713...
Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth...
Create a table ‘StudentInfo’ with following fields: ID First Name Last Name SSN Date of Birth Create a table ‘ClassInfo’ table: ID Class Name Class Description Create a table ‘RegisteredClasses’ table: StudentID ClassID The RegisteredClasses table should have a foreign key relationship to StudentInfo and ClassInfo tables for the respective IDs. Also the IDs in StudentInfo and ClassInfo need to be primary keys. When you submit the file your email should also contain the following SQL Queries: Query to show...
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number...
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. This class provides the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. b. The class  checkingAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
Problem 44 Write a query to display the employee number, last name, first name, and sum...
Problem 44 Write a query to display the employee number, last name, first name, and sum of invoice totals for all employees who completed an invoice. Sort the output by employee last name and then by first name (Partial results shown in Figure P7.44).
Please consider FIRST 6 Character from your name and LAST 2 digits of your students ID....
Please consider FIRST 6 Character from your name and LAST 2 digits of your students ID. Calculate the checksum for the above text. The text needs to be divided into 2-byte (16-bit) words. Also check that the data is reached without any alteration using your checksum. [ the text is MajedA08]
Create the following SQL queries using the lyrics database below 1. List the first name, last...
Create the following SQL queries using the lyrics database below 1. List the first name, last name, and region of members who do not have an email. 2. List the first name, last name, and region of members who do not have an email and they either have a homephone ending with a 2 or a 3. 3. List the number of track titles that begin with the letter 's' and the average length of these tracks in seconds 4....
QUESTION 71 For first name column in our subsidiary’s database in Japan, we should use _____...
QUESTION 71 For first name column in our subsidiary’s database in Japan, we should use _____ A. nchar(30) B. char(30) C. varchar(30) D. nvarchar(30) QUESTION 72 for column definition of middle name in US., we should use _____ A. nchar(20) B. char(20) C. varchar(20) D. varchar2(20)
List customer id, customer full name (Last name, full name, state) for those customers that have...
List customer id, customer full name (Last name, full name, state) for those customers that have ordered more than once. List customers (order id, customer id, and customer last name) that had more than 2 -- products in their order. Order your result based on customer id followed by order id SQL SERVER DATABASE
Develop an algorithm to implement an employee list with employee ID ,name designation and department using...
Develop an algorithm to implement an employee list with employee ID ,name designation and department using link list and perform the following operation on the list i)add employee details based on department ii)remove employee details iv)count the number of employee in each department
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT