In: Computer Science
I would like to create discount based on the following items on small piza , large pizza and drinks as well.
for instance for small pizza,
if qty of small pizaa is > 10 give 50% discount
if qty of small pizaa is > 8 give 40% discount
if qty of small pizaa is > 6 givs 30% discount
if qty of small pizaa is > 4 give 20% discount
else
0%
// Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then give total amount due with tax included.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.
string answar;
do {
System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza
int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza
System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.
int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.
System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.
int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.
System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.
int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.
System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.
int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks
System.out.print("Enter Amount tendered: ");
double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.
//stored the rates of all items
double smallPizzasRate = 8.99; // rate of small pizza
double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings
double largePizzasRate = 12.99; //rate of large pizza
double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings
double softDrinksRate = 1.5; // rate of soft drinks
//find the amount for all items
double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.
double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.
double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.
double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza
double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.
//find totalAmount,salesTax, amountDueWithTax and change
double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;
double salesTax = totalAmount*0.06;
double amountDueWithTax = totalAmount + salesTax;
double change = amountTendered - amountDueWithTax;
//prints the receipt
System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess
System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess
System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number
System.out.println("Your Order Was");
System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);
System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);
// prints out the total amount of small pizza and its toppings.
System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);
System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);
// prints out the total amount of large pizza and its toppings.
System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);
// prints out the total amount of Softdrinks.
System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax
System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax
System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax.
System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.
System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt
System.out.println("\nThank you! \nYour order is complete!");
//print the complete statment.
if (totalAmount < amountTendered) {
System.out.println("\nThank you! \nYour order is complete!");
}
else
System.out.println("\nYour money is not enough");
System.out.println("do you want to add anything else? Yes or No");
answar = sc.next();
}
while (answar.equals("Yes"));
}
}
i have added comments wherever changes were made or extra code was added
if you are not able to copy paste the code below just leave a comment
import java.util.Scanner;
public class starting_java{public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.
String answer;
do {
System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza
int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza
System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.
int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.
System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.
int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.
System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.
int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.
System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.
int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks
System.out.print("Enter Amount tendered: ");
double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.
//finding discount (added following lines of code)
int smallPizzasDiscountPer=0;
if(numSmallPizzas>10)smallPizzasDiscountPer=50;
if(numSmallPizzas>8 && numSmallPizzas<=10)smallPizzasDiscountPer=40;
if(numSmallPizzas>6&& numSmallPizzas<=8) smallPizzasDiscountPer=30;
if(numSmallPizzas>4 && numSmallPizzas<=6) smallPizzasDiscountPer=20;
int largePizzasDiscountPer=0;
if(numLargePizzas>10)largePizzasDiscountPer=50;
if(numLargePizzas>8 && numLargePizzas<=10)largePizzasDiscountPer=40;
if(numLargePizzas>6&& numLargePizzas<=8) largePizzasDiscountPer=30;
if(numLargePizzas>4 && numLargePizzas<=6) largePizzasDiscountPer=20;
int softDrinksDiscountper=0;
if(numSoftDrinks>10)softDrinksDiscountper=50;
if(numSoftDrinks>8 && numSoftDrinks<=10)softDrinksDiscountper=40;
if(numSoftDrinks>6&& numSoftDrinks<=8)softDrinksDiscountper=30;
if(numSoftDrinks>4 && numSoftDrinks<=6)softDrinksDiscountper=20;
//stored the rates of all items
double smallPizzasRate = 8.99; // rate of small pizza
double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings
double largePizzasRate = 12.99; //rate of large pizza
double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings
double softDrinksRate = 1.5; // rate of soft drinks
//find the amount for all items
double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.
double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.
double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.
double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza
double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.
//find discount for all items(added these lines)
double discountSmallPizzas=amountSmallPizzas*smallPizzasDiscountPer/100; //total discount on small pizza
double discountLargePizzas=amountLargePizzas*largePizzasDiscountPer/100; //total discount on large pizza
double discountSoftDrinks=amountSoftDrinks*softDrinksDiscountper/100; //total discount on soft drinks
double totalDiscount=discountSmallPizzas+discountLargePizzas+discountSoftDrinks; //net total discount
//find totalAmount,salesTax, amountDueWithTax and change
double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;
double salesTax = totalAmount*0.06;
double amountDueWithTax = totalAmount + salesTax-totalDiscount; //(made changes)
double change = amountTendered - amountDueWithTax;
//prints the receipt
System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess
System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess
System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number
System.out.println("Your Order Was");
System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);
System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);
// prints out the total amount of small pizza and its toppings.
System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);
System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);
// prints out the total amount of large pizza and its toppings.
System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);
// prints out the total amount of Softdrinks.
//printing discount(added this)
System.out.printf("\nDiscount on Small pizzas %d%%\n",smallPizzasDiscountPer);//prints discount on small pizza in percent
System.out.printf("Discount on large pizzas %d%%\n",largePizzasDiscountPer);//prints discount on large pizza in percent
System.out.printf("Discount on soft drinks %d%%\n",softDrinksDiscountper);//prints discount on Soft drink in percent
System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax
System.out.printf("Total discount: $%.4f\n", totalDiscount); // prints total discount(added this)
System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax
System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax after discount.
System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.
System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt
System.out.println("\nThank you! \nYour order is complete!");
//print the complete statment.
if (totalAmount < amountTendered) {
System.out.println("\nThank you! \nYour order is complete!");
}
else
System.out.println("\nYour money is not enough");
System.out.println("do you want to add anything else? Yes or No");
answer = sc.next();
}
while (answer.equals("Yes"));
}
}
(NOTE:for any doubt in the code just leave a comment else please press the like button)