-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
122 lines (102 loc) · 3.01 KB
/
Copy pathModel.java
File metadata and controls
122 lines (102 loc) · 3.01 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
115
116
117
118
119
120
121
122
/*
* The model, which has most of the validation and logic checking methods
*/
import java.util.ArrayList;
class Model {
private static boolean isAuthenticated;
private final ArrayList<Account> accounts;
private Bank bank;
private final ArrayList<Card> cards;
private Vault vault;
public Model() {
// initialize fields
this.accounts = new ArrayList<>();
this.bank = new Bank();
isAuthenticated = false;
this.cards = new ArrayList<>();
this.vault = new Vault();
// create a test account
Account acc = new Account();
acc.setAccountNumber(12345);
acc.setBalance(1000);
acc.setCard(new Card("1234567812341234", 000, "1234", "Bob Jones"));
acc.setName("Bob Jones");
accounts.add(acc);
// create 25 random test cards
cards.add(new Card("1234567891234567", 359, "1234", "Alex Fifield"));
for (int i = 0; i < 25; i++) {
int firstHalf = (int) (Math.random() * 100000000);
int secondHalf = (int) (Math.random() * 100000000);
String number = firstHalf + "" + secondHalf;
cards.add(new Card(number));
}
cards.add(new Card("1234567812341234"));
// fill up vault with twenty twenties
ArrayList<Bill> twenties = new ArrayList<>();
for (int i = 0; i < 20; i++) {
twenties.add(new Bill(20));
}
this.getVault().addTwenties(twenties);
}
/**
* Checks if the card is valid
*
* @param card The card
* @return Whether or not if the card is valid
*/
private boolean isValidCard(Card card) {
for (int i = 0; i < getCards().size(); i++) {
Card dbCards = getCards().get(i);
if (dbCards.equals(card)) {
return true;
}
}
return false;
}
public boolean isAuthenticated() {
return isAuthenticated;
}
public void setAuthenticated(boolean isAuthenticated) {
Model.isAuthenticated = isAuthenticated;
}
/**
* Finds an account from a card
*
* @param card The card
* @return The account associated with the card. If no accounts were found, it returns null
*/
public Account findAccount(Card card) {
for (Account account : accounts) {
if (account.getCard().equals(card)) {
return account;
}
}
return null;
}
public ViewEventResult verifyCCNumber(String ccNumber, String pin, Controller controller) {
Card card = new Card();
card.setNumber(ccNumber);
card.setPin(pin);
if (isValidCard(card)) {
return new ViewEventResult(true, "Card is valid");
} else {
return new ViewEventResult(false, "Card is invalid, try again.");
}
}
public Bank getBank() {
return bank;
}
public void setBank(Bank bank) {
this.bank = bank;
}
public Vault getVault() {
return vault;
}
public void setVault(Vault vault) {
this.vault = vault;
}
public void writeTransactionToFile(Transaction trans) {}
private ArrayList<Card> getCards() {
return cards;
}
}