-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarket.java
More file actions
360 lines (320 loc) · 9.89 KB
/
Copy pathMarket.java
File metadata and controls
360 lines (320 loc) · 9.89 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
352
353
354
355
356
357
358
359
import java.util.*;
import java.io.*;
public class Market{
private Player pl;
private ArrayList<Stock> stocks;
private InputStreamReader isr;
private BufferedReader in;
private int counter= 0;//for days of the week
public static double rounder(double u){// Static method to make our code look pretty
int temp = (int)(u *100);
double ret = temp/100.0;
return ret;
}
//initzlize a new market, along with input methods
public Market(){
pl = new Player("Max","Easy",1000.0);
stocks = new ArrayList<Stock>();
isr = new InputStreamReader( System.in );
in = new BufferedReader( isr );
}
public Market(Player pl, ArrayList<Stock> st){// constructor that we actually use
this.pl =pl;
stocks = st;
isr = new InputStreamReader( System.in );
in = new BufferedReader( isr );
}
public static void view(ArrayList<Stock> st){// To print out stock names, prices and changes
for(int i = 0; i < st.size(); i++){
Stock a = st.get(i);
System.out.println("[" + i + "]" + a.getName() + "|" + rounder(a.getCurVal()) +"|" + rounder(a.getChangeAmount()) + "\n");
}
}
public static void viewXTRA(ArrayList<Stock> st){// prints out stock graphs
for(int i = 0; i < st.size(); i++){
Stock a = st.get(i);
System.out.println("[" + i + "]" + a.getName() + "|" + rounder(a.getCurVal()) + "\n" +a);
}
}
public void run10(){// runs 10 times for the purpose of having a simulation set up at the start
for (int x = 0; x < 10; x++){
for (int i =0; i < stocks.size(); i++){
stocks.get(i).progress();
}
}
}
public void buy(){// Buys stocks
ArrayList<Stock> newe = this.stocks;// easily maniuplatable
view(newe);
int option = 0;
int num =0;
int temp = 0;
System.out.println("Select an option\n \n [0] View graphs\n [1] Sort by price\n [2] Sort by change percent \n [3] Buy/Sell a specific stock \n [4] Back \n [5] View your balance and stocks owned");// options
String fa = "0";//this stuff is all over and its just to snatitize inputs
try{
fa = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(fa);
}
catch (Exception e ) { }
if (temp > -1){
option=temp;
}
/////////////////////////
while (option != 4){// run until they press back
if (option == 1){
newe = viewByPrice(newe);//sort
//view(newe);
//break;
}
else if (option == 2){
newe = viewByChange(newe);//sort
//view(newe);
//break;
}
else if (option == 0){
viewXTRA(newe);
}
else if (option == 3){
int sto = 0;
System.out.println("Which stock would you like to buy/sell?(your balance is $" + rounder(pl.getDol()) + ")");
view(newe);
String foooo = "0";
try{
foooo = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(foooo);
}
catch (Exception e ) { }
if (temp < newe.size() && temp > -1){
sto = temp;
}
System.out.println("You have Chosen " + newe.get(sto).getName());
System.out.println("[1]Buy or [2]Sell?");
int b = 0;
String fooo = "0";
try{
fooo = in.readLine();
}
catch ( Exception e ) { }
try{
b = Integer.parseInt(fooo);
}
catch (Exception e ) { }
if ( b == 1){
System.out.println("How much? You have " + rounder( pl.getDol()) + " dollars and its price per stock is $" + rounder(newe.get(sto).getCurVal()));
String foo = "0";
try{
foo = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(foo);
}
catch (Exception e ) { }
num = temp;
int i;
i = sto;
if (pl.getDol() > newe.get(i).getCurVal() * num){// check to see if they have enough money
newe.get(i).setAmtOwned(newe.get(i).getAmtOwned() + num);
pl.newWorth(pl.getDol() - (newe.get(i).getCurVal() * num));
System.out.println("You bought " + num + " of " + newe.get(i).getName() + ". Your new balance is " + rounder(pl.getDol()) + "!");
}else{
System.out.println("Not enough money.");
}
}else if (b == 2){
System.out.println("How much? You own " + newe.get(sto).getAmtOwned() + " and its price per stock is $" + rounder(newe.get(sto).getCurVal()));
String bar = "0";
try{
bar = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(bar);
}
catch (Exception e ) { }
num = temp;
int i;
i = sto;
if (newe.get(i).getAmtOwned() >= num){ //check to see they have enough stock
newe.get(i).setAmtOwned(newe.get(i).getAmtOwned() - num);
pl.newWorth(pl.getDol() + (newe.get(i).getCurVal() * num));
System.out.println("You sold " + num + " of " + newe.get(i).getName() + ". Your new balance is " + rounder(pl.getDol()) + "!");
}else{
System.out.println("Not enough stock to sell.");
}
}
}
else if (option == 5){// just print some stats
System.out.println("Your Balance is: " +rounder( pl.getDol()));
System.out.println(pl);
for (Stock a:newe){
if( a.getAmtOwned() > 0){
System.out.println("You own " + a.getAmtOwned() + " of " + a.getName());
}
}
}
view(newe);
System.out.println("Select an option\n [0] View Graphs \n [1] Sort by price\n [2] Sort by percent change \n [3] Buy/Sell a specific stock \n [4] Back \n [5] View your balance/stocks owned");// this happens so it can keep looping and choosing options
String fooi = "0";
try{
fooi = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(fooi);
}
catch (Exception e ) { }
if ( temp > -1){
option= temp;
}
}
}
public void run(){// The main method
while(true){//always run until they quit
System.out.println(" Select an option\n [1] Buy and Sell and view stocks \n [2] View your balance/stocks owned \n [3] Progress \n [4] Retire \n [5] View goals");//options
int temp = 0;
String foog = "0";
try{
foog = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(foog);
}
catch (Exception e ) { }
//////////
int option = 0;
if ( temp > 0){
option= temp;
}
ArrayList<Stock> newe = this.stocks;
while (option != 3){
if (option == 1){
buy();
}else if (option == 2){// same as above
System.out.println("Your Balance is: " + rounder(pl.getDol()));
System.out.println(pl);
for (Stock a:newe){
if( a.getAmtOwned() > 0){
System.out.println("You own " + a.getAmtOwned() + " of " + a.getName());
}
}
}
else if (option == 5){//prints out all the goals with numbers in order
int num =1;
for (Goal g: pl.g){
System.out.println("[" + num + "]" + g);
num ++;
}
System.out.println("Cash in a goal? [1]Yes [2] No");
int temper = 0;
String fool = "0";
try{
fool = in.readLine();
}
catch ( Exception e ) { }
try{
temper = Integer.parseInt(fool);
}
catch (Exception e ) { }
if (temper == 1){
System.out.println("Which goal?");
num =1;
for (Goal g: pl.g){
System.out.println("[" + num + "]" + g);
num ++;
}
int temperal = 1;
String foom = "0";
try{
foom = in.readLine();
}
catch ( Exception e ) { }
try{
temperal = Integer.parseInt(foom);
}
catch (Exception e ) { }
pl.g.get(temperal -1 ).reward(pl);
}
else if (temper == 2){
System.out.println("Ok come back soon!");//i had to put something here
}
}
if (option == 4){
return;}
System.out.println(" Select an option\n [1] Buy and Sell and View stocks \n [2] View your balance/stocks owned \n [3] Progress \n [4] Retire \n [5] View Goals");//for looping sake
String fooy = "0";
try{
fooy = in.readLine();
}
catch ( Exception e ) { }
try{
temp = Integer.parseInt(fooy);
}
catch (Exception e ) { }
//////////
if ( temp > 0){
option= temp;
}
if (option == 4){
return;}
}
int rando= (int)(Math.random() * newe.size());//generate next weeks headline
Headline header = newe.get(rando).getRandoHead();
Stock headerstock = newe.get(rando);
if (header.changesMomentum){//start its effects (it only changes momementum so we have to start it in advance
headerstock.setMomentum(headerstock.getMomentum() + (header.worth/100.0)*(headerstock.getCurVal()));
}else{
headerstock.setCurVal(headerstock.getCurVal() + (header.worth/100 * headerstock.getMomentum()));
}
System.out.println("Day " + counter);
System.out.println(header); //print the headline
for (int i =0; i < newe.size(); i++){
//if (newe.get(i) instanceof Competitor){
// if (newe.get(i).getName() == "bp"|| newe.get(i).getName() == "Apple"){
// Competitor x = (Competitor)newe.get(i);
// x.progress(newe.get(i+1));
// newe.set(i,x);
// }
// else if (newe.get(i).getName() == "shell"|| newe.get(i).getName() == "Google"){
// Competitor x = (Competitor)newe.get(i);
// x.progress(newe.get(i-1));
// newe.set(i,x);
// }
// }
// else{
newe.get(i).progress();
}
counter += 7;
}
}
public static ArrayList<Stock> viewByPrice( ArrayList<Stock> st ) {// sorts using bubble sort
Stock first;
Stock second;
for(int x = 0; x < st.size() - 1; x ++){
for (int i = (st.size() - 1); i > 0 ; i--){
first = st.get(i - 1);
second = st.get(i);
if (first.getCurVal() > (second.getCurVal())){
st.set(i, first);
st.set((i - 1),second);}
}}
return st;}
public static ArrayList<Stock> viewByChange( ArrayList<Stock> st ) {// sorts using bubble sort
Stock first;
Stock second;
for(int x = 0; x < st.size() - 1; x ++){
for (int i = (st.size() - 1); i > 0 ; i--){
first = st.get(i - 1);
second = st.get(i);
if (first.getChangeAmount() > (second.getChangeAmount())){
st.set(i, first);
st.set((i - 1),second);}
}}
return st;}
}// end class market