Skip to content

Commit 248a4f9

Browse files
authored
Create basicBankingSystem.java
1 parent 2a518e3 commit 248a4f9

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

basicBankingSystem.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
public class BankAccount {
2+
private String accountHolderName;
3+
private String accountNumber;
4+
private double balance;
5+
6+
// Constructor
7+
public BankAccount(String accountHolderName, String accountNumber) {
8+
this.accountHolderName = accountHolderName;
9+
this.accountNumber = accountNumber;
10+
this.balance = 0.0; // Initialize balance to 0
11+
}
12+
13+
// Deposit money
14+
public void deposit(double amount) {
15+
if (amount > 0) {
16+
balance += amount;
17+
System.out.println("Successfully deposited: $" + amount);
18+
} else {
19+
System.out.println("Invalid deposit amount.");
20+
}
21+
}
22+
23+
// Withdraw money
24+
public void withdraw(double amount) {
25+
if (amount > 0 && amount <= balance) {
26+
balance -= amount;
27+
System.out.println("Successfully withdrawn: $" + amount);
28+
} else {
29+
System.out.println("Invalid or insufficient funds.");
30+
}
31+
}
32+
33+
// Display account information
34+
public void displayAccountInfo() {
35+
System.out.println("Account Holder: " + accountHolderName);
36+
System.out.println("Account Number: " + accountNumber);
37+
System.out.println("Current Balance: $" + balance);
38+
}
39+
}
40+
import java.util.Scanner;
41+
42+
public class Main {
43+
public static void main(String[] args) {
44+
Scanner scanner = new Scanner(System.in);
45+
46+
// Create a new bank account
47+
System.out.println("Enter account holder's name:");
48+
String name = scanner.nextLine();
49+
50+
System.out.println("Enter account number:");
51+
String accountNumber = scanner.nextLine();
52+
53+
BankAccount account = new BankAccount(name, accountNumber);
54+
55+
// Menu-driven program
56+
while (true) {
57+
System.out.println("\nBanking Menu:");
58+
System.out.println("1. Deposit");
59+
System.out.println("2. Withdraw");
60+
System.out.println("3. View Account Info");
61+
System.out.println("4. Exit");
62+
System.out.print("Choose an option: ");
63+
64+
int option = scanner.nextInt();
65+
66+
switch (option) {
67+
case 1: // Deposit
68+
System.out.print("Enter deposit amount: ");
69+
double depositAmount = scanner.nextDouble();
70+
account.deposit(depositAmount);
71+
break;
72+
73+
case 2: // Withdraw
74+
System.out.print("Enter withdrawal amount: ");
75+
double withdrawAmount = scanner.nextDouble();
76+
account.withdraw(withdrawAmount);
77+
break;
78+
79+
case 3: // View Account Info
80+
account.displayAccountInfo();
81+
break;
82+
83+
case 4: // Exit
84+
System.out.println("Thank you for using the banking system. Goodbye!");
85+
System.exit(0);
86+
break;
87+
88+
default:
89+
System.out.println("Invalid option. Please try again.");
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)