Collectible HW
here is my hw for the collectable team teach
List Task
public void addBulkItems(List<Collectible> newItems) {
my_collection.addAll(newItems); // adds teh items from the other lists
}
public void removeBulkItems(List<Collectible> itemsToRemove) {
my_collection.removeAll(itemsToRemove); // removes items from another list
}
Dictionary Task
// updates the value by percentage
public void updateItemValue(String itemName, double percentage) {
if (item_details.containsKey(itemName)) {
double currentValue = item_details.get(itemName).getValue();
item_details.get(itemName).setValue(currentValue * (1 + percentage / 100));
}
}
// filters the items by rarirty
public List<Collectible> filterByRarity(int rarityLevel) {
return item_details.values().stream()
.filter(item -> item.getRarity() == rarityLevel)
.collect(Collectors.toList());
}
Set Task
Set<Collectible> traded_items = new HashSet<>();
Set<Collectible> duplicate_items = new HashSet<>();
public void addItem(Collectible item) {
if (traded_items.contains(item)) {
duplicate_items.add(item); // stores/logs if its a dupe
} else {
traded_items.add(item); // if not, it adds to the main set
}
}
Print Method
public void printInventory() {
System.out.println("Item Name\tRarity\tValue");
for (Collectible item : my_collection) {
System.out.println(item.getName() + "\t" + item.getRarity() + "\t" + item.getValue());
}
}
Bonus Challenge
Stack<String> folderStack = new Stack<>();
folderStack.push("weapons");
folderStack.push("Magic");
folderStack.push("Fire");
// prints the current task
System.out.println(String.join(" > ", folderStack)); // output: weapons > Magic > Fire
// goes back from teh fire
folderStack.pop();
System.out.println(String.join(" > ", folderStack)); // output: weapons > Magic