CS 1401 Assignment #9

Date Assigned: Monday, October 29, or Tuesday, October 30, 2007.

Due Date: Monday, November 5, or Tuesday, November 6, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to use arrays.

Programming assignment: One of UTEP students is working part-time as a waiter. Let us write a program for helping this student. This program must allow the student to enter the names of different menu items (foods or drinks), the ordered amount of each item (e.g., 3 cokes), and the unit price of each item. Assume that the number of different items does not exceed 100. Your program must store all this data in several arrays, and then print the detailed menu including the information about all the items plus the following information:


Solution:

public static void main(String[] args){
  final double TAX_RATE = 8.25;
  Scanner reader = new Scanner(System.in);
  String[] name = new String[100];
  int[] amount = new int[100];
  double[] price = new double[100];

  Boolean done = false;
  char yesNo;
  int i = 0;
  int numberOfItems = 0;

  //reading the data
  while (!done)
  {
    System.out.print("Do you want to input more data? Y/N");
    yesNo = reader.next().charAt(0);
    if (yesNo == 'Y')
    {
      System.out.println("Enter the name of the item");
      name[i] = reader.next();
      System.out.println("Enter the amount of this item");
      amount[i] = reader.nextInt();
      System.out.println("Enter the price of this item");
      price[i] = reader.nextDouble();
      i++;
      numberOfItems++;
    }
    else
      {done = true;}
  }

  //computing the total
  double total = 0.0;
  for (i = 0; i < numberOfItems; i++)
    {total += amount[i] * price[i];}
  double tax = total * (1.0 + TAX_RATE/1000);
  double totalWithTax = total + tax;
  double tip = totalWithTax * 0.15;
  double totalWithTip = totalWithTax + tip;

  //printing the detailed bill
  System.out.println("Restaurant bill:");
  System.out.prinf("%15s%2s%6s%7s", "item name", "#", "unit", "price");

  for (i = 0; i < numberOfItems; i++)
  {  System.out.printf("%15s%2d%6.2f%7.2f%n", name[i], amount[i],
        price[i], amount[i] * price[i]);}
  System.out.println("");

  System.out.prinf("%15s%2s%6s%7.2f", "Total          ", "", "", total);
  System.out.prinf("%15s%2s%6s%7.2f", "Tax            ", "", "", tax);
  System.out.prinf("%15s%2s%6s%7.2f", "Total with tax ", "", "", totalWithTax);
  System.out.prinf("%15s%2s%6s%7.2f", "Tip            ", "", "", tip);
  System.out.prinf("%15s%2s%6s%7.2f", "Total with tip ", "", "", tip);
}

Homework assignment: on a separate sheet of paper, solve Ex. 2 and 4 at the end of Chapter 9 (on pp. 598 and 599). For extra credit: Ex. 10 on p. 602.

Deliverables: as instructed by your TA.