I've only been learning Java for two weeks, and I had to complete this task as part of my course. My instructor could not give specific feedback as answer file is already provided, the code works but I am just wondering what some of you might think of my code if it can be refined. It's a basic pizza ordering program.
The requirements were:
able to specify pizzas one at a time using an loop. loop can end or not based on input. order viauser input using the Scanner class. format currency with NumberFormat Thank you for your time and wisdom.
import java.util.Scanner;
import java.text.NumberFormat;
public class PizzaOrder {
public static void main(String[] args) {
//defining and initialising variables.
NumberFormat nf = NumberFormat.getCurrencyInstance();
Scanner keyboard = new Scanner(System.in);
int pizzaquantity=0;
int numberoftoppings = 0;
double toppingtotalprice =0;
char ordermore = 'n';
double runningtotal = 0;
final double stp = 1.0, ltp = 1.5, ftp = 2.0;
final double s = 8.0, l = 11.0, f = 14.0;
//using a do while loop, because condition to stop is at the end.
do{
System.out.println("Please place the order for your pizza.");
System.out.println("Size (s = small, l = large, f = family):");
char pizzasize = keyboard.next().charAt(0);
//if else-else if nested inside loop
//one iteration of the conditional statement for pizza size type.
//number format is applied to every iteration of cost.
if (pizzasize == 's')
{
System.out.println("Number of toppings?:");
numberoftoppings = keyboard.nextInt();
toppingtotalprice = (numberoftoppings * stp);
System.out.println("Pizza Quantity:");
pizzaquantity = keyboard.nextInt();
runningtotal = (pizzaquantity * toppingtotalprice * s);
System.out.println("Cost is: " + nf.format(runningtotal));
}
else if (pizzasize == 'l')
{
System.out.println("Number of toppings?:");
numberoftoppings = keyboard.nextInt();
toppingtotalprice = (numberoftoppings * ltp);
System.out.println("Pizza Quantity:");
pizzaquantity = keyboard.nextInt();
runningtotal = (pizzaquantity * toppingtotalprice * l);
System.out.println("Cost is:" + nf.format(runningtotal));
}
else if (pizzasize == 'f')
{
System.out.println("Number of toppings?:");
numberoftoppings = keyboard.nextInt();
toppingtotalprice = (numberoftoppings * ftp);
System.out.println("Pizza Quantity:");
pizzaquantity = keyboard.nextInt();
runningtotal = (pizzaquantity * toppingtotalprice * l);
System.out.println("Cost is:" + nf.format(runningtotal));
}
else
{
}
System.out.println("Order more? (y/n):");
ordermore = keyboard.next().charAt(0);
}
while (ordermore != 'n');
System.out.println("Total cost is: " + nf.format(runningtotal += runningtotal));
System.out.println("Thank you for your order!");
}
}