Using Java, write a GUI app using check boxes and combo boxes to create GUI obje
ID: 665654 • Letter: U
Question
Using Java, write a GUI app using check boxes and combo boxes to create GUI objects.
______________________________________
Cindy’s catering provides meals for parties and special events. Create an interactive GUI program that allows Cindy's Catering to better operate their business. The following data must be entered concerning the customer and the event:
• The customer’s name must be entered into a text field.
• The customer’s contact phone number into a text field.
• The number of guests invited to a Cindy catered event into a text field.
• The customer must choose one entrée from a group of at least four choices.
• The customer must choose up to two side dishes from a group of at least four choices.
• The customer must choose one desert from a group of at least three choices.
______________________________________
Display the cost of the event which is calculated as $35 per person.
To validate the input data, do the following:
• If the value entered for the number of guest is not numeric, set the event price to zero.
• If the user attempts to choose more than two side dishes, remove all the current side dish selections so that the user can start over.
______________________________________
After the final price for the dinner has been calculated, the event information must be saved to an output file so that Cindy’s Catering will have the information for the event. The items should be separated by spaces. If any item is not chosen, enter the word “none” for that item. The following fields must be written to the output file:
• the customer’s name
• the customer’s contact phone number
• the number of guests
• the one entrée chosen
• the two sides chosen
• the one desert chosen
______________________________________
Requirements
• The data input must be done with a GUI.
• The GUI must be defined in a programmer-defined class, named Catering.java, rather than in the main class.
• The main class, named Cindy.java, should instantiate the class Catering.
• The output file should be named Event.txt.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Chicken, Beef, Pork, Sea Food
// Potato, Rice, Salad, Beans
// Ice Cream, Apple Pie, Cup Cake
public class Try extends JApplet {
JLabel name,pass,num_of_guest,entree,side_dish,desert;
JTextField t_name,t_pass,t_num_of_guest;
JPanel panel;
JButton submit;
JRadioButton chicken;
JRadioButton beef;
JRadioButton pork;
JRadioButton sea;
JRadioButton potato;
JRadioButton rice;
JRadioButton salad;
JRadioButton beans;
JRadioButton ice_cream;
JRadioButton apple_pie;
JRadioButton cup_cake;
public Try(){
prepareGUI();
}
public static void main(String[] args){
Try swingControlDemo = new Try();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("TextDemo");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add contents to the window.
frame.add(new Try());
// Display the window.
frame.pack();
frame.setMinimumSize(frame.getPreferredSize());
frame.setVisible(true);
}
});
}
private void prepareGUI(){
setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
name = new JLabel("Enter the Name : ", JLabel.CENTER);
t_name = new JTextField(10);
pass = new JLabel("Enter the Phone Number : ",JLabel.CENTER);
t_pass = new JTextField(10);
num_of_guest = new JLabel("Enter the Number of Guest : ",JLabel.CENTER);
t_num_of_guest = new JTextField(10);
entree = new JLabel("Choose one of the following entree",JLabel.CENTER);
chicken = new JRadioButton("Chicken");
beef = new JRadioButton("Beef");
pork = new JRadioButton("Pork");
sea = new JRadioButton("Sea Food");
side_dish = new JLabel("Choose two of the following Side Dish",JLabel.CENTER);
potato = new JRadioButton("Potato");
rice = new JRadioButton("Rice");
salad = new JRadioButton("Salad");
beans = new JRadioButton("Beans");
desert = new JLabel("Choose one of the following desert",JLabel.CENTER);
ice_cream = new JRadioButton("Ice Cream");
apple_pie = new JRadioButton("Apple Pie");
cup_cake = new JRadioButton("Cup Cake");
submit = new JButton("Submit");
t_name.addActionListener(this);
name.addActionListener(this);
pass.addActionListener(this);
t_pass.addActionListener(this);
num_of_guest.addActionListener(this);
t_num_of_guest.addActionListener(this);
chicken.addActionListener(this);
beef.addActionListener(this);
pork.addActionListener(this);
sea.addActionListener(this);
potato.addActionListener(this);
rice.addActionListener(this);
salad.addActionListener(this);
beans.addActionListener(this);
ice_cream.addActionListener(this);
apple_pie.addActionListener(this);
cup_cake.addActionListener(this);
submit.addActionListener(this);
add(name); add(t_name); add(pass);
add(t_pass); add(num_of_guest); add(t_num_of_guest);
add(entree); add(chicken); add(beef); add(pork);
add(sea); add(potato); add(rice); add(salad); add(beans);
add(ice_cream); add(apple_pie); add(cup_cake);
}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if (str == "Submit"){
String u_name = t_name.getText();
String u_pass = t_pass.getText();
String u_num_of_guest = t_num_of_guest.getText();
int a = 0, b = 0, c = 0;
String o1,o2,o3;
if(chicken.isChecked()){
a++;
o1 = "c";
}
if(beef.isChecked()){
a++;
o1 = "b";
}
if(pork.isChecked()){
a++;
o1 = "p";
}
if(sea.isChecked()){
a++;
o1 = "s";
}
if(potato.isChecked()){
b++;
o2 = "p ";
}
if(rice.isChecked()){
b++;
o2 += " r";
}
if(salad.isChecked()){
b++;
o2 += "s";
}
if(beans.isChecked()){
b++;
o2 += " b";
}
if(ice_cream.isChecked()){
c++;
o3 += "i";
}
if(apple_pie.isChecked()){
c++;
o3 += "a";
}
if(cup_cake.isChecked()){
c++;
o3 += "c";
}
int cost = 0;
if (a > 1 || b > 2 || c > 1){
prepareGUI();
}
else{
cost = Integer.parseInt(u_num_of_guest)*35;
}
FileWriter sd=new FileWriter("output.txt");
sd.write("Name of the person is : "+u_name);
sd.write("Phone Number is : "+u_pass);
sd.write("Total number of guest is : "+u_num_of_guest);
sd.write("option choosen in entree : "+o1);
sd.write("Option choosen in Side Dish are : "+o2);
sd.write("Option choosen in Dessert : "+o3);
sd.write("Total Cost is : "+cost);
sd.close();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.