• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

Need help writing a Java Program

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
0
Megarooni
Asked:
Megarooni
  • 3
  • 2
  • 2
  • +1
1 Solution
 
oleberCommented:
This seems to  be a school assignment.

please, ask specific problems inside this problem, or give me your actual code with some additional questions.
0
 
staarlandCommented:
I tried to make your assignment so here it is.

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;
	}
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
staarlandCommented:
Also i forgot to add the result. Here it is:
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

Open in new window

0
 
MegarooniAuthor Commented:
To starrland.

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.
0
 
MegarooniAuthor Commented:
never mind, I figured it out.
0
 
oleberCommented:
No it didn't helped you. In the real world you will need to resold this small problems and staarland just distroid what could have been a good lesson.
0
 
MegarooniAuthor Commented:
In the real World I wont need this, I'll need English. "Destroid.......lol"
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now