以表单访问数据库

时间:2018-12-05 22:53:44

标签: java

正在与我正在建立的项目作斗争。我是Java的新手,这只是我整理在一起进行练习的东西。这是某种形式的收银机。您将选择一个项目并将其添加到卡中。我的问题在getById()行的for ( Inventory candidateItem : inventoryDao.findAll())方法中。特别是inventoryDao.findAll()。那是对我创建的数据库的调用,它可以在我的控制器中使用,但在这里不起作用。我觉得可能与该“静态”有关,因为我可以删除它,但会破坏其他内容。

public class Cart {

@Autowired
    private InventoryDao inventoryDao;

    static ArrayList<Inventory> cart = new ArrayList<>();


    public static ArrayList<Inventory> getAll() {
        return cart;
    }



    public static void add(int id) {
        Inventory cartItemToAdd = getById(id);
        cart.add(cartItemToAdd);
    }

    public static void remove(int id) {
        Inventory cartItemToRemove = getById(id);
        cart.remove(cartItemToRemove);
    }

    //Issue is here.....
    public static Inventory getById(int id) {

        Inventory theItem = null;
        // InventoryDao inventoryDao = null;
        for ( Inventory candidateItem : inventoryDao.findAll()) {  //<---here
            if (candidateItem.getId() == id) {
                theItem = candidateItem;
            }
        }
        return theItem;
    }

    public static void clearCart() { //this shoud effective reset teh array list. Used when sending the completed order to DB or just clearing car
        ArrayList<Inventory> cart = new ArrayList<>();
    }

}

0 个答案:

没有答案