-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
64 lines (56 loc) · 1.83 KB
/
Simulation.java
File metadata and controls
64 lines (56 loc) · 1.83 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
import java.io.*;
import java.util.*;
public class Simulation {
public ArrayList<Item> loadItems(String fileName) throws Exception {
File file = new File(fileName);
Scanner input = new Scanner(file);
ArrayList<Item> items = new ArrayList();
while(input.hasNextLine()){
String line = input.nextLine();
String[] oneItem = line.split("=");
items.add(new Item(oneItem[0], Integer.valueOf(oneItem[1])));
}
return items;
}
public ArrayList<Rocket> loadU1(ArrayList<Item> Items){
Rocket rocket = new U_1();
ArrayList<Rocket> u1_fullyLoaded = new ArrayList();
for(Item i : Items){
while(!rocket.canCarry(i)){
u1_fullyLoaded.add(rocket);
rocket = new U_1();
}
rocket.carry(i);
}
u1_fullyLoaded.add(rocket);
return u1_fullyLoaded;
}
public ArrayList<Rocket> loadU2(ArrayList<Item> Items){
Rocket rocket = new U_2();
ArrayList<Rocket> u2_fullyLoaded = new ArrayList();
for(Item i : Items){
while(!rocket.canCarry(i)){
u2_fullyLoaded.add(rocket);
rocket = new U_2();
}
rocket.carry(i);
}
u2_fullyLoaded.add(rocket);
return u2_fullyLoaded;
}
public int runSimulation(ArrayList<Rocket> list){
int cnt = 0;
for(Rocket r : list){
while(!r.launch()){
cnt++;
r.launch();
}
while(!r.land()){
cnt++;
r.land();
}
}
int budget = (list.size() + cnt)*list.get(0).cost;
return budget;
}
}