Programming Java

An Introduction to Computer Science



Coding Project 7

Use the skills you learned in chapter 25 ArrayList to complete this project.

Food Ordering System

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

Output 1 – The system displays the menu items like this.

- - - Our 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)
7. ** Order Complete **

Select one (1-7): 

Output 2 – The system handles valid entries (1-7) like this. When a customer enters 7, the system stops taking 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

Output 3 – The system handles invalid entries like this.

Select one (1-7): 0
Invalid entry. Enter 1-7: 8
Invalid entry. Enter 1-7: one
Invalid entry. Enter 1-7: 

Output 4 – Once an order is complete, the system prints an order summary like this.

- - - Order Summary - - -
Bacon Cheeseburger  ($3.25)
Soda    ($1.99)
Fries   ($2.15)

Total: $7.39

Output 5 – When a customer ends an order without ordering anything, this is output.

- - - Order Summary - - -
You didn't order anything.

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

Line 36 – Reference <output 1> for the expected outcome.
Lines 52-55 – Reference <output 2> for the expected outcome.
Line 76 – Reference <output 4> and <output 5> for the expected outcomes.

By:


Leave a comment

Design a site like this with WordPress.com
Get started