please, ask specific problems inside this problem, or give me your actual code with some additional questions.

Solved

Posted on 2009-02-21

I need to write a java program, but it is confusing me because of everything I need to add, such as tax, discount, etc. I am hoping someone could at least write out an example of what my program should look like. Here's the information I am given.

A catering service has the following schedule of charges for meals that it serves to groups and discounts according to how many were ordered:

Meal........... Cost Each.......... Discount............ If order more than

Breakfast..... $5.50................... 10%.........................10

Lunch ...........$9.50................... 15%........................ 15

Dinner...........$16.50.................. 12%......................... 8

The program should compute, for each category, the cost of the meals received in each category, the discount, and the final cost of the meals. It should then compute the total cost of all meals, add a 10% sates tax and print the total due. Be sure to format the output well and include appropriate comments in your program.

1. Enter the number of Breakfast ordered: 30

2. Enter the number of Lunch ordered: 28

3. Enter the number of Dinner ordered: 25

My Favorite Catering Service Receipt:

Meal..... ......Quantity....... Cost .......Discount .........Cost After Discount

Breakfast... .....30......... $165.00........ $16.50.......... $148.50

Lunch.............. 28......... $266.00........ $39.90........... $226.10

Dinner.............. 25........ $412.50........ $49.50............ $363.00

Total: $737.60

Sales Tax: $73.76

Total Due: $811.36

A catering service has the following schedule of charges for meals that it serves to groups and discounts according to how many were ordered:

Meal........... Cost Each.......... Discount............ If order more than

Breakfast..... $5.50................... 10%.......................

Lunch ...........$9.50..........

Dinner...........$16.50...

The program should compute, for each category, the cost of the meals received in each category, the discount, and the final cost of the meals. It should then compute the total cost of all meals, add a 10% sates tax and print the total due. Be sure to format the output well and include appropriate comments in your program.

1. Enter the number of Breakfast ordered: 30

2. Enter the number of Lunch ordered: 28

3. Enter the number of Dinner ordered: 25

My Favorite Catering Service Receipt:

Meal..... ......Quantity....... Cost .......Discount .........Cost After Discount

Breakfast... .....30......... $165.00........ $16.50.......... $148.50

Lunch.............. 28......... $266.00........ $39.90........... $226.10

Dinner.............. 25........ $412.50........ $49.50............ $363.00

Total: $737.60

Sales Tax: $73.76

Total Due: $811.36

8 Comments

please, ask specific problems inside this problem, or give me your actual code with some additional questions.

```
import java.util.InputMismatchException;
import java.util.Scanner;
public class CateringService {
public static void main(String[] args) {
// Making array of our meals, later we can easly change this
Meal[] meal = new Meal[3];
meal[0] = new Meal("Breakfast", 5.50f, 10, 10);
meal[1] = new Meal("Lunch", 9.50f, 15, 15);
meal[2] = new Meal("Dinner" , 16.50f, 12, 8);
// Adding simple input for user to insert qunaties
Scanner sc = new Scanner(System.in);
for (Meal m : meal) {
System.out.println("Enter the number of " + m.getName() + " ordered: ");
try {
int quant = sc.nextInt();
m.setQuantity(quant);
} catch (InputMismatchException e) {
System.out.println("Error with input!");
System.exit(0);
}
}
float total = 0;
float salesTax = 0;
float totalDue = 0;
System.out.format("%-10s %-10s %-10s %-10s %-10s", "Meal", "Quantity",
"Cost", "Discount", "Cost After Discount \n");
for (Meal m : meal) {
float discount = 0;
float mealDiscountedCost = m.getCost();
if (m.checkForDiscount()) {
discount = m.makeDiscount();
mealDiscountedCost = m.costAfterDiscount();
}
System.out.format("%-10s %-10s %-10s %-10s %-10s\n", m.getName(), String.valueOf(m.getQuantity()),
String.valueOf(m.getCost()), String.valueOf(discount), String.valueOf(mealDiscountedCost));
total += mealDiscountedCost;
}
System.out.println("Total: $" + total);
salesTax = (float) (total * 0.10);
System.out.format("Total sales tax: $%.2f\n", salesTax);
totalDue = total + salesTax;
System.out.println("Total Due: $" + totalDue);
}
}
//here is second class, make sure you make seperate class file
public class Meal {
// i am using BigDecimal class to get more precise value
String name;
float cost;
int discount;
int discountOfWhatQuant;
int quantity;
public Meal(String name, float cost, int discount, int discountOfWhatQuant) {
this.name = name;
this.cost = cost;
this.discount = discount;
this.discountOfWhatQuant = discountOfWhatQuant;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getCost() {
return cost;
}
public void setCost(float cost) {
this.cost = cost;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getDiscountOfWhatQuant() {
return discountOfWhatQuant;
}
public void setDiscountOfWhatQuant(int discountOfWhatQuant) {
this.discountOfWhatQuant = discountOfWhatQuant;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean checkForDiscount() {
return (discountOfWhatQuant < quantity) ? true : false;
}
public float makeDiscount() {
float discountMade = ((cost * quantity) * discount / 100);
return discountMade;
}
public float costAfterDiscount() {
float discountMade = (cost * quantity) - makeDiscount();
return discountMade;
}
}
```

```
Enter the number of Breakfast ordered:
30
Enter the number of Lunch ordered:
28
Enter the number of Dinner ordered:
25
Meal Quantity Cost Discount Cost After Discount
Breakfast 30 5.5 16.5 148.5
Lunch 28 9.5 39.9 226.1
Dinner 25 16.5 49.5 363.0
Total: $737.6
Total sales tax: $73,76
Total Due: $811.36
```

I tried out your code, but got an error that said, "class, interface, or enum expected on line 1. I've tried fixing it, but to no success.

Other then that I want to thank you. I didn't expect someone to write out a whole code. It will help me a lot.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Automated mobile software testing | 8 | 106 | |

fix34 challenge | 9 | 70 | |

strCopies challenge | 17 | 59 | |

solarwind tftp server | 2 | 16 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**10** Experts available now in Live!