I create a ProductMenu class to perform the add, search, delete and update product tasks and it only use the array. What problem that i face is how to add static variable in a method because i failed to add the maximum number of array after add a product into array. Hope can help and thank you.
public static void addProduct(){
Product product = new Product();
System.out.println("================================");
System.out.println(" Add Product ");
System.out.println("================================");
System.out.println("Product ID :- " + Product.getProdId());
System.out.print("Enter product name: ");
String prodName = input.next();
System.out.print("Enter product price: ");
double prodPrice = input.nextDouble();
product.setProductId(Product.getProdId());
product.setProductName(prodName);
product.setProductPrice((double)prodPrice);
products[productIndex] = product;
numOfArray++; // should be add ?
productIndex++; // should be add also?
ProductMenu.main(null); //back to main
}
My class
public class ProductMenu {
static Scanner input = new Scanner(System.in);
static int productIndex; //should be add after addProduct()
static int numOfArray = 1; //should be add after addProduct()
static Product[] products = new Product[numOfArray];
public static void main(String[] args) {
int menuOption;
do{
menu();
System.out.print("Enter your choice: ");
menuOption = input.nextInt();
switch(menuOption){
case 1:
addProduct();
break;
case 2:
System.out.print("halo" + products[0].toString());
break;
case 3:
break;
case 4:
break;
default:
System.out.print("Invalid option. Please try
again.\n");
break;
}
}while(menuOption!=1 && menuOption!=2 && menuOption!=3 &&
menuOption!=4 );
}
public static void menu(){
System.out.println("================================");
System.out.println(" Product Menu ");
System.out.println("================================");
System.out.println("1. Insert Product\n2. Update Product\n3.
Search Product\n4. Delete Product\n");
}
This is error msg that mean array number already more than limit and why i ask if i not wrong.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at ProductMenu.addProduct(ProductMenu.java:64)
at ProductMenu.main(ProductMenu.java:21)
at ProductMenu.addProduct(ProductMenu.java:68)
at ProductMenu.main(ProductMenu.java:21)