基于Arraylist的计划

时间:2015-10-14 11:45:48

标签: java class arraylist

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. 我无法在arraylist中搜索具有匹配序列号的特定监视对象。

  2.   
  3. 欢迎任何有关上述计划的建议。任何进步。

  4.   

3 个答案:

答案 0 :(得分:0)

您应该使用Map而不是ArrayList。另外:

  • 使用接口:

    列表watchStore = new ArrayList&lt;&gt;(); 代替 : ArrayList watchStore = new ArrayList&lt;&gt;();

  • 在商店课程中,您的列表和您的观看应该是私密的。

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

  • 你不应该有像&#34; y&#34;或&#34; \ n&#34;直接在代码中,它们应该被声明为常量。

  • &#34; serial_no&#34;不是正确的Java命名,它应该是&#34; serialNo&#34;。

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

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

  • 你应该有Javadoc和评论。

...(不要停在这里,使用CheckStyle,PMD和Findbugs检查代码并编写单元测试)

答案 1 :(得分:0)

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

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

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,....)< / p>

答案 2 :(得分:0)

您永远不会正确显示完整的商品清单。我可以看到它被卡在选项1,因为没有办法退出。我在这里纠正过了。请检查下面的代码,删除for循环的使用。它包含一个永久运行的while循环,但每次都显示正确的catalouge。

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);
                }
            }
}
}

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

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。