The main problem is just getting my Driver class to enter the Customer Records into the customer list.
(Also, would closing the data stream automatically save the info to the text file?)
Here's the 3 classes. (Last one isn't finished yet
public class CustomerRecords {
//Unique number assigned to each Customer
private int customerNumber = 0;
//Customer first name
private String firstName;
//Customer last name
private String lastName;
//Customer balance
private double balance =0;
public CustomerRecords(int newNum,String newFirst, String newLast,double newBalance){
firstName = newFirst;
lastName = newLast;
customerNumber = newNum;
balance = newBalance;
}
public String getLast(){
return lastName;
}
public String getFirst(){
return firstName;
}
public int getNumber(){
return customerNumber;
}
public double getBalance(){
return balance;
}
public void setLast(String newLast){
lastName = newLast;
}
public void setFirst(String newFirst){
firstName = newFirst;
}
public void setNumber(int newNum){
customerNumber = newNum;
}
public void setBalance(double newBalance){
balance = newBalance;
}
public String customerString(){
return customerNumber + " " +firstName + " " + lastName + " " + balance;
}
}
import java.io.*;
import java.util.Scanner;
public class CustomerList{
//Number of customers in the list
private int count = 0;
//array of CustomerRecords objects
private CustomerRecords[] data;
public void CustomerList(File fileName){
//Create a file instance
//Create a Scanner for the file
try{
Scanner input = new Scanner(fileName);
//Read data from a file
while(input.hasNext()){
int idNum = input.nextInt();
String first = input.next();
String last = input.next();
double balance = input.nextDouble();
CustomerRecords CR1 = new CustomerRecords(idNum,first,last,balance);
}
}
catch (IOException e){
System.out.print("Error, file could not be opened. Exception:" +e);
System.exit(-1);
}
}
public void enterCustomerRecords(CustomerRecords newRecord){
data[count] = newRecord;
count++;
}
public void getCustomer(int key){
for (int i = 0; i< count; i++){
int newNum =data[i].getNumber();
if (newNum == (key)){
System.out.println(data[i].getNumber() + " " +
data[i].getFirst() + " " + data[i].getLast()+
" " + data[i].getBalance());
}
else
System.out.print("Customer number does not exist. Please try again");
}
}
}
import java.io.*;
import java.util.Scanner;
public class CustomerListDriver {
public static void main(String[] args) throws Exception {
File file = new File("database.txt");
CustomerList CR2 = new CustomerList(file);
Scanner input = new Scanner(System.in);
while(true){
System.out.print("\nCommand list:\n");
System.out.print("a - add customer information to database\n");
System.out.print("f - use customer ID number to find customer\n");
System.out.print("q - update the customer list and terminate the program\n");
System.out.print("Enter command: \n");
String cmnd = input.next();
if( cmnd.equalsIgnoreCase("a")){
System.out.print("Enter 5 digit identification number: ");
int idNum = input.nextInt();
System.out.print("Enter first name: ");
String first = input.next();
System.out.print("Enter last name: ");
String last = input.next();
System.out.print("Enter customer balance: ");
double balance = input.nextDouble();
CustomerRecords CR1 = new CustomerRecords(idNum,first,last,balance);
CR2.enterCustomerRecords(CR1);
PrintWriter output = new PrintWriter(file);
output.print(idNum + " " + first + " " + last + " " + balance);
}
}
}
}
Thanks for any help or suggestions you can provide guys.
The text file provided by the teacher contains the following information:
12345 Jacky Smith 123.22 11111 Sarah Smith 45.89 22222 Sue Johnson 7765.98 33333 Billy Hunts 374.99

New Topic/Question
Reply




MultiQuote




|