In Java Netbeans these are my Account class and CheckingAccount class //the Acco
ID: 3911647 • Letter: I
Question
In Java Netbeans
these are my Account class and CheckingAccount class
//the Account class
public abstract class Account {
//instance variables
//private will hide data
protected int number;
protected String openDate;
protected String name;
protected double balance;
//the constructor
public Account(int number, String openDate, String name, double balance){
this.number = number;
this.openDate = openDate;
this.name = name;
this.balance = balance;
}
//get and set instance variables
public int getnumber(){
return number;
}
public void setname(String name){
this.number = number;
}
public String getopenDate(){
return openDate;
}
public void setopenDate(){
this.openDate = openDate;
}
public String getname(){
return name;
}
public void setname(){
this.name = name;
}
public double getbalance(){
return balance;
}
public void setbalance(){
this.balance = balance;
}
public void deposit(double amount){
balance = balance + amount;
}
public boolean withdraw(double amount){
if(amount<=0 || amount>balance) return false;
balance -= amount;
return true;
}
public abstract int transferTo(Account account, double amount);
public String toString() {
String message
= "Number:" + number +"<>"+"openDate:" + openDate + "<>"+ "Name:" + name +"<>"+ "Balance:" + balance;
return message;
}
}//end of class
//the CheckingAccount class
class CheckingAccount extends Account{
public CheckingAccount(int number, String openDate, String name, double balance){
super(number,openDate,name,balance);
}
public int transferTo(Account account, double amount)
{
// int res = 0;
if (balance>= 10000 && balance >= amount)
{withdraw(amount);
account.deposit(amount);
return 0;
}
else if (balance <10000 && balance >=amount+2)
{
withdraw(amount+2);
deposit(amount);
return 1;
}
else if (balance <10000 && balance >=amount && balance <=amount+2)
{
return -1;
}
else {
return -2;
}
}
}
I provide a text file, accounts.txt. You should download this text file into the directory of your Netbeans project. Each row in the file is an account record and is in the format of accountNumber<>openDate<>customerName<>balance, where <> is the partition between two fields and the openDate is in the format of year/month/day. Make sure when reading or writing this text file programmatically, your program uses the file name without an explicit path (so called relative path). For instance, File accountFile = new File("accounts.txt");
this is the account.txt file
1001<>2018/5/1<>Lily Makki<>2085.3
1002<>2018/5/1<>Bran Chan<>14500.05
1003<>2018/5/2<>Hebert Castro<>5698.04
1004<>2018/5/2<>Jane Brown<>4923.12
1005<>2018/5/4<>Alicia Gongalez<>3546.11
1006<>2018/5/15<>Carlos Lopez<>700.06
In the part 2, you re-use the Account and CheckingAccount classes from the prior part 1. So in addition to these two classes from the part 1 and the provided accounts.txt file, your part 2 project includes the following AccountUtility and TestPart2 classes.
AccountUtility
- listNumbers: ArrayList <String>
- mapNumAccounts: LinkedHashMap <String, Account>
+ AccountUtility()
+ readFile(): void + getListNumbers(): ArrayList<String>
+ getMapNumAccounts(): LinkedHashMap <String, Account>
+ saveFile(LinkedHashMap): void
1. AccountUtility class
This class has methods to read a provided file, accounts.txt, update the file, and provide data from the accounts.txt for other program(s).
1.1 Constructor (2 points)
public AccountUtility(): The constructor initializes the two instance variables.
1.2 Read the given text file (10 points)
public void readFile(): reads accounts.txt file and populates all account numbers into listNumbers ArrayList, and adds account numbers and corresponding CheckingAccount object as records to the LinkedHashMap object, mapNumAccounts.
1.3 Get account numbers (1 point)
public ArrayList<String> getListNumbers(): returns the instance variable, listNumbers.
1.4 Get LinkedHashMap objects (1 point)
public LinkedHashMap <String, Account> getMapNumAccounts(): returns the instance variable, mapNumAccounts.
1.5 Save content to the text file (6 points)
public void saveFile(LinkedHashMap map): the pass-in parameter is a LinkedHashMap object in which the keys are account numbers and values are CheckingAccount objects. This method writes content from the passed-in LinkedHashMap object to accounts.txt, overwrites the file’s entire old content.
2. TestPart2 class (10 points)
• This class has the main() method.
• You create an AccountUtility object in this class, and use the object to test readFile() and getListNumbers(), and use System.out.println() to clearly display all account numbers retrieved from the accounts.txt file.
• Using the above AccountUtility object to call getMapNumAccounts() method and assign the returned value to a LinkedHashMap object.
• You must modify balances of two different accounts from the LinkedHashMap object by calling deposit(), withdraw(), and transferTo() methods with hard-code values.
• You call the saveFile(LinkedHashMap) method to overwrite the accounts.txt file. The pass-in parameter is the above updated LinkedHashMap object.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
AccountUtility.java
---------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class AccountUtility {
private ArrayList<String> listNumbers;
private LinkedHashMap<String, Account> mapNumAccounts;
public AccountUtility()
{
listNumbers = new ArrayList<String>();
mapNumAccounts = new LinkedHashMap<String, Account>();
}
public ArrayList<String> readFile(){
try {
Scanner infile = new Scanner(new File("accounts.txt"));
while(infile.hasNextLine()){
String line = infile.nextLine();
String[] tokens = line.split("<>");
CheckingAccount acc = new CheckingAccount(Integer.parseInt(tokens[0]), tokens[1], tokens[2], Double.parseDouble(tokens[3]));
listNumbers.add("" + acc.getnumber());
mapNumAccounts.put("" + acc.getnumber(), acc);
}
infile.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
return listNumbers;
}
public ArrayList<String> getListNumbers(){
return listNumbers;
}
public LinkedHashMap <String, Account> getMapNumAccounts()
{
return mapNumAccounts;
}
public void saveFile(LinkedHashMap<String, Account> accounts){
try {
PrintWriter pw = new PrintWriter(new File("accounts.txt"));
for(String k : accounts.keySet())
{
Account acc = accounts.get(k);
pw.printf("%d<>%s<>%s<>%.2f ",acc.getnumber(), acc.getopenDate() ,acc.getname() ,acc.getbalance());
}
pw.close();
System.out.println("accounts.txt file written. please refresh and check");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
}
TestPart2.java
-----------------
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class TestPart2 {
public static void main(String[] args) {
AccountUtility util = new AccountUtility();
ArrayList<String> nums = util.readFile();
System.out.println("The account numbers are");
for(int i = 0; i < nums.size(); i++)
System.out.println(nums.get(i));
System.out.println();
LinkedHashMap<String, Account> map = util.getMapNumAccounts();
Account a1 = map.get(nums.get(0));
System.out.println("depositing 1000 to account number " + a1.getnumber());
a1.deposit(1000);
Account a2 = map.get(nums.get(1));
System.out.println("withdraing 200 from account number " + a2.getnumber());
a2.withdraw(200);
System.out.println("trasferring 500 from " + a2.getnumber() + " to " + a1.getnumber());
a2.transferTo(a1, 500);
System.out.println("saving the updated accounts to file");
util.saveFile(map);
}
}
output
-------
The account numbers are
1001
1002
1003
1004
1005
1006
depositing 1000 to account number 1001
withdraing 200 from account number 1002
trasferring 500 from 1002 to 1001
saving the updated accounts to file
accounts.txt file written. please refresh and check
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.