Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
784 B
2
Indexable
Never
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");
        }
    }


}
Leave a Comment