Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help... You should now have 4 classes that you built in MP #1. They are:

ID: 3855332 • Letter: P

Question

Please help...

You should now have 4 classes that you built in MP #1. They are: Account, Person, Customer and the AccountList. These 4 classes should be complete and Tested. Now we are going to make these classes talk to Files. We are going to read and write data to files for these business classes. There are only 2 classes that we need to tie to files. They are: Customer and Account. Part A:(Tie 2 Business Classes to Files) – Let’s start with the Account class.

Let’s make it so that we can look up and find an Account in the “Accounts.txt” file. The Accounts are organized by Account No. So we should be able to look in the File for AcctNo “90001”, and it should give us back all the data for that account, like, AcctNo, CID, Type and Balance, etc. So we will need to read from the “Accounts.txt” file and select the AcctNo “90001”. The File is delimited by “:”(colons). Take a look at the file.

Account:

Customer:

Code for testing ‘Select’ that goes in main:

Account a1 = new Account();

a1.select(“90001”);

a1.display();

Now do the same for the Customer class.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here's what I got...

public class Customer2 extends Person2{
//Properties
public int password;
public int customerID;
public AccountList2 list;
//Constructors
public Customer2(){
super();
list = new AccountList2();
password = 0;
customerID = 0;
}
public Customer2(int pw, int cid ,String fn, String ln, String addr, String em, AccountList2 li){
super(fn, ln, addr, em);
password = pw;
list = li;
customerID = cid;
}
//Behaviors
public void setPw(int pw){
password = pw;
}
public int getPw(){
return password;
}
public void setList(AccountList2 li){
list = li;
}
public AccountList2 getList(){
return list;
}
public void setCid(int cid){
customerID = cid;
}
public int getCid(){
return customerID;
}
public void display(){
System.out.println("");
System.out.println("Password: " + getPw());
System.out.println("Customer ID: " + getCid());
super.display();
list.display();
}
public static void main(String[] args){
Customer2 c2;
c2 = new Customer2();
c2.display();
Customer2 c3;
c3 = new Customer2(9999,3006,"Bill","Gates","Washington","bgates@msn.com", new AccountList2());
c3.display();
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Account2{
//Properties
private int accountNo;
private int customerId;
private String accountType;
private double balance;
//Constructors
public Account2(){
accountNo = 0;
customerId = 0;
accountType = "";
balance = 0.0;
}
public Account2(int acc, int cid, String accType, double bal){
accountNo = acc;
customerId = cid;
accountType = accType;
balance = bal;
}
//Behaviors
public void setAccNo(int acc){
accountNo = acc;
}
public int getAccNo(){
return accountNo;
}
public void setCid(int cid){
customerId = cid;
}
public int getCid(){
return customerId;
}
public void setAccType(String accType){
accountType = accType;
}
public String getAccType(){
return accountType;
}
public void setBal(double bal){
balance = bal;
}
public double getBal(){
return balance;
}
public void display(){
System.out.println("===================== ");
System.out.println("Account Number: " + getAccNo());
System.out.println("Customer ID: " + getCid());
System.out.println("Account Type: " + getAccType());
System.out.println("Balance: $" + getBal());
}
public static void main(String[] args){
Account2 a2;
a2 = new Account2(90000,3003,"SAV",8855.90);
a2.display();
Account2 a3;
a3 = new Account2(90001,3003,"CHK",786.54);
a3.display();
Account2 a4;
a4 = new Account2(90002,3001,"SAV",9654.13);
a4.display();
Account2 a5;
a5 = new Account2(90005,3006,"MMA",700356.23);
a5.display();
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.*;
import java.util.StringTokenizer;
public class GetData2{
public String data = "";
public String getData2(){
return data;
}
public GetData2(int accountNo, int customerId){
try{
int s1;
File f1 = new File(customerId);
FileInputStream fis = new FileInputStream(f1);
BufferedReader bin = new BufferedReader(new InputStreamReader(fis));
while(true){
s1 = bin.readLine();
if(s1 == null)
break;
System.out.println(s1);
StringTokenizer tok = new StringTokenizer(s1, ":");
String token = tok.nextToken();
System.out.println(token);
if(token.equals(accountNo)){
data = s1;
break;
}
}
}
catch(IOException ie){
System.out.println(ie);
}
}
public static void main(String[] args){
String token;
GetData2 gd = new GetData2("90000","Accounts.txt");
String d = gi.getData2();
System.out.println(d);
StringTokenizer tok = new StringTokenizer(d, ":");
token = token.nextToken();
System.out.println("Account Number: " + token);
token = token.nextToken();
System.out.println("Customer ID: " + token);
token = token.nextToken();
System.out.println("Account Type: " + token);
token = token.nextToken();
System.out.println("Balance: " + token);
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks in advance!

Explanation / Answer

Hello.. You have not provided the Person2 and AccountList2 classes which are needed to code the Cusotmer2.select() method. I have coded it and put comments for you to be able to fix them. You will have to use appropriate method names there from Person2 or AccountList2 classes.

Also when select () is called on Customer2, should it have the list of accounts to be fetched or should be empty ? That is not specified in the question. I have code it to fetch the Accounts file and get all accounts for the customer and fill up the list. You can remove that part of code if not needed and leave the list empty.

I have assumed the file names to be Customers.txt and Accounts.txt. If they are different from these, please update in code.

Let me know if you need any help. Post a comment. Please rate the answer if it helped. Thank you.

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

import java.util.StringTokenizer;

public class Customer2 extends Person2 {

   // Properties

   public int password;

   public int customerID;

   public AccountList2 list;

   // Constructors

   public Customer2() {

       super();

       list = new AccountList2();

       password = 0;

       customerID = 0;

   }

   public Customer2(int pw, int cid, String fn, String ln, String addr, String em, AccountList2 li) {

       super(fn, ln, addr, em);

       password = pw;

       list = li;

       customerID = cid;

   }

   // Behaviors

   public void setPw(int pw) {

       password = pw;

   }

   public int getPw() {

       return password;

   }

   public void setList(AccountList2 li) {

       list = li;

   }

   public AccountList2 getList() {

       return list;

   }

   public void setCid(int cid) {

       customerID = cid;

   }

   public int getCid() {

       return customerID;

   }

   public void display() {

       System.out.println("");

       System.out.println("Password: " + getPw());

       System.out.println("Customer ID: " + getCid());

       super.display();

       list.display();

   }

  

   public void select(String custId){

       try{

           //reset all fields to default before searching begins

           customerID = 0;

           password = 0;

          

           //Fix these by calling appropriate methods from Person2

           setFname(null);

           setLName(null);

           setEmail(null);

           setAddress(null);

          

           String line;

           File f1 = new File("Customers.txt");

           Scanner inFile = new Scanner(f1);

           while(inFile.hasNextLine()){

               line = inFile.nextLine();

               StringTokenizer tokenizer = new StringTokenizer(line, ":");

               String token = tokenizer.nextToken();

               System.out.println(token);

               if(token.equals(custId)){

                   customerID = Integer.parseInt(token);

                   password = Integer.parseInt(tokenizer.nextToken());

                  

                   //Please FIX these with appropriate method names from Person2 class

                   setFname(tokenizer.nextToken());

                   setLName(tokenizer.nextToken());

                   setAddress(tokenizer.nextToken());

                   setEmail(tokenizer.nextToken());

                   list = new AccountList2();

                  

                   //get all accounts from accounts file

                   Scanner accFile = new Scanner(new File("Accounts.txt"));

                  

                   while(accFile.hasNextLine())

                   {

                       tokenizer = new StringTokenizer(accFile.nextLine(), ":");

                       String accNo = tokenizer.nextToken(); // acount number

                       String cid = tokenizer.nextToken();

                       if(cid.equals(custId))

                       {

                           //Please FIX this line with approapriate method call from AccountList2

                           //if the list elements are just account numbers, add the number

                           //if list elements are Account2 objects ,then create an Account2 object and add

                          

                           /*

                           * You can create Account2 object like this

                           acc.setAccNo(Integer.parseInt(accNo));

                           acc.setCid(Integer.parseInt(cid));

                           acc.setAccType(tokenizer.nextToken());

                           acc.setBal(Double.parseDouble(tokenizer.nextToken())); */

                           list.add(Integer.parseInt(accNo));

                       }

                   }

                   accFile.close();

                  

                   break;

               }

           }

           inFile.close();

       }

       catch(IOException ie){

           System.out.println(ie);

       }

      

   }

   public static void main(String[] args) {

       Customer2 c2;

       c2 = new Customer2();

       c2.display();

       Customer2 c3;

       c3 = new Customer2(9999, 3006, "Bill", "Gates", "Washington", "bgates@msn.com", new AccountList2());

       c3.display();

      

       c2.select("3003");

       c2.display();

      

   }

}

===========

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Scanner;

import java.util.StringTokenizer;

public class Account2 {

   // Properties

   private int accountNo;

   private int customerId;

   private String accountType;

   private double balance;

   // Constructors

   public Account2() {

       accountNo = 0;

       customerId = 0;

       accountType = "";

       balance = 0.0;

   }

   public Account2(int acc, int cid, String accType, double bal) {

       accountNo = acc;

       customerId = cid;

       accountType = accType;

       balance = bal;

   }

   // Behaviors

   public void setAccNo(int acc) {

       accountNo = acc;

   }

   public int getAccNo() {

       return accountNo;

   }

   public void setCid(int cid) {

       customerId = cid;

   }

   public int getCid() {

       return customerId;

   }

   public void setAccType(String accType) {

       accountType = accType;

   }

   public String getAccType() {

       return accountType;

   }

   public void setBal(double bal) {

       balance = bal;

   }

   public double getBal() {

       return balance;

   }

   public void display() {

       System.out.println("===================== ");

       System.out.println("Account Number: " + getAccNo());

       System.out.println("Customer ID: " + getCid());

       System.out.println("Account Type: " + getAccType());

       System.out.println("Balance: $" + getBal());

   }

  

   public void select(String accNo){

       //before searching reset all of them to default values

       accountNo = 0;

       accountType = "";

       balance = 0;

       customerId = 0;

      

       try{

           String line;

           File f1 = new File("Accounts.txt");

           Scanner inFile = new Scanner(f1);

           while(inFile.hasNextLine()){

               line = inFile.nextLine();

               StringTokenizer tokenizer = new StringTokenizer(line, ":");

               String token = tokenizer.nextToken();

               System.out.println(token);

               if(token.equals(accNo)){

                   accountNo = Integer.parseInt(token);

                   customerId = Integer.parseInt(tokenizer.nextToken());

                   accountType = tokenizer.nextToken();

                   balance = Double.parseDouble(tokenizer.nextToken());

                   break;

               }

           }

           inFile.close();

       }

       catch(IOException ie){

           System.out.println(ie);

       }

   }

   public static void main(String[] args) {

       Account2 a2;

       a2 = new Account2(90000, 3003, "SAV", 8855.90);

       a2.display();

       Account2 a3;

       a3 = new Account2(90001, 3003, "CHK", 786.54);

       a3.display();

       Account2 a4;

       a4 = new Account2(90002, 3001, "SAV", 9654.13);

       a4.display();

       Account2 a5;

       a5 = new Account2(90005, 3006, "MMA", 700356.23);

       a5.display();

      

       a2.select("90004");

       a2.display();

   }

}