-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM.java
42 lines (33 loc) · 1.05 KB
/
ATM.java
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
public class ATM{
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
// Instance variables
public int money;
public ATM(int inputMoney){
this.money = inputMoney;
numATMs += 1;
totalMoney += inputMoney;
}
public void withdrawMoney(int amountToWithdraw){
if(amountToWithdraw <= this.money){
this.money -= amountToWithdraw;
totalMoney -= amountToWithdraw;
}
}
public static void averageMoney(){
System.out.println(totalMoney / numATMs);
}
public static void main(String[] args){
System.out.println("Total number of ATMs: " + ATM.numATMs);
ATM firstATM = new ATM(1000);
ATM secondATM = new ATM(500);
System.out.println("Total number of ATMs: " + ATM.numATMs);
System.out.println("Total amount of money in all ATMs: " + ATM.totalMoney);
firstATM.withdrawMoney(500);
secondATM.withdrawMoney(200);
System.out.println("Total amount of money in all ATMs: " + ATM.totalMoney);
// Call averageMoney() here
ATM.averageMoney();
}
}