-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarket.java
More file actions
351 lines (327 loc) · 16.4 KB
/
Copy pathMarket.java
File metadata and controls
351 lines (327 loc) · 16.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// public class for the Market
// Implements the Entity class
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Iterator;
public class Market implements Entity {
// private data members
private MarketInventory MarketInventory = new MarketInventory();
private String marketName;
private static Scanner input = new Scanner(System.in);
private boolean containsHero = false;
// constructor for a market
public Market(String marketName) {
this.marketName = marketName;
}
// private class for the Market's inventory
private class MarketInventory {
public ArrayList<Spell> spells = new ArrayList<Spell>(Arrays.asList(GameObjects.fs1,GameObjects.fs2,GameObjects.fs3,GameObjects.fs4,
GameObjects.fs5,GameObjects.ls1,GameObjects.ls2,GameObjects.ls3,GameObjects.ls4,GameObjects.is1,
GameObjects.is2, GameObjects.is3, GameObjects.is4, GameObjects.is5, GameObjects.li1, GameObjects.li2, GameObjects.li3));
public ArrayList<Potion> potions = new ArrayList<Potion>(Arrays.asList(GameObjects.po1, GameObjects.po2,
GameObjects.po3, GameObjects.po4, GameObjects.po5, GameObjects.po6, GameObjects.po7, GameObjects.po8));
public ArrayList<Weapon> weapons = new ArrayList<Weapon>(Arrays.asList(GameObjects.wpn1, GameObjects.wpn2,
GameObjects.wpn3, GameObjects.wpn4, GameObjects.wpn5, GameObjects.wpn6, GameObjects.wpn7,GameObjects.wpn8, GameObjects.wpn9));
public ArrayList<Armor> armors = new ArrayList<Armor>(Arrays.asList(GameObjects.ar1, GameObjects.ar2,
GameObjects.ar3, GameObjects.ar4,GameObjects.ar6, GameObjects.ar7));
public void spells() {
System.out.println("Spells \n");
if (spells.size() == 0) {
System.out.println("--no spells--");
} else {
for (int i = 0; i < spells.size(); i++) {
System.out.println(Integer.toString(i) + ". " + spells.get(i));
}
}
}
public void potions() {
System.out.println("\nPotions \n");
if (potions.size() == 0) {
System.out.println("--no potions--");
} else {
for (int i = 0; i < potions.size(); i++) {
System.out.println(Integer.toString(i) + ". " + potions.get(i));
}
}
}
public void weapons() {
System.out.println("\nWeapons \n");
if (weapons.size() == 0) {
System.out.println("--no weapons--");
} else {
for (int i = 0; i < weapons.size(); i++) {
System.out.println(Integer.toString(i) + ". " + weapons.get(i));
}
}
}
public void armor() {
System.out.println("\nArmor \n");
if (armors.size() == 0) {
System.out.println("--no armors--");
} else {
for (int i = 0; i < armors.size(); i++) {
System.out.println(Integer.toString(i) + ". " + armors.get(i));
}
}
}
}
// handles user input logic for buying an item
public void buy(HeroTeam heroTeam) {
HeroEntity buyer = heroTeam.getHero();
int option = 0;
while(true){
try {
System.out.println("\nCurrently purchasing for: " + buyer.toString() + ", with a balance of " + Colors.ANSI_YELLOW + Integer.toString(buyer.getMoney()) + Colors.ANSI_RESET + " gold.");
System.out.println("What item would you like to buy?:\n 1) Potion \n 2) Spell \n 3) Weapon\n 4) Armor\n 5) Heal Team\n 6) Back");
System.out.print("\nEnter move: ");
option = Integer.parseInt(input.nextLine());
if (option > 0 && option < 7) {
if (option == 1) {
MarketInventory.potions();
System.out.print("\nChoose which potion (-1 to go back): ");
option = Integer.parseInt(input.nextLine());
Potion toBuy = MarketInventory.potions.get(option);
if (toBuy.canBeUsed(buyer) && toBuy.getPrice() <= buyer.getMoney()) {
System.out.println(buyer.getName() + " succesfully purchased a " + toBuy.getName()+ ".");
buyer.setMoney(buyer.getMoney() - toBuy.getPrice());
buyer.addPotion(toBuy);
} else {
System.out.println("You are unable to purchase this.");
}
} else if (option == 2) {
MarketInventory.spells();
System.out.print("\nChoose which spell (-1 to go back): ");
option = Integer.parseInt(input.nextLine());
Spell toBuy = MarketInventory.spells.get(option);
if (toBuy.canBeUsed(buyer) && toBuy.getPrice() <= buyer.getMoney()) {
System.out.println(buyer.getName() + " succesfully purchased a " + toBuy.getName()+ ".");
buyer.setMoney(buyer.getMoney() - toBuy.getPrice());
buyer.addSpell(toBuy);
} else {
System.out.println("You are unable to purchase this.");
}
} else if (option == 3) {
MarketInventory.weapons();
System.out.print("\nChoose which weapon (-1 to go back): ");
option = Integer.parseInt(input.nextLine());
Weapon toBuy = MarketInventory.weapons.get(option);
if (toBuy.canBeUsed(buyer) && toBuy.getPrice() <= buyer.getMoney()) {
System.out.println(buyer.getName() + " succesfully purchased a " + toBuy.getName() + ".");
buyer.addWeaponToInventory(toBuy);
buyer.setMoney(buyer.getMoney() - toBuy.getPrice());
} else {
System.out.println("You are unable to purchase this.");
}
} else if (option == 4) {
MarketInventory.armor();
System.out.print("\nChoose which armor (-1 to go back): ");
option = Integer.parseInt(input.nextLine());
Armor toBuy = MarketInventory.armors.get(option);
if (toBuy.canBeUsed(buyer) && toBuy.getPrice() <= buyer.getMoney()) {
System.out.println(buyer.getName() + " succesfully purchased a " + toBuy.getName()+ ".");
buyer.addArmorToInventory(toBuy);
buyer.setMoney(buyer.getMoney() - toBuy.getPrice());
} else {
System.out.println("You are unable to purchase this.");
}
} else if (option == 5) {
healHandler(buyer, heroTeam);
} else if (option == -1) {
} else {
break;
}
} else {
System.out.println("Enter a number between 1-5");
}
}
catch(Exception e){
System.out.println("Please input a number.");
continue;
}
}
}
public void healHandler(HeroEntity buyer, HeroTeam heroTeam) {
int healCount = 0;
Iterator<HeroEntity> iter = heroTeam.heroIterator();
while (iter.hasNext()) {
HeroEntity curr = iter.next();
if (curr.getHealth() != curr.getLevel() * 100) {
healCount += 1;
}
}
int moneyRequired = (healCount * 1200);
iter = heroTeam.heroIterator();
String intro = "";
if (healCount == 1) {
intro = Colors.ANSI_GREEN + Integer.toString(healCount) + Colors.ANSI_RESET + " hero requires healing. ";
} else {
intro = Colors.ANSI_GREEN + Integer.toString(healCount) + Colors.ANSI_RESET + " heroes require healing. ";
}
if (buyer.getMoney() < moneyRequired) {
System.out.println(intro + "This action will cost you " + Colors.ANSI_YELLOW + Integer.toString(moneyRequired)
+ Colors.ANSI_RESET + " gold, however, you do not have enough gold.");
} else if (moneyRequired == 0) {
System.out.println("No hero requires healing.");
} else {
try {
System.out.print(intro + "Therefore, healing your team will cost you " + Colors.ANSI_YELLOW + Integer.toString(moneyRequired)
+ Colors.ANSI_RESET + " gold. Would you like to proceed? (Type 1 to PROCEED): ");
int option = Integer.parseInt(input.nextLine());
if (option == 1) {
System.out.println();
while (iter.hasNext()) {
HeroEntity curr = iter.next();
if (curr.isFainted() || curr.getHealth() < curr.getLevel() * 100) {
curr.setHealth(curr.getLevel() * 100);
System.out.println(curr.toString() + " has been fully " + Colors.ANSI_RED + "healed" + Colors.ANSI_RESET + ".");
}
}
buyer.setMoney(buyer.getMoney() - moneyRequired);
}
} catch (Exception e) {
System.out.println("Something seems to not be right... ");
}
}
}
// handles user input for selling an item
public void sell(HeroTeam heroTeam) {
HeroEntity buyer = heroTeam.getHero();
int option = 0;
while(true){
try {
System.out.println("\nCurrently selling for: " + buyer.toString() + ", who has " + Colors.ANSI_YELLOW + Integer.toString(buyer.getMoney()) + Colors.ANSI_RESET + " gold.");
System.out.println("What item would you like to sell?:\n 1) Potion \n 2) Spell \n 3) Weapon\n 4) Armor\n 5) Main Menu");
System.out.print("Enter move: ");
option = Integer.parseInt(input.nextLine());
if (option > 0 && option < 6) {
if (option == 1) {
if (buyer.hasPotion()) {
buyer.showPotion();
try {
System.out.print("Enter move: ");
int option_sell = 0;
option_sell = Integer.parseInt(input.nextLine());
Potion removed = buyer.removePotion(option_sell);
buyer.setMoney(buyer.getMoney() + (removed.getPrice() / 2));
System.out.println("Congratulations, you sold a " + removed.getName() + " for " + Colors.ANSI_YELLOW + Integer.toString((removed.getPrice() / 2)) + Colors.ANSI_RESET + " gold.");
} catch (Exception e) {
System.out.println("Sell unsuccesfull");
}
} else {
System.out.println("You have no potions.");
}
} else if (option == 2) {
if (buyer.hasSpells()) {
buyer.showSpells();
try {
System.out.print("Enter move: ");
int option_sell = 0;
option_sell = Integer.parseInt(input.nextLine());
Spell removed = buyer.removeSpell(option_sell);
buyer.setMoney(buyer.getMoney() + (removed.getPrice() / 2));
System.out.println("Congratulations, you sold a " + removed.getName() + " for " + Colors.ANSI_YELLOW + Integer.toString((removed.getPrice() / 2)) + Colors.ANSI_RESET + " gold.");
} catch (Exception e) {
System.out.println("Sell unsuccesfull");
}
} else {
System.out.println("You have no spells.");
}
} else if (option == 3) {
if (buyer.hasWeapons()) {
buyer.showWeapons();
try {
System.out.print("Enter move: ");
int option_sell = 0;
option_sell = Integer.parseInt(input.nextLine());
Weapon removed = buyer.removeWeaponFromInventory(option_sell);
buyer.setMoney(buyer.getMoney() + (removed.getPrice() / 2));
System.out.println("Congratulations, you sold a " + removed.getName() + " for " + Colors.ANSI_YELLOW + Integer.toString((removed.getPrice() / 2)) + Colors.ANSI_RESET + " gold.");
} catch (Exception e) {
System.out.println("Sell unsuccesfull");
}
} else {
System.out.println("You have no weapons.");
}
} else if (option == 4) {
if (buyer.hasArmors()) {
buyer.showArmors();
try {
System.out.print("Enter move: ");
int option_sell = 0;
option_sell = Integer.parseInt(input.nextLine());
Armor removed = buyer.removeArmorFromInventory(option_sell);
buyer.setMoney(buyer.getMoney() + (removed.getPrice() / 2));
System.out.println("Congratulations, you sold a " + removed.getName() + " for " + Colors.ANSI_YELLOW + Integer.toString((removed.getPrice() / 2)) + Colors.ANSI_RESET + " gold.");
} catch (Exception e) {
System.out.println("Sell unsuccesfull");
}
} else {
System.out.println("You have no armors.");
}
} else {
break;
}
} else {
System.out.println("Enter a number between 1-5");
}
}
catch(Exception e){
System.out.println("Please input a number.");
continue;
}
}
}
// handles logic for getting the hero's main option
public void getMainOption(HeroTeam heroTeam) {
int option = 0;
while(true){
try {
System.out.println("\nWelcome back to your Nexus! As always, our market is open. What would you like to do?:\n 1) Buy \n 2) Sell \n 3) Leave");
System.out.print("\nEnter move: ");
option = Integer.parseInt(input.nextLine());
if (option > 0 && option < 5) {
if (option == 1) {
buy(heroTeam);
} else if (option == 2) {
sell(heroTeam);
} else {
System.out.println("Thank you for the visit!");
break;
}
} else {
System.out.println("Enter a number between 1-3");
}
}
catch(Exception e){
System.out.println("This is not a valid option...");
continue;
}
}
}
// method from interface
public void setName(String newName) {
marketName = newName;
}
public String getName() {
return marketName;
}
// to string method
public String toString() {
return marketName;
}
// additional logic methods
public boolean containsHero() {
return containsHero;
}
public void arrive() {
containsHero = true;
}
public void leave() {
containsHero = false;
}
// visit a market
public void visit(HeroTeam heroTeam) {
getMainOption(heroTeam);
}
}