Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 4 The Cafe Problem Write a program for a cafe to display the bill for th

ID: 3767567 • Letter: P

Question

Project 4

The Cafe Problem

Write a program for a cafe to display the bill for the cashier. The cafe serves a chef’s menu with two options

Category

Price

Adjustment

Adults

$31.50

The total adult bill is reduced by $4.00 if there are 8 or more adults.

Children

$15.00

The program requests the name and zip code of the cafe and converts the text. The program then accepts the number of adults and children, performs the necessary calculations, computes the 6% tax, and displays the bill similar to the example below.

Enter name of cafe: moNet

Enter zip code:

21237

Enter number of

adults

(-1 to stop).....8

Enter number of

children......2

Monet Cafe

Rosedale

Price

Total

Adults

8

31.50

248.00

Children

2

15.00

30.00

Tax (6%)

16.68

Total bill

294.68

Enter number of

adults

(-1 to stop).....

CafeDemo should follow this sequence:

Request the name and zip code of the cafe from the user.

Use the Cafe class to set the cafe name and the location but do not display it. You will display the cafe information in the while loop with the other output.

The while loop:

Requests the number of meals for adults and children. Uses the Bill class to instantiate these objects:

adultBill childBill

Project Three                                                                                                                         Page 1

Uses the Bill class to calculate the adult and child costs.

Uses the Bill class to calculate the tax. The call should look like this:

??? = Bill.calcTax(???);

Totals the costs.

Displays the cafe name and location created earlier using the Cafe class – use the toString method.

Displays the two lines showing the adult and child costs using the Bill class

– use the display method.

Displays the tax. Use Bill.TAXRATE to get the current tax rate (6%) from the Bill class. Displays the total cost.

You will be coding two classes: Cafe and Bill.

The Cafe Class – write and test one method at a time.

Cafe has these instance variables

String name

String location

The constructor:

Call the setName method to assign a value to name, using the value passed to it (the name of the cafe as entered by the user.)

Call the setLocation method to assign a value to location, using the value passed to it (the zip code as entered by the user.)

Zip code should be a string object.

Cafe contains these methods.

setName method changes cafe name entered by the user so that the first character is uppercase, the rest of the characters are lower case, and “ Cafe” is attached. This method returns a string.

setLocation method uses the zip code entered by the user to determine the location. . This method returns a string.

Use a nested if or switch

Zip Code

Location

21204

Towson

21237

Rosedale

Any other value

Baltimore

Project Three

Page 2

toString method returns a string that displays the first two lines of the receipt. Here is an example of possible output:

Monet Cafe

Rosedale

The Bill Class

Set up a final for the tax rate like this: static final double TAXRATE = .06;

Bill has these instance variables:

String category

Integer mealQty

Double mealCost

Double mealPrice

The constructor:

category will be set to null.

mealQty will be passed the number of meals ordered for the category. mealCost will be initialized to 0.

mealPrice will be initialized to 0.

Bill contains these methods.

calcAdultCost sets the category to “Adults”, calculates the cost of the adult meals, sets the mealPrice, and sets and returns the mealCost.

calcChildCost sets the category to “Children”, calculates the cost of the children’s meals, sets the mealPrice, and sets and returns the mealCost.

calcTax calculates the tax. Return the calculated tax based on the total sent to this method. Set up the method like this:

static double calcTax(double total)

display displays the category, mealQty, mealPrice, and mealCost. Format the number accordingly using the classes learned.

The method header looks like this: public void display()

You will be submitting 3 java files. Any class, variable or method names in bold must be used in your program.

Grading:

CafeDemo Class (20 points)

Cafe Class (15 points)

Bill Class (15 points)

Testing and Accuracy (5 points)

Page 3

Category

Price

Adjustment

Adults

$31.50

The total adult bill is reduced by $4.00 if there are 8 or more adults.

Children

$15.00

Explanation / Answer

CafeDemo.java

package com.test.cafe;

import java.util.Scanner;

