我需要帮助。
我有一个完整的ArrayList,其中包含客户/客户的名称。我想要做的是编写一个代码,以便它询问我的客户端名称然后我输入它,如果它发现它添加一个电话号码给客户端。一旦添加,如果我想打印出列表,电话号码将在该特定客户端旁边。
我有3节课。 1是客户端类(带有变量 - 名称,地址,编号)和JobManager(整个测试程序,其中包括添加客户端和删除客户端等方法)。
这是我添加客户端的方式:
public static void addClient() {
System.out.println("Adding a client: \n");
System.out.print("What is the name of the new client?: ");
String name = keyboard.nextLine();
System.out.println();
if (name.length() == 0) {
boolean invalid = true;
while (invalid) {
System.out.println("You cannot enter a blank name for the client. ");
System.out.print("Please enter a valid name: ");
name = keyboard.nextLine();
if (name.length() > 0) {
invalid = false;
}
}
}
System.out.print("What is the client's address?: ");
String address = keyboard.nextLine();
boolean isDuplicate = false;
for (int i = 0; i < clientList.size(); i++) {
String listName = clientList.get(i).getName();
String listAddress = clientList.get(i).getAddress();
if (listName.equalsIgnoreCase(name)
&& listAddress.equalsIgnoreCase(address)) {
isDuplicate = true;
if (listName.equalsIgnoreCase(name)) {
isDuplicate = true;
}
}
if (isDuplicate) {
System.out.println();
System.out.println("Error, that client already exists! \n");
isDuplicate = false;
} else {
Client client = new Client(name, address);
clientList.add(client);
System.out.println();
System.out.println("Client has been added. \n");
}
}
}
这就是我的客户端类:
public class Client {
private ArrayList<JobManager> jobManager;
private ArrayList<TelephoneNumber> telephoneNumbers = new ArrayList<>();
private String name;
private String address;
private static int number;
public Client(String name,String address) {
this.name = name;
this.address = address;
}
// public void createNumber() {
// TelephoneNumber teleNumber = new TelephoneNumber(number);
// telephoneNumbers.add(teleNumber);
//}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String toString() {
String result;
result = name + "\n" + address;
result = (this.getName() + "\t" + this.getAddress());
return result;
}
}
先谢谢你的回答!
答案 0 :(得分:0)
你可以在这里完成:
if (listName.equalsIgnoreCase(name)
&& listAddress.equalsIgnoreCase(address)) {
isDuplicate = true;
do {
TelephoneNumber phoneNumber = ..;//ask user for phone number and keep repeating until user enters 0 as one user can have multiple numbers
clientList.get(i).addNumber(phoneNumber);
} while (!phoneNumber.getNumber().equals("0"))
//if (listName.equalsIgnoreCase(name)) { //removed duplicate conditions
// isDuplicate = true;
//}
}
在您的Client类中添加addNumber以将数字列表添加为:
public void addNumber(TelephoneNumber phoneNumber) {
telephoneNumbers.add(phoneNumber);
}
答案 1 :(得分:0)
如果您想先将电话号码添加到特定客户端,您必须在数据库中搜索该客户端:
public Client searchByName(String name){// find one client and return client;}
然后设置号码:
public void addNumber(int number){ Client client=searchByName(); client.setNumber(number);}
答案 2 :(得分:0)
public void addNumberToClients(String clientName, TelephoneNumber number) {
clientList.stream()
.filter(client -> client.getName().equals(clientName))
.forEach(client -> client.addNumber(number);
}
这会将数字添加到具有给定名称的所有客户端。要将其添加到一个,请将.forEach()
更改为.findFirst().ifPresent()
然后将方法添加到Client
public void addNumber(TelephoneNumber number) {
telephoneNumbers.add(number);
}
答案 3 :(得分:0)
我已经从您的示例中开发了一小段代码示例,其中我很难enter code here
编码电话号码,并且在主方法中我正在硬编码2,因为我在测试场景中输入用户输入为V3。 / p>
客户等级
public class Client {
private String name;
private List<String> telephoneNumber;
Client(String name)
{
this.name = name;
}
public List<String> getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(List<String> telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object obj)
{
Client c = (Client)obj;
return this.name.equals(c.name);
}
<强>识别TestClass 强>
public class HelloWorld{
Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) throws Exception {
List<Client> clientList = new ArrayList<Client>();
Client c1 = new Client("V1");
Client c2 = new Client("V2");
Client c3 = new Client("V3");
Client c4 = new Client("V4");
clientList.add(c1);
clientList.add(c2);
clientList.add(c3);
clientList.add(c4);
HelloWorld h = new HelloWorld();
h.addTelephone(clientList);
Client cTest = clientList.get(2);
System.out.println(cTest.getTelephoneNumber());
}
public void addTelephone(List<Client> clientList)
{
System.out.print("What is the name of the new client?: ");
String name = keyboard.nextLine();
Client c = new Client(name);
if(clientList.contains(c))
{//Client Already Exist
int i = clientList.indexOf(c);
Client c1 = clientList.get(i);
List<String> telNumber = new ArrayList<String>();
getTelephoneNumbers(telNumber);
c1.setTelephoneNumber(telNumber);
}
}
}
public void getTelephoneNumbers(List<String> telephone)
{
boolean isExit = false;
while(!isExit)
{
System.out.println("Enter Telephone Number or 0 for exiting : ");
String telp = keyboard.nextLine();
if("0".equalsIgnoreCase(telp)) {
System.out.println("Exiting");
isExit = true;
}
else {
telephone.add(telp);
}
}
}
您可以使用相同类型的addTelephone数字方法。