Programming Java

An Introduction to Computer Science



Coding Project 7 Solution

Food Ordering System

Food Ordering System takes orders from a customer and then prints an order summary with the final cost.

The base program is provided for you. Your assignment is to fill-in the missing codes.

import java.util.Scanner;
import java.util.ArrayList;
class Main {
  public static double formatPrice(double d) {
    // returns double value d rounded to nearest hundredths
    return (Math.round(d * 100.0) / 100.0);
  }
  
  public static void main(String[] args) {
    // list of menu items for sale
    ArrayList<String> menuItem = new ArrayList<String>();
    menuItem.add("Bacon Cheeseburger");
    menuItem.add("Fish Fillet Burger");
    menuItem.add("Soda");
    menuItem.add("Milkshake");
    menuItem.add("Fries");
    menuItem.add("Veggie Side");
    
    // list of price per menu item
    ArrayList<Double> menuPrice = new ArrayList<Double>();
    menuPrice.add(3.25);
    menuPrice.add(3.85);
    menuPrice.add(1.99);
    menuPrice.add(2.99);
    menuPrice.add(2.15);
    menuPrice.add(2.15);
    
    // list of customer orders
    ArrayList<Integer> customerOrders = new ArrayList<Integer>();
    
    // ----- display menu --------------
    System.out.println();
    System.out.println("- - - Our Menu - - -");
    // ################################
    //
    // Add code to display menu items
    //
    // ################################
    System.out.println("7. ** Order Complete **");
    System.out.println();
    System.out.print("Select one (1-7): ");
    
    // ----- get customer orders ----------------
    Scanner keyboard = new Scanner(System.in);
    int user_input = 0;

    while (true) {
      try {
        user_input = keyboard.nextInt();
        if ((user_input >= 1) && (user_input <= 6)) {
          // ################################
          // Add code to
          // (1) print user selection
          // (2) add user selection to customerOrders ArrayList
          // (3) ask for anything else
          // ################################
        } else if (user_input == 7) {
          break;
        } else {
          System.out.print("Invalid entry. Enter 1-7: ");
        }
      } catch (Exception e) {
        System.out.print("Invalid entry. Enter 1-7: ");
        keyboard.next();
        continue;
      }
    }

    // ----- print order summary --------------
    double total_price = 0;
    System.out.println();
    System.out.println("- - - Order Summary - - -");

    // ################################
    //
    // Add code to print order summary
    //
    // ################################

    keyboard.close();
  }
}

Solution


Let’s look at how ArrayList is used to store the order information.

ArrayList<String> menuItem = new ArrayList<String>();
ArrayList<Double> menuPrice = new ArrayList<Double>();
ArrayList<Integer> customerOrders = new ArrayList<Integer>();

menuItem is prepopulated with the menu items. menuPrice is also prepopulated with the unit price per menu item. customerOrders is empty.

The menu item and the menu price are related by the index value. When a customer orders “soda”, the index value 2 is stored in customerOrders.

Step 1 – Display Menu

1. Bacon Cheeseburger   ($3.25)
2. Fish Fillet Burger   ($3.85)
3. Soda ($1.99)
4. Milkshake    ($2.99)
5. Fries    ($2.15)
6. Veggie Side  ($2.15)

We can use a For-Loop statement to print the menu item and menu price. Since the menu item and menu price come from different ArrayList, we need to use string concatenation.

For index i from 0 to last_index,
Print (i+1) + ". " + menuItem[i] + " (" + menuPrice[i] + ")"

Here is the code.

// ----- display menu --------------
System.out.println();
System.out.println("- - - Our Menu - - -");
for (int i=0; i<menuItem.size(); i++) {
  System.out.println((i+1) + ". " + menuItem.get(i) + "\t($" + menuPrice.get(i) + ")");
}
System.out.println("7. ** Order Complete **");
System.out.println();
System.out.print("Select one (1-7): ");

Line 4 – Index i iterates from 0 to last index, which is size()-1.
Line 5 – We access ArrayList elements by using get() method. The escape sequence “\t” is used to print <tab> between menu item and menu price.

Step 2 – Get Customer Orders

Select one (1-7): 1
You ordered Bacon Cheeseburger
Anything else? Select one (1-7): 3
You ordered Soda
Anything else? Select one (1-7): 5
You ordered Fries
Anything else? Select one (1-7): 7

A valid entry for menu selection is 1-6. This corresponds to the index values 0-5 in menuItem ArrayList. So, when user_input is 1, the menuItem at index (user_input - 1) is selected.

Print "Your ordered " + menuItem[user_input - 1]

Here is the code.

// ----- get customer orders ----------------
Scanner keyboard = new Scanner(System.in);
int user_input = 0;

