-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.java
148 lines (127 loc) · 3.43 KB
/
Agent.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
143
144
145
146
147
import java.util.ArrayList;
import java.util.Iterator;
public class Agent{
// creates array list
private ArrayList<Client> Clients;
public Agent() {
Clients = new ArrayList<Client>();
}
// returns the length of the array list (number of clients)
public int getNumberOfClients() {
return Clients.size();
}
// returns the client name alongside the number of the funds they own.
public String getClientInfo(int ClientNumber) {
Client c = Clients.get(ClientNumber);
String text = "";
text += c.getName() + ": £" + c.getFunds() + "\n";
return text;
}
// returns the client name alongside the number of the funds they own but checks wether if its a stock or not to add the '£'.
public String getListOfClientNames() {
String text = "";
Iterator<Client> it = Clients.iterator();
while (it.hasNext()) {
Client c = it.next();
if(c.getName().equals("Mohammad Al-Mousawi")){
text += c.getName() + ": £" + c.getFunds() + "\n";
}
else{
text += c.getName() + ": " + c.getFunds() + "\n";
}
}
return text;
}
// adds a client/stock.
public void addClient(Client c) {
Clients.add(c);
}
// deposits money to clients account
public boolean deposit(String ClientName, int amount) {
Iterator<Client> it = Clients.iterator();
boolean found = false;
int num = 0;
while (it.hasNext()) {
Client c = it.next();
if (c.getName().equals(ClientName)) {
found = true;
c.deposit(amount);
}
}
return found;
}
// withdraws money from clients account
public boolean withdraw(String ClientName, int amount) {
Iterator<Client> it = Clients.iterator();
boolean found = false;
int num = 0;
while (it.hasNext()) {
Client c = it.next();
if (c.getName().equals(ClientName)) {
if (amount <= c.getFunds()) {
found = true;
c.withdraw(amount);
}
}
}
return found;
}
//Iterates throguh client class to check if its a stock, then deposits the stock amount to the account and
//assigns a value depending on the stock so it is known
// how much money to withdraw from the clients account.
public boolean buyStock(String ClientName, int amount) {
Iterator<Client> it = Clients.iterator();
Client b = Clients.get(0);
boolean found = false;
int num = 0;
if (ClientName.equals("Tesla")){
num = 1000;
}
else if (ClientName.equals("Amazon")){
num = 500;
}
else if (ClientName.equals("Apple")){
num = 750;
}
while (it.hasNext()) {
Client c = it.next();
if (c.getName().equals(ClientName)) {
if ((amount*num) <= b.getFunds()) {
found = true;
c.deposit(amount);
withdraw("Mohammad Al-Mousawi",amount*num);
}
}
}
return found;
}
//Iterates throguh client class to check if its a stock, then withdraws the stock amount from the account and
//assigns a value depending on the stock so it is known
// how much to deposit from the clients account.
public boolean sellStock(String ClientName, int amount) {
Iterator<Client> it = Clients.iterator();
Client b = Clients.get(0);
boolean found = false;
int num = 0;
if (ClientName.equals("Tesla")){
num = 1000;
}
else if (ClientName.equals("Amazon")){
num = 500;
}
else if (ClientName.equals("Apple")){
num = 750;
}
while (it.hasNext()) {
Client c = it.next();
if (c.getName().equals(ClientName)) {
if ((amount) <= c.getFunds()) {
found = true;
c.withdraw(amount);
deposit("Mohammad Al-Mousawi",amount*num);
}
}
}
return found;
}
}