PostgreSQL / Java购物车ArrayList打印空列表

时间:2018-04-23 15:34:16

标签: java sql postgresql arraylist

我遇到了将购物车打印到购物车中的问题。购物车是Book类型的ArrayList。我的程序是一个菜单实现,允许用户选择不同的选项(选项1,2等)来搜索书籍,即ISBN,作者等。还有另一个选项,用户选择显示购物车并最终从购物车中删除条目。我的问题是,当我尝试在option5()函数中调用displayShoppingCart()函数时,我收到一个空数组。但是,当我在option1()功能中调用option1()时,购物车会正确显示。我已添加option5()displayShoppingCart()List<Book> booklist = new ArrayList<>(); List <Book> shoppingCart = new ArrayList<>(); public void option1() throws SQLException, IOException { // attempts to connect to the database jdbc = new DBConnection(); connection = jdbc.getConnection(); // sql statements are here - this works properly // all of the following code is located in each option function System.out.println("Book added to cart successfully!"); Book cart = new BookSale().new Book(); cart = booklist.get(option); // option is the selection of book user chooses to add to cart cart.getSeller(); cart.getIsbn(); cart.getprice(); shoppingCart.add(cart); displayShoppingCart(shoppingCart); // this prints out the cart correctly } public void option5() throws SQLException, IOException { // display current shopping cart of member System.out.println("Your current shopping cart: "); *displayShoppingCart(shoppingCart); // prints out empty array* } // function to print shoppingCart public void displayShoppingCart(List <Book> shoppingCart) { System.out.println(shoppingCart.toString()); } 的代码,我们非常感谢任何帮助或反馈!

let compareJSON = (a, b) => {
  let ret = {};

  for (let i in b) {
    if (!a.hasOwnProperty(i) || b[i] !== a[i]) {
      ret[i] = b[i];
    }
  }
  return ret;
};

let a = [{"x":10,"y":10},{"a":10,"b":10,"c":10}]
let b = [{"x":10,"y":10},{"a":10,"b":25,"c":10}]

let length = a.length > b.length ? a.length : b.length;
for (let i = 0; i < length; i++) {
  console.log(compareJSON(a[i], b[i]));
}

0 个答案:

没有答案