-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameInventoryMVP.java
More file actions
129 lines (109 loc) · 3.61 KB
/
Copy pathGameInventoryMVP.java
File metadata and controls
129 lines (109 loc) · 3.61 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
import java.util.HashMap;
import java.util.Map;
/**
* Proof-of-Concept for an RPG GameInventory component.
*
* This is NOT the full OSU-discipline version.
* It is a minimal working implementation to prove feasibility.
*/
public class GameInventoryMVP {
// Representation: item name -> quantity
private Map<String, Integer> items;
/**
* Constructor initializes empty inventory.
*/
public GameInventoryMVP() {
this.items = new HashMap<>();
}
/**
* Adds a quantity of an item to the inventory.
*/
public void addItem(String item, int quantity) {
if (quantity <= 0) {
return;
}
this.items.put(item, this.getQuantity(item) + quantity);
}
/**
* Removes a quantity of an item.
*/
public void removeItem(String item, int quantity) {
if (!this.items.containsKey(item) || quantity <= 0) {
return;
}
int current = this.items.get(item);
if (quantity >= current) {
this.items.remove(item);
} else {
this.items.put(item, current - quantity);
}
}
/**
* Returns quantity of an item.
*/
public int getQuantity(String item) {
return this.items.getOrDefault(item, 0);
}
/**
* Returns whether inventory contains the item.
*/
public boolean hasItem(String item) {
return this.getQuantity(item) > 0;
}
/**
* Transfers items to another inventory.
*/
public void transferItem(GameInventoryMVP other, String item, int quantity) {
if (this.getQuantity(item) >= quantity) {
this.removeItem(item, quantity);
other.addItem(item, quantity);
}
}
/**
* Returns total number of all items combined.
*/
public int totalItems() {
int total = 0;
for (int qty : this.items.values()) {
total += qty;
}
return total;
}
/**
* Displays inventory contents.
*/
public void displayInventory() {
System.out.println("Inventory Contents:");
for (Map.Entry<String, Integer> entry : this.items.entrySet()) {
System.out.println("- " + entry.getKey() + ": " + entry.getValue());
}
System.out.println("Total Items: " + this.totalItems());
System.out.println();
}
/**
* Main method demonstrating proof-of-concept.
*/
public static void main(String[] args) {
GameInventoryMVP player = new GameInventoryMVP();
GameInventoryMVP chest = new GameInventoryMVP();
// Add items
player.addItem("Health Potion", 5);
player.addItem("Iron Sword", 1);
player.addItem("Gold Coin", 100);
System.out.println("Player inventory after adding items:");
player.displayInventory();
// Remove items
player.removeItem("Gold Coin", 25);
System.out.println("Player inventory after spending gold:");
player.displayInventory();
// Transfer items
player.transferItem(chest, "Health Potion", 2);
System.out.println("Player inventory after transferring potions:");
player.displayInventory();
System.out.println("Chest inventory after receiving potions:");
chest.displayInventory();
// Demonstrate hasItem
System.out.println("Does player have Iron Sword? " + player.hasItem("Iron Sword"));
System.out.println("Does player have Diamond? " + player.hasItem("Diamond"));
}
}