一个用户aaa 2024-05-01 09:44 采纳率: 50%
浏览 3
已结题

java疑似stdIn.readline()方法无法读取

疑似stdIn.readline()方法无法读取。问题位于第430行处,使用注释内容,即

product=catalog.getProduct("A001");

时可以正常输出预期结果。
尝试输出stdIn.readline()方法发现输出为空,即读取内容为空

import java.io.*;
import java.util.*;
import java.text.*;

/**
 * This class implements a gourmet coffee system.
 *
 * @author author name
 * @version 1.1.0
 * @see Product
 * @see Coffee
 * @see CoffeeBrewer
 * @see Catalog
 * @see OrderItem
 * @see Order
 */
public class GourmetCoffee  {

    private static BufferedReader  stdIn =
        new  BufferedReader(new  InputStreamReader(System.in));
    private static PrintWriter  stdOut =
        new  PrintWriter(System.out, true);
    private static PrintWriter  stdErr =
        new  PrintWriter(System.err, true);

    private static final NumberFormat CURRENCY =
        NumberFormat.getCurrencyInstance();

    private Catalog  catalog;
    private Order  currentOrder;
    private Sales  sales;

    /**
     * Loads data into the catalog and starts the application.
     *
     * @param args  String arguments.  Not used.
     * @throws IOException if there are errors in the input.
     */
    public static void  main(String[]  args) throws IOException  {

        GourmetCoffee  application = new  GourmetCoffee();
        application.run();

    }

    /**
     * Constructs a <code>GourmetCoffee</code> object and
     * initializes the catalog and sales data.
     *
     * @param initialCatalog a product catalog
     */
    private GourmetCoffee() {

        this.catalog = loadCatalog();
        this.sales = loadSales(this.catalog);
        this.currentOrder = new Order();
    }

    /**
     * Creates an empty catalog and then add products to it.
     *
     * @return a product catalog
     */
    private Catalog loadCatalog() {

        Catalog catalog = new Catalog();

        catalog.addProduct(new Coffee(
            "C001", "Colombia, Whole, 1 lb", 17.99,
            "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium", "Full"));
        catalog.addProduct(new Coffee(
            "C002", "Colombia, Ground, 1 lb", 18.75,
            "Colombia", "Medium", "Rich and Hearty", "Rich", "Medium","Full"));
        catalog.addProduct(new Coffee(
            "C003", "Italian Roasts, Whole, 1 lb", 16.80,
            "Latin American Blend", "Italian Roast", "Dark and heavy",
            "Intense", "Low", "Medium"));
        catalog.addProduct(new Coffee(
            "C004", "Italian Roasts, Ground, 1 lb", 17.55,
            "Latin American Blend", "Italian Roast", "Dark and heavy",
            "Intense", "Low", "Medium"));
        catalog.addProduct(new Coffee(
            "C005", "French Roasts, Whole, 1 lb", 16.80,
            "Latin American Blend", "French Roast", "Bittersweet, full intense",
            "Intense, full", "None", "Medium"));
        catalog.addProduct(new CoffeeBrewer(
            "B001", "Home Coffee Brewer", 150.00,
            "Brewer 100", "Pourover", 6));
        catalog.addProduct(new CoffeeBrewer(
            "B002", "Coffee Brewer, 2 Warmers", 200.00,
            "Brewer 200", "Pourover", 12));
        catalog.addProduct(new CoffeeBrewer(
            "B003", "Coffee Brewer, 3 Warmers", 280.00,
            "Brewer 210", "Pourover", 12));

        catalog.addProduct(
            new Product("A001", "Almond Flavored Syrup", 9.00));
        catalog.addProduct(
            new Product("A002", "Irish Creme Flavored Syrup", 9.00));
        catalog.addProduct(
            new Product("A003", "Mint Flavored syrup", 9.00));
        catalog.addProduct(
            new Product("A004", "Caramel Flavored Syrup", 9.00));
        catalog.addProduct(
            new Product("A005", "Gourmet Coffee Cookies", 12.00));

        return catalog;
    }

