Java中的膳食订购系统的收集错误

时间:2016-04-26 06:33:13

标签: java arrays arraylist hashmap

我目前正在用Java开展我的第一个编码项目。这是一个订购系统,应该使客户能够做到以下几点。首先,客户应该能够浏览应用程序,在哥本哈根选择他/她最喜欢的餐厅,并从他们各自的菜单列表中选择餐点。其次,每个订单都应该根据订单数量和交货地点进行定制。最后,系统应该指定最近的可用骑车人从餐馆领取订单并在规定的时间将餐食送到指定的目的地。

经过@Theresa的大力帮助,我再次测试了这个程序,但是我无法跑完最后一英里。系统应该指定最近的可用骑车人从餐厅领取订单的部分不起作用。我仍然感到有些无望,因为我已经尝试过所有可能的事情

任何其他评论,提示或帮助将受到高度赞赏。非常感谢。

这是更新的代码:[https://www.dropbox.com/s/awvohw7vholsdrd/Cleaner_DeliveryBike.zip?dl=0][1]

private static void setDelivery() throws Exception {

        double totalTime = 0;

        String delivery = "Now that you have chosen what you want to order, we would like to introduce to you our MadCyklers."
                + "All day we have 5 biker that are touring arround the city to deliver your order right to you. We only need"
                + "to know one thing from you: where in Copenhagen are you right now?";

        try {
            printString(navigationFrameTop);
            printString(delivery);
            printString(navigationFrameBottom);
            printString(categoryFrame);
            System.out.println("                         *COPENHAGEN*                  ");
            System.out.println("");
            System.out.println("   LONG> 12.52   12.54   12.56   12.58   12.60   12.62");
            System.out.println("           .       .       .       .       .       .  ");
            System.out.println("   55.70   + . . . + . . . + . . . + . . . + . . . +  ");
            System.out.println("           .       .       .       .       .       .  ");
            System.out.println("   55.69   + . . . + . . . + . . . + . . . + . . . +  ");
            System.out.println("           .       .       .       .       .       .  ");
            System.out.println("   55.68   + . . . + . . . + . . . + . . . + . . . +  ");
            System.out.println("           .       .       .       .       .       .  ");
            System.out.println("   55.67   + . . . + . . . + . . . + . . . + . . . +  ");
            System.out.println("           .       .       .       .       .       .  ");
            System.out.println("   55.66   + . . . + . . . + . . . + . . . + . . . +  ");
            System.out.println("     ^     .       .       .       .       .       .  ");
            System.out.println("    LAT");
            printString(categoryFrame);
            printString(inputFrame);
            customer.setLongitude(0);
            printString(inputFrame);
            customer.setLatitude(0);
            printString(inputFrame);
            double min = 9999999;
            double distance;
            // checking if Indications are correct.
            for(Biker biker : BIKERS) {
                biker.setLatitude(0);
                biker.setLongitude(0);
                distance = biker.distanceTo(customer) + (restaurants.get(restaurantPick)).distanceTo(customer);
                if(distance < min) {
                    min = min + distance;
                    activeBiker = biker.iD;  
                }
            }

            totalTime = (min / KILOMETRES_PER_HOUR) * 3600000
                    + restaurants.get(restaurantPick).getPreparationTime() * 60000;
            deliveryTime = (long) (currentTime + totalTime);
            String deliveryConfirmation = " Your order will be delivered to your location at: " + deliveryTime
                    + " from our MadCykler " + BIKERS.get(activeBiker).getTipFactor() + ".";

            printString(deliveryConfirmation);
            payment();
        } catch (Exception e) {
            printString(errorFrame);
            System.out.println("Error! Reloading Main Menu...");
            printString(errorFrame);
            main(null);
        }
    }

    private static void payment() throws Exception {

        BigDecimal tip = BigDecimal.ZERO;
        BigDecimal tipPrice = BigDecimal.ZERO;
        int payingOption = 0;
        String payment = "There is only one more thing left to do for you. Just initialize the payment and we will send"
                + "you the order confirmation. Then you just have to wait for" + BIKERS.get(activeBiker).getTipFactor() 
                + "to arrive with your order."; 
        String totalCost = "The total costs are " + getTotal() + "DKK. What % tip would you like to give to the Cykler? ";

        try {
            printString(navigationFrameTop);
            printString(payment);
            printString(navigationFrameBottom);
            printString(categoryFrame);
            printString(totalCost);
            printString(categoryFrame);
            printString(inputFrame);

            tip = BIKERS.get(activeBiker).getTipFactor();

            printString(inputFrame);

            tipPrice = tip.divide(tip, 100).multiply(getTotal());
            deliveryCost = deliveryCost.add(getTotal()).add(tipPrice);
            String totalDelivery = "The overall delivery cost are " + deliveryCost + "DKK. Press [1] if you would like to pay,"
                    + "[2] to pick a new restaurant, or [3] to return to the Main Menu.";
            String totalDeliveryError = "Error! Press [1] if you would like to pay,[2] to pick a new restaurant, or [3] to "
                    + "return to the Main Menu.";

            printString(categoryFrame);
            printString(totalDelivery);
            printString(categoryFrame);
            do {
                Scanner input = new Scanner(System.in);
                printString(inputFrame);
                payingOption = input.nextInt();
                printString(inputFrame);
                switch (payingOption) {
                case 1:
                    wrapOrder();
                    break;
                case 2:
                    choiceMeal = null;
                    quantityMeal = null;
                    pickingRestaurant();
                    break;
                case 3:
                    choiceMeal = null;
                    quantityMeal = null;
                    main(null);
                    break;
                default:
                    printString(errorFrame);
                    printString(totalDeliveryError);
                    printString(errorFrame);
                    break;
                }
            } while (payingOption != 1 && payingOption != 2 && payingOption != 3);
        } catch (Exception e) {
            printString(errorFrame);
            System.out.println("Error! Reloading Main Menu...");
            printString(errorFrame);
            main(null);
        }
    }

    private static void wrapOrder() {

        String s = "";
        UUID customerId;
        BigDecimal currentBalance = BigDecimal.ZERO;
        String customerDetails = customer.toString();
        long arrival = deliveryTime;
        BigDecimal bill = deliveryCost;

        currentBalance = creditAccounts.get(customerAccountLoggedIn).getBalance();
        if (currentBalance.compareTo(deliveryCost) > 0) {
            printString(categoryFrame);
            System.out.println(" Your payment was successful.");
            printString(categoryFrame);
            creditAccounts.get(customerAccountLoggedIn).withdraw(deliveryCost);
            for (int i = confirmations.size(); i < confirmations.size() + 1; i++) {
                customerId = confirmations.get(i).getOrderId();
                Confirmation confirmation = new Confirmation(customerId, customerDetails, arrival, bill);
                confirmations.add(confirmation);
                printString(navigationFrameTop);
                confirmation.toString();
                printString(navigationFrameBottom);
            }
        }
        else {
            printString(errorFrame);
            creditAccounts.get(customerAccountLoggedIn).withdraw(deliveryCost);
            printString(errorFrame);
            reCharging();
        }
        // ending the program and clearing all content during the order
    }

1 个答案:

答案 0 :(得分:0)

为什么你让你的系统硬编码? 更好的选择是从配置文件或数据库加载菜单/ restaraunts。如果餐厅离开或加入会发生什么?如果他们改变菜单怎么办?你每次都要重做这个程序......

全部采用数据驱动,您可以轻松更改。

查看系统中的实体,它们可能会发生什么变化 - 如果不是骑自行车的人,那么你的汽车人一次只能接受多次交付等等。

我的建议是找出如何使数据驱动然后你可以拆分功能MVC,然后问题可能会跳出来。

这是一个小建议

try {
    System.out.println("=====================================================");
    System.out.println("This is the menu of " + restaurants.get(restaurantPick).getName());
    System.out.println("=====================================================");
    printMenu(InitialiseSystem.MENUS.get(restaurantPick));
    orderingMeals();
} catch (Exception e) {
    System.out.println("Sorry, there is error. You will return to the Main Menu");
    main(null);
}

所以这是你可以整理的地方 当我完成它时,我会把完成的代码放完

  private void initMenus() {
         Map<String, Meal> tempMenu = new HashMap<String, Meal>();
        tempMenu.put("Norwegian Lobster", new Meal("Norwegian Lobster", new BigDecimal(145.00)));
        tempMenu.put("Pork Neck Jutland", new Meal("Pork Neck Jutland", new BigDecimal(195.00)));
        tempMenu.put("Tenderloin", new Meal("Tenderloin", new BigDecimal(249.50)));
        MENUS.add(tempMenu);
         tempMenu = new HashMap<String, Meal>();
        tempMenu.put("Kaiseki Sushi", new Meal("Kaiseki Sushi", new BigDecimal(300.00)));
        tempMenu.put("Tempura Tokyo", new Meal("Tempura Tokyo", new BigDecimal(140.00)));
        tempMenu.put("Tonkotsu Ramen", new Meal("Tonkotsu Ramen", new BigDecimal(140.00)));
        MENUS.add(tempMenu);
        tempMenu = new HashMap<String, Meal>();
        tempMenu.put("Los Burritos", new Meal("Los Burritos", new BigDecimal(105.00)));
        tempMenu.put("Beef Carnitas", new Meal("Beef Carnitas", new BigDecimal(145.00)));
        tempMenu.put("Chicken Enchiladas", new Meal("Chicken Enchiladas", new BigDecimal(119.00)));
        MENUS.add(tempMenu);
         tempMenu = new HashMap<String, Meal>();
        tempMenu.put("Hamburger", new Meal("Hamburger", new BigDecimal(129.00)));
        tempMenu.put("Cheeseburger", new Meal("Cheeseburger", new BigDecimal(129.00)));
        tempMenu.put("Portobello Burger", new Meal("Portobello Burger", new BigDecimal(149.00)));
        MENUS.add(tempMenu);
        tempMenu = new HashMap<String, Meal>();
        tempMenu.put("Fried Rice", new Meal("Fried Rice", new BigDecimal(68.00)));
        tempMenu.put("Noodle Salad", new Meal("Noodle Salad", new BigDecimal(62.00)));
        tempMenu.put("Meatball Soup", new Meal("Meatball Soup", new BigDecimal(96.00)));
        MENUS.add(tempMenu);

    }