From 248a4f937c4f10deb15f67c6a76d13820b035bef Mon Sep 17 00:00:00 2001 From: nikhilgupta0 <118151222+nikhilgupta0@users.noreply.github.com> Date: Tue, 15 Oct 2024 22:54:22 +0530 Subject: [PATCH] Create basicBankingSystem.java --- basicBankingSystem.java | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 basicBankingSystem.java diff --git a/basicBankingSystem.java b/basicBankingSystem.java new file mode 100644 index 000000000000..0eee9493cf57 --- /dev/null +++ b/basicBankingSystem.java @@ -0,0 +1,93 @@ +public class BankAccount { + private String accountHolderName; + private String accountNumber; + private double balance; + + // Constructor + public BankAccount(String accountHolderName, String accountNumber) { + this.accountHolderName = accountHolderName; + this.accountNumber = accountNumber; + this.balance = 0.0; // Initialize balance to 0 + } + + // Deposit money + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("Successfully deposited: $" + amount); + } else { + System.out.println("Invalid deposit amount."); + } + } + + // Withdraw money + public void withdraw(double amount) { + if (amount > 0 && amount <= balance) { + balance -= amount; + System.out.println("Successfully withdrawn: $" + amount); + } else { + System.out.println("Invalid or insufficient funds."); + } + } + + // Display account information + public void displayAccountInfo() { + System.out.println("Account Holder: " + accountHolderName); + System.out.println("Account Number: " + accountNumber); + System.out.println("Current Balance: $" + balance); + } +} +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Create a new bank account + System.out.println("Enter account holder's name:"); + String name = scanner.nextLine(); + + System.out.println("Enter account number:"); + String accountNumber = scanner.nextLine(); + + BankAccount account = new BankAccount(name, accountNumber); + + // Menu-driven program + while (true) { + System.out.println("\nBanking Menu:"); + System.out.println("1. Deposit"); + System.out.println("2. Withdraw"); + System.out.println("3. View Account Info"); + System.out.println("4. Exit"); + System.out.print("Choose an option: "); + + int option = scanner.nextInt(); + + switch (option) { + case 1: // Deposit + System.out.print("Enter deposit amount: "); + double depositAmount = scanner.nextDouble(); + account.deposit(depositAmount); + break; + + case 2: // Withdraw + System.out.print("Enter withdrawal amount: "); + double withdrawAmount = scanner.nextDouble(); + account.withdraw(withdrawAmount); + break; + + case 3: // View Account Info + account.displayAccountInfo(); + break; + + case 4: // Exit + System.out.println("Thank you for using the banking system. Goodbye!"); + System.exit(0); + break; + + default: + System.out.println("Invalid option. Please try again."); + } + } + } +}