如何向 ArrayList 中的现有对象添加信息?
How do I add information to already existing objects in an ArrayList?
我需要帮助。
我有一个完整的 ArrayList,名称为 clients/customers。我想做的是编写一个代码,让它询问我的客户名称,然后我输入它,如果它找到它,就向客户添加一个 phone 号码。添加后,如果我想打印列表,phone 号码将位于该特定客户旁边。
我有 3 个 class。 1 是客户端 class(带有变量 - 名称、地址、编号)和 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");
}
}
}
这就是我的客户 class 的样子:
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;
}
}
在此先感谢各位的解答!
你可以在这里做:
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;
//}
}
然后在您的客户端 class 添加 addNumber 以添加号码列表:
public void addNumber(TelephoneNumber phoneNumber) {
telephoneNumbers.add(phoneNumber);
}
如果您想先将 phone 号码添加到特定客户,您必须在数据库中搜索该客户:
public Client searchByName(String name){// find one client and return client;}
然后设置数字:
public void addNumber(int number){ Client client=searchByName(); client.setNumber(number);}
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);
}
我已经从你的示例中开发了一小部分代码示例,我很难enter code here
编码电话号码,并且在主要方法中硬编码 2,因为在我的测试场景中输入用户输入作为 V3 .
客户端Class
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);
}
测试Class
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);
}
}
}
您可以使用同一种添加电话号码的方法。
我需要帮助。
我有一个完整的 ArrayList,名称为 clients/customers。我想做的是编写一个代码,让它询问我的客户名称,然后我输入它,如果它找到它,就向客户添加一个 phone 号码。添加后,如果我想打印列表,phone 号码将位于该特定客户旁边。
我有 3 个 class。 1 是客户端 class(带有变量 - 名称、地址、编号)和 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");
}
}
}
这就是我的客户 class 的样子:
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;
}
}
在此先感谢各位的解答!
你可以在这里做:
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;
//}
}
然后在您的客户端 class 添加 addNumber 以添加号码列表:
public void addNumber(TelephoneNumber phoneNumber) {
telephoneNumbers.add(phoneNumber);
}
如果您想先将 phone 号码添加到特定客户,您必须在数据库中搜索该客户:
public Client searchByName(String name){// find one client and return client;}
然后设置数字:
public void addNumber(int number){ Client client=searchByName(); client.setNumber(number);}
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);
}
我已经从你的示例中开发了一小部分代码示例,我很难enter code here
编码电话号码,并且在主要方法中硬编码 2,因为在我的测试场景中输入用户输入作为 V3 .
客户端Class
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);
}
测试Class
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);
}
}
}
您可以使用同一种添加电话号码的方法。