Java Code

Java Code

Write a program in Java (with a graphical user interface) that will allow the user to order a Pizza. For the pizza, it could be selected with a desired size, and toppings. The user should be able to pay the total cost of the ordered pizza, with 7.75% tax added, and receive the change back.



Must have the following toppings available for selection, with $0.25 for each Vegetarian topping and $0.50 for each Meat topping:

Vegetarian:

Baby Portabella Mushrooms
Fresh-Sliced Roma Tomatoes
Fresh-Sliced Onions
Fresh-Sliced Green Peppers
Black Olives
Sweet Pineapple
Banana Peppers
Jalapeño Peppers
Extra Cheese
Meat:

Pepperoni
Sausage
Spicy Italian Sausage
Ham
Hickory-Smoked Bacon
Grilled All-White Chicken
Turkey
Beef
Salami


Must have the following sizes available for selection, with corresponding price:

· Small - $6.50

· Medium - $8.50

· Large - $12.50

· Extra Large - $16.00

Must have a premium option, in which the extra large size and all of the toppings will be selected. If being requested, must have a receipt saved out at an external file, which could then be printed out later. If no size and no toppings were selected, a standard pizza would be ordered, where the size is small, and it contains only Cheese (same price as Extra Cheese).

View Answers

August 2, 2010 at 3:29 PM

Hi Friend,

Try the following:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import javax.swing.*;