    /**
     * Initializes the sales object.
     */
    private Sales loadSales(Catalog catalog) {

        Sales sales = new Sales();
        Order[] orders = new Order[6];

        orders[0] = new Order();
        orders[0].addItem(new OrderItem(catalog.getProduct("C001"), 5));
        sales.addOrder(orders[0]);

        orders[1] = new Order();
        orders[1].addItem(new OrderItem(catalog.getProduct("C002"), 2));
        orders[1].addItem(new OrderItem(catalog.getProduct("A001"), 2));
        orders[1].addItem(new OrderItem(catalog.getProduct("A003"), 2));
        sales.addOrder(orders[1]);

        orders[2] = new Order();
        orders[2].addItem(new OrderItem(catalog.getProduct("B002"), 1));
        orders[2].addItem(new OrderItem(catalog.getProduct("A003"), 3));
        sales.addOrder(orders[2]);

        orders[3] = new Order();
        orders[3].addItem(new OrderItem(catalog.getProduct("B003"), 2));
        orders[3].addItem(new OrderItem(catalog.getProduct("C001"), 3));
        orders[3].addItem(new OrderItem(catalog.getProduct("C003"), 3));
        orders[3].addItem(new OrderItem(catalog.getProduct("C005"), 3));
        orders[3].addItem(new OrderItem(catalog.getProduct("A001"), 3));
        orders[3].addItem(new OrderItem(catalog.getProduct("A004"), 2));
        sales.addOrder(orders[3]);
        
        orders[4] = new Order();
        orders[4].addItem(new OrderItem(catalog.getProduct("B001"), 1));
        orders[4].addItem(new OrderItem(catalog.getProduct("C002"), 2));
        orders[4].addItem(new OrderItem(catalog.getProduct("C003"), 2));
        orders[4].addItem(new OrderItem(catalog.getProduct("A001"), 2));
        orders[4].addItem(new OrderItem(catalog.getProduct("A002"), 6));
        sales.addOrder(orders[4]);
        
        orders[5] = new Order();
        orders[5].addItem(new OrderItem(catalog.getProduct("B001"), 1));
        orders[5].addItem(new OrderItem(catalog.getProduct("C001"), 1));
        orders[5].addItem(new OrderItem(catalog.getProduct("C005"), 5));
        orders[5].addItem(new OrderItem(catalog.getProduct("A001"), 5));
        orders[5].addItem(new OrderItem(catalog.getProduct("A004"), 4));
        sales.addOrder(orders[5]);
        
        return sales;
    }

    /*
     * Presents the user with a menu of options and executes the
     * selected task.
     */
    private void run() throws IOException  {

        int  choice = getChoice();

        while (choice != 0)  {

            if (choice == 1)  {
                displayCatalog();
            } else if (choice == 2)  {
                displayProductInfo();
            } else if (choice == 3)  {
                displayOrder();
            } else if (choice == 4)  {
                addModifyProduct();
            } else if (choice == 5)  {
                removeProduct();
            } else if (choice == 6)  {
                saleOrder();
            } else if (choice == 7)  {
                displayOrdersSold();
            } else if (choice == 8)  {
                displayNumberOfOrders(readProduct());
            } else if (choice == 9)  {
                displayTotalQuantityOfProducts();
            } 

            choice = getChoice();
        }
    }

    /*
     * Displays a menu of options and verifies the user's choice.
     *
     * @return an integer in the range [0,7]
     */
    private int  getChoice() throws IOException  {

        int  input;

        do  {
            try  {
                stdErr.println();
                stdErr.print(
                      "[0] Quit\n"
                    + "[1] Display catalog\n"
                    + "[2] Display product\n"
                    + "[3] Display current order\n"
                    + "[4] Add|modify product to|in current order\n"
                    + "[5] Remove product from current order\n"
                    + "[6] Register sale of current order\n"
                    + "[7] Display sales\n"
                    + "[8] Display number of orders with a specific product\n"
                    + "[9] Display the total quantity sold for each product\n"
                    + "choice> ");
                stdErr.flush();

                input = Integer.parseInt(stdIn.readLine());

                stdErr.println();

                if (0 <= input && 9 >= input)  {
                    break;
                } else {
                    stdErr.println("Invalid choice:  " + input);
                }
            } catch (NumberFormatException  nfe)  {
                stdErr.println(nfe);
            }
        }  while (true);

        return  input;
    }

