-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotion1
247 lines (200 loc) · 6.92 KB
/
potion1
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
247
// main class
package homework1;
import java.util.*;
public class Homework1 {
//heba mustafa
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PotionInventory inventory = new PotionInventory();
String ingredientNames[] = {"Adder's Tongue", "Dandelion Leaf", "Thyme", "Dandelion Root", "Cactus", "Laurel",
"Rose Petals", "Nettle", "Eyelashes", "Black Sand", "Gum Arabic", "Anemone",
"Sparrow Entrails", "Gargamel's white-hair", "Lemon Zest", "Oregano",
"Skullcap", "Four-leaf Clover", "Solomon's Seal"};
for (String name : ingredientNames) {
inventory.addIngredient(new Ingredient(name, 10));
// a count of 10 for each ingredient
}
// consume a potion
Potion potionToConsume = new Potion(3, "Youth Elixir", "Rare", new LinkedList<>(Arrays.asList(new Ingredient("Herb of Vitality", 1))));
inventory.consumePotion(potionToConsume);
// request a potion
Potion potionRequest = new Potion(4, "Invisibility Brew", "Legendary", new LinkedList<>(Arrays.asList(new Ingredient("Eyelashes", 1))));
inventory.requestPotion(potionRequest);
System.out.println(potionToConsume);
System.out.print("\nConsumed Potions: ");
inventory.ConsumedPotions();
System.out.print("Pending Potion Requests: ");
inventory.PotionRequests();
System.out.println();
System.out.print("Enter the number of potions to create: ");
int numPotions = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < numPotions; i++) {
System.out.println("Creating Potion #" + (i + 1));
System.out.print("Enter potion name: ");
String name = scanner.nextLine();
System.out.print("Enter potion rarity (Common/Rare/Legendary): ");
String rarity = scanner.nextLine();
System.out.print("Enter number of ingredients: ");
int numIngredients = scanner.nextInt();
scanner.nextLine();
LinkedList<Ingredient> potionIngredients = new LinkedList<>();
for (int j = 0; j < numIngredients; j++) {
System.out.print("Enter ingredient #" + (j + 1) + ": ");
String ingredientName = scanner.nextLine();
Ingredient ingredient = null;
for (Ingredient ing : inventory.getIngredients()) {
if (ing.getName().equalsIgnoreCase(ingredientName)) {
ingredient = ing;
break;
}
}
if (ingredient != null && ingredient.getCount() > 0) {
potionIngredients.add(ingredient);
ingredient.setCount(ingredient.getCount() - 1);
} else {
System.out.println("Ingredient " + ingredientName + " not found");
}
}
System.out.println();
System.out.println("Custom potions created:\n");
Potion potion = new Potion(i + 1, name, rarity, potionIngredients);
inventory.addPotion(potion);
System.out.println(potion);
System.out.println();
}
}
}
//class Ingredient
package homework1;
class Ingredient {
private String name;
private int count;
public Ingredient(String name, int count) {
this.name = name;
this.count = count;
}
//setters and getters
public String getName() {
return name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String toString() {
return name ;
}
}
//class potion
package homework1;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class Potion {
private int potionID;
private String name;
private String rarity;
private LinkedList<Ingredient> ingredients;
private double cost;
public Potion(int potionID, String name, String rarity, LinkedList<Ingredient> ingredients) {
this.potionID = potionID;
this.name = name;
this.rarity = rarity;
this.ingredients = ingredients;
this.cost = calculateCost();
}
private double calculateCost() {
int ingredientCount = ingredients.size();
switch (rarity.toLowerCase()) {
case "common":
return ingredientCount * 10 + 15;
case "rare":
return ingredientCount * 10 + 70;
case "legendary":
return ingredientCount * 10 + 120;
default:
return 0;
}
}
public int getPotionID() {
return potionID;
}
public String getName() {
return name;
}
public String getRarity() {
return rarity;
}
public LinkedList<Ingredient> getIngredients() {
return ingredients;
}
public double getCost() {
return cost;
}
@Override
public String toString() {
return "Potion ID: " + potionID +
"\nPotion Name: " + name +
"\nIngredients: " + ingredients +
"\nRarity: " + rarity +
"\nPotion Value: $" + cost;
}
}
// class PotionInventory
package homework1;
import java.util.*;
class PotionInventory {
private List<Potion> allPotions;
private Stack<Potion> consumedPotions;
private Queue<Potion> potionRequests;
private LinkedList<Ingredient> ingredients;
public PotionInventory() {
allPotions = new ArrayList<>();
consumedPotions = new Stack<>();
potionRequests = new LinkedList<>();
ingredients = new LinkedList<>();
}
public void addIngredient(Ingredient ingredient) {
ingredients.add(ingredient);
}
public void addPotion(Potion potion) {
allPotions.add(potion);
}
public void consumePotion(Potion potion) {
consumedPotions.push(potion);
allPotions.remove(potion);
}
public void requestPotion(Potion potion) {
potionRequests.add(potion);
}
public List<Potion> getAllPotions() {
return allPotions;
}
public Stack<Potion> getConsumedPotions() {
return consumedPotions;
}
public Queue<Potion> getPotionRequests() {
return potionRequests;
}
public LinkedList<Ingredient> getIngredients() {
return ingredients;
}
public void displayAllPotions() {
for (Potion potion : allPotions) {
System.out.println(potion);
}
}
public void ConsumedPotions() {
for (Potion potion : consumedPotions) {
System.out.println("[ "+potion.getName()+" ]");
}
}
public void PotionRequests() {
for (Potion potion : potionRequests) {
System.out.println("[ "+ potion.getName()+" ]");
}
}
}