为了我的任务,我创建了一个名为Agency的类,它将存储有关人才代理的数据,并创建了所需的属性。我还获得了一个数据文件需要从中读取。但首先我要测试我的类。我可以使用getter和setter来显示我想要的输出,但现在我想创建对象Agency的新实例并添加其详细信息,但我收到错误消息,说找不到合适的构造函数。我已经将设置与我的构造函数匹配,或者至少看不出我的错误在哪里。谢谢任何帮助。
public class Agency {
//Attributes for class Agency
private String name;
private String[] address;
private String[] adminStaff;
private int phoneNumber;
private String type;
/**
*Constructor that creates object Agency
*/
public Agency() {
}
/**
* Constructor that creates object Agency with values
* @param name
* @param address
* @param adminStaff
* @param phoneNumber
* @param type
*/
public Agency(String name, String[] address, String[] adminStaff, int phoneNumber, String type) {
this.name = name;
this.address = address;
this.adminStaff = adminStaff;
this.phoneNumber = phoneNumber;
this.type = type;
}
/**
*
* @return
*/
public String getName() {
return name;
}
/**
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
*/
public String[] getAddress() {
return address;
}
/**
*
* @return
*/
public String[] getAdminStaff() {
return adminStaff;
}
/**
*
* @return
*/
public int getPhoneNumber() {
return phoneNumber;
}
/**
*
* @param phoneNumber
*/
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
*
* @return
*/
public String getType() {
return type;
}
/**
*
* @param type
*/
public void setType(String type) {
this.type = type;
}
private String getArrayAsString(String[] a) {
String result = "";
/*for(int i = 0; i<a.length; i++)
{
result += a[i] +" ";
}*/
for(String s: a)
{
result += s + " ";
}
result = result.trim();
return result;
}
private String[] getStringAsArray(String a) {
String[] result = a.split(":");
return result;
}
public void setAddress(String Address) {
this.address = getStringAsArray(Address);
}
public void setAddress(String[] Address) {
this.address = Address;
}
public String getAddressAsString() {
return getArrayAsString(address);
}
public void setAdminStaff(String adminStaff) {
this.adminStaff = getStringAsArray(adminStaff);
}
public void setAdminStaff(String[] adminStaff) {
this.adminStaff = adminStaff;
}
public String getAdminStaffAsString() {
return getArrayAsString(adminStaff);
}
@Override
public String toString(){
return name +"\t"+ getAddressAsString() +"\t"+
getAdminStaffAsString() +"\t"+ phoneNumber +"\t"+ type + "\n";
}
}
我的主类
public class AgencyTest {
public static void main(String[] args) {
/*a.setName("Potters Talent Agency");
a.setAddress("126-182 Ashwood Road:Potters Bar:Hertfordshire EN6 2:UK");
a.setAdminStaff("Megan Speagle:Daron Spilman");
a.setPhoneNumber(2598052);
a.setType("All");
System.out.println(a.getName());
System.out.println(a.getAddressAsString());
System.out.println(a.getAdminStaffAsString());
System.out.println(a.getPhoneNumber());
System.out.println(a.getType());*/
Agency a = new Agency("Potters Talent Agency, 126-182 Ashwood Rd: Potters Bar: Hertfordshire EN6 2: UK, "
+ "Megan Speegle:Daron Spilman:Leonel Striegel:Hubert Tesoro:Nathanial Pompey, 2598052, All");
System.out.println(a);
}
注释掉的代码行是有效的,但是如果我尝试从“Agency a = new Agency()”一行开始全部执行,它就不起作用。
请帮助我,我是个新手,可能使用了错误的术语,所以不确定我要做的事情是否有意义。
好的,我添加了一个新的构造函数,它将其作为一个字符串来处理,但电话号码的整数值出现了错误。以下是代码:
`public Agency(String csvString) {
String[] attributes =csvString.split(",");
int i = 0;
for(String s : attributes)
{
switch(i)
{
case 0:
this.name = s;
break;
case 1:
this.address = getStringAsArray(s);
break;
case 2:
this.adminStaff = getStringAsArray(s);
break;
case 3:
this.phoneNumber = getIntAsString(s);
break;
case 4:
this.type = s;
break;
}
我尝试将其转换为字符串,但似乎不起作用。 更多代码:
private String getIntAsString(int a){
String result = Integer.toString(a);
return result;
}
public String getPhoneNumberAsString() {
return getIntAsString(phoneNumber);
}
public void setPhoneNumber(int phoneNumber){
this.phoneNumber = phoneNumber;
}
i++;
}`