Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有继承和抽象类的Person tester应用程序_Java_Class_Inheritance - Fatal编程技术网

Java 具有继承和抽象类的Person tester应用程序

Java 具有继承和抽象类的Person tester应用程序,java,class,inheritance,Java,Class,Inheritance,我提前道歉,如果有更多关于这方面的帖子,我已经查看了相关的帖子,但找不到解决方案。我在一个名为Person Tester的项目中工作 系统会提示用户在创建客户(c)或员工(e)之间进行选择 接下来,系统会提示用户输入姓名、电子邮件地址以及客户号码(如果选择了客户)或社会保险号码(如果选择了员工) 一旦收集到正确的数据,信息将以以下格式打印到控制台: name: first last email: jondoe@fakemail.com social security nu

我提前道歉,如果有更多关于这方面的帖子,我已经查看了相关的帖子,但找不到解决方案。我在一个名为Person Tester的项目中工作

  • 系统会提示用户在创建客户(c)或员工(e)之间进行选择
  • 接下来,系统会提示用户输入姓名、电子邮件地址以及客户号码(如果选择了客户)或社会保险号码(如果选择了员工)

  • 一旦收集到正确的数据,信息将以以下格式打印到控制台:

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

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

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

    我不知道如何实现这一点,因为它从来没有在课堂上讨论过。我试图简单地编写以下代码:System.out.print(aPerson.toString),但我得到的只是Name:和Social Security number:的空值。我快发疯了,我已经为此工作了几个小时,并重新阅读了相关的文本至少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;
        }
    }
    
    公共类CH08PR82App{
    公共静态void main(字符串[]args){
    System.out.print(“欢迎使用Person Tester应用程序”);
    System.out.println();
    扫描仪sc=新的扫描仪(System.in);
    人与人之间;
    aPerson=新人();
    if(aPerson!=null){
    System.out.println(aPerson.toString());
    }
    字符串选择=“y”;
    while(choice.equalsIgnoreCase(“y”)){
    //提示用户输入客户或员工
    系统输出打印(“创建客户或员工(c/e):”;
    字符串userType=sc.nextLine();
    if(userType.equalsIgnoreCase(“c”)){
    stringfirstname=Validator.getStringInput(sc,“输入名字:”);
    String lastName=Validator.getStringInput(sc,“输入姓氏:”);
    字符串email=Validator.getStringInput(sc,“输入电子邮件地址:”);
    字符串custNumber=Validator.getStringInput(sc,“客户编号:”);
    //系统输出打印项次(客户编号);
    }else if(userType.equalsIgnoreCase(“e”)){
    stringfirstname=Validator.getStringInput(sc,“输入名字:”);
    String lastName=Validator.getStringInput(sc,“输入姓氏:”);
    字符串email=Validator.getStringInput(sc,“输入电子邮件地址:”);
    int empSoc=Validator.getInt(sc,“社会保险号:”);
    }
    choice=Validator.getStringContinue(sc,“继续”(y/n):);
    }
    }
    }
    抽象类人{
    私有字符串名;
    私有字符串lastName;
    私人字符串电子邮件;
    公众人士(){
    firstName=“”;
    lastName=“”;
    电子邮件=”;
    }
    公共字符串getFirstName(){
    返回名字;
    }
    public void setFirstName(字符串firstName){
    this.firstName=firstName;
    }
    公共字符串getLastName(){
    返回姓氏;
    }
    public void setLastName(字符串lastName){
    this.lastName=lastName;
    }
    公共字符串geteMail(){
    回复邮件;
    }
    公用电子邮件(字符串电子邮件){
    this.eMail=电子邮件;
    }
    @凌驾
    公共字符串toString(){
    返回“Name:+this.firstName+this.lastName+”\n“+”电子邮件:
    +这是一封电子邮件;
    }
    抽象字符串getDisplayText();
    }
    抽象类Customer扩展Person{
    私有字符串customerNumber;
    公众客户(){
    super.toString();
    customerNumber=“”;
    }
    public void setcustomerNumber(字符串customerNumber){
    this.customerNumber=customerNumber;
    }
    公共字符串getcustomerNumber(){
    返回客户编号;
    }
    @凌驾
    公共字符串toString(){
    返回super.toString()+“社会保险号:”+客户号
    +“\n”;
    }
    }
    抽象类Employee扩展了Person{
    私有字符串编号;
    公职人员(){
    super.toString();
    socNumber=“”;
    }
    公共无效设置序号(字符串序号){
    this.socNumber=socNumber;
    }
    公共字符串getsocNumber(){
    返回号码;
    }
    @凌驾
    公共字符串toString(){
    返回super.toString()+“社会保险号:”+socNumber
    +“\n”;
    }
    }
    类验证器{
    公共静态字符串getStringContinue(扫描程序sc,字符串提示){
    布尔值isValid=false;
    字符串s=“”;
    while(isValid==false){
    系统输出打印(提示);
    如果(sc.hasNext(“y”)){
    s=sc.nextLine();//读取整行
    isValid=true;
    }如果(sc.hasNext(“n”)){
    s=sc.nextLine();
    isValid=true;
    }否则{
    s=sc.nextLine();
    isValid=f
    
    public static void print(Person person) {
        System.out.println(person.toString());
    }
    
    public abstract String getDisplayText(Person person);
    
    // Employee
    @Override
    public String getDisplayText(Person person) {
        return ((Employee)person).toString();
    }
    
    // Customer
    @Override
    public String getDisplayText(Person person) {
        return ((Customer)person).toString();
    }
    
    Person john = new Customer();
    ... // Fill john with data
    Person.print(john);
    
    Person john = new Person(); // Build error
    
    Person john = new Employee(); // Will compile and run