首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >公司库存维护

公司库存维护
EN

Code Review用户
提问于 2021-09-07 01:43:29
回答 2查看 193关注 0票数 0

此代码允许用户输入、编辑、搜索、删除和打印公司所有产品的报告。有什么方法可以改进我的代码吗?

代码语言:javascript
复制
public class Admin
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String menuTask, exit; 

        Scanner sc = new Scanner(System.in); // Allows user input

        Products product = new Products(); // Creates the Products class

        System.out.println("BRIGHT FUTURE TECHNOLOGIES APPLICATION");
        System.out.println("***************************************");
        System.out.println("Enter (1) to launch menu or any other key to exit: ");
        exit = sc.next();
        if (exit.equals("1") == false) {
            product.ExitApplication(); // Exits the application 
        }

        do {
            menuTask = product.DisplayMenu(); // Displays the menu
            switch (menuTask) {
                case "1":
                    exit = product.CaptureProduct(); // Allows user to capture a product
                    break;
                case "2":
                    exit = product.SearchProduct(); // Allows user to search for a product
                    break;
                case "3":
                    exit = product.UpdateProduct(); // Allows user to update the product data
                    break;
                case "4":
                    exit = product.DeleteProduct(); // Allows user to delete a product
                    break;
                case "5":
                    exit = product.DisplayReport(); // Displays all the products and its data
                    break;
                case "6":
                    product.ExitApplication(); // Exits the application
                    break;
                default:
                    System.out.println("Thats not a valid key"); // Displays if the user enters an invalid key
            }
        } while (exit.equals("1")); // Loops executes at least once and will continue until the user wants to exit

    }

}

Products.java

代码语言:javascript
复制
public class Products {

    Scanner sc = new Scanner(System.in);

    ReportData data = new ReportData();

    public String SearchProduct() {
 
        int locationOfProduct;
        
        System.out.println("Please enter the product code to search: ");
        locationOfProduct = data.getLocationOfProduct(sc.next());
        System.out.println("***************************************");
        
        if (locationOfProduct != -1) {
            System.out.println("***************************************");
            System.out.println("PRODUCT SEARCH RESULTS");
            System.out.println("***************************************");
            data.toString(locationOfProduct);
        } else {
            
            System.out.println("The product cannot be located. Invalid product key");
            
        }
        
        System.out.println("Enter (1) to launch the menu or any other key to exit");
        
        return sc.next();
    }

    public void SaveProduct(String prodName, String prodCat, String prodCode, String prodSup, String prodWar, int prodLevel, double prodPrice) {
        data.setProductName(prodName);
        switch (prodCat) {
            case "1":
                data.setProductCat("Desktop Computer");
                break;
            case "2":
                data.setProductCat("Laptop");
                break;
            case "3":
                data.setProductCat("Tablet");
                break;
            case "4":
                data.setProductCat("Printer");
                break;
            case "5":
                data.setProductCat("Gaming Console");
                break;
        }
        data.setProductCode(prodCode);
        data.setProductSup(prodSup);
        data.setProductWar(prodWar);
        data.setProductLevel(prodLevel);
        data.setProductPrice(prodPrice);

    }

