具有继承和抽象类的人员测试器应用程序

时间:2013-02-16 20:17:10

标签: java class inheritance

如果有更多关于此的帖子,我提前道歉,我查看了相关的帖子,但找不到解决方案。我正在开发一个名为Person Tester的项目,其中包含;

  1. 提示用户在创建客户(c)或员工(e)之间进行选择。
  2. 接下来,系统会提示用户输入姓名,电子邮件地址和客户编号(如果选择了客户)或社会安全号码(如果选择了员工)。

    < / LI>
  3. 收集到正确的数据后,信息将以这种格式打印到控制台:

        name: first last
        email: jondoe@fakemail.com
        social security number: XXXXXXXXXXX (if employee)
        customer number: XXXXXX  (if customer)
    
  4. 此程序处理继承和由员工类和客户类扩展的抽象人员类。另一个规定声明person类应该包含一个名为getDisplayText的抽象方法,该方法返回一个字符串。

  5. 这就是我的问题所在,这是我第一次使用抽象类。

    我的问题是为什么我只能在perosn类中打印toString方法来显示输入的所有用户数据,这些数据将我带到下一期,该程序需要有一个额外的组件(我引用我的任务):要将对象的数据打印到控制台,此应用程序应使用名为print的静态方法接受Person对象。

    我不知道如何实现这个,因为它从未在课堂上讨论过。我试图简单地编写以下代码:System.out.print(aPerson.toString)但我得到的只是Name的空白值:和社会安全号码:。我疯了,我已经在这个工作了几个小时,并重新阅读相关的文本至少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;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

我们先来看看打印方法:

public static void print(Person person) {
    System.out.println(person.toString());
}

现在你需要在Person类中使用抽象方法:

public abstract String getDisplayText(Person person);

您将在Employee和Customer类中覆盖:

// Employee
@Override
public String getDisplayText(Person person) {
    return ((Employee)person).toString();
}

// Customer
@Override
public String getDisplayText(Person person) {
    return ((Customer)person).toString();
}

他们希望您将方法设计为抽象,因为每个实现都不同 - 客户有一个客户ID,员工有一个Soc编号。

现在让我们解释一下静态和抽象方法:静态方法和抽象方法的区别在于抽象方法只是一个标题 - 实现在于子类。而静态方法意味着您可以在不首先创建对象的情况下调用它。

使用这种特定的静态方法非常简单。让我们说你有:

Person john = new Customer();
... // Fill john with data
Person.print(john);

在我看来,你对所有这些抽象和静态的东西有点困惑。抽象类是一个无法创建对象的类。在您的示例中,您有一个抽象类Person。这个类是抽象的,因为你需要一个客户或一个雇员(两者都是人,因此是Person类的继承),但你不想存储关于不属于这些人的信息。这也是你不能这样做的原因:

Person john = new Person(); // Build error

但你可以(而且应该)这样做:

Person john = new Employee(); // Will compile and run