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

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.

Solutions

Expert Solution

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


Related Solutions

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...
Create a program that allows a user to input customer records (ID number, first name, last...
Create a program that allows a user to input customer records (ID number, first name, last name, and balance owed) and save each record to a file. When you run the main program, be sure to enter multiple records. Once you create the file, open it and display the results to the user Save the file as  CustomerList.java
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...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT