-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransformation.java
executable file
·60 lines (50 loc) · 1.44 KB
/
Transformation.java
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
package textgame;
import java.util.*;
/**
* Represents some way in which items can be transformed to make new items.
*
* input is a list of String item types which are consumed from the user's
* inventory.
* output is a list of Items which are added to the user's inventory
*
* @author ColinRothwell
*
* @see Item
*/
public class Transformation {
private String name;
private List<String> input;
private List<Item> output;
public Transformation(String n, List<String> in, List<Item> out) {
name = n;
input = in;
output = out;
}
public UserResponse performTransformation(ItemContainer player) {
ArrayList<Item> consumed = new ArrayList<Item>();
for (String type : input) {
Item it = player.itemOfType(type);
if (it == null) {
return UserResponse.error("You must have an object of type " + type + " to " + name + ".");
}
consumed.add(it);
}
for (Item rem : consumed) {
player.removeItem(rem);
}
for (Item add : output) {
player.addItem(add);
}
return UserResponse.message("Removed " + ItemContainer.describeItems(consumed) +
", and added " + ItemContainer.describeItems(output) + ".");
}
public String getName() {
return name;
}
public List<String> getInput() {
return input;
}
public List<Item> getOutput() {
return output;
}
}