提问人:koolaids1 提问时间:12/7/2022 最后编辑:koolaids1 更新时间:12/7/2022 访问量:22
[JAVA]防止将特定的重复元素添加到自定义对象 arraylist
[JAVA]preventing specific duplicate elements from being added to a custom object arraylist
问:
因此,假设我有一个名为 Customer 的类,其中包含名字和姓氏以及用户名和密码 我还有另一个类,叫做 Bankaccounts,带有帐号和 Banlance; 我有一个客户数组列表; 我有一个需要读取的 TXT 文件 它有时包含相同的客户
我如何使它读取时,如果(名字,姓氏,用户名)相同 该程序不会将客户添加到列表中,而只会将该帐户添加到 列表中的现有客户。
对不起,如果我的整个代码一团糟,但我在每个地方都迷失了方向,我只想把这部分(标题)弄对,如果我的问题不合适,请告诉我,我会尽力编辑它
Bob, Mike, bobbmike, Ball!36Hi, 109001, Saving, 140.23, 0.05//good
Bob, Mike, bobbmike, Ball!36Hi, 109002, BasicChecking, 555.23//good
Mouse, Mickey, mickeymouse, MickCool890, ten, Saving, 89, 0.03//wrong numbber
Matt, Benjamin, jamimatt, Kit.24TT, 109201, InterestBearingChecking, 9000.00, 0.06//good
Keller, Helen, kel23hel, Poppins?89, 192911, Retirement, 11198.10//good
Mouse, Minnie, minniemouse, MinnCool19, 192922, Checking, 9000, 0.04//wrong type with interest
Keller, Helen, kel23hel, Poppins?89, 192912, InterestBearingChecking, 11198.10, 0.03//good
Tim, Mary, coolcats, Purr45??, 199122, BasicChecking, 201013.10//good
TheBuilder, Bob, bobbyNice18, 122101, Saving, 0.02//missing password, missing interest
class Customer{
protected String firstName;
protected String lastName;
protected String username;
protected String password;
protected ArrayList<BasicAccount> accounts;
public void setLastName(String ln){this.lastName=ln;}
public String getLastName(){return lastName;}
public void setFirstName(String fn){this.firstName=fn;}
public String getFirstName(){return firstName;}
public void setUsername(String un){this.username=un;}
public String getUserName(){return username;}
public void setPassword(String pw){this.password=pw;}
public String getPassword(){return password;}
public Customer(){}
public Customer(String f,String l,String u,String p) throws Exception{
this.username=f;
this.lastName=l;
this.username=u;
this.password=p;
}
public void addAccount(BasicAccount a){
this.accounts.add(a);
}
public void addAccount(String aType,float balance){
BasicAccount a=new BasicAccount(aType,balance);
}
}
class BasicAccount {
protected static int nextAvailableAccountNumber;
protected AccountType aType;
protected float balance;
protected int accountNum;
enum AccountType {BasicChecking, InterestBearingChecking, Saving, Retirement}
public BasicAccount(){};
public BasicAccount(String f,String l,String u,String p,int an,String type,float b){
}
public BasicAccount(Customer c,int a,String type,float b){
}
public BasicAccount(int a,String type,float b){
this.accountNum=a;
this.aType=AccountType.valueOf(type);
this.balance=b;
nextAvailableAccountNumber=accountNum+1;
}
public BasicAccount(String f,String l, String u,String p,String type,float b){
}
public BasicAccount(Customer c,String type,float b){
}
public BasicAccount(String type,float b){
this.accountNum=nextAvailableAccountNumber;
this.aType=AccountType.valueOf(type);
this.balance=b;
}
public static void main(String[] args)throws IOException {
ArrayList<Customer>cc=new ArrayList<Customer>();
Scanner k=new Scanner(System.in);
out.println("please enter the filename");
String filename=k.nextLine();
File inFile=new File(filename);
Scanner sc=new Scanner(inFile);
String error="";
ArrayList<String> errorLines=new ArrayList<String>();
while(sc.hasNext()){
String line=sc.nextLine();
try{
String []info=line.split(",");
if(info.length==7){
if(info[5].equals("BasicChecking")||info[5].equals("Retirement")){
Customer c=new Customer(info[0],info[1],info[2],info[3]);
BasicAccount act=new BasicAccount(c,Integer.parseInt(info[4]),info[5],Float.parseFloat(info[6]));
String [] user={c.firstName,c.lastName,c.username,c.password};
**for(Customer x:cc){
if(x.contains(c.getFirstName())&&c.getLastName()%%c.getUserName){
x.addAccount(act);
}
else{
c.addAccount(act);
cc.add(c);
}
}**//so if the customer object in the array list contains the same information from the
new Customer c it will only add the account to the x but if its not i will add to c and add
c to the arraylist but this doesn't work
}
else{
//error+="\n Wrong.";
//out.println(error);
}
}
}
catch(Exception e){
error+="\n wrong";
errorLines.add(line);
out.println("wrong data");
}
}
out.println(error);
if(errorLines.size()==0){}
else{
out.println(errorLines);
}
out.println(errorLines);
}
}
Main.java:277: error: cannot find symbol
if(x.contains(c.getFirstName())&&c.getLastName()){
^
symbol: method contains(String)
location: variable x of type Customer
Note: Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
对不起,如果我的整个代码一团糟,但我在每个地方都迷失了方向,我只想把这部分(标题)弄对,如果我的问题不合适,请告诉我,我会尽力编辑它
答: 暂无答案
评论