CS 1401 Assignment #10

Date Assigned: Monday, November 5, or Tuesday, November 6, 2007.

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

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

Programming assignment: The task is the same as in the previous Assignment 9: Write a program for helping waiters. This program must allow the user to enter the names of different menu items (foods or drinks), the amount of each item, and the price of each item. Assume that the number of different items does not exceed 100. Your program must store all this data, and then print the detailed menu including the information about all the items plus the following information:

Main difference: In the previous assignment 9, following our advise, you stored the names in one array (array of strings), the amounts in another array (array of integers), and the prices in yet another array (array of real numbers).

For this new assignment, define a class FoodItem whose objects contain three fields:

Instead of using three arrays, use a single array of food items (i.e., of objects of type FoodItem).

Deliverables: as instructed by your TA.


Solution:

import java.util.*;

public class FoodItem{
  private String name;
  private int amount;
  private double price;

  public FoodItem(String aname, int aamount, double aprice){
    name = aname; amount = aamount; price = aprice;}

  public String getName(){return name;}
  public int getAmount(){return amount;}
  public double getPrice(){return price;}

  public void setName(String newName){name = newName;}
  public void setAmount(int newAmount){amount = newAmount;}
  public void setPrice(double newPrice){price = newPrice;}
}

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

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

  String nam;
  int amoun;
  double pric;

  //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");
      nam = reader.next();
      System.out.println("Enter the amount of this item");
      amoun = reader.nextInt();
      System.out.println("Enter the price of this item");
      pric = reader.nextInt();
      item[i] = new FoodItem(nam, amoun, pric);
      i++;
      numberOfItems++;
    }
    else
      {done = true;}
  }

  //computing the total
  double total = 0.0;
  for (i = 0; i < numberOfItems; i++)
    {total += item[i].getAmount() * item[i].getPrice();}
  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", item[i].getName(),
        item[i].getAmount(), item[i].getPrice(),
        item[i].getAmount() * item[i].getPrice());}
  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);
}