using if and switch stmt

using if and switch stmt

A cloth showroom has announced the following discounts on purchase of items
T Shirt - 10% discount
Silk sari- 30% discount
Baba suit- 40% discount
Trousers - 15% discount

cost of the items can be assumed.
Write a program using "switch and if statement" to compute net amount to be paid by a customer.
View Answers

May 12, 2010 at 12:48 PM

Hi Friend,

Try the following 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<Shop> list = new ArrayList<Shop>();
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);
}
}
Thanks









Related Tutorials/Questions & Answers:
using if and switch stmt - Java Beginners
using if and switch stmt  A cloth showroom has announced... be assumed. Write a program using "switch and if statement" to compute net amount...(); switch(menu) { case 1: System.out.print("Quantity: "); int q1 = scan.nextInt
using switch,break and for loop
using switch,break and for loop  generate a 10 digit number and display the length of longest increasing series
Advertisements
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
using switch case
using switch case  Define a class MENU to perform the following operation depending upon the users choice using switch case 1)print square root... choice: "); int choice=input.nextInt(); switch(choice
change color of back ground using switch
change color of back ground using switch   how to change back ground color according to day using switch case
Write a java program to display the season given the month using switch case
Write a java program to display the season given the month using switch case  Write a java program to display the season given the month using switch case
core java ,io operation,calling methods using switch cases
core java ,io operation,calling methods using switch cases  How to create a dictionary program,providing user inputs using io operations with switch cases and providing different options for searching,editing,storing meanings
Switch
of the day, based on the value of week, using the switch statement... Switch       Sometimes it becomes cumbersome to write lengthy programs using if and if-else
Find Capital using Switch-Case statement
Find Capital using Switch-Case statement In this Java Tutorial section, we are going to find the capital of our states using Switch-Case statement. For this, we have created two arrays i.e. array of states and array of capitals. Then we
ModuleNotFoundError: No module named 'morningstar-stmt'
ModuleNotFoundError: No module named 'morningstar-stmt'  Hi, My... named 'morningstar-stmt' How to remove the ModuleNotFoundError: No module named 'morningstar-stmt' error? Thanks   Hi, In your
Showing Error for Prepared stmt in a jsp
Showing Error for Prepared stmt in a jsp  i hav a task to connect jsp to oracel database using preparestatement and gettin an error showin dat prepared statement obj cant be created pstmt is nt resloved here are the files
Showing Error for Prepared stmt in a jsp
Showing Error for Prepared stmt in a jsp  i hav a task to connect jsp to oracel database using preparestatement and gettin an error showin dat prepared statement obj cant be created pstmt is nt resloved here are the files
switch statement
switch statement   i want to write a java program that computes Fibonacci,factorial,string reversal and ackerman using switch case to run as a single program
The Switch statement
on the value of week, using the switch statement...Switch       Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements
Switch Statement
Switch Statement  How we can use switch case in java program ?   Note:-Switch case provides built-in multiway decision statement.It...); switch(days){ case 1: System.out.println("Sunday
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
Switch Case in Java
It is very difficult and lengthy to write programs using if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths.... Switch statements with String cases are supported in Java SE 7 but the earlier
programes on switch
=input.nextInt(); switch(choice){ case 1
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
Switch case in Jsp page
using switch case in jsp page...Switch case in Jsp page  <table> <tr><td>Enter Distance :</td> <td><input type="text" name="dis"/></td>
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello,ADS_TO_REPLACE_1 Switch statement is executed line by line... of the switch expression. It does execute the statement until the end of the block
Switch case + condition??
Switch case + condition??  I try to use a switch case with condition but it doesn't display anything have look: ......public void commandAction...(); } } else { List down = (List)display.getCurrent(); switch
switch functions - Java Beginners
friend, This is running code, switch option using in javaSctipt...switch functions  I am writing a script for use in a computer based... your month of hire:") switch(n) { case(n="January"): document.write("Last
ModuleNotFoundError: No module named 'enum-switch'
ModuleNotFoundError: No module named 'enum-switch'  Hi, My Python... 'enum-switch' How to remove the ModuleNotFoundError: No module named 'enum-switch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'e-switch'
ModuleNotFoundError: No module named 'e-switch'  Hi, My Python...-switch' How to remove the ModuleNotFoundError: No module named 'e-switch... to install padas library. You can install e-switch python with following
ModuleNotFoundError: No module named 'js.bootstrap-switch'
ModuleNotFoundError: No module named 'js.bootstrap-switch'  Hi, My... named 'js.bootstrap-switch' How to remove the ModuleNotFoundError: No module named 'js.bootstrap-switch' error? Thanks   Hi
ModuleNotFoundError: No module named 'mongo-switch'
ModuleNotFoundError: No module named 'mongo-switch'  Hi, My Python... 'mongo-switch' How to remove the ModuleNotFoundError: No module named 'mongo-switch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nose-switch'
ModuleNotFoundError: No module named 'nose-switch'  Hi, My Python... 'nose-switch' How to remove the ModuleNotFoundError: No module named 'nose-switch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pi_switch'
ModuleNotFoundError: No module named 'pi_switch'  Hi, My Python..._switch' How to remove the ModuleNotFoundError: No module named 'pi_switch' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'pulseaudio-switch'
ModuleNotFoundError: No module named 'pulseaudio-switch'  Hi, My... named 'pulseaudio-switch' How to remove the ModuleNotFoundError: No module named 'pulseaudio-switch' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'smt-switch'
ModuleNotFoundError: No module named 'smt-switch'  Hi, My Python... 'smt-switch' How to remove the ModuleNotFoundError: No module named 'smt-switch' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'switch-case'
ModuleNotFoundError: No module named 'switch-case'  Hi, My Python... 'switch-case' How to remove the ModuleNotFoundError: No module named 'switch-case' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'switch-case'
ModuleNotFoundError: No module named 'switch-case'  Hi, My Python... 'switch-case' How to remove the ModuleNotFoundError: No module named 'switch-case' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'switch-inputs'
ModuleNotFoundError: No module named 'switch-inputs'  Hi, My... 'switch-inputs' How to remove the ModuleNotFoundError: No module named 'switch-inputs' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'switch-mexico'
ModuleNotFoundError: No module named 'switch-mexico'  Hi, My... 'switch-mexico' How to remove the ModuleNotFoundError: No module named 'switch-mexico' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'switch-model'
ModuleNotFoundError: No module named 'switch-model'  Hi, My Python... 'switch-model' How to remove the ModuleNotFoundError: No module named 'switch-model' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'switch-payments'
ModuleNotFoundError: No module named 'switch-payments'  Hi, My... named 'switch-payments' How to remove the ModuleNotFoundError: No module named 'switch-payments' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ugly-switch'
ModuleNotFoundError: No module named 'ugly-switch'  Hi, My Python... 'ugly-switch' How to remove the ModuleNotFoundError: No module named 'ugly-switch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'ybc-switch'
ModuleNotFoundError: No module named 'ybc-switch'  Hi, My Python... 'ybc-switch' How to remove the ModuleNotFoundError: No module named 'ybc-switch' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'enum-switch'
ModuleNotFoundError: No module named 'enum-switch'  Hi, My Python... 'enum-switch' How to remove the ModuleNotFoundError: No module named 'enum-switch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'e-switch'
ModuleNotFoundError: No module named 'e-switch'  Hi, My Python...-switch' How to remove the ModuleNotFoundError: No module named 'e-switch... to install padas library. You can install e-switch python with following
ModuleNotFoundError: No module named 'Flask-Switch'
ModuleNotFoundError: No module named 'Flask-Switch'  Hi, My Python... 'Flask-Switch' How to remove the ModuleNotFoundError: No module named 'Flask-Switch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'js.bootstrap-switch'
ModuleNotFoundError: No module named 'js.bootstrap-switch'  Hi, My... named 'js.bootstrap-switch' How to remove the ModuleNotFoundError: No module named 'js.bootstrap-switch' error? Thanks   Hi
Version of com.braintreepayments>browser-switch dependency
List of Version of com.braintreepayments>browser-switch dependency

Ads