[2] Java : Please write a BankAccount class, which maintains a pair of account i
ID: 3772179 • Letter: #
Question
[2] Java : Please write a BankAccount class, which maintains a pair of account information (name, balance) of all the customers. You also need to build the main class such as BankAccountMain, which contains main() method and sequence of statements for: (1) reading account information from file; (2) constructing an array of BankAccount objects; and (3) calculating and printing the average balance, minimum, and maximum balance among the whole accounts. (5 pts)
The program is expected to read all the account information from “account.txt,” and assign the first integer number of the first line to the number of accounts variable (like a numAccount) and proceed to capture the accounts information from the successive name (String) and balance (double) pair in the file. Accounts information of the individual customers is kept in the array of BankAccount object.
“account.txt”
10
Helene 1000
Jordan 755
Eve 2500
Ken 80
Andrew 999
David 1743
Amy 12
Sean 98
Patrick 7
Joy 14
public class BankAccount {
} // end of BankAccount
public class BankAccountMain {
}
“account.txt”
10
Helene 1000
Jordan 755
Eve 2500
Ken 80
Andrew 999
David 1743
Amy 12
Sean 98
Patrick 7
Joy 14
public class BankAccount {
} // end of BankAccount
public class BankAccountMain {
}
Explanation / Answer
import java.io.*;
import java.util.*;
class BankAccount{
String name;
int balance;
public BankAccount(String n,int b){
name = n;
balance = b;
}
}
class BankAccountMain{
public static void main(String[] args) throws IOException{
BufferedReader filereader = new BufferedReader(new FileReader("account.txt"));
int n = Integer.parseInt(filereader.readLine());
BankAccount[] bank = new BankAccount[n];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double sum = 0.0;
for (int i = 0; i < n; i++){
String[] s = filereader.readLine().split(" ");
int bal = Integer.parseInt(s[1]);
BankAccount b = new BankAccount(s[0],Integer.parseInt(s[1]));
min = Math.min(min,bal);
max = Math.max(max,bal);
sum += bal;
bank[i] = b;
i += 1;
}
System.out.println("MIN VALUE IS : "+min);
System.out.println("MAX VALUE IS : "+max);
System.out.println("AVERAGE IS : "+sum/n);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.