public class PizzaOrder extends JFrame implements ActionListener, KeyListener {
double smallPizzaPrice = 6.50, mediumPizzaPrice = 8.50,largePizzaPrice = 12.50, elargePizzaPrice=16.00;
double vegtop1 = 0.25,vegtop2 = 0.25,vegtop3 = 0.25,vegtop4 = 0.25,vegtop5 = 0.25,vegtop6 = 0.25,vegtop7 = 0.25,vegtop8 = 0.25,vegtop9 = 0.25;
double meattop1 = 0.50,meattop2 = 0.50,meattop3 = 0.50,meattop4 = 0.50,meattop5 = 0.50,meattop6 = 0.50,meattop7 = 0.50,meattop8 = 0.50,meattop9 = 0.50;

JLabel lab1, lab2, lab3,lab4, lab5, vegtopLabel,meattopLabel, lab6, lab7;
Button button;
JTextField text1,text2,text3,text4;
ButtonGroup group;
JRadioButton small,medium, large,elarge;
JCheckBox chk1, chk2, chk3, chk4, chk5, chk6,chk7,chk8,chk9,chk10,chk11,chk12,chk13,chk14,chk15,chk16,chk17,chk18;

PizzaOrder(){
getContentPane().setLayout(null);
setBackground(Color.white);
lab1 = new JLabel("Name: ");
lab2 = new JLabel("Address: ");
lab3 = new JLabel("Contact No: ");
lab4 = new JLabel("Quantity: ");
lab5 = new JLabel("Select the size of the pizza(s):");
lab5.setForeground(new Color(0,0,205));
lab5.setFont(new Font("Arial",Font.BOLD,14));

vegtopLabel = new JLabel("Select Veg Toppings : ");
vegtopLabel.setForeground(new Color(0,0,205));
vegtopLabel.setFont(new Font("Arial",Font.BOLD,14));

meattopLabel = new JLabel("Select Meat Toppings : ");
meattopLabel.setForeground(new Color(0,0,205));
meattopLabel.setFont(new Font("Arial",Font.BOLD,14));

lab6 = new JLabel("Total price: ");
lab6.setForeground(new Color(200,0,0));
lab6.setFont(new Font("Arial",Font.BOLD,14));

lab7 = new JLabel("$0.00");
lab7.setForeground(new Color(235,0,0));

text1 = new JTextField(20);
text2 = new JTextField(20);
text3 = new JTextField(20);
text4 = new JTextField(20);
text4.setText("0");

small = new JRadioButton("Small", true);
medium = new JRadioButton("Medium", false);
large = new JRadioButton("Large", false);
elarge= new JRadioButton("Extra Large", false);
group = new ButtonGroup();
group.add(small);
group.add(medium);
group.add(large);
group.add(elarge);

chk1=new JCheckBox("Baby Portabella Mushrooms",false);
chk2=new JCheckBox("Fresh-Sliced Roma Tomatoes",false);
chk3=new JCheckBox("Black Olives",false);
chk4=new JCheckBox("Sweet Pineapple",false);
chk5=new JCheckBox("Banana Peppers",false);
chk6=new JCheckBox("Jalapeño Peppers",false);
chk7=new JCheckBox("Extra Cheese",true);
chk8=new JCheckBox("Fresh-Sliced Onions",false);
chk9=new JCheckBox("Fresh-Sliced Green Peppers ",false);

chk10=new JCheckBox("Pepperoni",false);
chk11=new JCheckBox("Sausage",false);
chk12=new JCheckBox("Spicy Italian Sausage",false);
chk13=new JCheckBox("Ham",false);
chk14=new JCheckBox("Grilled All-White Chicken",false);
chk15=new JCheckBox("Turkey",false);
chk16=new JCheckBox("Beef",false);
chk17=new JCheckBox("Salami",false);
chk18=new JCheckBox("Hickory-Smoked Bacon",false);

button = new Button("Order Now");

August 2, 2010 at 3:30 PM

continue.....

small.addActionListener(this);
medium.addActionListener(this);
large.addActionListener(this);
elarge.addActionListener(this);

chk1.addActionListener(this);
chk2.addActionListener(this);
chk3.addActionListener(this);
chk4.addActionListener(this);
chk5.addActionListener(this);
chk6.addActionListener(this);
chk7.addActionListener(this);
chk8.addActionListener(this);
chk9.addActionListener(this);

chk10.addActionListener(this);
chk11.addActionListener(this);
chk12.addActionListener(this);
chk13.addActionListener(this);
chk14.addActionListener(this);
chk15.addActionListener(this);
chk16.addActionListener(this);
chk17.addActionListener(this);
chk18.addActionListener(this);

text4.addKeyListener(this);
button.addActionListener(this);

lab1.setBounds(50,50,200,20);
lab2.setBounds(50,80,200,20);
lab3.setBounds(50,110,200,20);
lab4.setBounds(50,140,200,20);

text1.setBounds(200,50,200,20);
text2.setBounds(200,80,200,20);
text3.setBounds(200,110,200,20);
text4.setBounds(200,140,200,20);

lab5.setBounds(50,170,500,20);
small.setBounds(300,170,100,20);
medium.setBounds(400,170,100,20);
large.setBounds(500,170,100,20);
elarge.setBounds(600,170,100,20);

vegtopLabel.setBounds(50,200,300,20);
chk1.setBounds(50,230,300,20);
chk2.setBounds(50,260,300,20);
chk3.setBounds(50,290,300,20);
chk4.setBounds(50,320,300,20);
chk5.setBounds(50,350,300,20);
chk6.setBounds(50,380,300,20);
chk7.setBounds(50,410,300,20);
chk8.setBounds(50,440,300,20);
chk9.setBounds(50,470,300,20);

meattopLabel.setBounds(500,200,300,20);
chk10.setBounds(500,230,300,20);
chk11.setBounds(500,260,300,20);
chk12.setBounds(500,290,300,20);
chk13.setBounds(500,320,300,20);
chk14.setBounds(500,350,300,20);
chk15.setBounds(500,380,300,20);
chk16.setBounds(500,410,300,20);
chk17.setBounds(500,440,300,20);
chk18.setBounds(500,470,300,20);
lab6.setBounds(50, 550,500,40);
lab7.setBounds(200,550,500,40);

button.setBounds(50,600,100,20);
add(lab1);
add(lab2);
add(lab3);
add(lab4);
add(text1);
add(text2);
add(text3);
add(text4);
add(lab5);
add(small);
add(medium);
add(large);
add(elarge);
add(vegtopLabel);
add(chk1);
add(chk2);
add(chk3);
add(chk4);
add(chk5);
add(chk6);
add(chk7);
add(chk8);
add(chk9);
add(meattopLabel);
add(chk10);
add(chk11);
add(chk12);
add(chk13);
add(chk14);
add(chk15);
add(chk16);
add(chk17);
add(chk18);
add(lab6);
add(lab7);
add(button);
text4.selectAll();
setVisible(true);
setSize(1000,700);
}

public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }

