gpt4 book ai didi

java - 冲突包 PersonManager App Java

转载 作者:行者123 更新时间:2023-11-30 10:08:54 25 4
gpt4 key购买 nike

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

运行:

Welcome to the Person Manager

Create customer or employee? (c/e): c

First name: Steve Last name: Trevor Customer number: M10963

You entered a new pkg8.pkg2.person.manager.Customer: Name: Steve Trevor CustomerNumber: M10963

Continue? (y/n):

现在一切正常,直到我收到“您输入了一个新的 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;
}

}

客户.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;
}
}

员工.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();
}
}

控制台.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;
}
}

我知道这是一个有冲突的包,我只是不知道如何修复它。任何帮助将不胜感激。

最佳答案

如果您阅读文档,那么您会发现 getName() 的 javadoc状态:

If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by The Java™ Language Specification.

JLS 13.1. The Form of a Binary说:

The class or interface must be named by its binary name, which must meet the following constraints:

  • The binary name of a top level type (§7.6) is its canonical name (§6.7).

JLS 6.7. Fully Qualified Names and Canonical Names说:

For every primitive type, named package, top level class, and top level interface, the canonical name is the same as the fully qualified name.

The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.


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

如果您不想这样,请调用 getSimpleName() .

关于java - 冲突包 PersonManager App Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53566840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com