Repository navigation
Expand file tree
/
Copy pathbankSystem.java
More file actions
107 lines (95 loc) · 3.55 KB
/
Copy pathbankSystem.java
File metadata and controls
107 lines (95 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.Scanner;
// BankAccount class represents a single bank account
class BankAccount {
private String accountHolder;
private int accountNumber;
private double balance;
// Constructor
public BankAccount(String accountHolder, int accountNumber, double initialBalance) {
this.accountHolder = accountHolder;
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Deposit method
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
// Withdraw method
public void withdraw(double amount) {
if (amount > 0) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient balance.");
}
} else {
System.out.println("Withdrawal amount must be positive.");
}
}
// Check balance method
public void checkBalance() {
System.out.println("Current balance: $" + balance);
}
// Get account information
public void accountInfo() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: $" + balance);
}
}
// Main class to simulate banking operations
public class bankSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Create a bank account
System.out.print("Enter account holder name: ");
String name = sc.nextLine();
System.out.print("Enter account number: ");
int accNumber = sc.nextInt();
System.out.print("Enter initial balance: ");
double initialBalance = sc.nextDouble();
BankAccount account = new BankAccount(name, accNumber, initialBalance);
boolean running = true;
while (running) {
System.out.println("\n--- Banking Operations Menu ---");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Account Info");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter deposit amount: ");
double depositAmount = sc.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = sc.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.checkBalance();
break;
case 4:
account.accountInfo();
break;
case 5:
running = false;
System.out.println("Exiting banking system...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
sc.close();
}
}