未定义的代码的其余部分是好的,由于某种原因,我得到的UserInterface
类的错误。我是java新手,所以如果它是一个简单的错误,可以在几秒钟内解决,我提出这个问题,我提前道歉。我正在使用Java Eclipse Oxygen。如果您可以提供您的答案并解释您是如何做到的,那么我也可以在此过程中学习,而不仅仅是解决方案。Java的构造函数中的UserInterface
的错误是: 构造Bbat(String, String, int, Store, String, String, int)
未定义 - UserInterface.java
下面是Bbat类和用户界面类的代码。
package stage02;
public class Bbat extends SportsGoods {
private boolean fullTime;
private String studentType;
private double currentFees;
public Bbat() {
super (null,null,0,null);
fullTime = true;
studentType = null;
currentFees = 0.0;
}
public Bbat (String gN, String fN, int age, Store ei, boolean fT, String sT, double cF) {
super (gN, fN, age, ei);
fullTime = fT;
studentType = sT;
currentFees = cF;
}
public boolean getFullTime() {
return fullTime;
}
public String getStudentType() {
return studentType;
}
public double getCurrentFees() {
return currentFees;
}
public void setFullTime (boolean fT) {
fullTime = fT;
}
public void setStudentType (String sT) {
studentType = sT;
}
public void setCurrentFees (double cF) {
currentFees = cF;
}
public String toString() {
return super.toString() + "\nFull Time: " + (getFullTime()?"Yes":"No") +
"\nStudent Type: " + getStudentType() +
"\nCurrent Fees: $" + getCurrentFees() + " per hour\n";
}
}
用户界面:
package stage02;
import java.util.*;
import javax.swing.JOptionPane;
public class UserInterface {
public void begin() {
ArrayList <SportsGoods> people = new ArrayList <SportsGoods>();
boolean finished = false;
while (!finished) {
int selection = showMenu();
switch (selection) {
case 1:
people.add(addCbat());
break;
case 2:
people.add(addBbat());
break;
case 3:
JOptionPane.showMessageDialog(null, "Displaying the stock information ...", "Stock List", JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < people.size(); i++) {
JOptionPane.showMessageDialog(null, people.get(i), "Product Information", JOptionPane.PLAIN_MESSAGE);
}
JOptionPane.showMessageDialog(null, "There are " + people.size() + " record(s) in the list", "Total records", JOptionPane.PLAIN_MESSAGE);
break;
case 4:
finished = true;
JOptionPane.showMessageDialog(null, "*** Program Ended ***\n" +
"*** Thank you for using this program ***");
break;
default:
JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\n", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
public int showMenu() {
int selection = 0;
String stringSelection = JOptionPane.showInputDialog(
"******* MENU *******\n\n" +
"1. Add a new Cricket Bat\n" +
"2. Add a new BaseBall Bat\n" +
"3. Display all Details\n" +
"4. Exit Program\n\n" +
"Type the number of your selection, and click OK: ");
selection = Integer.parseInt(stringSelection.trim());
return selection;
}
public Cbat addCbat() {
String aName = JOptionPane.showInputDialog(null, "\nWhat is the Bat's model? ").trim();
String bName = JOptionPane.showInputDialog(null, "What is " + aName + "'s grip? ").trim();
int age = 0;
do {
String stringValidAge = JOptionPane.showInputDialog(null, "What is " + aName + "'s price? (no decimal places) ");
double doubleAge = Double.parseDouble(stringValidAge);
age = (int) doubleAge;
if (age <= 1 && age > 200) {
JOptionPane.showMessageDialog(null, "Error - cost must be greater than 1 to be sold by this store", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (age <= 1 && age > 200);
String eType = null;
do {
eType = JOptionPane.showInputDialog(null, "What is " + bName + "'s colour? ".trim());
if (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equals("Green")) {
JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\nBat grip colour must be Red, Blue, Yellow, Black, or Green",
"ERROR",
JOptionPane.ERROR_MESSAGE);
}
} while (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equalsIgnoreCase("Green"));
String eNum = JOptionPane.showInputDialog(null, "What is " + aName + "'s Product Code? ");
double nYE;
do {
String stringNYE = JOptionPane.showInputDialog(null, "How many " + aName + " are in stock?");
nYE = (int) Double.parseDouble(stringNYE);
if (nYE < 0) {
JOptionPane.showMessageDialog(null, "\n** Value must be zero or more **\n", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (nYE < 0);
String or = null;
or = JOptionPane.showInputDialog(null, "Name of store involved ? ");
String city = JOptionPane.showInputDialog(null, "City where " + or + " is ? ");
Store org = new Store(or, city);
Cbat s = new Cbat(aName, bName, age, org, eType, eNum, (int) nYE);
JOptionPane.showMessageDialog(null, "Thank you - bat added to the list", "Bat added", JOptionPane.PLAIN_MESSAGE);
return s;
}
public Bbat addBbat() {
String gName = JOptionPane.showInputDialog(null, "\nWhat is the Bat's model? ").trim();
String fName = JOptionPane.showInputDialog(null, "What is " + gName + "'s grip? ").trim();
int age = 0;
do {
String stringValidAge = JOptionPane.showInputDialog(null, "What is " + gName + "'s price? (no decimal places) ");
double doubleAge = Double.parseDouble(stringValidAge);
age = (int) doubleAge;
if (age <= 1 && age > 200) {
JOptionPane.showMessageDialog(null, "Error - cost must be greater than 1 to be sold by this store", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (age <= 1 && age > 200);
String eType = null;
do {
eType = JOptionPane.showInputDialog(null, "What is " + fName + "'s colour? ".trim());
if (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equals("Green")) {
JOptionPane.showMessageDialog(null, "\n** Invalid Selection **\nBat grip colour must be Red, Blue, Yellow, Black, or Green",
"ERROR",
JOptionPane.ERROR_MESSAGE);
}
} while (!eType.equals("Red") && !eType.equals("Blue") && !eType.equals("Yellow") && !eType.equals("Black") && !eType.equalsIgnoreCase("Green"));
String eNum = JOptionPane.showInputDialog(null, "What is " + gName + "'s Product Code? ");
double nYE;
do {
String stringNYE = JOptionPane.showInputDialog(null, "How many " + gName + " are in stock?");
nYE = (int) Double.parseDouble(stringNYE);
if (nYE < 0) {
JOptionPane.showMessageDialog(null, "\n** Value must be zero or more **\n", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (nYE < 0);
String or = null;
or = JOptionPane.showInputDialog(null, "Name of store involved ? ");
String city = JOptionPane.showInputDialog(null, "City where " + or + " is ? ");
Store org = new Store(or, city);
Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);
JOptionPane.showMessageDialog(null, "Thank you - bat added to the list", "Bat added", JOptionPane.PLAIN_MESSAGE);
return e;
}
}
您对这个问题的帮助将不胜感激。
我有一个构造函数在我Bbat类,所以我需要做什么来定义它是什么与用户界面?我猜可能用户界面中的代码是错误的? – Saada
首先决定在Bbat构造函数中需要哪些参数并相应地编写代码。阅读信息。我解释了。 –
因此,无论我在Bbat构造函数中有什么,我需要在用户界面中以相同的顺序具有完全相同的内容? – Saada