I am Stuck on the Write to File, its not Creating a File, as i dont have a Method in Main to Call my WritetoFile()
and like to get my Starting Balance, WithDraw and deposit Balance into a String and Write that to a File.
which should give me
Name: Danny
Starting Balance: 1000
Deposit : 200
Total Balance : 1200
I have also have done this to a clone of my Project, but on run time i am getting BankAccount.txt (No such file or directory)
BankAccount.txt not found
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
This program tests the Bank class.
*/
public class BankTester
{
public static void main(String[] args)
{
try
{
BufferedReader inFileStream =
new BufferedReader(new FileReader("BankAccount.txt"));
Bank bank = new Bank();
String name = null;
int dannysAccount = 0;
int sallysAccount = 1;
int harrysAccount = 2;
int jerrysAccount = 3;
int DavidsAccounts = 110;
bank.addAccount(dannysAccount, 1000);
bank.addAccount(sallysAccount, 2000);
bank.addAccount(harrysAccount, 3000);
bank.addAccount(jerrysAccount, 10000);
bank.RemoveAccount(DavidsAccounts);
bank.deposit(dannysAccount, 200);
bank.withdraw(sallysAccount, 500);
bank.deposit(harrysAccount, 1000);
bank.withdraw(jerrysAccount, 7000);
String line = inFileStream.readLine();
while (line != null)
{
StringTokenizer parser =
new StringTokenizer(line);
// reading name, age, gpa from the line
name = parser.nextToken();
dannysAccount = Integer.parseInt(parser.nextToken());
//gpa = Integar.parseFloat(parser.nextToken());
System.out.println(
"Danny's Account Balance: " + bank.getBalance(dannysAccount));
System.out.println("Expected: 1200");
System.out.println(
"Sally's Account Balance: " + bank.getBalance(sallysAccount));
System.out.println("Expected: 1500");
System.out.println(
"Harry's Account Balance: " + bank.getBalance(harrysAccount));
System.out.println("Expected: 4000");
System.out.println(
"Jerry's Account Balance: " + bank.getBalance(jerrysAccount));
System.out.println("Expected: 3000");
line = inFileStream.readLine();
}
inFileStream.close();
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
System.out.println("BankAccount.txt not found");
System.exit(-1);
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Error reading BankAccount.txt");
System.exit(-1);
}
try
{
PrintWriter outFileStream =
new PrintWriter(new FileOutputStream("BankAccount.txt"));
outFileStream.println("Hello World");
outFileStream.println("Good Morning");
outFileStream.close();
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
System.out.println("Can't open BankAccount.txt");
System.exit(-1);
}
catch(IOException e)
{
System.out.println(e.getMessage());
System.out.println("Error writing BankAccount.txt");
System.exit(-1);
}
File outFile = new File("BankAccount.txt");
System.out.println(outFile.exists());
System.out.println(outFile.length());
System.out.println(outFile.getName());
System.out.println(outFile.getPath());
}
}