In: Computer Science
You will use your previous programming project as a starting point. Be sure to copy and rename your file from that project and edit the internal documentation to reflect the new name and date.
Use your previous working Java program as a starting point (Project 3). Create another function that determines whether the customer should receive a discount. Assume your food truck gives a 10% discount on orders over $50 (the total BEFORE tax). If the order is eligible for a discount, then calculate and display the discount. If the order does not get a discount, then still display the discount, but it should be 0. Include a line before this that tells every customer that your business gives a 10% discount on all orders over $50 so they understand this part of their receipt.
And this was the project 3 to be used to write this program.
import java.util.Scanner;
public class Main
{
static double salesTax, amountDueWithTax, totalAmount, amountTendered, change;
static Scanner sc = new Scanner(System.in);
//get all the information about items ordered
public static int[] readData(String items[])
{
int num[] = new int[5];
for(int i=0; i<5; i++)
{
System.out.print("Enter quantity of " + items[i] + " : ");
//get all the information about items ordered and the amount of money tendered as mentioned in ques
num[i] = sc.nextInt();
}
return num;
}
//method to calculate amount with tax
public static void getAmountWithTax(int num[], double rate[], double amount[])
{
double totalAmount = 0;
//find the amount for all items and totalAmount
for(int i=0; i<5; i++)
{
amount[i] = num[i]*rate[i];
totalAmount = totalAmount + amount[i];
}
//find salesTax, amountDueWithTax
salesTax = totalAmount*0.06;
amountDueWithTax = totalAmount + salesTax;
}
//method to print the receipt
public static void printInfo(int num[], String items[], double rate[], double amount[])
{
System.out.println("Thank you for visiting Roronoa Pizza Place!"); //print the title and Address
System.out.println("109th Avenue, Mount Olympia");
System.out.println("Your Order Was");
for(int i=0; i<5; i++)
System.out.printf("%d %s @ %.4f each: $%.4f\n",num[i], items[i], rate[i] ,amount[i]);
System.out.printf("Amount due for pizzas and drinks is: $%.4f\n", totalAmount);
System.out.printf("Sales tax amount is: $%.4f\n", salesTax);
System.out.printf("Total amount due, including tax is: $%.4f\n", amountDueWithTax);
System.out.printf("Amount tendered: $%.3f\n", amountTendered);
System.out.printf("Change: $%.4f\n", change);
}
//main method
public static void main(String[] args)
{
double rate[] ={8.99, 1.5, 12.99, 2.5, 1.5};
double amount[] = new double[5];
String items[] = {"Small pizzas", "Small pizza toppings", "Large pizzas", "Large pizza toppings", "Soft Drinks" };
///get all the information about items ordered
int num[] = readData(items);
//calculate amountDueWithTax
getAmountWithTax(num, rate, amount);
do{
System.out.print("Enter Amount tendered : ");
amountTendered = sc.nextDouble();
if(amountTendered >= amountDueWithTax)
break;
System.out.println ("Money given is not enough!Try again");
}while(true);
//calculate the change
change = amountTendered - amountDueWithTax;
//print the receipt
printInfo(num, items, rate, amount);
}
}
import java.util.Scanner;
public class Main
{
static double salesTax, amountDueWithTax, totalAmount, amountTendered, change;
static Scanner sc = new Scanner(System.in);
//get all the information about items ordered
public static int[] readData(String items[])
{
int num[] = new int[5];
for(int i=0; i<5; i++)
{
System.out.print("Enter quantity of " + items[i] + " : ");
//get all the information about items ordered and the amount of money tendered as mentioned in ques
num[i] = sc.nextInt();
}
return num;
}
//Method to calculate and return discount
public static double getDiscount(int num[], double rate[], double amount[])
{
double tempAmount = 0,discount=0;
//find the amount for all items and totalAmount
for(int i=0; i<5; i++)
{
tempAmount = tempAmount + num[i]*rate[i];
}
//10% discount on order over $50
if(tempAmount>50)
{
discount=0.1*tempAmount;
}
//if the order is not eligible for discount,then it becomes zero
else
{
discount=0;
}
return discount;
}
//method to calculate amount with tax
// discount variable passes as a parameter,So that the totalAmount can be calculate...
public static void getAmountWithTax(int num[], double rate[], double amount[],double discount)
{
double totalAmount = 0;
//find the amount for all items and totalAmount
for(int i=0; i<5; i++)
{
amount[i] = num[i]*rate[i];
totalAmount = totalAmount + amount[i];
}
if(discount>0)
{
totalAmount=totalAmount-discount;
}
System.out.print("Total Amount"+totalAmount);
//find salesTax, amountDueWithTax
salesTax = totalAmount*0.06;
amountDueWithTax = totalAmount + salesTax;
}
//method to print the receipt
public static void printInfo(int num[], String items[], double rate[], double amount[])
{
System.out.println("Thank you for visiting Roronoa Pizza Place!"); //print the title and Address
System.out.println("109th Avenue, Mount Olympia");
System.out.println("Your Order Was");
for(int i=0; i<5; i++)
System.out.printf("%d %s @ %.4f each: $%.4f\n",num[i], items[i], rate[i] ,amount[i]);
System.out.printf("Amount due for pizzas and drinks is: $%.4f\n", totalAmount);
System.out.printf("Sales tax amount is: $%.4f\n", salesTax);
System.out.printf("Total amount due, including tax is: $%.4f\n", amountDueWithTax);
System.out.printf("Amount tendered: $%.3f\n", amountTendered);
System.out.printf("Change: $%.4f\n", change);
}
//main method
public static void main(String[] args)
{
double rate[] ={8.99, 1.5, 12.99, 2.5, 1.5};
double amount[] = new double[5];
String items[] = {"Small pizzas", "Small pizza toppings", "Large pizzas", "Large pizza toppings", "Soft Drinks" };
///get all the information about items ordered
int num[] = readData(items);
//calculate amountDueWithTax
double d=getDiscount(num, rate, amount);
System.out.print("Your discount is : "+d);
getAmountWithTax(num, rate, amount,d);
do{
System.out.print("Enter Amount tendered : ");
amountTendered = sc.nextDouble();
if(amountTendered >= amountDueWithTax)
break;
System.out.println ("Money given is not enough!Try again");
}while(true);
//calculate the change
change = amountTendered - amountDueWithTax;
//print the receipt
printInfo(num, items, rate, amount);
}
}