如果有更多关于此的帖子,我提前道歉,我已经查看了看起来相关但找不到解决方案的帖子。我在一个名为 Person Tester 的项目中工作;
- 提示用户在创建客户 (c) 或员工 (e) 之间进行选择。
接下来,提示用户输入名字、姓氏、电子邮件地址和客户编号(如果选择了客户)或社会保险号(如果选择了员工)。
一旦收集到正确的数据,信息就会以这种格式打印到控制台:
name: first last email: jondoe@fakemail.com social security number: XXXXXXXXXXX (if employee) customer number: XXXXXX (if customer)
该程序处理继承和一个由员工类和客户类扩展的抽象人员类。另一项规定是,person 类应包含一个名为 getDisplayText 的抽象方法,该方法返回一个字符串。
这是我的问题所在,这是我第一次使用抽象类。
我的问题是为什么我不能简单地在我的 perosn 类中打印 toString 方法来显示输入的所有用户数据,这使我进入下一个问题,程序需要一个额外的组件(我引用我的作业) :要将对象的数据打印到控制台,此应用程序应使用名为 print 的静态方法,该方法接受 Person 对象。
我不确定如何实现这一点,因为它从未在课堂上讨论过。我试图简单地编写以下代码: System.out.print(aPerson.toString) 但我得到的只是名称的空白值:和社会安全号码:。我疯了,我已经为此工作了几个小时,并且至少重新阅读了相关文本 4 次。这是我最后的手段。请引导我朝着正确的方向前进,我不介意长时间工作来做到这一点。
我已经编写了大部分应用程序,现在只是停留在如何将数据打印到控制台上。感谢任何和所有建议,这是我的代码:
public class CH08PR82App {
public static void main(String[] args) {
System.out.print("Welcome to the Person Tester Application");
System.out.println();
Scanner sc = new Scanner(System.in);
Person aPerson;
aPerson = new Person();
if (aPerson != null) {
System.out.println(aPerson.toString());
}
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
//prompt user to enter customer or employee
System.out.print("Create customer or employee (c/e): ");
String userType = sc.nextLine();
if (userType.equalsIgnoreCase("c")) {
String firstName = Validator.getStringInput(sc, "Enter first name: ");
String lastName = Validator.getStringInput(sc, "Enter last name: ");
String email = Validator.getStringInput(sc, "Enter email address: ");
String custNumber = Validator.getStringInput(sc, "Customer number: ");
//System.out.println(custNumber);
} else if (userType.equalsIgnoreCase("e")) {
String firstName = Validator.getStringInput(sc, "Enter first name: ");
String lastName = Validator.getStringInput(sc, "Enter last name: ");
String email = Validator.getStringInput(sc, "Enter email address: ");
int empSoc = Validator.getInt(sc, "Social security number: ");
}
choice = Validator.getStringContinue(sc, "Continue? (y/n): ");
}
}
}
abstract class Person {
private String firstName;
private String lastName;
private String eMail;
public Person() {
firstName = "";
lastName = "";
eMail = "";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String geteMail() {
return eMail;
}
public void seteMail(String eMail) {
this.eMail = eMail;
}
@Override
public String toString() {
return "Name: " + this.firstName + this.lastName + "\n" + "E-mail: "
+ this.eMail;
}
abstract String getDisplayText();
}
abstract class Customer extends Person {
private String customerNumber;
public Customer() {
super.toString();
customerNumber = "";
}
public void setcustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
public String getcustomerNumber() {
return customerNumber;
}
@Override
public String toString() {
return super.toString() + "Social Security number: " + customerNumber
+ "\n";
}
}
abstract class Employee extends Person {
private String socNumber;
public Employee() {
super.toString();
socNumber = "";
}
public void setsocNumber(String socNumber) {
this.socNumber = socNumber;
}
public String getsocNumber() {
return socNumber;
}
@Override
public String toString() {
return super.toString() + "Social Security number: " + socNumber
+ "\n";
}
}
class Validator {
public static String getStringContinue(Scanner sc, String prompt) {
boolean isValid = false;
String s = "";
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNext("y")) {
s = sc.nextLine(); // read entire line
isValid = true;
} else if (sc.hasNext("n")) {
s = sc.nextLine();
isValid = true;
} else {
s = sc.nextLine();
isValid = false;
System.out.println("Error! Invalid string value. Try again.");
}
}
return s;
}
public static String getStringInput(Scanner sc, String prompt) {
boolean isValid = false;
String s = "";
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNext()) {
s = sc.nextLine(); // read entire line
isValid = true;
} else {
System.out.println("Error! Invalid string value. Try again.");
}
}
return s;
}
public static int getInt(Scanner sc, String prompt) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(Scanner sc, String prompt, int min, int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(sc, prompt);
if (i <= min) {
System.out.println("Error! Number must be greater than " + min
+ ".");
} else if (i >= max) {
System.out.println("Error! Number must be less than " + max
+ ".");
} else {
isValid = true;
}
}
return i;
}
public static double getDouble(Scanner sc, String prompt) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(Scanner sc, String prompt, double min,
double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(sc, prompt);
if (d <= min) {
System.out.println("Error! Number must be greater than " + min
+ ".");
} else if (d >= max) {
System.out.println("Error! Number must be less than " + max
+ ".");
} else {
isValid = true;
}
}
return d;
}
}