-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
76 lines (67 loc) · 1.79 KB
/
Simulation.java
File metadata and controls
76 lines (67 loc) · 1.79 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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Simulation
{
public Simulation(){
}
public ArrayList<Item> loadItem (String fileName) throws FileNotFoundException
{
File file = new File(fileName);
Scanner scan = new Scanner(file);
ArrayList<Item> items = new ArrayList<>();
while(scan.hasNextLine())
{
String line = scan.nextLine();
String[] lineParts = line.split("=");
items.add(new Item(lineParts[0] , Integer.valueOf(lineParts[1])));
}
return items;
}
public ArrayList<Rocket> loadU1 (ArrayList<Item> itemList)
{
ArrayList<Rocket> rocketU1 = new ArrayList<>();
int i = 0;
while(i<itemList.size()){
Rocket u1 = new U1();
while(i<itemList.size() && u1.canCarry(itemList.get(i))){
u1.carry(itemList.get(i));
i++;
}
rocketU1.add(u1);
}
return rocketU1;
}
public ArrayList<Rocket> loadU2 (ArrayList<Item> itemList)
{
ArrayList<Rocket> rocketU2 = new ArrayList<>();
int i = 0;
while(i<itemList.size()){
Rocket u2 = new U2();
while(i<itemList.size() && u2.canCarry(itemList.get(i))){
u2.carry(itemList.get(i));
i++;
}
rocketU2.add(u2);
}
return rocketU2;
}
public int runSimulation(ArrayList<Rocket> rocketList)
{
int totalBudget =0;
int fails =0;
for(Rocket r : rocketList)
{
totalBudget += r.cost;
System.out.println("now launch and land will acessed" + " # cost : " + r.cost);
if(!r.launch() || !r.land())
{
System.out.println("Rocket Failed");
totalBudget += r.cost;
fails++;
}
}
System.out.println("No of Fails : " + fails);
return totalBudget;
}
}