    public String UpdateProduct() {
        
        String update, war;
        int locationOfProduct;

        System.out.println("Please enter the product code to update: ");
        locationOfProduct = data.getLocationOfProduct(sc.next());
        System.out.println("***************************************");

        if (locationOfProduct != -1) {
            System.out.println("Update the warranty? (y)  Yes, (n) No ");
            update = sc.next();

            if (update.equals("y")) {

                System.out.printf("Indicate the new warrenty for %s. Enter (1) for 6 months or any other key for 2 years ", data.getProductName().get(locationOfProduct));
                if (sc.next().equals("1")) {
                    war = "6 Months";
                } else {
                    war = "2 years";
                }

                data.getProductWar().set(locationOfProduct, war);

                System.out.println("Update the product price? (y) Yes, (n) No ");
                update = sc.next();

                if (update.equals("y")) {

                    System.out.printf("Enter the new price for %s ", data.getProductName().get(locationOfProduct));
                    data.getProductPrice().set(locationOfProduct, sc.nextDouble());

                }

                System.out.println("Update the stock level? (y) Yes, (n) No ");
                update = sc.next();

                if (update.equals("y")) {
                    System.out.printf("Enter the new stock level for %s ", data.getProductName().get(locationOfProduct));
                    data.getProductLevel().set(locationOfProduct, sc.nextInt());
                }

            }

        } else {
            System.out.println("The product cannot be located. Invalid product key");
        }

        System.out.println("Product details have been saved successfully");
        System.out.println("Enter (1) to launch the menu or any other key to exit");
        return sc.next();

    }

    public String DeleteProduct() {
        int locationOfProduct;

        System.out.println("Please enter the product code to delete: ");
        System.out.println("***************************************");

        locationOfProduct = data.getLocationOfProduct(sc.next());

        if (locationOfProduct != -1) {
            data.getProductCat().remove(locationOfProduct);
            data.getProductCode().remove(locationOfProduct);
            data.getProductLevel().remove(locationOfProduct);
            data.getProductName().remove(locationOfProduct);
            data.getProductPrice().remove(locationOfProduct);
            data.getProductSup().remove(locationOfProduct);
            data.getProductWar().remove(locationOfProduct);
        } else {

            System.out.println("The product cannot be located. Invalid product key");

        }

        System.out.println("Enter (1) to launch the menu or any other key to exit");

        return sc.next();

    }

    public String DisplayMenu() {
        System.out.println("Please select one of the following menu items: ");
        System.out.println("(1) Capture a new product. \n" + "(2) Search for a product. \n" + "(3) Update a product. \n" + "(4) Delete a product. \n" + "(5) Print report. \n" + "(6) Exit application. ");
        return sc.next();
    }

    public String CaptureProduct() {
        String name, cat, sup, war;
        int level;
        double price;

        System.out.println("CAPTURE A NEW PRODUCT");
        System.out.println("*********************");

        System.out.println("Enter the product code: ");
        String code = sc.nextLine();

        System.out.println("Enter the product name: ");
        name = sc.nextLine();

        System.out.print("Select the product Category:\n" + "Desktop Computer - 1\n" + "Laptop - 2\n" + "Tablet - 3\n" + "Printer - 4\n" + "Gaming Console - 5 \nProduct Category >> ");
        cat = sc.nextLine();

        System.out.println("Indicate the product warrenty. Enter (1) for 6 months or any other key for 2 years");
        if (sc.next().equals("1")) {
            war = "6 Months";
        } else {
            war = "2 years";
        }

        System.out.printf("Enter the price for %s: ", name);
        price = sc.nextDouble();

        System.out.printf("Enter the stock level for %s: ", name);
        level = sc.nextInt();

        System.out.printf("Enter the supplier for %s: ", name);
        sup = sc.next();

        SaveProduct(name, cat, code, sup, war, level, price); // Saves the details of the product

        System.out.println("Product details have been saved successfully");
        System.out.println("Enter (1) to launch the menu or any other key to exit");
        return sc.next();
    }

    public void ExitApplication() {
        System.exit(0);
    }

    public String DisplayReport() {

        double totalPrice = 0, itemPrice;
        int count;

        System.out.println("PRODUCT REPORT");
        System.out.println("=====================================================");
        count = data.getProductCat().size();

        for (int i = 0; i < count; i++) {

            System.out.printf("PRODUCT %s\n", i + 1);
            System.out.println("-----------------------------------------------------");
            data.toString(i);
            System.out.println("-----------------------------------------------------");
            itemPrice = Integer.parseInt(data.getProductSup().get(i)) * data.getProductPrice().get(i);
            totalPrice = totalPrice + itemPrice ;
            
        }

        System.out.println("=====================================================");
        System.out.printf("TOTAL PRODUCT COUNT: \t%s\n", count);
        System.out.printf("TOTAL PRODUCT VALUE: \tR %s\n", totalPrice);
        System.out.printf("AVERAGE PRODUCT VALUE: \tR %s\n", totalPrice / count);

        System.out.println("=====================================================");
        System.out.println("Enter (1) to launch the menu or any other key to exit");
        return sc.next();
    }
}

