【问题标题】:Arraylist based program基于数组列表的程序
【发布时间】:2015-10-14 11:45:47
【问题描述】:
package arrayprogram;
public class ArrayProgram {

public static void main(String[] args) {
            Store watchStore = new Store();
            String wcome = Menu.welcome();
            int opt = Integer.parseInt(wcome);


            switch(opt){
                case 1:
                    watchStore.addWatches();
                    for(;;){
                        String option = InputHelper.getInput("you want to add another watch (Y/N) ?: "); 
                        if ("y".equalsIgnoreCase(option)) {
                            watchStore.addWatches();
                        }else{
                            break;
                        }
                    }    
                case 2:
                    String back = Menu.welcome();
                    int option = Integer.parseInt(back);
                    if(option == 2)
                        watchStore.viewWatch();
                    break;

                case 3:
                    Watch found;
                    found = watchStore.findWatchBySerialNumber(333);
                    System.out.println(found);
                    break;
            }
}

}

上面的代码包含我的主要方法,其余的类将遵循

商店类

package arrayprogram;
import java.util.ArrayList;

public class Store {

    ArrayList<Watch> watchStore = new ArrayList<>();
    Watch wt = new Watch();

    public void addWatches(){

            String name = InputHelper.getInput("Enter name: ");
            String serial = InputHelper.getInput("Enter serial: ");
            String desc = InputHelper.getInput("Enter description: ");
            String color = InputHelper.getInput("Enter Color: ");
            String price = InputHelper.getInput("Enter price: ");
            String weight = InputHelper.getInput("Enter weight: ");

            double pr = Double.parseDouble(price);
            double wg = Double.parseDouble(weight);
            long sr = Long.parseLong(serial);

            wt.addWatch(name, sr, desc, color, pr, wg); // this is a method inside Watch class that is responsible for adding watch information
            watchStore.add(wt);
    }

    public void viewWatch(){
        for (int i = 0; i<watchStore.size(); i++){
            System.out.println(watchStore.get(i));
        }

    }

    public Watch findWatchBySerialNumber(long serial) {
        for(Watch w : watchStore) {
            if(w.getSerial() == serial) {
                System.out.print("found");
                return w;
            }
        }
        System.out.print("not found");
        return null;
    }

    public void deleteWatch(){
        //the code will follow
    }

    public void averageWatch(){
        //the code will follow
    }

}

InputHelper 类

package arrayprogram;
import java.util.Scanner;

public class InputHelper {
    public static String getInput(String prompt){
        Scanner input = new Scanner(System.in);
        System.out.print(prompt);
        System.out.flush();

        try{
            return input.nextLine();
        }
        catch (Exception e){
            return "Invalid entry";
        }
    }
}

菜单类

package arrayprogram;
public class Menu {

    public static String welcome(){
        return InputHelper.getInput("Welcome our watch store\n"
                    + "1. Add new watch\n"
                    + "2. Delete Watch by serial number\n"
                    + "3. View all watches information\n"
                    + "4. Search watch by serial number\n"
                    + "5. View average weight of available watch\n"
                    + "\n"
                    + "Choice: "
            );


    }


}

最后,上面的程序运行得很好。但上面的程序就是这样。

  1. 我无法在数组列表中搜索具有匹配序列号的特定监视对象。

  2. 欢迎对上述程序提出任何建议。任何改进。

【问题讨论】:

  • 对于第 2 部分 codereview 将是一个更好的地方
  • 如果您能告诉我们您的意见是什么,那就太好了。
  • 你能不能这样试试:ArrayList watchStore = new ArrayList(); ?此外,我们想知道您的代码是如何失败的。输入是什么,您期望什么以及意外的输出是什么?
  • 如果我没有被那一堆代码完全弄错,那么您总是将相同的Watch 实例添加到您的ArrayList。你永远不会重新初始化你的 wt 变量,它奇怪地是一个类变量。您可以尝试在您的列表中找到您最新的手表,应该可以找到。
  • 我不确定是否是这种情况,但在您的getInput() 中,搜索手表是#4,但在您的switch 中是第 3 位。确保您输入的是#3

标签: java class arraylist


【解决方案1】:

您应该使用 Map 而不是 ArrayList。还有:

  • 使用接口:

    List watchStore = new ArrayList(); 代替 : ArrayList watchStore = new ArrayList();

  • 在 Store 类中,您的 List 和 Watch 应该是私有的。

  • 代码中不应包含文本,应使用资源(例如属性文件)来提供文本。

  • 你不应该在代码中直接包含“y”或“\n”之类的东西,它们应该被声明为常量。

  • “serial_no”不是正确的 Java 命名,应该是“serialNo”。

  • 对于选项,您应该使用枚举而不是整数值。

  • addWatch(...) 的意义何在,为什么不使用构造函数?为什么你一遍又一遍地修改同一个 Watch 对象而不是每次都创建一个新对象?您确定您的程序有效吗?

  • 你应该有 Javadoc 和 cmets。

...(不止于此,使用 CheckStyle、PMD 和 Findbugs 来检查您的代码并编写单元测试)

