Managing a file

41 Views Asked by At

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;
    }
}


1

There are 1 best solutions below

0
Grinding For Reputation On

You can implement the following code.

public class IDManager implements Serializable {

    private int lastAccountID = -1;

    public int createAccountID() {
        lastAccountID++;

        File serializedManager = new File("idManager.ser");
        try {
            serializedManager.createNewFile(); // Creates idManager.ser if it doesn't exist
            new ObjectOutputStream(new FileOutputStream(serializedManager)).writeObject(idManager); // Save the current state of iDManager
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return lastAccountID;
    }
}

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 createAccountID to retrieve the accounts ID.

Here is a simple program to demonstrate.

public class Main {

    static IDManager idManager = new IDManager();

    public static void main(String[] args) {

        File serializedManager = new File("idManager.ser");

        if(serializedManager.exists()) {
            try {
                idManager = (Main.IDManager) new ObjectInputStream(new FileInputStream(serializedManager)).readObject();
            }
            catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("Use (C) to create an account");

        String input = sc.nextLine();
        
        do {
            if(input.equalsIgnoreCase("c")) {
                System.out.println("Created Account - " + new Account("Lobster Man", "Savings", "Male", "30/2/1900"));
            }
            
            input = sc.nextLine();
            
        } while(!input.equalsIgnoreCase("quit"));
    }

    static class IDManager implements Serializable {

        private int lastAccountID = -1;

        public int createAccountID() {
            lastAccountID++;

            File serializedManager = new File("idManager.ser");
            try {
                serializedManager.createNewFile();
                new ObjectOutputStream(new FileOutputStream(serializedManager)).writeObject(idManager);
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            return lastAccountID;
        }

    }

    static class Account implements Serializable {

        private String name;
        private String type;
        private String sex;
        private String DOB;
        private String accNo;

        Account(String n, String t, String s, String dob) {
            name = n;
            type = t;
            sex = s;
            DOB = dob;
            accNo = "ACC" + Main.idManager.createAccountID();
        }

        public String getAccNo() {
            return accNo;
        }

        public String toString() {
            return "\n" + accNo + "\n" + name + "\n" + type + "\n" + sex + "\n" + DOB;
        }
    }

}