有冲突的软件包PersonManager App Java

时间:2018-12-01 00:57:55

标签: java

所以我的问题是,当我试图为我的Person Manager应用获取结果时,由于某种原因,经过一堆反复试验后,我得到了这样的结果

运行:

  

欢迎来到人事经理

     

创建客户还是员工? (c / e):c

     

名字:Steve姓:Trevor客户编号:M10963

     

您输入了一个新的pkg8.pkg2.person.manager.Customer:名称:Steve   Trevor客户编号:M10963

     

继续吗? (是/否):

现在一切正常,直到出现“您输入了新的pkg8.pkg2.person.manager.Customer:” pkg8.pkg2.person.manager。不应该在那里。

这是我的代码

PersonManager.java

package pkg8.pkg2.person.manager;

/**
*
* @author Zachary
*/
public class PersonManager {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Welcome to the Person Manager");
    System.out.println("");
    String choice = "y";
    while (choice.equalsIgnoreCase("y")){
        String type = Console.getString("Create customer or employee? (c/e): " , "c", "e");
        System.out.println("");

        String firstName = Console.getString("First name: ");
        String lastName = Console.getString("Last name: ");

        Person person;
        if(type.equalsIgnoreCase("c")){
            String customerNumber = Console.getString("Customer number: ");
            person = new Customer(firstName, lastName, customerNumber);

        }else{
            String ssn = Console.getString("SSN: ");
            person = new Employee(firstName, lastName, ssn);             
        }
        Class c = person.getClass();

        System.out.println("");
        System.out.println("You entered a new " + c.getName() + ":");
        System.out.println(person.toString());
        System.out.println("");




        System.out.println("");            
        choice = Console.getString("Continue? (y/n): ", "y", "n");
        System.out.println();
     }
 }

}

Person.java

package pkg8.pkg2.person.manager;

/**
*
 * @author Zachary
*/
public class Person {

private String firstName;
private String lastName;


public Person(String first, String last){
firstName = first;
lastName = last;

}

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;
}

@Override
public String toString(){
return "Name: " + firstName + " " + lastName;
}

}

Customer.java

package pkg8.pkg2.person.manager;

/**
 *
 * @author Zachary
*/
public class Customer extends Person {

private String customerNumber;


public Customer(String first, String last, String number) {
    super(first, last);
    this.customerNumber = number;
}

public void setCustomerNumber(String number){
    this.customerNumber = number;
}

public String getCustomerNumber(){
    return customerNumber;
}

@Override
public String toString(){
    String name = super.toString();
    return name + "\n" + "CustomerNumber: " + customerNumber;
}
}

Employee.java

package pkg8.pkg2.person.manager;

/**
*
 * @author Zachary
*/
public class Employee extends Person {

private String ssn;

public Employee(String first, String last, String ssn){
super(first, last);
this.ssn = ssn;
}

public String getSsn(){
return "xxx-xx-" + ssn.substring(ssn.length() - 4);    
}

public void setSsn(String ssn){
this.ssn = ssn;
}
@Override
public String toString() {
   String name = super.toString();
   return name + "\n" + "SSN: " + getSsn();
}
}

Console.java

package pkg8.pkg2.person.manager;


import java.util.*;

/**
*
* @author Zachary
*/
public class Console {
private static Scanner sc = new Scanner(System.in);

public static String getString(String prompt) {
    String s = "";
    boolean isValid = false;
    while (!isValid) {
        System.out.print(prompt);
        s = sc.nextLine();
        if (s.equals("")) {
            System.out.println("Error! This entry is required. Try again.");
        } else {
            isValid = true;
        }
    }
    return s;
}

public static String getString(String prompt, String s1, String s2) {
    String s = "";
    boolean isValid = false;
    while (!isValid) {
        s = getString(prompt);
        if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2)) {
            System.out.println("Error! Entry must be '" + s1 + "' or '" +
                    s2 + "'. Try again.");
        } else {
            isValid = true;
        }
    }
    return s;
}

public static int getInt(String prompt) {
    int i = 0;
    boolean isValid = false;
    while (!isValid) {
        System.out.print(prompt);
        if (sc.hasNextInt()) {
            i = sc.nextInt();
            isValid = true;
        } else {
            System.out.println("Error! Invalid integer. Try again.");
        }
        sc.nextLine(); // discard any other data entered on the line
    }
    return i;
}

public static int getInt(String prompt, int min, int max) {
    int i = 0;
    boolean isValid = false;
    while (!isValid) {
        i = getInt(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(String prompt) {
    double d = 0;
    boolean isValid = false;
    while (!isValid) {
        System.out.print(prompt);
        if (sc.hasNextDouble()) {
            d = sc.nextDouble();
            isValid = true;
        } else {
            System.out.println("Error! Invalid number. Try again.");
        }
        sc.nextLine(); // discard any other data entered on the line
    }
    return d;
}

public static double getDouble(String prompt, double min, double max) {
    double d = 0;
    boolean isValid = false;
    while (!isValid) {
        d = getDouble(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 :(得分:1)

如果阅读文档,则会发现getName()的Javadoc指出:

  

如果该类对象表示的引用类型不是数组类型,则返回该类的 二进制名称 ,具体由 Java™指定语言规范

JLS 13.1. The Form of a Binary说:

  

类或接口必须使用其 二进制名称 命名,该名称必须满足以下约束:

     
      
  • 顶级类型(§7.6)的二进制名称是其 规范名称 §6.7)。
  •   

JLS 6.7. Fully Qualified Names and Canonical Names说:

  

对于每种原始类型,命名包,顶级类和顶级接口, 规范名称 完全限定的名称相同名称

  在命名包中声明的顶级类或顶级接口的 完全限定名称 由该包的完全限定名称组成,后跟“。”,然后是类的简单名称或接口。


总结: getName()返回该类的完全限定名称。

如果您不想这样做,请致电getSimpleName()