forked from VihaanVerma89/javaCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleTank.java
More file actions
159 lines (139 loc) · 3.89 KB
/
BattleTank.java
File metadata and controls
159 lines (139 loc) · 3.89 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
/*
* Assumption made for coding
* 1. The tanks can overlap
* 2. The starting points for all tanks are (50,50).
* 3. Tanks can only move between (0,0) and (100,100).
* 4. Default tank is the most recently created one.
*/
public class BattleTank
{
public static void listTanks(Iterator it)
{
while(it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
Util.sopln(pairs.getKey() + "." +((Tank)pairs.getValue()).getName());
}
}
public static void main(String [] args)
{
Map tankMap = new HashMap<Integer , Tank>();
Util.sopln("\t\tBattle Tank");
Scanner in = new Scanner(System.in);
Scanner reader = new Scanner(System.in);
int command=0, choice, capacity, i = 1, currentTank=1;
do
{
switch(command)
{
case 0:
Util.sopln("Select one of the following Tanks by specifying the corresponding number:");
Util.sopln("1.Jumbo Tank");
Util.sopln("2.Mini Tank");
choice = reader.nextInt();
Util.sopln("Enter the tank capacity:");
capacity = reader.nextInt();
if(choice == 1)
{
Tank tank = new JumboTank(capacity);
currentTank = i;
tankMap.put(i++,tank);
}
else if (choice == 2)
{
Tank tank = new MiniTank(capacity);
currentTank = i;
tankMap.put(i++,tank);
}
else
{
Util.promptUser("Enter a valid input.");
}
break;
case 1:
((Tank)tankMap.get(currentTank)).moveAhead();
break;
case 2:
((Tank)tankMap.get(currentTank)).moveBack();
break;
case 3:
((Tank)tankMap.get(currentTank)).turnLeft();
break;
case 4:
((Tank)tankMap.get(currentTank)).turnRight();
break;
case 5:
Util.promptUser("Direction: " + ((Tank)tankMap.get(currentTank)).getDirection() +"WARDS");
break;
case 6:
Util.promptUser("Distance Travelled: " + ((Tank)tankMap.get(currentTank)).getMeter() + " km/kms.");
break;
case 7:
int [] cooridnates = ((Tank)tankMap.get(currentTank)).getLocation();
Util.promptUser("X: " + cooridnates[0] + " Y: " +cooridnates[1]);
break;
case 8:
Util.promptUser("Fuel Level :" + ((Tank)tankMap.get(currentTank)).getFuelLevel() + " Liter/Liters.");
break;
case 9:
((Tank)tankMap.get(currentTank)).fire();
break;
case 10:
if(tankMap.size() > 0)
{
Util.sopln("List of tanks:");
Iterator it = tankMap.entrySet().iterator();
listTanks(it);
Util.sopln("Enter the Tank Number to choose:");
choice = reader.nextInt();
if(choice >= 1 && choice <= tankMap.size())
{
currentTank = choice;
}
else
{
Util.promptUser("Inavlid tank number.");
}
}
else
{
Util.promptUser("There are no tanks at the moment. Please create one first.");
}
case 11:
Util.sopln("\nList of tanks:");
Iterator it = tankMap.entrySet().iterator();
listTanks(it);
Util.promptUser(" ");
case 12:
Util.sopln(((Tank)tankMap.get(currentTank)).getName());
Util.promptUser("");
break;
case 111:
Util.sopln("End of Game.");
} //switch
Util.sopln("\t\tCommand Menu");
Util.sopln("0.Create Tank");
Util.sopln("1.Move Forward");
Util.sopln("2.Move Back");
Util.sopln("3.Turn Left");
Util.sopln("4.Turn Right");
Util.sopln("5.Get Direction");
Util.sopln("6.Get Distance Travelled");
Util.sopln("7.Get Location");
Util.sopln("8.Get Fuel Level");
Util.sopln("9.Fire");
Util.sopln("10.Switch Tank");
Util.sopln("11.List Tanks");
Util.sopln("12.Print Tank Name");
Util.sopln("111.To Exit Program.");
Util.sopln("\n\nPlease enter a command for the Tank:");
command = in.nextInt();
}while(command != 111);
in.close();
reader.close();
}
}