我在大学里面是一个面向对象的编程课程,我必须使用Java来开始一个程序,该程序最终会在课程结束时整合GUI。对于这个项目的开始,我必须基本上使用消息框来设置客户如何订购子服务到他们的家。
这是我到目前为止所拥有的:
这是主要的课程
import javax.swing.*;
//Here is the main class
public class Subs {
public static void main(String[] args) {
// Begin Main Method
char letter;
String input;
String input1, input2, input3, input4, input6, input8;
int input5, input7;
int subL; //length of sub in inches
int cup; //size of drink in ounces
JFrame frame = new JFrame("Message");
JOptionPane.showMessageDialog(frame, "Welcome to Famous Subs! ");
input1 = JOptionPane.showInputDialog(frame, "Please Enter Your Name: ");
input2 = JOptionPane.showInputDialog(frame, "Please Enter Your Address: ");
do {
input3 = JOptionPane.showInputDialog(frame, "What kind of sub would "
+ "you like? " +
"\n Turkey Club" +
"\n Philly" +
"\n Meatball" +
"\n Chicken Parm");
input4 = JOptionPane.showInputDialog(frame, "What type of bread? " +
"\n White" +
"\n Wheat" +
"\n Rosemary" +
"\n Italian Herb");
subL = getValidLength();
input6 = JOptionPane.showInputDialog(frame, "What would you like to "
+ "to drink? " +
"\n Water" +
"\n Soda" +
"\n Juice");
cup = getValidCup ();
input8 = JOptionPane.showInputDialog(frame, "Do you wish to continue?\n "+
"'y' or 'Y' for YES\n"+
"'n' or 'N' for NO\n");
Order firstOrder = new Order(input1, input2, input3, input4, input6, subL, cup);
JOptionPane.showMessageDialog(frame, firstOrder.toString());
letter = input1.charAt(0);
}
while (letter == 'Y'|| letter == 'y');
System.exit(0);
}
private static int getValidLength()
{
int s;
String input5;
do{
input5 = JOptionPane.showInputDialog(null, "What size of sub do you wish "
+ "to order? "+
"\n 6 inch"+
"\n 12 inch");
s = Integer.parseInt(input5);
} while (!(s==6 || s==12));
return s;
}
private static int getValidCup()
{
int c;
String input8;
do{
input8 = JOptionPane.showInputDialog(null, "What size drink? " +
"\n Small 12oz." +
"\n Medium 24oz." +
"\n Large 36oz.");
c = Integer.parseInt(input8);
}
while (!(c==12 || c==24 || c==36));
return c;
}
}
这是我的子类
//This is the class for the order
public class Order {
//creating my variables
private String Customer;
private String Address;
private String name;
private String bread;
private String drink;
private int length; //in inches
private int size; //in ounces
private double SubPrice;
private double DrinkPrice;
private double total;
//blank constructor
public Order(){}
//Create a constructor to hold variables
public Order (String Customer, String Address, String name, String bread, String drink, int subL, int cup){
this.Customer = Customer;
this.Address = Address;
this.name = name;
this.bread = bread;
this.drink = drink;
subL = length;
cup = size;
}
//create the getters and setters for the variables
public String getCustomer(){
return Customer;
}
public void setCustomer(String Customer){
this.Customer = Customer;
}
public String getAddress(){
return Address;
}
public void setAddress(String Address){
this.Address = Address;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getBread(){
return bread;
}
public void setBread(String bread){
this.bread = bread;
}
public String getDrink(){
return drink;
}
public void setDrink(String drink){
this.drink = drink;
}
public int getLength(){
return length;
}
public void setLength (int length){
this.length = length;
}
public int getSize(){
return size;
}
public void setSize (int size){
this.size = size;
}
public void setSubPrice (int subL, double SubPrice){
if (subL == 6)
SubPrice = 7.95;
else if (subL == 12)
SubPrice = 12.75;
}
public void setDrinkPrice(int cup, double DrinkPrice){
if (cup == 12)
DrinkPrice = 2.00;
else if (cup == 24)
DrinkPrice = 4.00;
else if (cup == 36);
DrinkPrice = 6.00;
}
public void setTotal(){
total = SubPrice + DrinkPrice;
}
@Override
public String toString(){
String grandOrder = "Greetings " + Customer +
"\nHere is your order: " +
"\n" + name +
"\n" + bread +
"\n" + drink +
"\nThe length of your sub is: " + length +
"\nThe size of your drink is: " + size +
"\nThe Price for your sub is: " + SubPrice +
"\nThe Price for your drink is: " + DrinkPrice +
"\nHere is your total: $" + calculateTotal(DrinkPrice, SubPrice) +
"\nThis will be delivered to: " + Address;
return grandOrder;
}
}
一切运行得很好,除了显示的最后一个框将所有字符串字段返回为null,将int和double变量返回为0或0.0。
如何返回用户在每个对话框中输入的值?此外,如何让客户的姓名和地址显示在最终屏幕上?感谢。
答案 0 :(得分:0)
您永远不会初始化订单的字段。您调用没有参数public Order(){}的构造函数。所以你看到了字段的默认值。
你应该做什么:
请确保在变量中保留客户端名称的值。 (input = JOptionPane.showInputDialog(frame,“Please Enter your Name:”);)
请确保在“input = JOptionPane.showInputDialog(frame,”what of sub sub“)之后保留变量用户的输入 +“你喜欢?”......“。
获取用户的所有输入后,创建传递用户值的Order对象 订购firstOrder =新订单(名称,面包,饮料,int subL,杯子);
使用以下参数更改构造函数: public order(String name,String bread,String drink,int subL,int cup)。您不应该传递subPrice和drinkPrice,因为Order类已经知道这些值(请参阅setdrinkPrice())。您可以根据int cup确定杯子的价格。顺便说一句,你能改变方法的名称吗?类似于setDrinkPrice()。
你永远不会调用setDrinkPrice()和setsubPrice()(应该是setSubPrice()。你可以在计算总数时这样做。
从这些更改开始,如果您有更多问题,请询问。
答案 1 :(得分:0)
好的,这里发生了一些事情。
Order firstOrder;
firstOrder = new Order();
可以写成
Order firstOrder = new Order();
没有必要在两条线上做到这一点。但是,更重要的是,你没有给它任何参数,所以Java将它链接到空构造函数(那个没有分配任何东西的构造函数。)你所拥有的所有优秀的构造函数代码都不是'被叫。
要做到这一点,你需要对你一直指定的那些input
字段做一些事情(此时你只是忽略它们并写下它们);具体来说,您应该将它们存储在局部变量中,然后将它们传递给构造函数,如:
firstOrder = new Order(arg1, arg2, ...)
此外,Order
不是子类;它没有扩展任何东西。 (Object
除外,但我们通常不会将某些内容称为子类。
答案 2 :(得分:0)
input = JOptionPane.showInputDialog(frame, "Please Enter Your Name: ");
input = JOptionPane.showInputDialog(frame, "Please Enter Your Address: ");
在上面的第一行,您将获得客户的姓名。然后你打电话给第二行,抛弃顾客的名字而不保存在任何地方。