好的,所以我有一个任务,我们必须创建一个 java 程序,该程序询问用户联系人的姓名以及与联系人关联的可变数量的号码和号码类型(工作,voip ..)。toString() 方法假设打印联系人姓名和相关的号码和号码类型。
我是否正确地将我的数组从我的主要方法传递给 phoneBookEntry 构造函数?(我知道有编译错误等,但我想确保我正确传递了数组。另外,我的方法是否正确?
假设在此处相应地遵循 UML 表:
电话簿条目
- 名称:字符串
- 电话号码:字符串[]
电话类型:字符串 []
电话簿条目()
- PhoneBookEntry(名称:字符串):
- PhoneBookEntry(nam:字符串,数字:字符串 [],类型:字符串 []):
- 获取名称():字符串
- 设置名称(南:字符串):无效
- getPhoneNumber(类型:字符串):字符串
- setNumbers(数字:字符串 [],类型:字符串 []):无效
- toString() : 字符串
谢谢!
这是我到目前为止所拥有的:
package phonebookentry;
import java.awt.List;
import java.util.*;
public class PhoneBookEntry
{
private String name;
private String[] phoneNumbers,phoneTypes;
/**
* @param args
*/
public PhoneBookEntry()
{
}
public PhoneBookEntry(String nam, String[]numbers, String[]types)
{
phoneNumbers = numbers;
name = nam;
phoneTypes = types;
toString();
}
public String getName()
{
return name;
}
public void setName(String nam)
{
}
public String[] getPhoneNumber(String type)
{
return phoneTypes;
}
public void setNumbers(String[] numbers, String[] types)
{
this.phoneNumbers = numbers;
this.phoneTypes = types;
}
public String toString()
{
for (int index = 0; index < phoneNumbers.length; index ++ )
return System.out.println(nam, this.phoneNumbers, this.phoneTypes) ;
}
public static void main(String[] args)
{
String phoneN = "0";
ArrayList<String> Ptypes = new ArrayList<String>();
ArrayList<String> Pnumbers = new ArrayList<String>();
while (!phoneN.equals("-1"))
{
String phoneT;
Scanner input = new Scanner(System.in);
// Create an ArrayList to hold some names.
System.out.println("Phone number of Contact: (Input -1, to end)");
phoneN = input.nextLine();
if (phoneN.equals("-1"))
break;
Pnumbers.add(phoneN);
System.out.print("Type of phone number(mobile,home,VOIP,work,etc..):");
phoneT = input.nextLine();
Ptypes.add(phoneT);
}
String nam = "fas";
String[] types = Ptypes.toArray(new String[Ptypes.size()]);
String[] numbers = Pnumbers.toArray(new String[Pnumbers.size()]);
PhoneBookEntry passPhone = new PhoneBookEntry(nam,numbers,types);
passPhone.setNumbers(numbers,
types);
}
}