dhunter214
October 28th, 2009, 07:23 AM
This is my homework and I've done it but my teacher said that it is not the real way of doing it although it produces the correct output ... please help me to fix it
Scenario
The owner of the ‘Lite Lunch Lounge’ aims to sell daily a reasonably-priced three-course lunch to the local student community. To save costs, each day there is only a single choice of starter, main course and dessert. As with most establishments of its kind, the largest profit comes from selling the main courses. Unfortunately, the local students being frugal, hard-working and cost-conscious tend to only order a starter and/or a dessert.
As an incentive to order main courses, the owner has decided – where customers order a main course – to provide, free of charge:
• a soft drink if they also order a starter;
• coffee if they also order a dessert;
• both a soft drink and a coffee if all 3 courses are ordered.
If customers do not order a main course or if they only order a main course, then there are no drinks provided.
You are to write a program that advises staff what to provide. Using dialog boxes, the program should ask what course(s) the customer is ordering and output what is to be provided. The choices are:
• starter
• main course
• dessert
• free soft drink (only if both a starter & a main course have been ordered)
• free coffee (only of both a dessert & a main course have been ordered)
You may wish to use boolean (primitive) variables to indicate whether each course is required. Remember that you can use the wrapper class Boolean to convert Strings “true” & “false” obtained from input dialogs into their boolean equivalents.
e.g. String input = JOptionPane (null, “Enter true or false”, “Boolean example”, JOptionPane.PLAIN_MESSAGE);
boolean result = Boolean.parseBoolean (input);
Must use dialog boxes(JOptionPane) to ask for order and output order.
import javax.swing.JOptionPane;
public class Assignment4
{
public static void main(String[] args)
{
String a = "";
String b = "";
String c = "";
String d = "";
String e = "";
String starter = JOptionPane.showInputDialog(null,"Do you want a starter?", "Starter, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean starterYes = Boolean.parseBoolean (starter);
if(starterYes)
{
a = "a starter";
b = "a soft drink";
}
String mainCourse = JOptionPane.showInputDialog(null,"Do you want a main course?", "Main course, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean mainCourseYes = Boolean.parseBoolean (mainCourse);
if(mainCourseYes)
{
c = "a main course";
}
String dessert = JOptionPane.showInputDialog(null,"Do you want a dessert?", "Dessert, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean dessertYes = Boolean.parseBoolean (dessert);
if(dessertYes)
{
d = "a coffee";
e = "a dessert";
}
if(a == "a starter" && c == "a main course")
{
if(e == "a dessert" && c == "a main course")
{
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else;
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + b + "\n" + c, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else if(e == "a dessert" && c == "a main course")
{
JOptionPane.showMessageDialog(null, "The order is for:\n" + "\n" + c + "\n" + d + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else;
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
}
}
Scenario
The owner of the ‘Lite Lunch Lounge’ aims to sell daily a reasonably-priced three-course lunch to the local student community. To save costs, each day there is only a single choice of starter, main course and dessert. As with most establishments of its kind, the largest profit comes from selling the main courses. Unfortunately, the local students being frugal, hard-working and cost-conscious tend to only order a starter and/or a dessert.
As an incentive to order main courses, the owner has decided – where customers order a main course – to provide, free of charge:
• a soft drink if they also order a starter;
• coffee if they also order a dessert;
• both a soft drink and a coffee if all 3 courses are ordered.
If customers do not order a main course or if they only order a main course, then there are no drinks provided.
You are to write a program that advises staff what to provide. Using dialog boxes, the program should ask what course(s) the customer is ordering and output what is to be provided. The choices are:
• starter
• main course
• dessert
• free soft drink (only if both a starter & a main course have been ordered)
• free coffee (only of both a dessert & a main course have been ordered)
You may wish to use boolean (primitive) variables to indicate whether each course is required. Remember that you can use the wrapper class Boolean to convert Strings “true” & “false” obtained from input dialogs into their boolean equivalents.
e.g. String input = JOptionPane (null, “Enter true or false”, “Boolean example”, JOptionPane.PLAIN_MESSAGE);
boolean result = Boolean.parseBoolean (input);
Must use dialog boxes(JOptionPane) to ask for order and output order.
import javax.swing.JOptionPane;
public class Assignment4
{
public static void main(String[] args)
{
String a = "";
String b = "";
String c = "";
String d = "";
String e = "";
String starter = JOptionPane.showInputDialog(null,"Do you want a starter?", "Starter, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean starterYes = Boolean.parseBoolean (starter);
if(starterYes)
{
a = "a starter";
b = "a soft drink";
}
String mainCourse = JOptionPane.showInputDialog(null,"Do you want a main course?", "Main course, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean mainCourseYes = Boolean.parseBoolean (mainCourse);
if(mainCourseYes)
{
c = "a main course";
}
String dessert = JOptionPane.showInputDialog(null,"Do you want a dessert?", "Dessert, (true / false?)", JOptionPane.QUESTION_MESSAGE);
boolean dessertYes = Boolean.parseBoolean (dessert);
if(dessertYes)
{
d = "a coffee";
e = "a dessert";
}
if(a == "a starter" && c == "a main course")
{
if(e == "a dessert" && c == "a main course")
{
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else;
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + b + "\n" + c, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else if(e == "a dessert" && c == "a main course")
{
JOptionPane.showMessageDialog(null, "The order is for:\n" + "\n" + c + "\n" + d + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else;
JOptionPane.showMessageDialog(null, "The order is for:\n" + a + "\n" + e, "Your order", JOptionPane.INFORMATION_MESSAGE);
}
}