ReportData.java

代码语言:javascript
复制
public class ReportData {

    ArrayList<String> productName = new ArrayList<>();
    ArrayList<String> productCat = new ArrayList<>();
    ArrayList<String> productCode = new ArrayList<>();
    ArrayList<String> productSup = new ArrayList<>();
    ArrayList<String> productWar = new ArrayList<>();
    ArrayList<Integer> productLevel = new ArrayList<>();
    ArrayList<Double> productPrice = new ArrayList<>();

    public void setProductName(String productName) {
        this.productName.add(productName);
    }

    public void setProductCat(String productCat) {
        this.productCat.add(productCat);
    }

    public void setProductCode(String productCode) {
        this.productCode.add(productCode);
    }

    public void setProductSup(String productSup) {
        this.productSup.add(productSup);
    }

    public void setProductWar(String productWar) {
        this.productWar.add(productWar);
    }

    public void setProductLevel(int productLevel) {
        this.productLevel.add(productLevel);
    }

    public void setProductPrice(double productPrice) {
        this.productPrice.add(productPrice);
    }

    public ArrayList<String> getProductName() {
        return productName;
    }

    public ArrayList<String> getProductCat() {
        return productCat;
    }

    public ArrayList<String> getProductCode() {
        return productCode;
    }

    public ArrayList<String> getProductSup() {
        return productSup;
    }

    public ArrayList<String> getProductWar() {
        return productWar;
    }

    public ArrayList<Integer> getProductLevel() {
        return productLevel;
    }

    public ArrayList<Double> getProductPrice() {
        return productPrice;
    }
    
    public int getLocationOfProduct(String prodCode) {
        
        return getProductCode().indexOf(prodCode); // Gets the location of the product in the array
       
    }
    
    public void toString(int locationOfProduct){
            System.out.printf("PRODUCT CODE:\t\t%s\n", getProductCode().get(locationOfProduct));
            System.out.printf("PRODUCT NAME:\t\t%s\n", getProductName().get(locationOfProduct));
            System.out.printf("PRODUCT WARRANTY:\t%s\n", getProductWar().get(locationOfProduct));
            System.out.printf("PRODUCT CATEGORY:\t%s\n", getProductCat().get(locationOfProduct));
            System.out.printf("PRODUCT PRICE:\t\tR%s\n", getProductPrice().get(locationOfProduct));
            System.out.printf("PRODUCT STOCK LEVEL:\t%s\n", getProductLevel().get(locationOfProduct));
            System.out.printf("PRODUCT SUPPLIER:\t%s\n", getProductSup().get(locationOfProduct));
    }

    

}
EN

回答 2

Code Review用户

回答已采纳

发布于 2021-09-08 20:12:35

命名约定

Java中的方法使用lowerCamelCase,如您的ReportData类所示。PascalCase ist仅用于类,因此应更改Products类中的方法名。

混淆名称

  • Products不是一个与产品相关的类,它处理用户输入(建议使用Menu )。
  • exit不是退出标志,它存储用户的菜单选择(建议使用input )。
  • setProduct*()没有设置任何内容,而是添加到列表中(建议使用addProduct*() )。
  • getProduct*()没有获得单个产品的价值,而是获得了所有产品的值列表(建议使用getAllProducts*() )。
  • ReportData.toString()打印由列表组成的产品。由于这会重载Object.toString(),这尤其有问题,因为使用该名称的方法将返回整个ReportData对象的字符串表示形式。建议用printAll()代替。

