【发布时间】:2022-09-24 02:10:23
【问题描述】:
我试图开发一个允许用户购买文具的订单系统。 下面是它的工作原理:
然后将选中的商品放入购物车中,如果用户想再次购买相同的商品,系统会将数量相加并显示小计。
但是,当用户想要添加另一个不在购物清单中的产品时,它会显示这个
数量与以前的产品相加,这不是我想要的。 我尝试更改代码,但它仍然给了我相同的输出 如果有人能指出我的错误,我将不胜感激。
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\');
}
}
-
您可能需要在循环开始时将
boolean exist重置为false。目前,一旦它设置为true它将永远保持该值,因此每次您向购物车添加新订单时,您的程序都会认为“存在为真,因此我需要重新计算 finalQuantity” -
@OHGODSPIDERS,我把布尔存在设置为false后,购物车给了我同样的输出,我怀疑这段代码有逻辑问题
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(); } }
标签: java