Java-Shop系统中的错误

问题描述

所以我几乎没有库存/商店互动,这是我找不到解决方案的地方。

玩家应该能够在我的Item类构造函数中买卖具有预定义购买/出售值的物品。借助扫描仪,我可以毫无问题地进行销售。但是,如果我购买任何数量的产品,那么全部数量都会以某种方式购买。我一次购买任意数量后,商品数量设置为0。

    private void buyItem() {
    System.out.println("Which item would you like to buy?");
    showStock();

    @SuppressWarnings("resource")
    Scanner sc = new Scanner(system.in);
    String itemName = "" + sc.next();
    
    for (int i = 0; i < store.length; i++) {
        if (store[i] != null && store[i].getName().equals(itemName)) {
            // Checks if the given item name is available in the store
            System.out.println("We have " + store[i].getQuantity() + " " + store[i].getName() + "s in our stock. " + store[i].getBuyPrice() + " each");
            System.out.println("How many would you like to buy?");
            int buyQuantity = sc.nextInt();
            // Checks if there is enough in the stock
            if (buyQuantity > store[i].getQuantity()) {
                System.out.println("Not enough " + store[i].getName() + " available in the stock.");
                break;
            } else {
                // Checks if Player can afford it
                if (Player.getInstance().getCoins() >= store[i].getBuyPrice()) {
                    // Update player inventory
                    Player.getInstance().removeCoins(buyQuantity * store[i].getBuyPrice());
                    Item item = store[i];
                    item.setQuantity(buyQuantity);
                    Player.getInstance().getInv().addItem(item);
                    
                    // Buy transaction
                    store[i].setQuantity(store[i].getQuantity() - buyQuantity);
                    System.out.println("You have purchased " + buyQuantity + "x " + itemName);
                    System.out.println("Coins: " + Player.getInstance().getCoins());
                    
                    // Checks if the entire amount has been bought
                    if (buyQuantity == store[i].getQuantity()) {
                        store[i] = null;
                        System.out.println("That's all " + itemName + " the store had");
                    }
                    break;
                } else {
                    System.out.println("You don't have enough coins to buy " + buyQuantity + " " + itemName + "s");
                    break;
                }
            }

        }
    }
}

这是我的控制台输出示例:

Which item would you like to buy?
Slot 0: Weapon
Slot 1: Cake
Slot 2: Arrow
Arrow
We have 100 Arrows in our stock. 2 each
How many would you like to buy?
10
You have purchased 10x Arrow
Coins: 980
That's all Arrow the store had

我已经尝试和思考了很长时间,但是我无法弄清我的错误。你有什么建议吗?我尝试调试,一切都在“工作”,值是正确的。但是,当我输入要购买的数量时,我的int输出没有任何影响。非常感谢任何提示

解决方法

Item item = store[i];

不会创建副本,而是引用与数组store中存储的对象相同的对象。以下

item.setQuantity(buyQuantity); 

store中的项目数量设置为buyQuantity及以下

store[i].setQuantity(store[i].getQuantity() - buyQuantity);

有效地将物品的数量减少到0(在商店和玩家清单中)。

至于修复,可以使用clone()但可以使用this is regarded as broken

我会建议一种语义上不同的方法。每个项目不代表单个项目,而是一捆项目。因此,可以向束中添加特定量或从束中移除特定量。为了使这可行,我将在ItemT extends Item<T>中使通用超类abstract(我假设存在)通用,并为其提供一个public abstract T take(int amount);方法:

public abstract class Item<T extends Item<T>> {
    ...
    public abstract T take(int amount);
    ...
}

然后,我将更改所有子类以扩展此超类,例如

public class Arrow extends Item<Arrow> {
    public Arrow take(int amount) {
        ...
    }
}

最后,我将重命名类以清楚地反映出每个Item是一捆物品。例如,Item可以重命名为ItemBundle,而Arrow可以重命名为ArrowBundleArrows

,

您正在此处将购买的物品的数量明确更改为玩家要求的数量:

'defaults' => [
    'guard' => 'api','passwords' => 'users',],'guards' => [
    'web' => [
        'driver' => 'session','provider' => 'users','api' => [
        'driver' => 'jwt',

如果您想添加x数量的Item item = store[i]; item.setQuantity(buyQuantity); 玩家的库存,则需要创建该Item的新实例,在该实例上您将Item设置为购买的数量。这样,您可以保持商店库存的完整性。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...