    /**
     * Displays the catalog.
     */
    public void displayCatalog() {

        int size = this.catalog.getNumberOfProducts();

        if (size == 0) {
            stdErr.println("The catalog is empty");
        } else {
            for (Product  product : this.catalog) {

                stdOut.println(product.getCode() + " " +
                    product.getDescription());
            }
        }
    }

    /**
     * Displays the information of a product
     */
    public void displayProductInfo() throws IOException  {

        Product product = readProduct();

        stdOut.println("  Description: " + product.getDescription());
        stdOut.println("  Price: " + product.getPrice());
        if (product instanceof Coffee) {

            Coffee coffee = (Coffee) product;

            stdOut.println("  Origin: " + coffee.getOrigin());
            stdOut.println("  Roast: " + coffee.getRoast());
            stdOut.println("  Flavor: " + coffee.getFlavor());
            stdOut.println("  Aroma: " + coffee.getAroma());
            stdOut.println("  Acidity: " + coffee.getAcidity());
            stdOut.println("  Body: " + coffee.getBody());
        } else if (product instanceof CoffeeBrewer) {

            CoffeeBrewer brewer = (CoffeeBrewer) product;

            stdOut.println("  Model: " + brewer.getModel());
            stdOut.println("  Water Supply: " + brewer.getWaterSupply());
            stdOut.println("  Number of Cups: " + brewer.getNumberOfCups());
        }
    }

    /**
     * Displays the current order.
     */
    public void displayOrder() {

        int size = this.currentOrder.getNumberOfItems();

        if (size == 0) {
            stdErr.println("The current order is empty");
        } else {
            for (OrderItem  orderItem : this.currentOrder) {
                stdOut.println(orderItem.toString());
            }
            stdOut.println("Total: " +
                CURRENCY.format(this.currentOrder.getTotalCost()));
        }
    }

    /**
     * Modifies the current order: if the specified product is not already
     * part of the order, it is added; otherwise, the quantity of the
     * specified product is updated.
     */
    public void addModifyProduct()  throws IOException  {

        Product product = readProduct();
        int quantity = readQuantity();
        OrderItem orderItem = this.currentOrder.getItem(product);

        if (orderItem == null) {
            this.currentOrder.addItem(new OrderItem(product, quantity));
            stdOut.println("The product " + product.getCode()
             + " has been added");
        } else {
            orderItem.setQuantity(quantity);
            stdOut.println("The quantity of the product " +
                product.getCode() + " has been modified");
        }
    }

    /**
     * Removes a product from the current order.
     */
    public void removeProduct()  throws IOException  {

        Product product = readProduct();
        OrderItem orderItem = this.currentOrder.getItem(product);

        if (orderItem != null) {
            this.currentOrder.removeItem(orderItem);
            stdOut.println("The product " + product.getCode()
                 + " has been removed from the current order");
        } else {
            stdErr.println(
                "There are no products in the current order with that code");
        }
    }

    /**
     * Registers the sale of the current order.
     */
    public void saleOrder()  {

        if (this.currentOrder.getNumberOfItems() > 0) {
            this.sales.addOrder(this.currentOrder);
            this.currentOrder = new Order();
            stdOut.println("The sale of the order has been registered");
        } else {
            stdErr.println("The current order is empty");
        }
    }

    /**
     * Displays the orders that have been sold.
     */
    public void displayOrdersSold() {

        int numOrders = this.sales.getNumberOfOrders();

        if (numOrders != 0) {
            int orderNumber = 1;
            for (Order  order : this.sales) {

                stdOut.println("Order " + orderNumber++);

                for (OrderItem  orderItem : order) {
                    stdOut.println("   " + orderItem.toString());
                }
                stdOut.println("   Total: " +
                    CURRENCY.format(order.getTotalCost()));
            }
        } else {
            stdErr.println("There are no sales");
        }
    }
    

