java help Write a program that takes in input the price of an item followed by t
ID: 3912032 • Letter: J
Question
java help
Write a program that takes in input the price of an item followed by the desired discount option 1 (for 10%), 2 (for 20%), 3(for 30%) or 4 (for custom discount values). If option 4 is chosen, it should be followed by desired discount value. Enter these values in the input box. The program will output the final price of the item The final price is $4.50 Check the value for option (must be greater than 0 or less than or equal to 4). If it's not correct, the output should be Wrong option! Check for the correctness of the price (must be greater than 0). If it's not correct, the output message should contain the word Error If you choose option 4 but did not enter the correct discount value! if discount is less than zero or if discount is greater than 100), the out message should contain the word Wrong Use switch statements to choose the options lini In java you can use Scanner class to feed input to your code from command line Values read from command prompt by Scanner class are of the type String. For ex. If you enter 42 at command prompt, Scanner class will read it as a string To, convert a string to a number we can do o o int value Integer.parselnt( 42"); 1I "42" is the string read from command promptExplanation / Answer
import java.util.Scanner;
public class Item
{
public static void main (String args[])
{
double tot, amt;
int ch;
Scanner scan = new Scanner(System.in);
System.out.println(" Enter the price of an item: ");
int price = scan.nextInt();
if(price > 0)
{
System.out.println(" MENU");
System.out.println(" 1. Option 1 (for 10%)");
System.out.println(" 2. Option 2 (for 20%)");
System.out.println(" 3. Option 3 (for 30%)");
System.out.println(" 4. Option 4 (for custom discount value)");
System.out.println(" Enter ur option: ");
ch = scan.nextInt();
switch(ch)
{
case 1:
tot = 100 - 10;
amt = (tot * price) / 100;
System.out.println(" The final price is $" +amt);
break;
case 2:
tot = 100 - 20;
amt = (tot * price) / 100;
System.out.println(" The final price is $" +amt);
break;
case 3:
tot = 100 - 30;
amt = (tot * price) / 100;
System.out.println(" The final price is $" +amt);
break;
case 4:
System.out.println(" Enter discount rate: ");
int rate = scan.nextInt();
if(rate > 0)
{
tot = 100 - rate;
amt = (tot * price) / 100;
System.out.println(" The final price is $" +amt);
}
else
{
System.out.println(" Wrong");
}
break;
default:
System.out.println(" Error");
}
}
else
{
System.out.println(" Wrong");
}
}
}
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 1
The final price is $90.0
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 2
The final price is $80.0
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 3
The final price is $70.0
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 4
Enter discount rate: 50
The final price is $50.0
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 4
Enter discount rate: 0
Wrong
OUTPUT
Enter the price of an item: 100
MENU
1. Option 1 (for 10%)
2. Option 2 (for 20%)
3. Option 3 (for 30%)
4. Option 4 (for custom discount value)
Enter ur option: 7
Error
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.