public void keyReleased(KeyEvent e) {

try {
Integer.parseInt(text4.getText());
}
catch (NumberFormatException fe) {
text4.setText("0");
}

refreshPrice();
}

August 2, 2010 at 3:30 PM

continue..

public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
JOptionPane.showMessageDialog(this, text1.getText()+", Thank you" +
"\n\nYour pizza will be delivered in a few minutes. ","Orders Confirmed",JOptionPane.INFORMATION_MESSAGE);
}
refreshPrice();
}
private void refreshPrice() {
double price = 0;
int pizzaAmount = Integer.parseInt(text4.getText());

NumberFormat numberForm = NumberFormat.getNumberInstance();
DecimalFormat moneyForm = (DecimalFormat)numberForm;
moneyForm.applyPattern("0.00");

if (small.isSelected()) {
price+= smallPizzaPrice * pizzaAmount;
}
if (medium.isSelected()) {
price+= mediumPizzaPrice * pizzaAmount;
}
if (large.isSelected()) {
price+= largePizzaPrice * pizzaAmount;
}
if (elarge.isSelected()) {
price+= elargePizzaPrice * pizzaAmount;
}
if (chk1.isSelected()) {
price+= vegtop1 * pizzaAmount;
}
if (chk2.isSelected()) {
price+= vegtop2 * pizzaAmount;
}
if (chk3.isSelected()) {
price+= vegtop3 * pizzaAmount;
}
if (chk4.isSelected()) {
price+= vegtop4 * pizzaAmount;
}
if (chk5.isSelected()) {
price+= vegtop5 * pizzaAmount;
}
if (chk6.isSelected()) {
price+= vegtop6 * pizzaAmount;
}
if (chk7.isSelected()) {
price+= vegtop7 * pizzaAmount;
}
if (chk8.isSelected()) {
price+= vegtop8 * pizzaAmount;
}
if (chk9.isSelected()) {
price+= vegtop9 * pizzaAmount;
}
if (chk10.isSelected()) {
price+= meattop1 * pizzaAmount;
}
if (chk11.isSelected()) {
price+= meattop2 * pizzaAmount;
}
if (chk12.isSelected()) {
price+= meattop3 * pizzaAmount;
}
if (chk13.isSelected()) {
price+= meattop4 * pizzaAmount;
}
if (chk14.isSelected()) {
price+= meattop5 * pizzaAmount;
}
if (chk15.isSelected()) {
price+= meattop6 * pizzaAmount;
}
if (chk16.isSelected()) {
price+= meattop7 * pizzaAmount;
}
if (chk17.isSelected()) {
price+= meattop8 * pizzaAmount;
}
if (chk18.isSelected()) {
price+= meattop9 * pizzaAmount;
}
double value=price*0.0775;
double totalPrice=value+price;
lab7.setText("$"+moneyForm.format(totalPrice)+" along with the tax of $ "+value);
}
public static void main(String[]args){
PizzaOrder order=new PizzaOrder();
}
}

Thanks