while (true) {
  try {
    user_input = keyboard.nextInt();
    if ((user_input >= 1) && (user_input <= 6)) {
      System.out.println("You ordered " + menuItem.get(user_input - 1));
      customerOrders.add(user_input - 1);
      System.out.print("Anything else? Select one (1-7): ");
    } else if (user_input == 7) {
      break;
    } else {
      System.out.print("Invalid entry. Enter 1-7: ");
    }
  } catch (Exception e) {
    System.out.print("Invalid entry. Enter 1-7: ");
    keyboard.next();
    continue;
  }
}

Line 9 – We access ArrayList elements by using get() method.
Line 10 – We add a new element by using add() method.

Step 3 – Print Order Summary

Bacon Cheeseburger  ($3.25)
Soda    ($1.99)
Fries   ($2.15)

Total: $7.39

We can use a For-Loop statement to print customer orders. Recall that the customerOrders contains integer values that correspond to menu items and menu prices.

So, we want to print menu item and menu prices where index value comes from customerOrders.

For index i in customerOrders,
Print menuItem[i]

In other words,

For index i from 0 to last_index,
Print menuItem[ customerOrders[i] ]

Here is the code.

// ----- print order summary --------------
double total_price = 0;
System.out.println();
System.out.println("- - - Order Summary - - -");

if (!customerOrders.isEmpty()) {
  for (int i=0; i<customerOrders.size(); i++) {
    System.out.println(menuItem.get(customerOrders.get(i)) + "\t($" + menuPrice.get(customerOrders.get(i)) + ")");
    total_price += menuPrice.get(customerOrders.get(i));
  }
  System.out.println();
  System.out.println("Total: $" + formatPrice(total_price));
} else {
  System.out.println("You didn't order anything.");
}

Line 7 – Index i iterates from 0 to last index, which is size()-1.
Line 8 – We access ArrayList elements by using get() method. First, we get customerOrders element which is an integer. Then use this value to get menuItem and menuPrice elements.
Line 9 – In each iteration, we aggregate menuPrice.
Line 12 – Invokes the static method formatPrice() to round the total price.

Here is the complete code.

import java.util.Scanner;
import java.util.ArrayList;
class Main {
  public static double formatPrice(double d) {
    // returns double value d rounded to nearest hundredths
    return (Math.round(d * 100.0) / 100.0);
  }
  
  public static void main(String[] args) {
    // list of menu items for sale
    ArrayList<String> menuItem = new ArrayList<String>();
    menuItem.add("Bacon Cheeseburger");
    menuItem.add("Fish Fillet Burger");
    menuItem.add("Soda");
    menuItem.add("Milkshake");
    menuItem.add("Fries");
    menuItem.add("Veggie Side");
    
    // list of price per menu item
    ArrayList<Double> menuPrice = new ArrayList<Double>();
    menuPrice.add(3.25);
    menuPrice.add(3.85);
    menuPrice.add(1.99);
    menuPrice.add(2.99);
    menuPrice.add(2.15);
    menuPrice.add(2.15);
    
    // list of customer orders
    ArrayList<Integer> customerOrders = new ArrayList<Integer>();
    
    // ----- display menu --------------
    System.out.println();
    System.out.println("- - - Our Menu - - -");
    for (int i=0; i<menuItem.size(); i++) {
      System.out.println((i+1) + ". " + menuItem.get(i) + "\t($" + menuPrice.get(i) + ")");
    }
    System.out.println("7. ** Order Complete **");
    System.out.println();
    System.out.print("Select one (1-7): ");
    
    // ----- get customer orders ----------------
    Scanner keyboard = new Scanner(System.in);
    int user_input = 0;

    while (true) {
      try {
        user_input = keyboard.nextInt();
        if ((user_input >= 1) && (user_input <= 6)) {
          System.out.println("You ordered " + menuItem.get(user_input - 1));
          customerOrders.add(user_input - 1);
          System.out.print("Anything else? Select one (1-7): ");
        } else if (user_input == 7) {
          break;
        } else {
          System.out.print("Invalid entry. Enter 1-7: ");
        }
      } catch (Exception e) {
        System.out.print("Invalid entry. Enter 1-7: ");
        keyboard.next();
        continue;
      }
    }

    // ----- print order summary --------------
    double total_price = 0;
    System.out.println();
    System.out.println("- - - Order Summary - - -");

    if (!customerOrders.isEmpty()) {
      for (int i=0; i<customerOrders.size(); i++) {
        System.out.println(menuItem.get(customerOrders.get(i)) + "\t($" + menuPrice.get(customerOrders.get(i)) + ")");
        total_price += menuPrice.get(customerOrders.get(i));
      }
      System.out.println();
      System.out.println("Total: $" + formatPrice(total_price));
    } else {
      System.out.println("You didn't order anything.");
    }

    keyboard.close();
  }
}

By:


Design a site like this with WordPress.com
Get started