Home Tutorial Java Core Java Showroom Application

 
 

Share on Google+Share on Google+
Java Showroom Application
Posted on: May 21, 2010 at 12:00 AM
Advertisement
In this section you will learn how to calculate the amount to be paid by the customers after the discount on purchase of items.

Java Showroom Application

We have created an application for showroom that imposes discounts on purchase of items. Here we are going to calculate the amount to be paid by the customers after the discount on purchase of items. There is a discount on the following items:

T Shirt - 10% discount
Silk sari- 30% discount
Baba suit- 40% discount
Trousers - 15% discount

You can see in the given code, we have used switch case statement to allow user to select the item they want to purchase. All these items and their cost will get added in the list until you select Exit option and when you will select Exit option, a bill get displayed consisting of all the information regarding item, cost after discount and the total amount to be paid by the customer.

Here is the code:

import java.util.*;
import java.text.*;

class Shop {
	public String item;
	public int quantity;
	public double price;
	public double discount;
	public double total;

	public Shop() {
	}

	public Shop(String item, int quantity, double price, double discount,
			double total) {
		super();
		this.item = item;
		this.quantity = quantity;
		this.price = price;
		this.discount = discount;
		this.total = total;
	}

	public String getItem() {
		return item;
	}

	public int getQuantity() {
		return quantity;
	}

	public double getPrice() {
		return price;
	}

	public double getDiscount() {
		return discount;
	}

	public double getTotal() {
		return total;
	}

	public static void main(String[] args) throws Exception {
		double sum = 0.0;
		DecimalFormat df = new DecimalFormat("##%");
		List list = new ArrayList();
		Scanner scan = new Scanner(System.in);
		int menu = 0;
		System.out.println("Cloth Showroom");
		System.out.println();
		System.out.println("1. T Shirt");
		System.out.println("2. Silk sari");
		System.out.println("3. Baba suit");
		System.out.println("4. Trousers ");
		System.out.println("5. Exit");

		boolean quit = false;
		do {
			System.out.print("Please enter your choice: ");
			menu = scan.nextInt();
			System.out.println();
			switch (menu) {
			case 1:
				System.out.print("Quantity: ");
				int q1 = scan.nextInt();
				System.out.print("Price: ");
				double p1 = scan.nextDouble();
				double d1 = 0.10;
				double t1 = (p1 - (d1 * p1)) * q1;
				list.add(new Shop("T Shirt", q1, p1, d1, t1));
				break;
			case 2:
				System.out.print("Quantity: ");
				int q2 = scan.nextInt();
				System.out.print("Price: ");
				double p2 = scan.nextDouble();
				double d2 = 0.30;
				double t2 = (p2 - (d2 * p2)) * q2;
				list.add(new Shop("Silk sari", q2, p2, d2, t2));
				break;
			case 3:
				System.out.print("Quantity: ");
				int q3 = scan.nextInt();
				System.out.print("Price: ");
				double p3 = scan.nextDouble();
				double d3 = 0.40;
				double t3 = (p3 - (d3 * p3)) * q3;
				list.add(new Shop("Baba suit", q3, p3, d3, t3));
				break;
			case 4:
				System.out.print("Quantity: ");
				int q4 = scan.nextInt();
				System.out.print("Price: ");
				double p4 = scan.nextDouble();
				double d4 = 0.15;
				double t4 = (p4 - (d4 * p4)) * q4;
				list.add(new Shop("Trousers", q4, p4, d4, t4));
				break;
			case 5:
				quit = true;
				System.out
						.println("Items   Quantity   Price   T.Price   Discount(%)   Total");
				for (Shop s : list) {
					System.out.println(s.getItem() + "    " + s.getQuantity()
							+ "        " + s.getPrice() + "        "
							+ (s.getQuantity() * s.getPrice()) + "       "
							+ df.format((s.getDiscount())) + "        "
							+ s.getTotal());
					sum += s.getTotal();
				}
				System.out.println("Total= " + sum);

				break;
			default:
				System.out.println("Invalid Entry!");
			}
		} while (!quit);
	}
}

Output:

Cloth Showroom

1. T Shirt
2. Silk Sari
3. Baba suit
4. Trousers
5. Exit
Please enter your choice: 2

Quantity:  2
Price: 1000
Please enter your choice: 1

Quantity: 2
Price: 250
Please enter your choice: 5

Items    Quantity   Price   T.Price   Discount(%)   Total
Silk Sari     2        1000.0    2000.0          30%         1400.0
T Shirt       2           250.0     500.0        10%           450.0
Total= 1850.0

Advertisement

Related Tags for Java Showroom Application:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 21, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com