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

public class Account { // data fields private int ACCNumber; private double ACCB

ID: 3880891 • Letter: P

Question

public class Account {

// data fields

private int ACCNumber;

private double ACCBalance;

private String CustomerID;

private String DebitNo;

private String BName;

private String BID;

private String CreditNo;

private double MaxCredit;

//Savings Account

public Account(int ACCNumber, String CustomerID, double ACCBalance) {

this.ACCNumber = ACCNumber;

this.CustomerID = CustomerID;

this.ACCBalance = ACCBalance;}

//Checking Account

public Account(int ACCNumber, String CustomerID, double ACCBalance, String DebitNo)

{

this.ACCNumber = ACCNumber;

this.CustomerID = CustomerID;

this.ACCBalance = ACCBalance;

this.DebitNo = DebitNo;

}

//Business Account

public Account (int ACCNumber, String CustomerID, double ACCBalance, String BName, String BID)

{

this.ACCNumber = ACCNumber;

this.CustomerID = CustomerID;

this.ACCBalance = ACCBalance;

this.BName = BName;

this.BID = BID;

}

//Credit Card Account

public Account(String CustomerID, String CreditNo, double ACCBalance, double MaxCredit)

{

this.CustomerID = CustomerID;

this.CreditNo = CreditNo;

this.ACCBalance = ACCBalance;

this.MaxCredit = MaxCredit;

}

public String getCreditNo() {return CreditNo;}

public void setCreditNo(String CreditNo) {this.CreditNo = CreditNo;}

public double getMaxCredit() {return MaxCredit;}

public void setMaxCredit(double MaxCredit) {this.MaxCredit = MaxCredit;}

public String getDebitNo() {return DebitNo;}

public void setDebitNo(String DebitNo) {this.DebitNo = DebitNo;}

public String getBID() {return BID;}

public void setBID(String BID) {this.BID = BID;}

public String getBName() {return BName;}

public void setBName(String BName) {this.BName = BName;}

public String getCustomerID() {return CustomerID;}

public void setCustomerID(String CustomerID) {this.CustomerID = CustomerID;}

public int getACCNumber() {return ACCNumber;}

public void setACCNumber(int ACCNumber) {this.ACCNumber = ACCNumber;}

public double getACCBalance() {return ACCBalance;}

public void setACCBalance(double ACCBalance) {this.ACCBalance = ACCBalance;}

public class Customer{

private String ID;

private String LName;

private String FName;

private String Address;

private String City;

public Customer(String ID, String LName, String FName, String Address, String City)

{

this.ID =ID;

this.LName = LName;

this.FName = FName;

this.Address = Address;

this.City = City;

}

public String getID() {return ID;}

public void setID(String iD) {ID = iD;}

public String getLName() { return LName;}

public void setLName(String LName) {this.LName = LName;}

public String getFName() {return FName;}

public void setFName(String FName) {this.FName = FName;}

public String getAddress() {return Address;}

public void setAddress(String Address) {this.Address = Address;}

public String getCity() {return City;}

public void setCity(String City) {this.City = City;}}

Q: In the main class, write a method getDataFromFile to read data from a given file. This method should read data from the file, create appropriate objects for the customer and account, save the data into an array.

Make sure to use Scanner, not the BufferedReader.

If the account is Checking, order is CustomerID - LName - FName - Address - City - AccountType - ACCNumber - ACCBalance - DebitNo

If the account is Savings, order is CustomerID - LName - FName - Address - City - AccountType - ACCNumber- ACCBalance

If the account is Business, order is CustomerID - LName - FName - Address - City - AccountType - ACCNumber - ACCBalance - BName - BID

If the account is CreditCard, order is CustomerID - LName - FName - Address - City - AccountType - CreditNo - ACCBalance - MaxCredit

THis is the .txt that has to be used

C8392380567 Sage, Amy Ping Ting Road Edmonton Checking 873387 5000 0000-6666-6666-6666

C8954385123 Lee, Bob Texaco Road Calgary Savings 827366 9480

C2389490434 Neil, Carson Deerfoot Trail Otawa Business 763655 65000 Emporia LLC 87-927736

C9384899234 Ko, David Unversity Drive Stillwater CreditCard 7667-9899-8776-1234 430 4000

C0930238083 Warren, John Ogden Road Tyler Checking 726615 1230 0000-2222-2222-2222

Explanation / Answer

public class Account {

