Repository navigation
Expand file tree
/
Copy pathLP3Driver.java
More file actions
136 lines (108 loc) · 2.96 KB
/
Copy pathLP3Driver.java
File metadata and controls
136 lines (108 loc) · 2.96 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
package rsn170330.lp3;
import java.io.File;
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
public class LP3Driver {
public static void main(String[] args) throws Exception {
Scanner in;
if (args.length > 0 && !args[0].equals("-")) {
File file = new File(args[0]);
in = new Scanner(file);
}
else {
in = new Scanner(System.in);
}
boolean VERBOSE = false;
if (args.length > 1) { VERBOSE = Boolean.parseBoolean(args[1]); }
String operation = "";
long lineNo = 0;
MDS mds = new MDS();
Timer timer = new Timer();
long id, result, total = 0;
MDS.Money price;
List<Long> name = new LinkedList<>();
whileloop:
while (in.hasNext()) {
lineNo++;
result = 0;
operation = in.next();
if(operation.charAt(0) == '#') {
in.nextLine();
continue;
}
switch (operation) {
case "End":
break whileloop;
case "Insert":
id = in.nextLong();
price = new MDS.Money(in.next());
name.clear();
while(true) {
long val = in.nextLong();
if(val == 0) { break; }
else { name.add(val); }
}
result = mds.insert(id, price, name);
break;
case "Find":
id = in.nextLong();
result = mds.find(id).dollars();
break;
case "Delete":
id = in.nextLong();
result = mds.delete(id);
break;
case "FindMinPrice":
result = mds.findMinPrice(in.nextLong()).dollars();
break;
case "FindMaxPrice":
result = mds.findMaxPrice(in.nextLong()).dollars();
break;
case "FindPriceRange":
result = mds.findPriceRange(in.nextLong(), new MDS.Money(in.next()), new MDS.Money(in.next()));
break;
case "PriceHike":
result = mds.priceHike(in.nextLong(), in.nextLong(), in.nextDouble()).dollars();
break;
case "RemoveNames":
id = in.nextLong();
name.clear();
while(true) {
long val = in.nextLong();
if(val == 0) { break; }
else { name.add(val); }
}
result = mds.removeNames(id, name);
break;
default:
System.out.println("Unknown operation: " + operation);
}
total += result;
if(VERBOSE) {
System.out.println(lineNo + "\t" + operation + "\t" + result + "\t" + total);
}
}
System.out.println(total);
System.out.println(timer.end());
}
public static class Timer {
long startTime, endTime, elapsedTime, memAvailable, memUsed;
public Timer() {
startTime = System.currentTimeMillis();
}
public void start() {
startTime = System.currentTimeMillis();
}
public Timer end() {
endTime = System.currentTimeMillis();
elapsedTime = endTime-startTime;
memAvailable = Runtime.getRuntime().totalMemory();
memUsed = memAvailable - Runtime.getRuntime().freeMemory();
return this;
}
public String toString() {
return "Time: " + elapsedTime + " msec.\n" + "Memory: " + (memUsed/1048576) + " MB / " + (memAvailable/1048576) + " MB.";
}
}
}