I get an IOException error that says:
Unreported exception IOException, must be intercepted or declared to be dropped
I was looking for answers, but none of them were helpful. That is why I asked. The debugger displays this line:
public static article Article = new article ();
Just the right side gives mistakes. I was trying to use try catch, but it only resulted in more errors. I also tried to take this line within the menu class, but it always comes back to mistakes. I'm pretty sure I'm missing something that's so simple, but I really want to ask you.
package proje;
import java.util.ArrayList;
Import java.util.Scanner;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class home extends item {
static ArrayList- Cart = new ArrayList <> ();
public static article Article = new article ();
static int id;
static scanner scan = new scanner (System.in);
public static void menu (string type, string username) {
Scanner input = new scanner (System.in);
if (type.equals ("admin") || type.equals ("admin")) {
System.out.println ("********** ADMIN *********");
System.out.println ("0 - LOGOUT");
System.out.println ("1 - ADD PRODUCT");
System.out.println ("2 - REMOVE PRODUCT");
System.out.println ("3 - LIST PRODUCT");
System.out.println ("4 - EXIT");
int slc = input.nextInt ();
Switch (slc) {
Case 0:
Proje.entry ();
break;
Case 1:
System.out.println ("Product Type:");
String prdType = input.next ();
System.out.println ("product name:");
String prdName = input.next ();
System.out.println ("Product Price:");
double prdPrice = input.nextDouble ();
System.out.println ("Quantity:");
int prdQuantity = input.nextInt ();
System.out.println ("Product Description:");
String prdDescription = input.next ();
checkID ();
Item tempNewItem = new Item (id, prdName, prdPrice, prdDescription, prdQuantity, prdType);
item.addItemToList (tempNewItem);
System.out.println ("You have added an article!");
String str;
BufferedOutputStream b = null;
To attempt {
BufferedOutputStream b2 = new BufferedOutputStream (new FileOutputStream ("products.txt", true));
str = String.valueOf (tempNewItem);
byte[] byteCal = str.getBytes ();
b2.write (byteCal);
b2.flush ();
b2.close ();
}
catch (IOException e) {
e.printStackTrace (); // unnecessary
}
Home.Menu ("Admin", username);
Case 2:
for (Article i: itemList) {
System.out.println ("productname:" + i.getName ());
}
System.out.println ("Which product will be removed:");
String removeItem = input.next ();
item.removeItem (removeItem);
System.out.println (removeItem + "will be removed!"););
Home.Menu ("Admin", username);
Case 3:
printIDs ();
System.out.println ("ID:");
int selectionId = input.nextInt ();
item.getItemDescription (selectionId);
Home.Menu ("Admin", username);
Case 4:
System.exit (4);
break;
Default:
break;
}
}
else if (type.equals ("user") || type.equals ("user")) {
System.out.println ("********* USER MENU *********");
System.out.println ("0 - LOGOUT");
System.out.println ("1 - LIST PRODUCTS");
System.out.println ("2 - LIST PRODUCT TYPES");
System.out.println ("3 - DESCRIPTION OF PRODUCTS");
System.out.println ("4 - ADD TO BASKET");
System.out.println ("5 - REMOVE SHOPPING CART");
System.out.println ("6 - HISTORY");
System.out.println ("7 - LIST ITEMS");
System.out.println ("8 -> EXIT");
int selection = input.nextInt ();
Switch (selection) {
Case 0:
Proje.entry ();
System.out.println ("-------------");
Menu ("User", Username);
Case 1:
item.getItemList ();
System.out.println ("-------------");
Menu ("User", Username);
Case 2:
item.ListProductOfType ();
System.out.println ("-------------");
Menu ("User", Username);
Case 3:
printIDs ();
System.out.println ("Product ID:");
int selectionId = input.nextInt ();
item.getItemDescription (selectionId);
System.out.println ("-------------");
Menu ("User", Username);
Case 4:
item.getItemList ();
System.out.println (("elementname:"));
String optionName = input.next ();
for (Article i: itemList) {
if (i.getName (). equals (optionname)) {
Cart.add (i);
}
}
for (article i: shopping cart) {
System.out.println (i.getName () + "has been added to your cart!");
}
System.out.println ("-------------");
Menu ("User", Username);
Case 5:
if (Cart.size () == 0) {
System.out.println ("No item in shopping cart!");
Menu ("User", Username);
}
miscellaneous
System.out.println ("product name:");
String selectionName = input.next ();
for (int i = 0; i <Cart.size (); i ++) {
if (Cart.get (i) .getName (). equals (selectionName)) {
Cart.remove (i);
}
}
for (article i: shopping cart) {
System.out.println (i.getName ());
}
System.out.println (selectionName + "is canceled!");
System.out.println ("-------------");
}
Menu ("User", Username);
Case 6:
System.out.println ("History could not be loaded!");
System.out.println ("-------------");
Menu ("User", Username);
Case 7:
System.out.println ("Product Type:");
String selectType = input.next ();
item.getItemType (selectType);
System.out.println ("-------------");
Menu ("User", Username);
Case 8:
System.exit (8);
break;
Default:
break;
}
}
miscellaneous
System.out.println ("Invalid input!");
}
}
public static void printIDs () {
for (Article i: itemList) {
System.out.println ("Article ID:" + i.getID () + "/" + "Product Name:" + i.getName ());
}
}
public static int checkID () {
System.out.println ("ID:");
int id = scan.nextInt ();
for (Item temp: itemList) {
if (temp.getID () == id) {
printIDs ();
System.out.println ("ID already exists! NID:");
checkID ();
}
}
Return ID;
}
}