Skip to content

Create basicBankingSystem.java #5854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions basicBankingSystem.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
}