close

build a GUI that allows a store clerk to enter a pizza order. You should be able to create a PizzaOrder object once all data has been entered into the UI. 

The UI works as follows:
1. When a customer calls up to order a pizza, they provide their phone number which is entered into the phone number text box. When a newline (return) character is entered in the phone number text box, a list of Customers should be searched for a matching phone
number. The matching customer’s name should be placed in the name text box.
2. If the customer cannot be found, pop up a dialog box with an appropriate message. This UI does not allow new customers to be entered – that would be a different UI which we will not implement. Thus this UI only supports existing customers we have in our database.
3. The remaining data – size, pickup or delivery, and toppings can be entered
4. The Total button should calculate the cost of the pizza and put the cost in the cost text box 
5. The Enter button should extract the data from the UI and instantiate a PizzaOrder object that represents the order. The PizzaOrder object should then be printed.

For your solution, you should use at least two different layout managers. Two of the text fields should not be editable the customer name (which will be looked up when given a phone number), and the cost of the pizza (which should be calculated from the other data entered).

Break your GUI into multiple JPanel classes. This allows you to instantiate one of your panels and add it to a JFrame to test it. Once you have all the panels working, you can combine them together to create a more complex UI.

 


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


class lab4
{
public static void main(String a[])
{
Frame f = new Frame();
f.pack();
}
}


class Frame extends JFrame
{
/* clientDataPanel p = new clientDataPanel();
deliveryandsize p2 = new deliveryandsize();
toppingcheckbox p3 = new toppingcheckbox();*/
checkout p = new checkout();
public Frame()
{
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setTitle("test");
add(p);
}
}


class clientDataPanel extends JPanel
{
JButton confirm = new JButton("Confirm");
JTextField phonenumberTField = new JTextField(10);
JTextField clientnameTField = new JTextField(10);
JLabel L1 =  new JLabel("Phone: ");
JLabel L2 =  new JLabel("name: ");
String phoneArray []={"408-123-4567","408-888-8888","408-222-3333"};
String nameArray[] = {"Bob Williams","Sue Gao","Ron Brown"};
String name = "";
public clientDataPanel()
{
add(L1);
add(phonenumberTField);
add(L2);
add(clientnameTField);
clientnameTField.setEnabled(false);
add(confirm);
confirm.addActionListener(new confirmAction());
}
class confirmAction implements ActionListener
{
public void actionPerformed(ActionEvent confirmbtnaction)
{
String s = phonenumberTField.getText();
for (int i = 0;i<phoneArray.length;i++)
{
if (s.equals(phoneArray[i]))
{
name = nameArray[i];
clientnameTField.setText(name);
break;
}

else
{
clientnameTField.setText("");
String msg[]={"Customer cannot be Found!"};
JOptionPane pane = new JOptionPane();
pane.setMessage(msg);
JDialog d = pane.createDialog(null, "Error");
d.setVisible(true);
break;


}
}
}
}


public String getPhone()
{ String phone = phonenumberTField.getText();
return(phone);
}


public String getName()
{
String Name = clientnameTField.getText();
return(Name);
}
}


class deliveryandsize extends JPanel
{
JComboBox pizzasize = new JComboBox();
JComboBox delivery = new JComboBox();
int sizecost = 0;
String size = "";
String deliveryorpickup = "";
public deliveryandsize()
{
pizzasize.addItem("Pizza Size");
pizzasize.addItem("Large");
pizzasize.addItem("Medium");
pizzasize.addItem("Small");
add(pizzasize);
pizzasize.addActionListener(new sizebox());
delivery.addItem("Delivery or Pick Up");
delivery.addItem("Self Pick Up");
delivery.addItem("Delivery");
add(delivery);
delivery.addActionListener(new deliverybox());

}
class sizebox implements ActionListener
{
public void actionPerformed(ActionEvent sizeboxaction)
{
size = (String) pizzasize.getSelectedItem();
if (size.equals("Large"))
{
sizecost = 12;
}
if (size.equals("Medium"))
{
sizecost = 11;
}
if (size.equals("Samll"))
{
sizecost = 10;
}

}
}





class deliverybox implements ActionListener
{
public void actionPerformed(ActionEvent deliveryboxaction)
{
deliveryorpickup = (String) delivery.getSelectedItem();
}
}
}


