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
Based on your design/code, please draw a simple but detailed UML diagram as well? (This is the question that needs to be answered!) the code isn't needed for this.
package file;
import java.io.*;
import java.util.*;
// Defines a class Customer to store customer information
class Customer
{
// Instance variable to store customer information
String customerID;
String firstName, lastName;
double balance;
// Default constructor to assign default values
// to instance variables
Customer()
{
customerID = firstName = lastName = "";
balance = 0.0;
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variables
Customer(String id, String fn, String ln, double ba)
{
customerID = id;
firstName = fn;
lastName = ln;
balance = ba;
}// End of parameterized constructor
// Method to set id
void setID(String id)
{
customerID = id;
}// End of method
// Method to set first name
void setFirstName(String fn)
{
firstName = fn;
}// End of method
// Method to set last name
void setLastName(String ln)
{
lastName = ln;
}// End of method
// Method to set balance
void setBalance(double ba)
{
balance = ba;
}// End of method
// Method to return id
String getID()
{
return customerID;
}// End of method
// Method to return first name
String getFirstName()
{
return firstName;
}// End of method
// Method to return last name
String getLastName()
{
return lastName;
}// End of method
// Method to return balance
double getBalance()
{
return balance;
}// End of method
// Overrides toString() method to return customer information
public String toString()
{
return "\n Customer ID: " + customerID +
"\n First Name: " + firstName +
"\n Last Name: " + lastName +
"\n Balance: $" + balance;
}// End of method
}// End of class Customer
// Driver class CustomerFile definition
public class CustomerFile
{
// Creates a Scanner class object to accept data from console
static Scanner read = new Scanner(System.in);
// Declares an array of object
static Customer cus[];
// Method to display menu and return user choice
static char menu()
{
// Displays menu
System.out.println("\n\n************* MENU *************");
System.out.println("\n a) Reading and loading customer’s " +
"info from the text file \n" +
" b) Entering new customer’s info \n" +
" c) Looking up existing customer’s info " +
"using customer’s number(ID) \n" +
" d) Deleting the customer’s info \n" +
" e) Updated database will be stored in the " +
"text file");
// Accepts and returns user choice
System.out.print("\n Enter your choice: ");
return read.next().charAt(0);
}// End of method
// Method to red file contents and stores it in
// Customer array of object
static int readFile()
{
// To store number of records
int len = 0;
// Local variable to store data read from file
String ID;
String fName, lName;
double bal;
// Try block
try
{
// File Reader object created to read data from
// customerData.txt file
File reader = new File("customerData.txt");
// Scanner class object created read file
Scanner sc = new Scanner(reader);
// Loops till data available
while(sc.hasNextDouble())
{
// Reads data
ID = sc.next();
fName = sc.next();
lName = sc.next();
bal = sc.nextDouble();
// Crates an object using parameterized constructor
// Assigns the object at len index position
cus[len] = new Customer(ID, fName, lName, bal);
// Increase counter by one
len++;
}//End of while
//Closer the file
sc.close();
} //End of try block
//Catch block to handle file not found exception
catch(FileNotFoundException e)
{
System.out.println("File Not found");
e.printStackTrace();
}//End of catch
// Returns the record counter
return len;
}//End of method
// Method to write customer data to file
static void writeFile(int len)
{
// File class object created
File file = new File ("customerData.txt");
Scanner sc = new Scanner(System.in);
// PrintWriter object initialized to null
PrintWriter pw = null;
// Try block begins
try
{
// PrintWriter object created to write onto
// the file name specified with file object
pw = new PrintWriter (file);
// Loops till number of records
for(int c = 0; c < len; c++)
{
// Writes data to file
pw.printf(cus[c].getID() + " ");
pw.printf(cus[c].getFirstName() + " ");
pw.printf(cus[c].getLastName() + " ");
pw.printf(String.valueOf(cus[c].getBalance()));
if(c != len-1)
// For new line
pw.println();
}// End of for loop
}// End of try block
// Catch to handle file not found exception
catch (FileNotFoundException e)
{
System.out.println("\n Error: File not found for " +
"writing!");
e.printStackTrace();
}// End of catch
// Close printWriter
pw.close();
}//End of method
// Method to add a record to array of object
static int addCustomer(int len)
{
// Local variables
String id;
String fName, lName;
double bal;
// Accepts data from the user
System.out.print("\n Enter customer ID: ");
id = read.next();
System.out.print("\n Enter customer first name: ");
fName = read.next();
System.out.print("\n Enter customer last name: ");
lName = read.next();
System.out.print("\n Enter balance: ");
bal = read.nextDouble();
// Creates an object using parameterized constructor
// stores the object at len index position
// Increase the index counter by one
cus[len++] = new Customer(id, fName, lName, bal);
// Returns the record counter
return len;
}// End of method
// Method to search a customer based on the id
// passed as parameter
// Returns the found index position if found
// Otherwise returns -1
static int searchCustomer(int len, String id)
{
// To store the found index position
// Initially -1 for not found
int pos = -1;
// Loops till number of records
for(int c = 0; c < len; c++)
{
// Checks if current customer id is equals to
// parameter customer id
if(cus[c].getID().compareTo(id) == 0)
{
// Assigns the counter value as found index position
pos = c;
// Stop the loop
break;
}// End of if condition
}// End of for loop
// Returns the position
return pos;
}// End of method
// Method to delete a customer record based on the
// customer id passed as parameter
static int deleteCustomer(int len, String id)
{
// Calls the method to search the id
// Stores the return found status
int pos = searchCustomer(len, id);
// If the found position is not -1 record found
if(pos != -1)
{
// Loops from found position till length
for(int c = pos; c < len; c++)
// Shifts each record to left
cus[c] = cus[c + 1];
// Displays the deleted record
System.out.print("\n Record deleted successfuly \n" +
cus[pos]);
// Decrease the record counter by one
len--;
}// End of if condition
// Otherwise record not found, display error message
else
System.out.print("\n Record not found to delete: " +
id);
// Returns the record counter
return len;
}// End of method
// main method definition
public static void main(String ss[])
{
// Creates an array of object of size 100
cus = new Customer[100];
// Record counter initially 0
int len = 0;
// To store the customer id entered by the user
String id;
// Loops till user choice is not 'e' or 'E'
do
{
// Calls the method to accept user choice
// Checks the returned user choice
// and calls the appropriate method
switch(menu())
{
case 'a':
case 'A':
len = readFile();
break;
case 'b':
case 'B':
len = addCustomer(len);
break;
case 'c':
case 'C':
System.out.print("\n Enter the id to search: ");
id = read.next();
int pos = searchCustomer(len, id);
if(pos != -1)
System.out.println(cus[pos]);
else
System.out.println("\n No record found on" +
"ID: " + id);
break;
case 'd':
case 'D':
System.out.print("\n Enter the id to delete: ");
id = read.next();
len = deleteCustomer(len, id);
break;
case 'e':
case 'E':
writeFile(len);
System.exit(0);
default:
System.out.print("\n Invalid choice!");
}// End of switch case
}while(true);// End of do - while loop
}// End of main method
}// End of driver class
Sample Output:
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: a
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: c
Enter the id to search: 4545
No record found onID: 4545
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: c
Enter the id to search: 12345
Customer ID: 12345
First Name: Sebastian
Last Name: vanDelden
Balance: $123.22
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: b
Enter customer ID: 8888
Enter customer first name: Mohan
Enter customer last name: Sahu
Enter balance: 89000
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: b
Enter customer ID: 9999
Enter customer first name: Pyari
Enter customer last name: Sahu
Enter balance: 98000
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: c
Enter the id to search: 9999
Customer ID: 9999
First Name: Pyari
Last Name: Sahu
Balance: $98000.0
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: d
Enter the id to delete: 12345
Record deleted successfuly
Customer ID: 11111
First Name: Sarah
Last Name: Smith
Balance: $45.89
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: c
Enter the id to search: 12345
No record found onID: 12345
************* MENU *************
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
e) Updated database will be stored in the text file
Enter your choice: e
customerData.txt before update
12345 Sebastian vanDelden 123.22
11111 Sarah Smith 45.89
22222 Sue Johnson 7765.98
33333 Billy Hunts 374.99
customerData.txt after update
11111 Sarah Smith 45.89
22222 Sue Johnson 7765.98
33333 Billy Hunts 374.99
8888 Mohan Sahu 89000.0
9999 Pyari Sahu 98000.0