public class CafeDemo {
   public static void main(String[] args) {
       String cafeName = null;
       String zipcode = null;
       int numOfAdults = 0;
       int numOfchildren = 0;
       double tax = 0.0;
       double totalCost = 0.0;
       double childTotal = 0.0;
       double adultTotal = 0.0;
       double finalAmount = 0.0;
       Scanner sc = new Scanner(System.in);
       System.out.print(" Enter name of cafe:");
       cafeName = sc.next();
       System.out.print(" Enter zip code:");
       zipcode = sc.next();
       Cafe cafe = new Cafe(cafeName, zipcode);

       while (true) {
           System.out.print(" Enter Number of Adults:");

           numOfAdults = sc.nextInt();
           if (numOfAdults == -1) {
               System.exit(0);
           }
           System.out.print(" Enter Number of Children:");
           numOfchildren = sc.nextInt();
           Bill adultBill = new Bill(numOfAdults);
           Bill childBill = new Bill(numOfchildren);
           adultTotal = adultBill.calcAdultCost();
           childTotal = childBill.calcChildCost();
           totalCost = adultTotal + childTotal;

           tax = Bill.calcTax(totalCost);
           finalAmount = tax + totalCost;
           System.out.println(cafe.toString());

           adultBill.display();
           childBill.display();
           System.out.println("Tax (6%) " + tax);
           System.out.println("TotalBill " + finalAmount);
       }

   }
}

Cafe.Java

package com.test.cafe;

public class Cafe {
   private String name;

   private String location;

   private String CAFE = "Cafe";

   /**
   * @param name
   * the name to set
   */
   public String setName(String name) {
       String tempName = name;
       tempName = tempName.substring(0, 1).toUpperCase()
               + tempName.substring(1).toLowerCase();
       tempName = tempName + " " + CAFE;
       this.name = tempName;
       return name;
   }

   /**
   * @param location
   *
   */
   public String setLocation(String location) {
       int tempCode = 0;
       String tempLocation = location;
       if (null != location) {
           tempCode = Integer.parseInt(location);
       }
       switch (tempCode) {
       case 21204:
           tempLocation = "Towson";
           break;
       case 21237:
           tempLocation = "Rosedale";
           break;
       default:
           tempLocation = "Baltimore";
       }
       this.location = tempLocation;
       return location;
   }

   /**
   * Constructor
   *
   * @param cafeName
   * @param zipcode
   */
   Cafe(String cafeName, String zipcode) {
       setLocation(zipcode);
       setName(cafeName);
   }

   public String toString() {
       return name + " " + location;
   }
}

Bill.java

package com.test.cafe;

public class Bill {
   static final double TAXRATE = .06;
   static final double ADULT_PRICE = 31.50;
   static final double CHILD_PRICE = 15.00;
   static final String ADULTS = "Adults";
   static final String CHILDRENS = "Children";

   private String category;

   private Integer mealQty;
   private Double mealCost;

   private Double mealPrice;

   Bill(int quntity) {
       this.category = null;
       this.mealQty = quntity;
       this.mealPrice = 0.0;

   }

   public Double calcAdultCost() {
       category = ADULTS;
       mealPrice = ADULT_PRICE;
       mealCost = ADULT_PRICE * mealQty;
       if (mealQty == 8 || mealQty > 8) {
           mealCost -= 4;
       }
       return mealCost;

   }

   public Double calcChildCost() {
       category = CHILDRENS;
       mealPrice = CHILD_PRICE;
       mealCost = CHILD_PRICE * mealQty;

       return mealCost;

   }

   static double calcTax(double total) {
       double tax = total * TAXRATE;
       return tax;

   }

   /**
   * Method to display details
   */
   public void display() {
       System.out.println(category+ " " + mealQty + " " + mealPrice
               + " " + mealCost);

   }
}

Output:

Enter name of cafe:Monet

Enter zip code:23

Enter Number of Adults:8

Enter Number of Children:2
Monet Cafe
Baltimore
Adults       8       31.5       248.0
Children       2       15.0       30.0
Tax (6%)    16.68
TotalBill    294.68

Enter Number of Adults:-1