I'm trying to store user account information into a separate text file, which I have accomplished after many attempts...
However each time I create a new user it overwrites the last one put into the file. Also, the only thing that is being stored is Strings, I have a PIN number and an account balance that I will need for later that are very important.
Here is was I have so far, I believe that the problem is that each time I run the code an Object
named user is being created, and since it's not dynamic it just overwrites itself every time.
I am a noob when it comes to serializing anything so if you could explain the problem in the simplest terms that would be awesome!
The goal is for me to be able to store each new users account, and then come back at a later point to display them to a user if the account information matches the pin number and username
public class ATM implements Serializable {
public static void main(String[] args) {
// variables
String dash = "-------------------\n";
int accounts = 0;
// Scanner
Scanner scanner = new Scanner(System.in);
// Welcome screen
System.out.print(dash);
System.out.print("Welcome to the Bank\n");
System.out.print(dash);
System.out.println("Do you have an account with us? (y/n) ");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("y")) {
} else {
// new user is created
Bank bank = new Bank();
accounts++;
System.out
.println("Enter your full name below (e.g. John M. Smith): ");
String name = scanner.nextLine();
System.out.println("Create a username: ");
String userName = scanner.nextLine();
System.out.println("Enter your starting deposit amount: ");
int balance = scanner.nextInt();
System.out.print(dash);
System.out.print("Generating your information...\n");
System.out.print(dash);
int pin = bank.PIN();
String accountNum = bank.accountNum();
String id = name + accountNum;
User user = new User(name, userName, pin, accountNum, balance);
// new user gets added to the array list
Bank.users.add(user);
String test = "Test989898998";
System.out.println(user);
try {
FileOutputStream fileOut = new FileOutputStream("users.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(Bank.users);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
}
}
Bank Class
public class Bank implements Serializable {
//Generate a random 16 digit bank account number
public String accountNum() {
int max = 9999;
int min = 1000;
int a1 = (int) (Math.random() * (max - min) + min);
int a2 = (int) (Math.random() * (max - min) + min);
int a3 = (int) (Math.random() * (max - min) + min);
int a4 = (int) (Math.random() * (max - min) + min);
String accountNum = a1 + "-" + a2 + "-" + a3 + "-" + a4;
return accountNum;
}
//Generate a random 4 digit PIN
public int PIN() {
int max = 9999;
int min = 1000;
int PIN = (int) (Math.random() * (max - min) + min);
return PIN;
}
//array list for users
static ArrayList<User> users = new ArrayList<User>() {
};
}
User Class
public class User implements Serializable{
String name;
String userName;
String accountNum;
int pin;
int balance;
public User (String name, String userName, int pin, String accountNum, int balance) {
this.name = name;
this.userName = userName;
this.accountNum = accountNum;
this.pin = pin;
this.balance = balance;
}
public String toString() {
return "Name: " + this.name + "\n\nUsername: " + this.userName + " | " + "Pin: " + this.pin + "\n\n"
+ "Account Number: " + this.accountNum + "\n\nAccount Balance: $" + this.balance +
"\n\nNever share your login information with anyone!";
}
}