I have created a simple program that will create a bank account and balance This program will ask the number of accounts you want to create then you will input account number and balances.
Now the problem is when its writing new account number it just Overwriting it, so the text that entering in my file is only 1 line;
Here's my current code:
import java.util.*;
import javax.swing.JOptionPane;
public class CreateBankFile {
public static Formatter sample;
public static int account;
public static int balance;
public static void main(String[] args) {
createBank();
createFile();
addRecords();
closeFile();
}
public static void createBank() {
int loops = Integer.parseInt(JOptionPane.showInputDialog(null,
"How many accounts \nyou wanted to create?"));
for (int i = 1; i <= loops; i++) {
account = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter account number:"));
balance = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter balance:"));
}
}
public static void createFile() {
try {
sample = new Formatter("BankAccounts.txt");
} catch (Exception e) {
System.out.println("You got an error!");
}
}
public static void addRecords() {
sample.format("%s%s", account + "\t", balance);
}
public static void closeFile() {
sample.close();
JOptionPane.showMessageDialog(null, "You successfully created bank accounts!");
}
}
Thank you, hope you will answer this question