I'm tasked with creating a small, menu-driven banking system in Java using I/O streams and data structures. The system will provide options for users to view all accounts, delete an account, and exit the program.
The main feature involves using a serialized Account class to store account information in a file via an ObjectOutputStream. When the application closes, all accounts are stored in this file. Upon restarting, all accounts are reloaded into a HashMap.
My concern is that the automatic generation of account numbers using a static variable in the Account class may lead to account number collisions when the program is reloaded. I'm seeking advice on how to avoid this issue and manage the ObjectOutputStream to properly handle the file.
My account class :
class Account implements Serializable{
private String Name;
private String Type;
private String sex;
private String DOB;
private String accNo="ACC1025";
static int id=0;
Account(String n,String t,String s,String dob){
Name=n;
Type=t;
sex=s;
DOB=dob;
accNo=acc+id;
id++;
}
public String getAccNo() {
return accNo;
}
public String toString(){
return "\n"+accNo+"\n"+Name+"\n"+Type+"\n"+sex+"\n"+DOB;
}
}
You can implement the following code.
It's a very basic file of code but all we are doing is saving the lastAccountID, whenever you create a new account you can use
createAccountIDto retrieve the accounts ID.Here is a simple program to demonstrate.