-1

I am currently doing my very first coding project in Java. It is an Ordering System that should enable the customer the following things. First the customer should be able to navigate through the application, select his/her favourite restaurant within Copenhagen, and choose meals from their individual menu lists. Second, each order should be customizable with relation to the order quantity and delivery location. Lastly, the system should assign the closest available biker to pick up the order from the restaurant and deliver the meal to the indicated destination at the defined time.

After much help of @Theresa I tested the programme again, but I can't get the last mile to run. The part where the system should assign the closest available biker to pick up the order from the restaurant does not work. I still feel kind of hopeless, because I haven tried been everything I could

Any other comments, hints or help would be highly appreciated. Thanks a lot in advance.

This is the updated code:[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
    }
maximal16
  • 1
  • 1

1 Answers1

0

Why have you made your system have everything hard coded? A better option would be to have the menus/restaraunts loaded from config files or Database. What happens if a restaraunt leaves or joins? What if they change their menu? You would have to redo the program each time...

Do it all Data driven and you can change it easily.

Look at the entities in your system, where might it change - what if instead of bikers you had people with cars that can take more than one delivery at a time etc.

My suggestion is to work out how to make it data driven then you can split the functionality MVC and then maybe the issue will leap out at you.

Here is a small suggestion

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

So this is where you can make it neater I will put the finished code up when i can finish it

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

    }
Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
  • It would like to do what you are saying, but we have not worked with Databases at all. Hence, I guess I only know how to hard code... :/ The system itself would be more effective and should be adjustable, but the stuff we have learned won't allow me to do it any different. Would you have any other suggestions how I could deal with my hard code? @TheresaForster – maximal16 Apr 26 '16 at 07:41
  • Then i suggest you hard code in one place and build the rest around it, I am guessing you are doing this as a college project then – Theresa Forster Apr 26 '16 at 07:42
  • Yes exactly. To be honest, I am infantile when it comes to coding. I have IT business background and selected a programming minor to really learn a high-level language. I have invested 90hours the past 3 weeks in making this project working. Thanks so much! I will implement this now and try see if I can build around it. I hope it will stop making problems. – maximal16 Apr 26 '16 at 08:29
  • Try understanding how i have cleaned up the initialisation (its not set to run yet but you should get the idea) https://www.dropbox.com/s/s45vnb6pbg3mk2o/cleaner-deliveryBike.zip?dl=0 – Theresa Forster Apr 26 '16 at 08:29
  • Wow, this such a great help. I ran it a couple of time and I detected a problem. `Files.write(Paths.get("Customers.txt"), customer.toString().getBytes(), StandardOpenOption.APPEND);` does not add any tab space, so the last name and the new username are not detached from eachother. Hence, I can't login as a second user because the `FileReader` can't find the password. I searched online, but I can't find a solution appropriate to this. – maximal16 Apr 26 '16 at 10:09
  • have you tried something like Files.write(Paths.get("customers.txt"),customer.toString()+"\t".getBytes(), That will add a tab onto the end, but why are you doing it this way, actually make a toString() in customer that creates the file output you want. and maybe use Buffered Writer so you can output line by line. http://stackoverflow.com/questions/14503595/write-file-using-bufferedwriter-in-java – Theresa Forster Apr 26 '16 at 10:20