Need help writing a Java Program

Posted on 2009-02-21
Last Modified: 2013-11-23
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
Question by:Megarooni
    LVL 10

    Expert Comment

    This seems to  be a school assignment.

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

    Expert Comment

    LVL 2

    Accepted Solution

    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(;
    		for (Meal m : meal) { 
    			System.out.println("Enter the number of " + m.getName() + " ordered: ");
    			try {
    				int quant = sc.nextInt();
    			} catch (InputMismatchException e) {
    				System.out.println("Error with input!");
    		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) {
  = name;
    		this.cost = cost;
  = discount;
    		this.discountOfWhatQuant = discountOfWhatQuant;
    	public String getName() {
    		return name;
    	public void setName(String 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) {
  = 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

    LVL 2

    Expert Comment

    Also i forgot to add the result. Here it is:
    Enter the number of Breakfast ordered: 
    Enter the number of Lunch ordered: 
    Enter the number of Dinner ordered: 
    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


    Author Comment

    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.

    Author Comment

    never mind, I figured it out.
    LVL 10

    Expert Comment

    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.

    Author Comment

    In the real World I wont need this, I'll need English. ""

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    Automated mobile software testing 8 106
    fix34  challenge 9 70
    strCopies  challenge 17 59
    solarwind tftp server 2 16
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    759 members asked questions and received personalized solutions in the past 7 days.

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

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now