[英]Passing Scanner object as a constructor parameter to the UserInterface class
[英]Java constructor is undefined in UserInterface class
其余的代码很好,由于某种原因,我在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;
}
}
您对这个问题的帮助将不胜感激。
你调用了错误的构造函数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
也是布尔类型。 但是你逝去的String
作为参数,因此无论是做fullTime
的String
或代替传递布尔eType
从正在调用( Bbat e = new Bbat(gName, fName, age, org, eType, eNum, (int) nYE);
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.