java - onRemoveClicked() 未在 Java Swing 中显示正确的项目移除价格
问题描述
所以我有一个 BookStore Swing 应用程序,其中我在 addClicked() 的方法中显示了正确的价格
//ACTION LISTENERS
/*
* AddButtonListener - adds selected item to shopping cart upon selection
*/
private void onAddClicked(){
selectedIndex = booksList.getSelectedIndex();
selectedBookName = booksList.getSelectedValue();
books = booksList.getModel();
shoppingCart = selectedList.getModel();
shoppingCartDFM = new DefaultListModel();
for(count=0; count<shoppingCart.getSize(); count++){
shoppingCartDFM.addElement(shoppingCart.getElementAt(count));
}
switch (selectedButton) {
case FICTION:
bookPrice += bookPrices[0];
break;
case NON_FICTION:
bookPrice += bookPrices[bookPrices.length-1];
break;
default:
bookPrice += bookPrices[selectedIndex];
break;
}
shoppingCartDFM.addElement(selectedBookName);
selectedList.setModel(shoppingCartDFM);
}
当小说和非小说书籍混合在一起然后单独删除时,Removal Click 的方法会显示错误的价格。当购物车中没有书籍时,最终结帐将始终超过 0。
/*
* RemoveButtonListener - Removes selected item from shopping cart upon selection
*/
private void onRemoveClicked(){
index = selectedList.getSelectedIndex();
if(index>=0){
((DefaultListModel)selectedList.getModel()).remove(index);
switch (selectedButton) {
case FICTION:
bookPrice -= bookPrices[0];
break;
case NON_FICTION:
bookPrice -= bookPrices[bookPrices.length-1];
break;
default:
bookPrice -= bookPrices[selectedIndex];
break;
}
}
}
所有全局变量都在这里
private static final int ALL=1;
private static final int FICTION=2;
private static final int NON_FICTION=3;
private static final int WINDOW_WIDTH= 800; //Width of GUI frame
private static final int WINDOW_LENGTH = 250; //Length of GUI frame
private JPanel booksPanel; //Holds all the books
private JPanel buttonsPanel; //Has add/remove/checkout buttons
private JPanel shoppingCartPanel; //To hold books added by user
private JPanel bannerPanel; //Banner panel
private JPanel fictionButtonsPanel; //Holds search/showall buttons
private JList booksList; //List with all book names
private JList selectedList; //List in shopping cart
private JButton addSelected; //Adds book to shopping cart
private JButton removeSelected; //Removes book from shopping cart
private JButton checkOut; //Adds all books prices + taxes
private JButton fictionButton; //Searches for desired book
private JButton showAllButton; //shows all books available
private JButton nonFictionButton; //Searches for Non Fiction Books
private BookList bookList;
private String[] bookNames;
private String[] fictionBooksNames;
private String[] nonFictionBookNames;
private int selectedButton=ALL;
private double[] bookPrices;//Array that holds all book prices
private JScrollPane scrollPane1; //Holds available books list
private JScrollPane scrollPane2; //Holds selected book list
private JLabel panelTitle; //Panel title
private JLabel cartTitle; //Panel title
private JLabel banner; //Panel title
private int element = -1; // control variable
private int selectedIndex; //Index selected among available books
private int index; //Int that holds selected index.
private int i,count; //Control variables
private double total; //Total of prices
private double bookPrice; //Hold book prices
private final double TAX=0.06; //Constant for tax value
private ListModel books; //List model for book name list
private ListModel shoppingCart; //List model for shopping cart list
private DefaultListModel shoppingCartDFM;
private DecimalFormat money; //Money format
private Object selectedBookName; //Selected book
private String searchResults; //Hold search results
private String notFound = " Title not found"; //Holds search results
private boolean found = false;
有没有人看到在混合小说和非小说时使删除书籍产生不正确价格的逻辑缺陷?
解决方案
推荐阅读
- windows - 如何在 Powershell 变量输出中保留文本格式?
- linux-kernel - 允许单个用户访问 /proc/kallsyms
- marklogic - cts:在搜索结果上应用包装元素时突出显示函数抛出错误
- html - 如何在绝对定位文本上实现与分词相同的效果?
- android - 生产中的条形图动画滚动问题
- swift - 使用 use_modular_headers 复制 React 定义 cocoapods
- javascript - JQuery 检查笑话测试中的元素可见性
- ios - 斯威夫特5。协议扩展引发编译错误“无法使用 Self 类型的参数列表调用函数”
- android - 引发用户未通过身份验证访问三星 S10 中 KeyStore 中的密钥
- python - 使用 Pandas/ExcelWriter 为单元格中的文本部分着色