Using the attached text file, write a program in java that prints the following
ID: 3780051 • Letter: U
Question
Using the attached text file, write a program in java that prints the following information:
The total deposits (add all deposits for all customers)
The total withdrawals (add all withdrawals for all customers)
The average deposits (total/number of customers)
The average withdrawals (total/number of customers)
The customer with the highest final balance
AccoutInfo.dat
Sarah Eaton,12345,1000,d,250,d,45,w,100,d,65,w,25
Tristan White,35462,750,w,500,d,35,d,240,w,300
Sophia King,51723,800,d,45,w,100,w,200
Isaac Smith,36917,500,d,550,d,45,w,345,d,45
Meghan Wilson,64378,875,w,500,w,250,d,50
Jordan Alexander,91725,75,d,100,d,250,w,300,d,200
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class AccountInfo {
public static void main(String [] args ){
String NameOfCustomer;
double accountBal = 0;
String HighBalCustomerName = "";
double highestBal = 0;
int numOfCustomer = 0;
StringTokenizer data = new StringTokenizer(",");
double totalDeposit = 0.0;
double totalWithdrawal = 0.0;
try {
BufferedReader br = new BufferedReader(new FileReader("input.txt")); // read the file
/***
* here input is in the form of
* Sarah Eaton,12345,1000,d,250,d,45,w,100,d,65,w,25
* here we assume that first token is name i.e sarah eaton
* second token that is integer is his initial account balance i.e 12345
* third token is the amount i.e 1000 and with next token i.e 'd' we get to know it is for deposit
* same for all
* but in last i.e 25 we do not know this is withdrawal for deposit
* so i am passing final input is as
* Sarah Eaton,12345,1000,d,250,d,45,w,100,d,65,w,25,w
*
* for all last amount is withdrawal;
*/
boolean flag = true; // used for first chaking the name;
String line = br.readLine(); // read the next line
while (line != null) {
flag = true;
numOfCustomer++;
data = new StringTokenizer(line,",");// number after comma
NameOfCustomer = data.nextToken();
accountBal = Integer.parseInt(data.nextToken());
while (data.hasMoreElements()) {
double value = Double.parseDouble(data.nextToken());
String action = data.nextToken();
if(action.equalsIgnoreCase("d")){//Assume after d the value is for deposit
totalDeposit = totalDeposit + value; // overall deposit
accountBal = accountBal + value; //if deposit then add to local balance
}
else if(action.equalsIgnoreCase("w")){//Assume after w the value is for withdrawal
totalWithdrawal = totalWithdrawal + value; // overall withdrawal
accountBal = accountBal - value; // if withdrawal then deduct from local account
}
}
if(accountBal > highestBal){ // set the highest balance if the local balance is more than highest balance
highestBal = accountBal;
HighBalCustomerName = NameOfCustomer;
}
line = br.readLine();
}
System.out.println("The total Deposit : "+totalDeposit);
System.out.println("The total withdrawal : "+totalWithdrawal);
System.out.println("The Average Deposit : "+totalDeposit/numOfCustomer);
System.out.println("The Average Withdrawal : "+totalWithdrawal/numOfCustomer);
System.out.println("The Custome with highest final balance is : ");
System.out.println("Name: "+HighBalCustomerName+" final balance : "+highestBal);
br.close();
}catch(Exception e){
System.out.println("File Not found Exception ");
}
}
}
input: input.txt
Sarah Eaton,12345,1000,d,250,d,45,w,100,d,65,w,25,w
Tristan White,35462,750,w,500,d,35,d,240,w,300,w
Sophia King,51723,800,d,45,w,100,w,200,w
Isaac Smith,36917,500,d,550,d,45,w,345,d,45,w
Meghan Wilson,64378,875,w,500,w,250,d,50,w
Jordan Alexander,91725,75,d,100,d,250,w,300,d,200,w
output:
The total Deposit : 4805.0
The total withdrawal : 3735.0
The Average Deposit : 800.8333333333334
The Average Withdrawal : 622.5
The Custome with highest final balance is :
Name: Jordan Alexander
final balance : 91750.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.