Home Answers Viewqa Java-Beginners using if and switch stmt

 
 


vanshi
using if and switch stmt
1 Answer(s)      3 years ago
Posted in : Java Beginners

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 Pages:
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
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
on the value of week, using the switch statement. class ... Switch       Sometimes it becomes cumbersome to write lengthy programs using if and if-else
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
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
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
of week, using the switch statement. class Switch{  ...Switch       Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements
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
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
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
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 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
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
Switch Statement example in Java
Switch Statement example in Java    ... the switch statement. Here, you will learn how to use the switch statement in your java... the switch statement in Java. The following java program tells you for entering numbers
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 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
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
Strings in Switch statement
Strings in Switch statement In this section, you will learn about strings in switch statements which is recently added feature in Java SE 7. Using JDK 7, you can pass string as expression in switch statement. The switch statement
Java - The switch construct in Java
Java - The switch construct in Java       Switch is the control statement in java which... same as If-Else construct. This is the difference between Switch and If-Else
Switch application
In this application we are going to create it by using Window based application... are going to add Switch to Window. Create a new project by selecting Window... file and add a Switch from the library.   here we are declaring Switch
iPhone Switch Changes Toolbar Color
iPhone Switch Changes Toolbar Color In this tutorial we will use Switch button to change the Color of Toolbar. The Switch is added through the Interface..., first will declare the variable for the Toolbar, Switch and then will write
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
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO...); System.out.println(); switch (question...("| Retrieve objects in an LinkedList container using an Iterator
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO...); System.out.println(); switch (question...("| Retrieve objects in an LinkedList container using an Iterator
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO...); System.out.println(); switch (question...("| Retrieve objects in an LinkedList container using an Iterator
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO...); System.out.println(); switch (question...("| Retrieve objects in an LinkedList container using an Iterator
programes on switch
=input.nextInt(); switch(choice){ case 1
Switch Statement in java 7
executed. Example : In this example we are using switch -case statement... Switch Statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. Switch Statements
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
switch Example - Random Insults
. This is a common style when they do similar short actions. It makes the switch statements much easier to read. Arrays? This version is written without using arrays... insult = "bad value from badInputInsult???"; switch (rand(0, 1
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello, Switch statement is executed line by line. PHP executes the statement only when the case statement matches the value of the switch
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 Case - Java Interview Questions
Switch Case  int x=100000; switch(x) { case 100000: printf("Lacks"); } output= Lacks I want reason why x matches switch case although range for int is in between something -32678 to +32676. Plz give me ans quickly
switch Java Keyword
switch Java Keyword       The switch is a keyword defined in the java programming language. Keywords... in java programming language likewise the switch keyword indicates the following
switch CAPSLOCK button in my progranm...
switch CAPSLOCK button in my progranm...  I found that function which does the Capslocks job,when am running my program if the capslock button... the capslocks switch button to become a button that i can switch it between on and off
switch Statement - Overview
Java Notes switch Statement - Overview Purpose of switch: select one of many possible statements to execute... on a boolean value (only two possible values). The switch statement allows
Conditional Examples(if - else- switch case) in JavaScript
; This example is display to all months in a year for using the switch case... Conditional Examples(if - else- switch case) in JavaScript... to be executed . switch statement:-This statement is used to if you want to select
PHP Switch Case
Switch Case Control Structure: Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful... should know how the switch case works, in switch case every case is considered
Java Switch Statement
Java Switch Statement       In java, switch is one of the control statement which turns the normal flow... then switch statement can be used. It checks your choice and jump on that case label
What is the difference between an if statement and a switch statement?
What is the difference between an if statement and a switch statement?   Hi, What is the difference between an if statement and a switch statement... values). Whereas the Switch statement offers you multiple choice of option from
Mysql Switch CASE
Mysql Switch CASE       Mysql Switch Case perform one of several different actions based on several... an example from 'Mysql Switch Case'.To understand an example from 'Mysql Switch
save switch data into database - Java Beginners
save switch data into database  switch(menu) { case 1://add a student System.out.print("Enter student ID: "); int ID = scan.nextInt...(); switch(menu) { case 1: System.out.print("Enter student ID: "); int ID
C Break with Switch statement
C Break with Switch statement   ... with Switch-Case in C. The switch case statements allows to control complex... the user to enter any number. The expression defined in the switch statement finds
How to use switch statement in jsp code
How to use switch statement in jsp code       switch is a type of control statement used to control the flow of program execution. Its body is known as a switch block
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i%3)==0) system.out.println("java"); if((i%5)==0) system.out.println("technology
Controlling your program
(if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching... In this section we will learn how to use if-then,  if-the else and switch...;java compare a is smaller The switch statement Sometimes

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.