CS 1401 Assignment #11

Date Assigned: Monday, November 12, or Tuesday, November 13, 2007.

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

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

Programming assignment: The task is the same as in the previous Assignments 9 and 10: 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 10, we advised you to define a class FoodItem whose objects contain three fields:

More and more students are now vegetarian, so they also want to know whether the food items are vegetarian or not. To be able to store this additional information, design a new class FoodItemsVeg which contains a new field: a true-false variable indicating whether a food item is vegetarian. Use inheritance to design this new class. Use objects of the new type in your program.

Deliverables: as instructed by your TA.


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 class FoodItemsVeg extends FoodItem{
  private boolean veg;

  public FoodItemsVeg(String aname, int aamount, double aprice,
      boooean aveg){
    super(aname, aamount, aprice); veg = aveg;}

  public boolean getVeg(){return veg;}
  public void setVeg(int newVeg){veg = newVeg;}

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

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

  String nam;
  int amoun;
  double pric;
  String vegetarian;
  boolean veget;

  //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();
      System.out.println("Is this item vegetarian? Y/N");
      vegetarian = reader.next();
      veget = (vegetarian[0] == "Y"); 
      item[i] = new FoodItemsVeg(nam, amoun, pric, veg);
      {done = true;}

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