0

我正在实现一个菜单驱动的程序,用户可以根据不同的选项搜索书籍,例如:ISBN、标题等。我遇到的问题是,如果用户两次选择相同的选项,它将返回相同的卖家和上一次运行的价格。

例如,如果用户选择选项 1,并输入 isbn 18937646282。假设返回以下内容:seller: adrake Price: 20.56。然后,用户再次选择选项1,输入isbn:87473626262。应该返回如下:seller aharrison Price 10:20:。但是,它返回与第一次运行相同的内容:seller: adrake Price: 20.56.

如果我退出程序并重新运行整个程序,该程序可以正常工作。另外,我已经打印出了 isbn,它正在为每次运行检索正确的 isbn、作者、标题。它只是没有检索到正确的卖家和价格。任何帮助或见解将不胜感激。下面的代码显示了 option1():

List<BookSale> booklist = new ArrayList<>();
public void option1() throws SQLException, IOException
   {
      String sql;
      String sql_;
      String sql_info;
      int option;
      int i;
      int count = 0;


      // asks user to enter ISBN 
      System.out.print("Enter ISBN of the book you are searching for: ");
      isbn = stdin.next();

       //sql statement to select seller and price for user desired ISBN
       sql_ = "SELECT *" + " from BOOK_SALE " + "WHERE isbn = " +
            "'" + isbn + "'" + "and status =" + "'active'";

      // execute query
      result2 = select.executeQuery(sql_);

      // display Seller and Price of the desired ISBN, add book to ArrayList
      while(result2.next())
      {
         BookSale book = new BookSale(listing_no, seller,isbn, condition,price);
         book.setListingNumber(result2.getInt(1));
         book.setSeller(result2.getString(2));
         book.setISBN(result2.getString(3));
         book.setCondition(result2.getString(4));
         book.setPrice(result2.getDouble(5));
         booklist.add(book);
         count++;
      } // while

for (i = 0; i < count; i++) //0
         {
            // display number of the selections
            System.out.println(i + " " + booklist.get(i));
         }// for

在课堂上打印卖家和价格的功能:

@Override
      public String toString(){
         return ". Seller:" + seller + " Price: $" + price;
         }
4

0 回答 0