r/javahelp • • May 19 '24

Core Java Banking app

I have to make a banking app(on console) with feature like withdrawal and deposit and account info using core java concepts like loops,threads, exception handling,file I/O etc! Plss help me with what I have to do!

Code is in comments!

0 Upvotes

17 comments sorted by

View all comments

1

u/Optimist_Design5444 May 21 '24

package pack1;

import java.util.HashMap; import java.util.Map; import java.util.Scanner;

class BankAccount { private static Map<String, Double> accountBalances = new HashMap<>(); private static Map<String, String> accountPasswords = new HashMap<>(); private String accountNumber;

public BankAccount(String accountNumber, double balance, String password) {
    this.accountNumber = accountNumber;
    accountBalances.put(accountNumber, balance);
    accountPasswords.put(accountNumber, password);
}

public synchronized void deposit(double amount) {
    double balance = accountBalances.getOrDefault(accountNumber, 0.0);
    System.out.println("Depositing: " + amount);
    balance += amount;
    accountBalances.put(accountNumber, balance);
    System.out.println("Deposit successful. New balance: " + balance);
}

public synchronized void withdraw(double amount) {
    double balance = accountBalances.getOrDefault(accountNumber, 0.0);
    if (balance >= amount) {
        System.out.println("Withdrawing: " + amount);
        balance -= amount;
        accountBalances.put(accountNumber, balance);
        System.out.println("Withdrawal successful. New balance: " + balance);
    } else {
        System.out.println("Insufficient funds");
    }
}

public double getBalance() {
    return accountBalances.getOrDefault(123, 120.0);
}

public static boolean authenticate(String accountNumber, String password) {
    return accountPasswords.containsKey(accountNumber) && accountPasswords.get(accountNumber).equals(password);
}

}

public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.print("Enter your account number: "); String accountNumber = scanner.nextLine(); System.out.print("Enter your account password: "); String password = scanner.nextLine(); System.out.println("Welcome, " + name + "! Account number: " + accountNumber);

    BankAccount account = new BankAccount(accountNumber, 0, password);

    Thread depositThread = new Thread(() -> {
        System.out.print("Enter the amount to deposit: ");
        double amount = scanner.nextDouble();
        account.deposit(amount);
    });

    Thread withdrawThread = new Thread(() -> {
        System.out.print("Enter the amount to withdraw: ");
        double amount = scanner.nextDouble();
        account.withdraw(amount);
    });

    depositThread.start();
    withdrawThread.start();
    

    try {
        depositThread.join();
        withdrawThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Final balance: " + account.getBalance());
    
}

}

1

u/severoon pro barista May 23 '24

Please format your code properly.