Repository navigation
Expand file tree
/
Copy pathInventory.java
More file actions
246 lines (222 loc) · 6.31 KB
/
Copy pathInventory.java
File metadata and controls
246 lines (222 loc) · 6.31 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* title: Inventory.java
* description: This class contains the methods for the
* bag and hearts storage.
* date: January 23, 2021
* @author Jenna McDonnell
* @version 1.0
* 3435506
*/
/************************************************************
* METHODS
************************************************************
* <H3> Methods <H3>
*
* <P>
* public Inventory(){<BR>
Creates an Inventory object that can use all the methods
and variables in this class.
* public void heartMeter(float add){<BR>
Adds hearts to the player's heart count and output
the number of hearts they have.
* public String openBag(){<BR>
Outputs the items currently in the player's bag and
calls the appropriate methods to find use an item
if the player says they want to use something.
* public String useItem(){<BR>
Determines which item the player wants to use and calls
findItem() to check if that item is in the player's bag.
* public void itemUses(String item){<BR>
Contains the uses for each of the items and uses a switch
statement to determine which item uses should be returned.
* public boolean findItem(String item){<BR>
Checks to see if a certain item is in the
player's bag.
* public void cannotUseItem(){<BR>
Informs user that the item they want to use cannot
be used and asks if they want to use another item.
* public void addToBag(String item){<BR>
Adds an item to the player's bag.
* public boolean searchBag(String item){<BR>
Checks if an item is in the bag.
* </P>
*/
/************************************************************
* VARIABLES
************************************************************
* <H3> Variables Used </H3>
*
* <P>
* Scanner scan = new Scanner(System.in); -
* the Scanner used in this class
*
* String answer; - the user's answer
*
* ArrayList<String> bag = new ArrayList<String>(); -
* the bag containing the user's items
*
* float hearts = 3; - number of hearts the player has
*
* boolean found; - true when an item has been found
* </P>
*/
import java.util.*;
public class Inventory{
/*
* Creates an Inventory object that can use all the methods
* and variables in this class.
*/
public Inventory(){
}
//the Scanner used in this class
Scanner scan = new Scanner(System.in);
//the user's answer
String answer;
//the bag containing the user's items
ArrayList<String> bag = new ArrayList<String>();
//number of hearts the player has
float hearts = 3;
/*
* Adds hearts to the player's heart count and output
* the number of hearts they have.
*
* @param float add the number of hearts to add
* (a negative number subtracts hearts)
*/
public void heartMeter(float add){
hearts = hearts + add;
System.out.println("You have " + hearts + " hearts.");
if(hearts <= 0){
System.out.println("You ran out of hearts!");
System.out.println("\nGAME OVER");
System.exit(0);
}
}
/*
* Outputs the items currently in the player's bag and
* calls the appropriate methods to find use an item
* if the player says they want to use something.
*
* @return String answer the current answer
*/
public String openBag(){
System.out.println("Here are the items in your bag: \n");
for(int i = 0; i < bag.size(); i++){
System.out.println("\t" + (i + 1) + ". " + bag.get(i));
if (i == (bag.size() - 1)){
System.out.println();
}
}
System.out.println("Would you like to use one of the "
+ "items?");
answer = scan.nextLine();
if(answer.equals("yes")){
answer = useItem();
}else{
System.out.println("You've closed your bag.");
answer = "noAnswer";
}
return answer;
}
/*
* Determines which item the player wants to use and calls
* findItem() to check if that item is in the player's bag.
*
* @return String answer the current answer
*/
public String useItem(){
System.out.println("Which item do you want to use?");
answer = scan.nextLine();
while (findItem(answer) == false){
System.out.println("Item not found. Contradiction.");
openBag();
answer = scan.nextLine();
}
System.out.println("Here are the things you can do with this "
+ "item:");
itemUses(answer);
return answer;
}
/*
* Contains the uses for each of the items and uses a switch
* statement to determine which item uses should be returned.
*
* @param String item the item the player wants to use
*/
public void itemUses(String item){
switch(item){
case "matches":
case "candlestick":
case "pick axe":
System.out.println(" a) swing axe");
System.out.println(" b) drop axe");
break;
case "potion":
System.out.println(" a) drink potion");
break;
case "piece of metal":
}
}
//true when an item has been found
boolean found;
/*
* Checks to see if a certain item is in the
* player's bag.
*
* @param String item the item being searched for
*
* @return boolean returns true if item has been found,
* otherwise returns false
*/
public boolean findItem(String item){
for(int i = 0; i < bag.size(); i++){
if(item.equals(bag.get(i))){
System.out.println("Item found.");
answer = bag.get(i);
return true;
}else{
try{
if(Integer.parseInt(item) == (i + 1)){
System.out.println("Item found.");
answer = bag.get(i);
return true;
}
}catch(NumberFormatException e){
}
}
}
return false;
}
/*
* Informs user that the item they want to use cannot
* be used and asks if they want to use another item.
*/
public void cannotUseItem(){
System.out.println("You can't do anything with that"
+ " right now. \nDo you want to use another item?");
if((scan.next()).equals("yes")){
openBag();
}else{
System.out.println("You have closed your bag.");
}
}
/*
* Adds an item to the player's bag.
*
* @param String item item being added to the bag
*/
public void addToBag(String item){
bag.add(item);
}
/*
* Checks if an item is in the bag.
*
* @param String item the item being searched for
*
* @return boolean returns true if item is found,
* otherwise returns false
*/
public boolean searchBag(String item){
return bag.contains(item);
}
}