/
StockExchange.java
138 lines (133 loc) · 3.68 KB
/
StockExchange.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* A program for stock exchange. Accepted indata is: from commandline and from
* file. Indata should be formated: NAME ACTION PRICE1 PRICE2(not needed for most actions)
*
* @author Robin Persson Sšderholm, Andreas KŠllberg
*
*/
public class StockExchange {
public static void main(String[] args) {
String name;
String action;
int price1;
int price2;
int numberOfBuyers = 0;
int numberOfSellers = 0;
boolean file = true;
Heap buyersHeap = new Heap(true);
Heap sellersHeap = new Heap(false);
// Ta emot indata
Scanner sc;
if (args.length > 0) {
String fileName = args[0];
try {
sc = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
return;
}
} else {
sc = new Scanner(System.in);
file = false;
}
while (true) {
if (!file) {
System.out.println("What do you want to do: ");
}
if (sc.hasNext()) {
name = sc.next();
} else {
System.out.println("Order book: ");
System.out.println("Buyers: ");
buyersHeap.printMe();
System.out.println("Sellers: ");
sellersHeap.printMe();
return;
}
if (sc.hasNext()) {
action = sc.next();
} else {
return;
}
if (sc.hasNextInt()) {
price1 = sc.nextInt();
} else {
if (sc.hasNext()) {
System.out.println("Wrong input format!");
sc.next();
continue;
}
return;
}
// Ska Šven kontrollera sŒ att alla namn Šr unika! DONE!
if (action.equalsIgnoreCase("K")) {
if (!buyersHeap.hashMap.hasName(name)) {
buyersHeap.add(name, price1);
numberOfBuyers++;
} else {
System.out.println("All names must be unique.");
}
} else if (action.equalsIgnoreCase("S")) {
if (!sellersHeap.hashMap.hasName(name)) {
sellersHeap.add(name, price1);
numberOfSellers++;
} else {
System.out.println("All names must be unique.");
}
} else if (action.equalsIgnoreCase("NK") && sc.hasNextInt()) {
price2 = sc.nextInt();
if (buyersHeap.hashMap.hasName(name)) {
if (buyersHeap.getPrice(buyersHeap.getPosition(name)) == price1) {
buyersHeap.change(name, price1, price2);
} else {
System.out.println("The old price must be accurate");
}
} else {
System.out
.println("There exist no bids from the person you have given.");
}
} else if (action.equalsIgnoreCase("NS")) {
if (sc.hasNextInt()) {
price2 = sc.nextInt();
if (sellersHeap.hashMap.hasName(name)) {
if (sellersHeap.getPrice(sellersHeap.getPosition(name)) == price1) {
sellersHeap.change(name, price1, price2);
} else {
System.out
.println("The old price must be accurate");
}
} else {
System.out
.println("There exist no bids from the person you have given.");
}
} else {
System.out
.println("You must state the old price and a new price.");
}
} else {
System.out.println("The input should be in the format:");
System.out.println("Name Action(S, K, NS, NK) Price "
+ "Price2(if Ns or NK has been given as action).");
continue;
}
if (numberOfBuyers > 0 && numberOfSellers > 0) {
if (buyersHeap.getPrice(0) >= sellersHeap.getPrice(0)) {
String buyersName = buyersHeap.getName(0);
int buyersPrice = buyersHeap.getPrice(0);
String sellersName = sellersHeap.getName(0);
buyersHeap.remove(buyersName);
sellersHeap.remove(sellersName);
System.out.println("" + buyersName
+ " has bought stock from " + sellersName
+ " for $" + buyersPrice);
numberOfBuyers--;
numberOfSellers--;
}
}
}
}
}