Hi,
I'm working on my final project and I'm almost done , but I notice error in my program
I have JList and csv file the user load the file then the user can add new item to the JList they need to enter the id,title,publisher ..etc
the problem is when I check for the id if it in the array or not it add the item and don't check if the id is already excist
here is my code for the add button
try {
int id = Integer.parseInt(txtID.getText());
String author = txtAuthor.getText();
String publisher = txtPub.getText();
String title = txtTitle.getText();
String pubDate = txtPubDate.getText();
String category = txtCategory.getText();
String subCat = txtSubCat.getText();
double price = Double.parseDouble(txtPrice.getText());
String issueNo = txtIssueNo.getText();
String brandName = txtBrandName.getText();
String desc = txtDesc.getText();
String stock = txtStock.getText();
int currId = 0;
for (BookStore temp : bookStore) {
currId = temp.getId();
}
if (currId == id) {
JOptionPane.showMessageDialog(this, "Product with the " + id + " is already excist");
} else {
if (category.equals("Book")) {
Book b = new Book(title, author, publisher, pubDate, subCat, category, price, id, stock);
model.addElement(b);
bookStore.add(b);
} else if (category.equals("Magazine")) {
Magazien m = new Magazien(category, title, issueNo, publisher, pubDate, subCat, price, id, stock);
model.addElement(m);
bookStore.add(m);
} else if (category.equals("Office Supplies")) {
officeSupplies office = new officeSupplies(category, subCat, brandName, desc, price, id, stock);
model.addElement(office);
bookStore.add(office);
} else {
JOptionPane.showMessageDialog(this, "The category " + category + " cannot be added");
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please select category then enter the data. you need to enter number for price and id");
txtOutputErrors.append("Please select category then enter the data. you need to enter number for price and id");
}