I was trying to develop an order system where it allows users to purchase stationery. Here is how it works:
First, it will show the stationery menu and ask user to enter the id and quantity.

Then the item selected will be put inside a shopping cart, and if the user wants to buy the same product again, the system will add the quantity together and show the subtotal.

However, when user wanted to add another product that is not in the shopping list, it will show this

The quantity was added up with the previous product, which is not what i wanted. I tried changing the code but it still gave me the same output I would appreciate if anyone can point out my mistakes.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static java.nio.file.Files.size;
import java.util.Scanner;
public class Order {
private int product_id;
private String product_Name;
private int orderQuantity = 0;
private double price = 0.0;
private double subTotal = 0;
int size = 100;
Product[] productList = new Product[size];
int count = 0;
Order[] orderList = new Order[size];
public Order() {
}
public Order(int product_id, String product_Name, double price, int orderQuantity) {
this.product_id = product_id;
this.product_Name = product_Name;
this.orderQuantity = orderQuantity;
}
public Order(int product_id, String product_Name, double price, double subTotal, int orderQuantity) {
this.product_id = product_id;
this.product_Name = product_Name;
this.price = price;
this.subTotal = subTotal;
this.orderQuantity = orderQuantity;
}
public String getProduct_Name() {
return product_Name;
}
public void setProduct_Name(String product_Name) {
this.product_Name = product_Name;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getOrderQuantity() {
return orderQuantity;
}
public void setOrderQuantity(int orderQuantity) {
this.orderQuantity = orderQuantity;
}
public Order(double subTotal) {
this.subTotal = subTotal;
}
public double getSubTotal() {
return subTotal;
}
public void setSubTotal(double subTotal) {
this.subTotal = subTotal;
}
public void Menu() {
System.out.println("Stationaries");
System.out.println("================");
System.out.println("ID Name Price Quantity");
System.out.println("=== =================== ===== ========");
try {
File product = new File("src/product.txt");
Scanner order = new Scanner(product);
while (order.hasNextLine()) {
String[] data = order.nextLine().split("\\|");
int product_id = Integer.parseInt(data[0]);
String product_name = data[1];
double product_price = Double.parseDouble(data[2]);
int product_quantity = Integer.parseInt(data[3]);
productList[count] = new Product(product_id, product_name, product_price, product_quantity);
count++;
System.out.printf("%-4d%-20s%-6.2f%-10d\n", product_id, product_name, product_price, product_quantity);
}
order.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public void makeOrder() {
char yesno;
boolean check = false;
int index = 0;
int count1 = 0;
int totalprice = 0;
int finalQuantity = 0;
boolean exist = false;
double product_price;
String product_Name;
Scanner order = new Scanner(System.in);
do {
System.out.print("Enter product ID :");
int product_Id = order.nextInt(); //1
product_Name = productList[product_Id - 1].getProduct_name();
product_price = productList[product_Id - 1].getProduct_price();
for (int i = 0; i < count; i++) {
if (product_Id == productList[i].getProduct_id()) {
check = true;
}
}
if (check) {
System.out.print("Enter quantity : ");
int quantity = order.nextInt();
orderList[count1] = new Order(product_Id, product_Name, product_price, quantity); //start from 0
count1++; //move to 1 //total 2 products
}
for (int k = 0; k < count1; k++) {
if (product_Id == orderList[k].getProduct_id()) { //if user enter prod 1, and prod 1 exist in the cart
index = k;
exist = true;
orderList[k].getProduct_id();
orderList[k].getOrderQuantity();
orderList[k].getPrice();
break;
}
}
System.out.println("Shopping Cart");
System.out.println("================");
System.out.println("ID Name Price Quantity Subtotal");
System.out.println("=== =================== ===== ======== ========");
if (exist) {
finalQuantity += orderList[index].getOrderQuantity();
orderList[index].setOrderQuantity(finalQuantity); //reset the quantity to the final version
orderList[index].setSubTotal(orderList[index].getOrderQuantity() * product_price);
System.out.printf("%-4d%-20s%-6.2f%-10d%-6.2f\n", orderList[index].getProduct_id(), orderList[index].getProduct_Name(), product_price, orderList[index].getOrderQuantity(), orderList[index].getSubTotal());
} else {
index++;
orderList[index].setProduct_id(orderList[index].getProduct_id()); //store at next array
orderList[index].setOrderQuantity(orderList[index].getOrderQuantity());
orderList[index].setSubTotal(orderList[index].getOrderQuantity() * product_price);
System.out.printf("%-4d%-20s%-6.2f%-10d%-6.2f\n", orderList[index].getProduct_id(), orderList[index].getProduct_Name(), product_price, orderList[index].getOrderQuantity(), orderList[index].getSubTotal());
}
// } else {
// index++;
// orderList[index].setProduct_id(orderList[index].getProduct_id()); //store at next array
// orderList[index].setOrderQuantity(orderList[index].getOrderQuantity());
// orderList[index].setSubTotal(orderList[index].getOrderQuantity() * product_price);
// System.out.printf("%-4d%-20s%-6.2f%-10d%-6.2f\n", orderList[index].getProduct_id(), orderList[index].getProduct_Name(), product_price, orderList[index].getOrderQuantity(), orderList[index].getSubTotal());
// }
//use later
// System.out.println("Final Shopping Cart");
// System.out.println("================");
// System.out.println("ID Name Price Quantity Subtotal");
// System.out.println("=== =================== ===== ======== ========");
//
// System.out.printf("%-4d%-20s%-6.2f%-10d%-6.2f\n", orderList[i + 1].getProduct_id(), orderList[i + 1].getProduct_Name(), product_price, orderList[i + 1].getOrderQuantity(), orderList[i + 1].getSubTotal());
// int num = 0;
// boolean exist = false;
// for (int j = 0; j < count1; j++) {
//
// if (orderList[j].getProduct_id() == num + 1) { //if id is 1
// exist = true;
// index = j;
//
// } else {
// num++;
// }
//
// }
//
// if (exist) {
// System.out.println("final quantity=" + (finalQuantity += orderList[index].getOrderQuantity()));
// }
System.out.print("Continue Order?(Y|N)");
yesno = order.next().toUpperCase().charAt(0);
} while (yesno == 'Y');
}
}