System.exit()

一般来说,这种方法应该很少使用。它会立即停止JVM,因此程序的其余部分可能无法优雅地清理和/或终止。这里不是问题,但如果您想将数据保存到文件中,这将是不可能的,除非您使用关闭钩子,引入不必要的复杂性。

而且,在这种情况下也没有必要。相反,exit可能被设置为"1"以外的任何东西,而do-while循环通常会终止,就好像用户在其他对话中请求它一样。

与布尔

的比较

exit.equals("1") == false读起来很笨重。equals()已经返回一个布尔值,它可以直接在if语句中使用,因此不需要将其与false进行比较。

代码语言:javascript
复制
if (exit.equals("1") == false)
// reads "if exit equals "1" is the same as false" --> weird and long

if (!exit.equals("1"))
// reads "if not exit equals "1"" --> better and shorter

ReportData类

似乎每个产品的属性(名称、代码、保证)都有一个数组列表,而产品就是一个索引中的一切。这太复杂了,最终会导致错误和不一致的数据。

相反,创建一个类Product,其中每个实例只有一个名称/代码/等等。然后,您可以操作一个更简单和一致的ArrayList<Product>。这个列表甚至可以是Product的静态成员。毕竟,OOP就是这样的:以一种简单/抽象的方式建模一个真实的产品,例如,一个Product类。

输入验证

在大多数情况下,程序进行简单的字符串比较,这似乎是足够稳定的。但是,在某个时候,用户必须输入一个数字(int/float),如果输入了无效的内容,这将导致InputMismatchExceptions。这些不是在这里抓到的。此外,数字应该根据常识和/或商业逻辑进行检查,输入负面奖励充其量是荒谬的,最坏的情况是昂贵的。

菜单实现

该程序在预选菜单中要求一个"1“来启动菜单或其他要退出的内容,然后在主菜单中提供一个单独的退出选项。这可能会让人感到困惑或烦人。如果删除了预菜单,main中的do-while循环可以在每次循环时打印主菜单,同时还提供退出程序的功能。这样,Products中的方法就不需要打印预菜单本身(这是不必要的重复代码,如果对话被更改,就会浪费时间),并且exit标志可以从大多数情况下删除。在现有的情况下,它仍然是需要的,因为do-while循环必须以某种方式结束。

数字菜单选择可以是常量,也可以是enum

在大菜单中,可以将每个对话移动到自己的类中,然后让这些类扩展抽象基类或实现接口。由于在您的情况下,让一个类处理整个接口仍然是合理的,所以我会保持简短和模糊。

Long println

代码语言:javascript
复制
System.out.println("(1) Capture a new product. \n" + "(2) Search for a product. \n" + "(3) Update a product. \n" + "(4) Delete a product. \n" + "(5) Print report. \n" + "(6) Exit application. ");

这一行读起来很奇怪,应该分成多个println()s。

票数 4
EN

Code Review用户

发布于 2021-09-09 16:35:04

另一项评论是:

基于文本的菜单不是创建用户界面的专业方法(感觉就像在上世纪80年代那样)。在您的学习曲线上,您可能还没有涉及图形用户界面(例如使用Swing或web服务器),所以这很好。但也许你想读一读如何提高你的专业技能。

基于文本的程序主要在由命令行参数控制时有其专业用途,这意味着从args获得main(String[] args)中的所有输入。然后,可以轻松地在批处理文件或shell脚本中实现自动化。交互式程序通常具有GUI或web前端。

谈到用户输入/输出:专业代码将数据处理与用户界面分开,有一组类负责处理(维护产品列表、搜索、计算成本等,而不是与用户交谈),以及不同的输入和输出类(输入产品描述、查询列表、接受订单等)。

设计良好的代码甚至允许通过使用不同的用户界面类和使用相同的处理类,轻松地在GUI和web前端之间切换。

票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/267746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档