【讨论】:

    【解决方案2】:

    我在您的代码中发现了两个主要问题:

    第一个,就我所见,您永远不会在代码中重复您的菜单调用。你可以这样做(注意我唯一改变的是菜单的递归调用)。里面可能还有其他问题。 封装数组程序;

    public class ArrayProgram {
        static Store watchStore = new Store();
    
        public static void main(String[] args) {
            print();
        }
    
        private static void print() {
    
            String wcome = Menu.welcome();
            int opt = Integer.parseInt(wcome);
            switch (opt) {
            case 1:
                watchStore.addWatches();
                for (;;) {
                    String option = InputHelper.getInput("you want to add another watch (Y/N) ?: ");
                    if ("y".equalsIgnoreCase(option)) {
                        watchStore.addWatches();
                    } else {
                        print();
                        break;
                    }
                }
            case 2:
                String back = Menu.welcome();
                int option = Integer.parseInt(back);
                if (option == 2)
                    watchStore.viewWatch();
                print();
                break;
    
            case 3:
                Watch found;
                found = watchStore.findWatchBySerialNumber(333);
                System.out.println(found);
                print();
                break;
            }
        }
    }
    

    第二个主要缺陷是您总是在处理Watch 的同一个实例。如果您要添加序列号为1 的Watch 并添加另一个序列号为2 的Watch,那么您的ArrayList 基本上包含两次相同的对象,其中填充了您的第二个手表的信息。 此更改应该是一个小更改,只需将您的类变量 wt 移动到方法 addWatches 内即可。

    public void addWatches(){
        String name = InputHelper.getInput("Enter name: ");
        String serial = InputHelper.getInput("Enter serial: ");
        String desc = InputHelper.getInput("Enter description: ");
        String color = InputHelper.getInput("Enter Color: ");
        String price = InputHelper.getInput("Enter price: ");
        String weight = InputHelper.getInput("Enter weight: ");
    
        double pr = Double.parseDouble(price);
        double wg = Double.parseDouble(weight);
        long sr = Long.parseLong(serial);
    
        Watch wt = new Watch();
        wt.addWatch(name, sr, desc, color, pr, wg); // this is a method inside Watch class that is responsible for adding watch information
        watchStore.add(wt);
    }
    

    请注意,您的项目仍有很大的改进空间,并且它还有一些缺陷(与您的菜单不匹配开关条件,它只搜索序列号333,....)

    【讨论】:

    • 每次我让用户添加手表时如何添加每个实例
    【解决方案3】:

    您永远不会正确显示完整的项目列表。我可以看到它卡在选项 1 上,因为无法退出。我在这里更正了。请检查下面的代码,它删除了 for 循环的使用。它包括一个永远运行但每次都显示正确目录的 while 循环。

    public class ArrayProgram {
    
    public static void main(String[] args) 
    {
                Store watchStore = new Store();
    
                while( true )
                {
                    String wcome = Menu.welcome();
                    int opt = Integer.parseInt(wcome);
    
                    switch(opt){
                        case 1:
                            watchStore.addWatches();
                            break;
                        case 2:
                            String back = Menu.welcome();
                            int option = Integer.parseInt(back);
                            if(option == 2)
                                watchStore.viewWatch();
                            break;
    
                        case 3:
                            Watch found;
                            found = watchStore.findWatchBySerialNumber(17);
                            System.out.println(found);
                            break;
                        case 4: System.exit(0);
                    }
                }
    }
    }
    

    然后是您一直使用单个 watch 实例的部分。这只会增加麻烦。您应该每次都实例化手表类。你必须将它添加到数组列表中。以下是更正后的代码。请注意,手表商店实例每次都会创建。

    import java.util.ArrayList;
    
    public class Store 
    {
        ArrayList<Watch> watchStore = new ArrayList<>();
    
        public void addWatches()
        {
                Watch wt = new Watch();
                String name = InputHelper.getInput("Enter name: ");
                String serial = InputHelper.getInput("Enter serial: ");
                String desc = InputHelper.getInput("Enter description: ");
                String color = InputHelper.getInput("Enter Color: ");
                String price = InputHelper.getInput("Enter price: ");
                String weight = InputHelper.getInput("Enter weight: ");
    
                double pr = Double.parseDouble(price);
                double wg = Double.parseDouble(weight);
                long sr = Long.parseLong(serial);
    
                wt.addWatch(name, sr, desc, color, pr, wg); // this is a method inside Watch class that is responsible for adding watch information
                watchStore.add(wt);
        }
    
        public void viewWatch(){
            for (int i = 0; i<watchStore.size(); i++){
                System.out.println(watchStore.get(i));
            }
    
        }
    
        public Watch findWatchBySerialNumber(long serial) {
            for(Watch w : watchStore) {
                if(w.getSerial() == serial) {
                    System.out.print("found");
                    return w;
                }
            }
            System.out.print("not found");
            return null;
        }
    
        public void deleteWatch(){
            //the code will follow
        }
    
        public void averageWatch(){
            //the code will follow
        }
    
    }
    

    显示目录的主菜单搞砸了。代码中有 3 个 case 语句,UI 中有 5 个选项。这里没有1-1对应。

    此外,您必须在该目录中添加一个允许用户退出应用程序的新条目。

    关于代码性能审查,正如有人指出的那样,请访问 codereview.stackexchange.com。

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 2011-11-20
      • 2023-03-11
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      相关资源
      最近更新 更多