-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.java
142 lines (131 loc) · 4.55 KB
/
Bank.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.ArrayList;
import java.util.Date;
/** HW1 solution
* Represents a bank
* @author Rui Yang
* @version 1.0
* @copyright Rui Yang
*/
public class Bank {
private String id;
private ArrayList<ATM> atms;
private ArrayList<Account> accounts;
private Account currentAccount;
private ATM currentATM;
public ATM getCurrentATM() {
return currentATM;
}
public Account getCurrentAccount() {
return currentAccount;
}
public void setCurrentAccount(Account currentAccount) {
this.currentAccount = currentAccount;
}
public Bank(String id){
this.id = id;
this.accounts = new ArrayList<Account>();
this.atms = new ArrayList<ATM>();
}
public void addAccount(Account acc){
accounts.add(acc);
}
public void addATM(ATM atm){
atms.add(atm);
}
public ArrayList<ATM> getAtms() {
return atms;
}
public void setAtms(ArrayList<ATM> atms) {
this.atms = atms;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ArrayList<Account> getAccounts() {
return accounts;
}
public void setAccounts(ArrayList<Account> accounts) {
this.accounts = accounts;
}
/** Check do we have the ATM when use input.
* @param inputATMame user's input.
* @param theBank user's input bank.
*/
public boolean isCurrentATM(String inputATMame,Bank theBank) {
boolean found = false;
for(int i=0;i<theBank.getAccounts().size();i++){
if(inputATMame.equals(theBank.getAtms().get(i).getName())){
this.currentATM = theBank.getAtms().get(i);
found = true;
}
}
return found;
}
/** Check user's input of cardnumber.
* @param cardNumber user's input of cashcard.
*/
public boolean isValided(String cardNumber) {
Date currentTime = new Date();
boolean valid = false;
boolean isBefore= false;
String bankCashCard = "";
int i=0;
while(i<currentATM.getBank().getAccounts().size()){
bankCashCard = currentATM.getBank().getId() + currentATM.getBank().getAccounts().get(i).getAccountId();
if(bankCashCard.equals(cardNumber)){
currentATM.getBank().setCurrentAccount(currentATM.getBank().getAccounts().get(i));
i= currentATM.getBank().getAccounts().size();
}else {
i++;
}
}
if(currentAccount==null||(currentATM.getBank().id.equals(cardNumber.charAt(0)))){
System.out.println("This card is not supported by this ATM");
}
if(currentAccount!=null){
isBefore = currentTime.before(currentAccount.getCard().getExpiredDate());
if(isBefore){
valid = true;
}else{
System.out.println("This card is expired and returned to you. ");
}
}
return valid;
}
/** Check user's input if it matches the current cashcard.
* @param inputPassword user's input.
*/
public boolean authorizePssword(int inputPassword){
boolean matches = false;
if(this.currentAccount.getPassword()==inputPassword){
matches = true;
}
return matches;
}
/** check user's input of amount wanted to withdraw..
* @param input user's input of withdraw..
*/
public boolean withDrawMoney(double input){
boolean notPassLimit = false;
if(input<=currentAccount.getBalance()&&input<=currentATM.getTransactions()){
currentATM.setTransactions(currentATM.getTransactions()-input);
currentAccount.setBalance(currentAccount.getBalance()-input);
System.out.println(input+"is withdrawn from your account. The remaining balance of this account is" +
currentAccount.getBalance()+"$. " +
" If you have more transactions, enter the amount or quit. ");
if(currentAccount.getBalance()==0){
notPassLimit = true;
}
}else if(input>currentATM.getTransactions()){
System.out.println("This amount exceeds the maximum amount you " +
"can withdraw per transaction. Please enter the amount or quit.");
}else if(input>currentAccount.getBalance()){
System.out.println("The amount exceeds the current balance. " +
"Enter another amount or quit. ");
}
return notPassLimit;
}
}