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). 8. Provide the toString method that returns a well formatted string showing the detailed composition of the sandwich e.g. Bread Slices : 2 Cheese Slices : 2 Patties: 1 Tomato Slices: 0 Mustard: No (if not added) Ketchup: Yes Iceberg : Yes Grilled: No Price: Rs. 200 (dummy value given here but you will calculate programmatically and provide correct value here)
import java.util.Scanner;
public class Main{
static void applyDiscount(){
Scanner input=new Scanner(System.in);
System.out.println("Enter the price of the item");
int price=input.nextInt();
if(price>0 && price<=100){
System.out.println("Discount is 2");
double discount=price*2/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>100 && price<=200){
System.out.println("Discount is 2");
double discount=price*4/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>200 && price<=300){
System.out.println("Discount is 4");
double discount=price*4/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>300 && price<=400){
System.out.println("Discount is 6");
double discount=price*6/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>400 && price<=500){
System.out.println("Discount is 8");
double discount=price*8/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>500 && price<=600){
System.out.println("Discount is 10");
double discount=price*10/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>600 && price<=800){
System.out.println("Discount is 12");
double discount=price*12/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>800 && price<=1000){
System.out.println("Discount is 14");
double discount=price*14/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
else if(price>1000){
System.out.println("Discount is 20");
double discount=price*20/100;
double disc=price-discount;
System.out.println("Discounted price is: "+disc);
}
}
static void to_String(){
Scanner input=new Scanner(System.in);
System.out.print("Bread Slices : ");
int B=input.nextInt();
System.out.print("Cheese Slices : ");
int C=input.nextInt();
System.out.print("Patties Slices : ");
int P=input.nextInt();
System.out.print("Tomato Slices : ");
int T=input.nextInt();
System.out.print("Mustard: ");
String M=input.next();
System.out.print("Ketchup:: ");
String K=input.next();
System.out.print("Iceberg : ");
String I=input.next();
System.out.print("Grilled : ");
String G=input.next();
double prices=B*25+C*25+P*100;
System.out.print("Price: Rs "+prices);
}
public static void main(String[]args){
applyDiscount();
to_String();
}
}
Comments
Leave a comment