-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLP4Driver.java
More file actions
134 lines (115 loc) · 2.8 KB
/
Copy pathLP4Driver.java
File metadata and controls
134 lines (115 loc) · 2.8 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
import java.util.*;
import java.io.*;
/**
* Sample driver code for Project LP4. Modify as needed. Do not change
* input/output formats.
*
* @author rbk
*/
public class LP4Driver {
static long[] description;
static final int DLENGTH = 100000;
public static void main(String[] args) throws FileNotFoundException {
Scanner in;
if (args.length > 0) {
in = new Scanner(new File(args[0]));
} else {
in = new Scanner(System.in);
}
String s;
double rv = 0;
description = new long[DLENGTH];
timer();
MDS mds = new MDS();
loop: while (in.hasNext()) {
s = in.next();
if (s.charAt(0) == '#') {
s = in.nextLine();
continue;
}
switch (s) {
case "Insert":
long id = in.nextLong();
double price = in.nextDouble();
long des = in.nextLong();
int index = 0;
while (des != 0) {
description[index++] = des;
des = in.nextInt();
}
description[index] = 0;
rv += mds.insert(id, price, description, index);
break;
case "Find":
id = in.nextLong();
rv += mds.find(id);
break;
case "Delete":
id = in.nextLong();
rv += mds.delete(id);
break;
case "FindMinPrice":
des = in.nextLong();
rv += mds.findMinPrice(des);
break;
case "FindMaxPrice":
des = in.nextLong();
rv += mds.findMaxPrice(des);
break;
case "FindPriceRange":
des = in.nextLong();
double lowPrice = in.nextDouble();
double highPrice = in.nextDouble();
rv += mds.findPriceRange(des, lowPrice, highPrice);
break;
case "PriceHike":
long minid = in.nextLong();
long maxid = in.nextLong();
double rate = in.nextDouble();
rv += mds.priceHike(minid, maxid, rate);
break;
case "Range":
lowPrice = in.nextDouble();
highPrice = in.nextDouble();
rv += mds.range(lowPrice, highPrice);
break;
case "SameSame":
rv += mds.sameSame();
break;
case "End":
break loop;
default:
System.out.println("Houston, we have a problem.\nUnexpected line in input: " + s);
System.exit(0);
}
}
in.close();
System.out.printf("%.2f\n", rv);
timer();
}
private static int phase = 0;
private static long startTime, endTime, elapsedTime;
/**
* Timer to calculate the running time
*/
public static void timer() {
if (phase == 0) {
startTime = System.currentTimeMillis();
phase = 1;
} else {
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
System.out.println("Time: " + elapsedTime + " msec.");
memory();
phase = 0;
}
}
/**
* This method determines the memory usage
*/
public static void memory() {
long memAvailable = Runtime.getRuntime().totalMemory();
long memUsed = memAvailable - Runtime.getRuntime().freeMemory();
System.out.println("Memory: " + memUsed / 1000000 + " MB / " + memAvailable / 1000000 + " MB.");
}
}