Related Tutorials/Questions & Answers:
JAVA code For
JAVA code For   JAVA code For "Traffic signals Identification for vehicles
java code
java code  what is the code to turn off my pc through java program
Advertisements
JAVA CODE
JAVA CODE  JAVA SOURCE CODE TO BLOCK A PARTICULAR WEB SITES(SOCIAL WEB SITE
java code
java code  write a java code to convert hindi to english using arrays
java code
java code  hi any one please tell me the java code to access any link i mean which method of which class is used to open any link in java program
java code
java code  need java code for chart or graph which compare the performance of aprior algorithm and coherent rule algorithm.plz any one help me out
java code
java code  write a java code for finding a string in partiular position in a delimited text file and replace the word with the values given by user and write the file in new location
java code
java code  sir how to merge the cells in excel using java code please help me and also how to make the text placed in the cell to be center
java code
java code  sir how to merge the cells in excel using java code please help me and also how to make the text placed in the cell to be center
java code
java code  I need the java code that would output the following: HARDWARE ITEMS CODE DESCRIPTION UNIT PRICE K16 Wood screws,brass,20mm $7.75 D24 Wood glue,clear,1 liter $5.50 M93
Java code
Java code   Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent, switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes
java code
java code  develop a banking system in java
java code
java code  HOW TO PRINT 1 TO 100 WITHOUT USING CONDITIONAL,ANY LOOP AND ARRAY IN JAVA AND C.URGENT SIR PLZ  Hi, You can use following code: class MyClass { public static void main(String[] args) { int
java code
java code  how to extract html tags using java
Java code
Java code  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any... the following code: import java.util.*; import java.text.*; class NumberProgram
java code
java code  int g() { System.out.println("Inside method g"); int h() { System.out.println("Inside method h"); } } Find the error in the following... cannot define a method inside another method. Anyways, we have modified your code
java code
java code  what is meaning bufferedreader
java code
java code  what is meaning bufferedreader
java code
java code  i want HSVcolor descriptor for color image in java coding
Java code
Java code   Create a calculator class which will have methods add, multiply, divide & subtract   Hi Friend, Try the following code: class Calculation{ public int add(int a,int b){ return a+b
Java code
Java code  An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus...; Hi Friend, Try the following code: import java.util.*; import
java code
java code  I am beginer in java my question is how can i fill data from mysql database to jcombobox using netbeans
Java Code
Java Code  Write a java program, which creates multiple threads. Each thread performs add/delete/update operations on an ArrayList simultaneously
java code
java code  hi, Can any one tell me " How to read the data which is present om an image in java
java code
java code  Write a program in java for lru caching using two inputs one using integer and another using string of uppercase only
Java code
Java code  Create a class called Student which has the following methods: i. Average: which would accept marks of 3 examinations & return... & returns the name.   Hi Friend, Try the following code: import
Java code
Java code  Create a Bank class with methods deposit & withdraw. The deposit method would accept attributes amount & balance & returns....   Hi Friend, Try the following code: import javax.swing.*; class
Java code
Java code   Create an Employee class which has methods netSalary which would accept salary & tax as arguments & returns the netSalary which... the following code: import java.util.*; class Employee { static Scanner input=new
java code
java code  An employee _id consist of 5 digits is stored in a string variable strEmpid. Now Mr.Deb wants to store this Id in Integer type to IntEmpid. write Java statements to do
java code
java code  input any word ie risk,resul is-- r ri ris risk
java code
java code  Write a program to find the difference between sum of the squares and the square of the sums of n numbers
java code
java code   Develop a program that accepts the area of a square and will calculate its perimeter
java code
java code  Create a calculator class which will have methods add, multiply, divide & subtract
Java code
Java code  Create Book having following attributes: Book ID, Title, Author and Price. Create Periodical which has the following additional attributes... Friend, Try the following code: import java.util.*; class Book{ int id
java code
java code  Develop the program calculateCylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder
java code
java code  Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder
java code
java code  Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent, switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes
java code
java code  java database code to retrieve data at runtime and please... language="java"%> <%@page import="java.sql.*"%> <form method="post... e){} %>   If you want the application Java Swing then Visit
java code
java code   Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean
java code
java code  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
java code
java code  Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean
Java code
Java code  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java code
java code  An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the program calculateTotalProfit that consumes
java code
java code  Develop the program calculateHeight, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g · t in t time units and a height
java code
java code  Develop a program that accepts an initial amount of money (called the principal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional
java code
java code  Create an Employee class which has methods netSalary which would accept salary & tax as arguments & returns the netSalary which is tax deducted from the salary. Also it has a method grade which would accept
java code
java code  Create Product having following attributes: Product ID, Name, Category ID and UnitPrice. Create ElectricalProduct having the following additional attributes: VoltageRange and Wattage. Add a behavior to change
java code
java code  Create Book having following attributes: Book ID, Title, Author and Price. Create Periodical which has the following additional attributes: Period (weekly, monthly etc...) .Add a behavior to modify the Price
java code
java code  Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional attributes:loading capacity( 100 tonsâ?¦).Add a behavior to change the color
Java code
Java code  Create Product having following attributes: Product ID, Name, Category ID and UnitPrice. Create ElectricalProduct having the following additional attributes: VoltageRange and Wattage. Add a behavior to change

Ads