   // data fields

   private int ACCNumber;

   private double ACCBalance;

   private String CustomerID;

   private String DebitNo;

   private String BName;

   private String BID;

   private String CreditNo;

   private double MaxCredit;

   //Savings Account

   public Account(int ACCNumber, String CustomerID, double ACCBalance) {

   this.ACCNumber = ACCNumber;

   this.CustomerID = CustomerID;

   this.ACCBalance = ACCBalance;}

   //Checking Account

   public Account(int ACCNumber, String CustomerID, double ACCBalance, String DebitNo)

   {

   this.ACCNumber = ACCNumber;

   this.CustomerID = CustomerID;

   this.ACCBalance = ACCBalance;

   this.DebitNo = DebitNo;

   }

   //Business Account

   public Account (int ACCNumber, String CustomerID, double ACCBalance, String BName, String BID)

   {

   this.ACCNumber = ACCNumber;

   this.CustomerID = CustomerID;

   this.ACCBalance = ACCBalance;

   this.BName = BName;

   this.BID = BID;

   }

   //Credit Card Account

   public Account(String CustomerID, String CreditNo, double ACCBalance, double MaxCredit)

   {

   this.CustomerID = CustomerID;

   this.CreditNo = CreditNo;

   this.ACCBalance = ACCBalance;

   this.MaxCredit = MaxCredit;

   }

   public String getCreditNo() {return CreditNo;}

   public void setCreditNo(String CreditNo) {this.CreditNo = CreditNo;}

   public double getMaxCredit() {return MaxCredit;}

   public void setMaxCredit(double MaxCredit) {this.MaxCredit = MaxCredit;}

   public String getDebitNo() {return DebitNo;}

   public void setDebitNo(String DebitNo) {this.DebitNo = DebitNo;}

   public String getBID() {return BID;}

   public void setBID(String BID) {this.BID = BID;}

   public String getBName() {return BName;}

   public void setBName(String BName) {this.BName = BName;}

   public String getCustomerID() {return CustomerID;}

   public void setCustomerID(String CustomerID) {this.CustomerID = CustomerID;}

   public int getACCNumber() {return ACCNumber;}

   public void setACCNumber(int ACCNumber) {this.ACCNumber = ACCNumber;}

   public double getACCBalance() {return ACCBalance;}

   public void setACCBalance(double ACCBalance) {this.ACCBalance = ACCBalance;}

   /* (non-Javadoc)

   * @see java.lang.Object#toString()

   */

   @Override

   public String toString() {

       return "Account [ACCNumber=" + ACCNumber + ", ACCBalance=" + ACCBalance + ", CustomerID=" + CustomerID

               + ", DebitNo=" + DebitNo + ", BName=" + BName + ", BID=" + BID + ", CreditNo=" + CreditNo

               + ", MaxCredit=" + MaxCredit + "]";

   }

}

public class Customer{

private String ID;

private String LName;

private String FName;

private String Address;

private String City;

public Customer(String ID, String LName, String FName, String Address, String City)

{

this.ID =ID;

this.LName = LName;

this.FName = FName;

this.Address = Address;

this.City = City;

}

public String getID() {return ID;}

public void setID(String iD) {ID = iD;}

public String getLName() { return LName;}

public void setLName(String LName) {this.LName = LName;}

public String getFName() {return FName;}

public void setFName(String FName) {this.FName = FName;}

public String getAddress() {return Address;}

public void setAddress(String Address) {this.Address = Address;}

public String getCity() {return City;}

public void setCity(String City) {this.City = City;}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

   return "Customer [ID=" + ID + ", LName=" + LName + ", FName=" + FName + ", Address=" + Address + ", City=" + City

           + "]";

}

}

import java.io.File;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

   //Arraylist as we can save any number of details you can alse you fixed size array

   ArrayList<Customer> customers;

   ArrayList<Account> accounts;

  

