-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
105 lines (86 loc) · 3.4 KB
/
Copy pathAccount.java
File metadata and controls
105 lines (86 loc) · 3.4 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
import java.util.Scanner;
// ===== Account class: holds the balance and knows how to change it =====
// This is the OOP part: instead of loose variables floating around in main(),
// the balance and the actions that affect it (deposit, withdraw, transfer)
// are bundled together inside one class.
class Account {
private double balance; // private = only this class can touch it directly
public Account(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double depositamount) {
balance = balance + depositamount;
System.out.println("New balance:" + balance);
}
public void withdraw(double withdrawamount) {
if (withdrawamount > balance) {
System.out.println("Insufficient funds");
} else {
balance = balance - withdrawamount;
System.out.println("New balance" + balance);
}
}
public void transferTo(Account other, double transferamount) {
if (transferamount > balance) {
System.out.println("Insufficient funds");
} else {
balance = balance - transferamount;
other.balance = other.balance + transferamount;
System.out.println("Transfer successful.New balance:" + balance);
}
}
public void checkBalance() {
System.out.println("Your balance" + balance);
}
}
// ===== ATM class: same as your original file/class name, still holds main() =====
class ATM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your initial account balance:");
double mybalance = sc.nextDouble();
// Create Account objects instead of plain double variables
Account myAccount = new Account(mybalance);
Account otherAccount = new Account(0);
int choice = 0;
while (choice != 5) {
System.out.println("========= Simple ATM ================");
System.out.println("1.Check balance");
System.out.println("2.Deposit");
System.out.println("3.Withdraw");
System.out.println("4.Transfer to other account");
System.out.println("5.Exit");
System.out.print("Enter your choice:");
choice = sc.nextInt();
switch (choice) {
case 1:
myAccount.checkBalance();
break;
case 2:
System.out.println("Enter amount to deposit:");
double depositamount = sc.nextDouble();
myAccount.deposit(depositamount);
break;
case 3:
System.out.println("Enter amount to withdraw:");
double withdrawamount = sc.nextDouble();
myAccount.withdraw(withdrawamount);
break;
case 4:
System.out.println("Enter amount to transfer:");
double transferamount = sc.nextDouble();
myAccount.transferTo(otherAccount, transferamount);
break;
case 5:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice");
}
}
sc.close();
}
}