-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
115 lines (95 loc) · 3.11 KB
/
ATM.java
File metadata and controls
115 lines (95 loc) · 3.11 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
108
109
110
111
112
113
114
import java.util.*;
class Transaction {
String type;
double amount;
Transaction(String type, double amount){
this.type = type;
this.amount = amount;
}
public String toString(){
return type + " : Rs." + amount;
}
}
class Account {
private String name;
private double balance;
private int accountNumber;
private ArrayList<Transaction> transactions = new ArrayList<>();
public Account(String name, int accountNumber){
this.name = name;
this.accountNumber = accountNumber;
this.balance = 0;
}
public void deposit(double amount){
if(amount > 0){
balance += amount;
transactions.add(new Transaction("Deposit", amount));
System.out.println("Deposit successful!");
} else {
System.out.println("Invalid amount!");
}
}
public void withdraw(double amount){
if(amount > 0 && amount <= balance){
balance -= amount;
transactions.add(new Transaction("Withdrawal", amount));
System.out.println("Withdraw successful!");
} else {
System.out.println("Insufficient balance!");
}
}
public void checkBalance(){
System.out.println("Current Balance: Rs." + balance);
}
public void viewTransactions(){
if(transactions.isEmpty()){
System.out.println("No transactions yet.");
} else {
for(Transaction t : transactions){
System.out.println(t);
}
}
}
}
public class ATM {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter account holder name: ");
String name = sc.nextLine();
int accountNo = new Random().nextInt(10000) + 10000;
Account account = new Account(name, accountNo);
System.out.println("Account created successfully!");
System.out.println("Account Number: " + accountNo);
int choice;
do {
System.out.println("\n1.Deposit");
System.out.println("2.Withdraw");
System.out.println("3.Check Balance");
System.out.println("4.View Transactions");
System.out.println("5.Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch(choice){
case 1:
System.out.print("Enter amount: ");
account.deposit(sc.nextDouble());
break;
case 2:
System.out.print("Enter amount: ");
account.withdraw(sc.nextDouble());
break;
case 3:
account.checkBalance();
break;
case 4:
account.viewTransactions();
break;
case 5:
System.out.println("Thank you!");
break;
default:
System.out.println("Invalid choice!");
}
} while(choice != 5);
}
}