CS 1401 Assignment #12

Date Assigned: Monday, November 19, or Tuesday, November 20, 2007.

Due Date: Wednesday, November 28, or Thursday, November 29, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to handle exceptions.

Programming assignment: The task is similar to the previous Assignments 9-11: write a program for helping waiters. However, this time, we assume that all the data about the food items (their names, amounts, and prices) is already stored in a file food:

Example: For two food items, pizza and coke, the file has the following form:


Programming assignment (continued): the task is to read the values from this file into the corresponding array of objects (as in Assignment 10), and then print the detailed menu including the information about all the items plus the following information:

New part: If the file is empty or if one of the numbers in the file is negative, throw and handle appropriate exceptions -- and inform the user about the problem.

Clarification: in contrast to Assignment 11, there is no need to check whether the items are vegetarian or not.

Homework assignment: on a separate sheet of paper, solve Ex. 4 and 6 at the end of Chapter 12 (on pp. 799 and 800).

Deliverables: as instructed by your TA.


import java.util.*;
import java.io.*;

class NegativeException extends Throwable{}
class FileEmptyException extends Throwable{}

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) throws IOException{
  final double TAX_RATE = 8.25;
  Scanner reader = new Scanner(new FileReader("food"));
  FoodItem[] item = new FoodItem[100];

  int i = 0;
  int numberOfItems = 0;

  String nam;
  int amoun;
  double pric;

    //reading the data
      nam = reader.next();
      amoun = reader.nextInt();
      if (amoun < 0)
        {throw new NegativeException();}
      pric = reader.nextInt();
      if (amoun < 0)
        {throw new NegativeException();}
      item[i] = new FoodItem(nam, amoun, pric);
    if (numberOfItems == 0)
      {throw new FileEmptyException();}

    //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.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);
  catch(NegativeException e){
    System.out.println("One of the numbers in the file is negative.");
  catch(FileEmptyException e){
    System.out.println("The file is empty.");