Provide a method calculatePrice() that calculates and returns the price of this sandwich according to the rates given in the above table. E.g. the price of a sandwich with 3 bread slices, 2 cheese slices, 2 tomato slices, 3 patties, mustard, no ketchup, no ice berg with grilling will be calculated as 3 * 20 + 2 * 30 + 2 * 5 + 3 * 70 + 5+ 0 + 0 + 10 7. Provide a method applyDiscount(double disc) that calculates the price, applies the discount % given in the argument and returns the discounted price of the sandwich.E.g. if the method is called on a sandwich whose real price returned by the calculatePrice function is 550 and applyDiscount(10) is called, then a ten percent discount will be calculated as 550 * 10/100 = 55. Now this function will return the discounted price as 550 – 55 = 495. (Please note that this is just an example, do not hard code these values in your function).
import java.util.Scanner;
public class Main{
static void calculatePrice(){
Scanner scan=new Scanner(System.in);
System.out.print("Bread Slices : ");
int bre=scan.nextInt();
System.out.print("Cheese Slices : ");
int Chee=scan.nextInt();
System.out.print("Patties Slices : ");
int Pat=scan.nextInt();
System.out.print("Tomato Slices : ");
int Tom=scan.nextInt();
System.out.print("Mustard: ");
String Mus=scan.next();
System.out.print("Ketchup: ");
String Ket=scan.next();
System.out.print("Iceberg: ");
String Ice=scan.next();
System.out.print("Grilled : ");
String Gril=scan.next();
double prices=bre*20+Chee*30+Tom*5+Pat*70+5+0+0+10;
System.out.print("Price: Rs "+prices);
}
static void applyDiscount(){
Scanner scan=new Scanner(System.in);
System.out.println("Input the price of the item");
int Amount=scan.nextInt();
if(Amount>0 && Amount<=100){
System.out.println("Discount is 2");
double Dsc=Amount*2/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>100 && Amount<=200){
System.out.println("Discount is 2");
double Dsc=Amount*4/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>200 && Amount<=300){
System.out.println("Discount is 4");
double Dsc=Amount*4/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>300 && Amount<=400){
System.out.println("Discount is 6");
double Dsc=Amount*6/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>400 && Amount<=500){
System.out.println("Discount is 8");
double Dsc=Amount*8/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>500 && Amount<=600){
System.out.println("Discount is 10");
double Dsc=Amount*10/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>600 && Amount<=800){
System.out.println("Discount is 12");
double Dsc=Amount*12/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>800 && Amount<=1000){
System.out.println("Discount is 14");
double Dsc=Amount*14/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
else if(Amount>1000){
System.out.println("Discount is 20");
double Dsc=Amount*20/100;
double disc=Amount-Dsc;
System.out.println("Discounted price is: "+disc);
}
}
public static void main(String[]args){
calculatePrice();
applyDiscount();
}
}
Comments
Leave a comment