3
\$\begingroup\$

I am a freshman Computer Engineering student.

Variable names are pretty straightforward; I believe it would be easy to understand.

Program explanation:

First you have to create an account and deposit money into it. Create method that runs and writes your id and password in separate text files and calls deposit_first which writes your balance in another text file.
Then when you run it again you can choose access and it will take your id and password from you and check if they match using another method. Which checks if the given id's index and given PIN's index match. If they do it gives you 3 choices: withdraw, deposit or balance. There is a deposit() method and both withdraw and deposit call it. Withdraw gives a negative int while deposit gives a positive.

Also any project ideas like this so I can exercise would be greatly appreciated.

import java.io.*;
import java.util.Scanner;

public class ATM {
    static Scanner scanner = new Scanner(System.in);
    static String ID = "";
    static int index_no;
    static int balance_account;
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("Type 'access' to access your bank account, Type 'create' to create a bank account");
        String access_create = scanner.nextLine();

        if ((access_create.equals("access"))){
            if (check()){
                access(ID);
            }else{
                System.out.println("An error occurred");
            }
        }else if (access_create.equals("create")){
            create();
        }

    }

     static void access(String id) throws FileNotFoundException {
        System.out.println("Type 'withdraw' to withdraw from your bank account, Type 'deposit' to deposit to your account, Type 'balance' to see your balance");
        String withdraw_deposit = scanner.nextLine();

        switch (withdraw_deposit) {
            case ("withdraw") -> {
                balance("New_Balance.txt");
                withdraw();}
            case ("deposit") -> {
                balance("New_Balance.txt");
                deposit_money();}
            case ("balance") -> {
                balance("New_Balance.txt");
                access(ID);}
        }
    }

    static void create() {
        System.out.println("Please Type in Your ID Number ");
        String client_ID = scanner.nextLine();

        System.out.println("Please Type in PIN ");
        String  client_password = scanner.nextLine();

        try {
            FileWriter ID = new FileWriter("ID_CHECK.txt",true);
            FileWriter passwords = new FileWriter("PIN_CHECK.txt",true);
            ID.write(client_ID + "\n");
            passwords.write(client_password + "\n");

            deposit_first();

            ID.close();
            passwords.close();

            System.out.println("Successfull.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }

    }

    static void withdraw() throws FileNotFoundException {
        System.out.println("Please Type in the amount you want to withdraw");
        int new_balance_amount = scanner.nextInt();
        deposit("", -new_balance_amount);

    }

    static void deposit_first() {
        System.out.println("Please Type in the amount you want to deposit, if you wont deposit Type '0' ");
        String balance_amount = scanner.nextLine();

        try {
            FileWriter balance = new FileWriter("New_Balance.txt",true);
            balance.write(balance_amount + "\n");
            balance.close();

        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    static void deposit_money() throws FileNotFoundException {
        System.out.println("Please Type in the amount you want to deposit");
        int new_balance_amount = scanner.nextInt();
        deposit("",new_balance_amount);
    }

    static void deposit(String id, int nw) throws FileNotFoundException {

        balance_account += nw;
        System.out.println("New Account Balance: " + balance_account);

        try {
            FileWriter balance = new FileWriter("New_Balance.txt");
            File balance1 = new File("Balance.txt");
            Scanner myReader = new Scanner(balance1);
            int i = 0;
            while (myReader.hasNextLine()) {
                i++;
                String data = myReader.nextLine();
                if (i == index_no){
                    balance.write(balance_account + "\n");
                }else{
                    balance.write(data + "\n");
                }
            }

            balance.close();

        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    static void balance(String a) throws FileNotFoundException {
        int i = 0;
        File balance = new File(a);
        Scanner myReader = new Scanner(balance);
        while (myReader.hasNextLine()) {
            i++;
            String data = myReader.nextLine();
            if (i == index_no){
                System.out.println("Your current balance: " + data);
                balance_account = Integer.parseInt(data);
            }
        }
    }

    static boolean check() throws FileNotFoundException {
        System.out.println("Please Type in Your ID Number");
        String client_ID = scanner.nextLine();

        ID = client_ID;

        System.out.println("Please Type in PIN");
        String  client_password = scanner.nextLine();

        File ID = new File("ID_CHECK.txt");
        File PIN = new File("PIN_CHECK.txt");

        return check_Reader(client_ID, ID) == check_Reader(client_password, PIN);

    }

    static int check_Reader(String checker, File files) throws FileNotFoundException {
        int i = 0;
        Scanner myReader = new Scanner(files);
        while (myReader.hasNextLine()) {
            i++;
            String data = myReader.nextLine();
            if (checker.equals(data)){
                index_no = i;
                return i;
            }
        }
        System.out.println("No Account Found");
        main(null);
        myReader.close();
        return 0;
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Everything being in one big class with everything static is misdesign. At the least, you'll want an Account class that gets instantiated when the file is loaded.

Convention for variable names in Java, rather than being lower_snake_case, is camelCase.

Every switch branch within access() calls balance in the same way, so do that before the switch body once.

In the phrase Please type, type should not be capitalised.

Your FileWriters should be put in try-with-resources and you should not explicitly close() them.

Your int currencies don't support cents. Consider instead BigDecimal. Don't use float or double due to accuracy loss.

After every time that you scanner.nextInt(), you need to scanner.nextLine() - otherwise the scanner will enter an unhelpful state where subsequent .next calls will return unexpected results.

When you call System.out.println("New Account Balance: " + balance_account);, consider instead using printf and printing the balance as formatted by NumberFormat.getCurrencyInstance().

Never acquire PINs or passwords using default scanner methods as these will expose the password on screen. Use Console.readPassword instead.

Never store PINs or passwords in plain text. Use a key derivation function instead.

Do not recurse into main() from check_Reader(). You need to restructure your code so that this kind of repetition is implemented with a loop and not recursion.

In create(), only keep ID and passwords (which are poorly named; consider instead idWriter and passwordWriter) open for as long as needed. Put each in its own try-with-resources, writing out the string on the inside.

The implementation of deposit is strange and inefficient. It seems like you're manually copying from the first to second file for every line up to a given index, writing out a new balance and then closing. Would this even work if the index isn't the last line in the file? In that case trailing entries wouldn't be copied over. "New_Balance.txt" shouldn't exist at all. Can you not just append to the existing file, if this is intended to be a running ledger for one account?

Similar problems exist for balance. If the current balance is assumed to be the last entry in the file, you certainly shouldn't be iterating over every single line in the file to get there.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.