   public Main() {

       customers = new ArrayList<>();

       accounts = new ArrayList<>();

      

   }

  

   public void getDataFromFile(String fileName){

       Scanner sc;

       try{

           //file

       File file = new File(fileName);

       //scanner

       sc = new Scanner(file);

       //read line

   while ( sc.hasNextLine()) {

       //split line

       String[] tokens = sc.nextLine().split(" ");

       Customer customer = new Customer(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);

       customers.add(customer);

       if (tokens[5].equalsIgnoreCase("Checking")){

           Account account= new Account(Integer.parseInt(tokens[6]), tokens[0], Double.parseDouble(tokens[7]), tokens[8]);

          accounts.add(account);

       }

       if (tokens[5].equalsIgnoreCase("Savings")){

           Account account= new Account(Integer.parseInt(tokens[6]), tokens[0],Double.parseDouble( tokens[7]));

          accounts.add(account);

       }

       if (tokens[5].equalsIgnoreCase("Business")){

           Account account= new Account(Integer.parseInt(tokens[6]), tokens[0], Double.parseDouble(tokens[7]), tokens[8], tokens[9]);

           accounts.add(account);

       }

       if (tokens[5].equalsIgnoreCase("CreditCard")){

           Account account= new Account(tokens[0], tokens[6], Double.parseDouble(tokens[7]), Double.parseDouble(tokens[8]));

           accounts.add(account);

       }

     

   }

       }

       catch(Exception e){

           System.err.println(e);

       }

      

   }

   //prints both arrays using for loop

   public void printArray(){

       System.out.println("Customers:");

       for (Customer customer:customers){

           System.out.println(customer.toString());

       }

       System.out.println("Accounts:");

       for (Account account:accounts){

           System.out.println(account.toString());

       }

   }

   public static void main(String[] args) {

       Main main = new Main();

       //you can change file name here

       main.getDataFromFile("/Users/sw007/Documents/workspace/test/src/test/detail.txt");

       main.printArray();

      

   }

}

Sample output:

Customers:

Customer [ID=C8392380567, LName=Sage, FName=Amy, Address=PingTingRoad, City=Edmonton]

Customer [ID=C8954385123, LName=Lee, FName=Bob, Address=TexacoRoad, City=Calgary]

Customer [ID=C2389490434, LName=Neil, FName=Carson, Address=DeerfootTrail, City=Otawa]

Customer [ID=C9384899234, LName=Ko, FName=David, Address=UnversityDrive, City=Stillwater]

Customer [ID=C0930238083, LName=Warren, FName=John, Address=OgdenRoad, City=Tyler]

Accounts:

Account [ACCNumber=873387, ACCBalance=5000.0, CustomerID=C8392380567, DebitNo=0000-6666-6666-6666, BName=null, BID=null, CreditNo=null, MaxCredit=0.0]

Account [ACCNumber=827366, ACCBalance=9480.0, CustomerID=C8954385123, DebitNo=null, BName=null, BID=null, CreditNo=null, MaxCredit=0.0]

Account [ACCNumber=763655, ACCBalance=65000.0, CustomerID=C2389490434, DebitNo=null, BName=Emporia, BID=LLC87-927736, CreditNo=null, MaxCredit=0.0]

Account [ACCNumber=0, ACCBalance=430.0, CustomerID=C9384899234, DebitNo=null, BName=null, BID=null, CreditNo=7667-9899-8776-1234, MaxCredit=4000.0]

Account [ACCNumber=726615, ACCBalance=1230.0, CustomerID=C0930238083, DebitNo=0000-2222-2222-2222, BName=null, BID=null, CreditNo=null, MaxCredit=0.0]

detail.txt

C8392380567 Sage Amy PingTingRoad Edmonton Checking 873387 5000 0000-6666-6666-6666

C8954385123 Lee Bob TexacoRoad Calgary Savings 827366 9480

C2389490434 Neil Carson DeerfootTrail Otawa Business 763655 65000 Emporia LLC87-927736

C9384899234 Ko David UnversityDrive Stillwater CreditCard 7667-9899-8776-1234 430 4000

C0930238083 Warren John OgdenRoad Tyler Checking 726615 1230 0000-2222-2222-2222