其余的代码很好,出于某种原因我在UserInterface
类中遇到错误。我是java的新手,所以我提前道歉,如果这是一个简单的错误,可以在几秒钟内解决,我问这个问题。我正在使用java Eclipse Oxygen。如果你能提供你的答案以及你如何做到的解释,那么我也可以在这个过程中学习,而不仅仅是解决方案。
错误是:
构造函数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;
}
}
非常感谢您对此问题的帮助。
答案 0 :(得分:0)
您正在调用未定义的错误构造函数Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);
,即参数类型不匹配。
所以,你的构造函数应该有以下定义:
public Bbat (String gN, String fN, int age, Store ei, String fT, String sT, double cF)
同样fullTime
的类型为boolean。但是您要将String
作为参数传递,因此要么fullTime
为String
,要么传递布尔代替eType
来自您调用的地方Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);
)