class toppingcheckbox extends JPanel
{
JCheckBox onions = new JCheckBox("onions");
JCheckBox tomatos = new JCheckBox("tomatos");
JCheckBox pepproni = new JCheckBox("pepperoni");
JCheckBox sausage = new JCheckBox("sausage");
int toppingASonions;
int toppingAStomatos;
int toppingASpepproni;
int toppingASsausage;
String toppingname1 = "";
String toppingname2 = "";
String toppingname3 = "";
String toppingname4 = "";
JButton test = new JButton("test");
public toppingcheckbox()
{
add(onions);
onions.addActionListener(new oni());
add(tomatos);
tomatos.addActionListener(new tom());
add(sausage);
sausage.addActionListener(new sau());
add(pepproni);
pepproni.addActionListener(new peppr());
// add(test);
test.addActionListener(new testbtnaction());
}


class testbtnaction implements ActionListener
{
public void actionPerformed(ActionEvent jesttest)
{
int testoutput = 0;
testoutput = testoutput+getOC()+getTC()+getPC()+getSC();
System.out.println(testoutput);
}
}


class oni implements ActionListener
{
public void actionPerformed(ActionEvent onioncheckbox)
{
boolean oc = onions.isSelected();

if(oc)
{
toppingASonions = 1;
toppingname1 = "Onions";
}
else
{ toppingASonions = 0;
toppingname1 = "";}
}
}
public int getOC()
{ int toppingASonions = this.toppingASonions;
return(toppingASonions);
}


class tom implements ActionListener
{
public void actionPerformed(ActionEvent tomatochackbox)
{
boolean tc = tomatos.isSelected();
if(tc)
{
toppingAStomatos = 1;
toppingname2 = "Tomatos";
}
else
{ toppingAStomatos = 0;
toppingname2 = "";}
}
}
public int getTC()
{ int toppingAStomatos = this.toppingAStomatos;
return(toppingAStomatos);
}


class peppr implements ActionListener
{
public void actionPerformed(ActionEvent peppronicheckbox)
{
boolean pc = pepproni.isSelected();
if(pc)
{
toppingASpepproni = 1;
toppingname3 = "Pepperoni";
}
else
{ toppingASpepproni = 0;
toppingname3 = "";}
}
}
public int getPC()
{ int toppingASpepproni = this.toppingASpepproni;
return(toppingASpepproni);
}


class sau implements ActionListener
{
public void actionPerformed(ActionEvent sausagecheckbox)
{
boolean sc = sausage.isSelected();
if(sc)
{
toppingASsausage = 1;
toppingname4 = "Sausage";
}
else
{ toppingASsausage = 0;
toppingname4 ="";}
}
}
public int getSC()
{ int toppingASsausage = this.toppingASsausage;
return(toppingASsausage);
}

}


class checkout extends JPanel
{
JTextField costtext = new JTextField(10);
JLabel costlabel = new JLabel("Cost: ");
JButton totalbtn = new JButton("Total");
JButton enterbtn = new JButton("enter");
clientDataPanel p = new clientDataPanel();
deliveryandsize p2 = new deliveryandsize();
toppingcheckbox p3 = new toppingcheckbox();
JPanel subPanel = new JPanel();
public checkout()
{
setLayout(new GridLayout(4,1));
add(p);
add(p2);
add(p3);
subPanel.add(costlabel);
subPanel.add(costtext);
costtext.setEnabled(false);
subPanel.add(totalbtn);
totalbtn.addActionListener(new totalbtnaction());


subPanel.add(enterbtn);
enterbtn.addActionListener(new enterbtnaction());

add(subPanel);


}
class totalbtnaction implements ActionListener
{
public void actionPerformed(ActionEvent totalbtnaction)
{
double priceCalcu = 0 + p3.toppingASonions + p3.toppingAStomatos + p3.toppingASpepproni + p3.toppingASsausage;
double toppingcost = (0.75*priceCalcu)+p2.sizecost;
costtext.setText(Double.toString(toppingcost));
}
}


class enterbtnaction implements ActionListener
{
public void actionPerformed(ActionEvent enterbtnaction)
{
double priceCalcu = 0 + p3.toppingASonions + p3.toppingAStomatos + p3.toppingASpepproni + p3.toppingASsausage;
double toppingcost = (0.75*priceCalcu)+p2.sizecost;
System.out.println();
System.out.println("Invoice");
System.out.println("--------------------------------------------------------------");
System.out.println("Customer Phone Number: " + p.phonenumberTField.getText() + "\tCustomer Name: " + p.name);
System.out.println("Pizza Size: "+p2.size+"\t$"+p2.sizecost);
System.out.println("Toppings: "+p3.toppingname1+" "+p3.toppingname2+" "+p3.toppingname3+" "+p3.toppingname4+"\t$"+(0.75*priceCalcu));
System.out.println("Total: \t$"+ toppingcost);
System.out.println(p2.deliveryorpickup);
}
}
}



























arrow
arrow
    全站熱搜
    創作者介紹
    創作者 archerdevil 的頭像
    archerdevil

    Archer, who doesn't know how to shoot...

    archerdevil 發表在 痞客邦 留言(0) 人氣()