    /**
     * Displays the number of orders that contain a specified product
     *
     * @param product the <code>Product</code> object to be displayed.
     */
    public void displayNumberOfOrders(Product product) {

        /* PLACE YOUR CODE HERE */
        int numbers=0;
        for(Order order:sales) {
            if(order.getItem(product)!=null) {
                numbers++;
            }
        }
        System.out.println("Number of orders that contains the product "+product.getCode()+": "+numbers);
    }

    /**
     * Displays the total quantity of product that have has sold for each
     * product in the catalog.
     */
    public void displayTotalQuantityOfProducts() {

        /* PLACE YOUR CODE HERE */
        for(Product product:catalog) {
            int numbers=0;
            for(Order order:this.sales) {
                if(order.getItem(product)!=null) {
                    numbers+=order.getItem(product).getQuantity();
                }
            }
            System.out.println(product.getCode()+" "+numbers);
        }
    }

    /*
     * Prompts user for a product code and locates the associated
     * <code>Product</code> object.
     *
     * @return reference to the <code>Product</code> object with
     *         the specified code
     */
    private Product readProduct() throws IOException  {

        do  {            
            stdErr.print("Product code> ");
            stdErr.flush();
            
            Product product = this.catalog.getProduct(stdIn.readLine());
            //product=catalog.getProduct("A001");
            if (product != null) {

                return product;
            
            } else {    
                stdErr.println("There are no products with that code");
            }
        }  while (true);
    }

    /*
     * Prompts user for the product quantity and verifies the
     * user's response.
     *
     * @return a positive integer
     */
    private int readQuantity() throws IOException  {

        int quantity;

        do  {
            try  {

                stdErr.print("Quantity> ");
                stdErr.flush();
                quantity = Integer.parseInt(stdIn.readLine());
                if (quantity > 0) {

                    return quantity;

                } else  {
                    stdErr.println(
                        "Invalid input. Please enter a positive integer");
                }
            } catch (NumberFormatException  nfe)  {
                stdErr.println(nfe);
            }
        }  while (true);
    }
}

img


运行结果见上图,谢谢大家

  • 写回答

4条回答 默认 最新

  • 专家-皆知 2024-05-01 09:49
    关注

    参考免费微信小程序:皆我百晓生 作答,经由专家-皆知整理回复,如问题解决,望采纳;如仍有疑问,请评论回复。

    根据您提供的Java代码和描述,问题似乎在于readProduct()方法中尝试通过用户输入的产品代码查找产品时没有得到预期的结果,导致后续逻辑无法正确执行。这通常意味着stdIn.readLine()未能正确获取到用户的输入,或者用户输入的内容在查找产品时未匹配到任何产品。

    首先,确认程序运行时确实有用户交互,并且用户输入了有效的产品代码。其次,检查是否有其他部分的代码可能影响了输入流的状态,比如之前有未处理的异常或者未关闭的输入流。

    如果在实际运行环境中确实输入了有效的产品代码但仍然出现问题,这里有几个检查点和可能的解决方案:

    1. 确保输入不包含额外的空白:有时候用户可能会输入额外的空格,而代码中没有处理这种情况。可以通过trim()方法去除字符串两端的空白。

      Product product = this.catalog.getProduct(stdIn.readLine().trim());
      
    2. 调试输出:在读取输入前后添加打印语句,确认是否成功读取到了用户输入。

      System.out.println("Input: " + stdIn.readLine());
      
    3. 检查catalog.getProduct(String code)实现:确保这个方法能正确处理代码,包括大小写敏感性、错误处理等。如果这个方法内部有逻辑错误,即使输入正确也无法找到产品。

    4. 资源管理:确保程序中没有其他地方错误地操作了输入流,比如提前关闭了它。

    如果您已经检查了上述点,问题依然存在,您可以尝试一个简单的测试,直接在代码中调用readProduct()并传入硬编码的输入,看看是否能正常工作,这样可以排除是用户交互环节的问题。

    如果您想要我提供更具体的修改代码,请告诉我catalog.getProduct(String code)的具体实现,这样我可以给出更精确的建议。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月16日
  • 已采纳回答 5月8日
  • 创建了问题 5月1日

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?