【问题标题】:Why the second product quantity override the first product quantity in java?为什么第二个产品数量会覆盖 java 中的第一个产品数量?
【发布时间】:2022-09-24 02:10:23
【问题描述】:

我试图开发一个允许用户购买文具的订单系统。 下面是它的工作原理:

首先,它会显示文具菜单并要求用户输入 ID 和数量。

然后将选中的商品放入购物车中,如果用户想再次购买相同的商品,系统会将数量相加并显示小计。

但是,当用户想要添加另一个不在购物清单中的产品时,它会显示这个

数量与以前的产品相加,这不是我想要的。 我尝试更改代码,但它仍然给了我相同的输出 如果有人能指出我的错误,我将不胜感激。

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 &lt; 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


【解决方案1】:

编码

finalQuantity += orderList[index].getOrderQuantity();

将始终增加变量finalQuantity 中的值,您稍后使用代码保存该值

orderList[index].setOrderQuantity(finalQuantity);

但是,您不会将变量 finalQuantity 设置(或重置)为您的订单/产品的当前数量。这就是为什么您将得到3 的当前结果而不是1 的预期结果,因为在之前的循环/迭代中,该变量被设置为2

当您要更新数量时,您需要先读取当前数量并将其设置在变量finalQuantity 中,然后再通过用户输入增加它。


附带说明:您的代码结构不清楚,看起来(也许) 错误的。您的Order 类包含不同的代码逻辑,一种用于管理一个订单条目,还用于管理菜单/用户交互。这让Order[] orderList = new Order[size]; 变得很奇怪里面Order 类(特别是作为字段成员)。也不清楚Order 类实际上是针对可能的多个产品(不同数量)的一个订单,还是它有多个订单(什么?)。

您可能还想查看java.util.List&lt;T&gt;java.util.Map&lt;K,V&gt; 以获得具有动态大小的容器并保存所选每种产品的数量。

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 2014-08-29
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多