Untitled
Anonymous
java
01/02/2024 5:56 PM
1.0 KB
18
Indexable
import java.util.HashMap;
import java.util.Scanner;
public class Shop {
static HashMap<String,Item> items = new HashMap<>();
public Shop(){
items.put("car", new Item(6,7));
items.put("pc", new Item(7, 8));
}
public void buy(String name, int amount, User buyer){
if(items.containsKey(name)) {
Item itemToBuy = items.get(name);
int price = (itemToBuy.getPrice()) * (itemToBuy.getAmount());
if (price <= buyer.getAmountOfMoney()) {
buyer.pay(price);
} else {
System.out.println(" you can not buy this item! ");
}
} else {
System.out.println("This item does not exist in the store");
}
}
}Editor is loading...
Leave a Comment