In: Computer Science
Task #1 The if Statement, Comparing Strings, and Flags (JAVA using Eclipse IDE 14)
Welcome to May and Adam’s
Pizzeria
Enter your first name: Amy
Pizza
Size(inches) Cost
10
$10.99
12
$12.99
14
$14.99
16
$16.99
What size pizza would you like?
10, 12, 14, or 16 (enter the number only): 12
What type of crust do you want?
(H)Hand-tossed, (T) Thin-crust, or (D) Deep-dish
(enter
H, T, or D): H
All pizzas come with cheese.
Additional toppings are $1.25 each, choose from:
Pepperoni, Sausage, Onion, Mushroom
Do you want Pepperoni? (Y/N): n
Do you want Sausage? (Y/N): n
Do you want Onion? (Y/N): y
Do you want Mushroom? (Y/N): y
Your order is as follows:
12-inch pizza
Hand-tossed crust
Cheese Onion Mushroom
The cost of your order is: $15.49
The tax is: $1.23
The total due is: $16.72
Your order will be ready for pickup in 30 minutes.
In this task you need to print out the menu, declare your variables, and assign their initial values. You will be able to make selections, but initially you will assign a hand-tossed pizza for your crust and a cost of $12.99 as a base cost for the size of your pizza.
Implementaion in JAVA:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Pizzaorder {
static Scanner s= new Scanner(System.in);
// driver method
public static void main(String[] args) {
// TODO Auto-generated method
stub
Pizza p=new Pizza();
mainmenu(p);
}
// main menu
public static void mainmenu(Pizza p) {
System.out.println("Welcome to May
and Adam’s Pizzeria");
System.out.print("Enter your first
name : ");
String name = s.next();
System.out.println();
System.out.println("Pizza Size(in
inches) cost");
System.out.println(" 10 $10.99
");
System.out.println(" 12 $12.99
");
System.out.println(" 14 $14.99
");
System.out.println(" 16 $16.99
");
System.out.println();
System.out.println("What size pizza
would you like?");
System.out.print("10, 12, 14, or 16
(enter the number only) : ");
int size=s.nextInt();
switch(size) {
case 10:
double
price=10.99;
p.crust(price);
return;
case 12:
price=12.99;
p.crust(price);
return;
case 14:
price=14.99;
p.crust(price);
return;
case 16:
price=16.99;
p.crust(price);
return;
default:
System.out.println("Invalid choice");
mainmenu(p);
}
}
}
//pizza class
class Pizza{
Scanner s= new Scanner(System.in);
// will take input of crust
public void crust(double price) {
System.out.println();
System.out.println("What type of
crust do you want?");
System.out.print("(H)Hand-tossed,
(T) Thin-crust, or (D) Deep-dish (enter H, T, or D) : ");
char
crust=s.next().charAt(0);
// using switch condition
statement
switch(crust) {
case 'H' :
topping_print(price,crust);
break;
case 'T' :
topping_print(price,crust);
break;
case 'D' :
topping_print(price,crust);
break;
default:
System.out.println("Invalid choice");
crust(price);
}
}
// method for take input of topping nad print
order
public void topping_print(double price,char crust)
{
double tempp=price;
System.out.println();
System.out.println("All pizzas come
with cheese.");
System.out.println("Additional
toppings are $1.25 each, choose from:\r\n" +
"Pepperoni, Sausage, Onion, Mushroom");
System.out.print("Do you want
Pepperoni? (Y/N) : ");
char Pepperoni
=s.next().charAt(0);
if(Pepperoni=='y' ||
Pepperoni=='Y') {
price+=1.25;
}
while(Pepperoni!='y' ||
Pepperoni!='Y' || Pepperoni!='n' || Pepperoni!='N') {
if(Pepperoni=='y' ||
Pepperoni=='Y') {
price+=1.25;
break;
}
else if(Pepperoni=='n' ||
Pepperoni=='N') {
break;
}
else {
System.out.println("Invalid Choice");
System.out.print("Do you want Pepperoni? (Y/N) : ");
Pepperoni
=s.next().charAt(0);
}
}
System.out.print("Do you want
Sausage? (Y/N) : ");
char
Sausage=s.next().charAt(0);
if(Sausage=='y' || Sausage=='Y')
{
price+=1.25;
}
while(Sausage!='y' || Sausage!='Y'
|| Sausage!='n' || Sausage!='N') {
if(Sausage=='y' || Sausage=='Y')
{
price+=1.25;
break;
}
else if(Sausage=='n' ||
Sausage=='N') {
break;
}
else {
System.out.println("Invalid Choice");
System.out.print("Do you want Sausage? (Y/N) : ");
Sausage
=s.next().charAt(0);
}
}
System.out.print("Do you want
Onion? (Y/N) : ");
char
Onion=s.next().charAt(0);
if(Onion=='y' || Onion=='Y')
{
price+=1.25;
}
while(Onion!='y' || Onion!='Y' ||
Onion!='n' || Onion!='N') {
if(Onion=='y' || Onion=='Y')
{
price+=1.25;
break;
}
else if(Onion=='n' || Onion=='N')
{
break;
}
else {
System.out.println("Invalid Choice");
System.out.print("Do you want Onion? (Y/N) : ");
Onion
=s.next().charAt(0);
}
}
System.out.print("Do you want
Mushroom? (Y/N) : ");
char
Mushroom=s.next().charAt(0);
if(Mushroom=='y' || Mushroom=='Y')
{
price+=1.25;
}
while(Mushroom!='y' ||
Mushroom!='Y' || Mushroom!='n' || Mushroom!='N') {
if(Mushroom=='y' || Mushroom=='Y')
{
price+=1.25;
break;
}
else if(Mushroom=='n' ||
Mushroom=='N') {
break;
}
else {
System.out.println("Invalid Choice");
System.out.print("Do you want Mushroom? (Y/N) : ");
Mushroom
=s.next().charAt(0);
}
}
// print the order
System.out.println();
System.out.println("Your order is
as follows : ");
int size = 0;
String type = null;
String topping="Cheese, ";
double tax=1.23;
if(tempp==10.99)
size=10;
else if(tempp==12.99)
size=12;
else if(tempp==14.99)
size=14;
else if(tempp==16.99)
size=16;
if(crust=='H')
type="Hand-tossed";
else if(crust=='T')
type="Thin-crust";
else if(crust=='D')
type="Deep-dish";
if(Pepperoni=='Y' ||
Pepperoni=='y') {
topping+="Pepperoni, ";
}
if(Sausage=='Y' || Sausage=='y')
{
topping+="Sausage, ";
}
if(Onion=='Y' || Onion=='y')
{
topping+="Onion,
";
}
if(Mushroom=='Y' || Mushroom=='y')
{
topping+="Mushroom, ";
}
// for upto 2 decimal format
DecimalFormat df2 = new
DecimalFormat("#.##");
System.out.println(size+"-inch
Pizza");
System.out.println(type+"
crust");
System.out.println(topping);
System.out.println("The cost of
your order is : $"+df2.format(price));
System.out.println("The tax is $"+
tax);
System.out.println("The total Due
is : $"+df2.format((price+tax)));
System.out.println("Your order will
be ready for pickup in 30 minutes.");
}
}
SAMPLE OUTPUT:
1:
2.
If you have any doubt regarding this question please ask me in comments
// THANK YOU:-)