如何更改LInked List中的某个元素?

时间:2016-03-13 16:41:53

标签: java arrays sorting linked-list

我试图更改我的链接列表中的名字和姓氏,但是当我调用set方法时,它不会更改名字和姓氏。另外,我已经存款,取款方式,但我无法从链接列表中拨打电话。所有这些都在一个while循环中,如果用户输入Q作为命令,那么程序应该退出。我在while循环之后制作了我的布尔值,而且我按下Q并没有退出程序。             LinkedList bankRecords = new LinkedList<>();

        bankRecords.add(new Customer2("James", "Shown", 150));

        bankRecords.add(new Customer2("Carl", "Bob",250));

Scanner input=new Scanner(System.in);
System.out.println("Enter Command");
String x=input.nextLine();

while(true){
if(x.equals("a")){
    printList(bankRecords);
}else if(bankRecords.isEmpty()){
    System.out.println("Records are empty");
}
if(x.equals("r")){
    bankRecords.removeLast();
    }
if(x.equals("f")){
    bankRecords.set(bankRecords.size()-1, bankRecords.getFirst().setFirstName("Jack"));
    printList(bankRecords);

}
if(x.equals("l")){
    bankRecords.set(bankRecords.size(), bankRecords.getLast().setLastName("Kyle"));
}
if(x.equals("d")){
bankRecords.
}
if(x.equals(w)){

}
if(x.equals("n")){
    System.out.print("Enter first name: ");
    String name=input.nextLine();
    System.out.println("Enter Last name");
    String lname=input.nextLine();
    System.out.print("Enter Balance:");
    int balance=input.nextInt();
    bankRecords.add(new Customer2(name, lname, balance));
}


    if(x.equals("s")){
        System.out.println("Enter first name");
        String fname=input.nextLine();
        System.out.println("Enter Last name");
        String lname=input.nextLine();

            if(bankRecords.contains(fname)){

            }
        }
    }
if(x.equals("q")){
    false;
}



        public static void printList(LinkedList<Customer2> list) {

        for (Customer2 data : list) {

        System.out.println(data.getFirstName()+"\t"+data.getLastName()+"\t"+data.getAccountBalance());

        }

        }

        }

AccountClass

private String firstName;
private String lastName;
private int accountBalance;
public int total=0;
public Customer2(String fName, String lName, int balance){
    firstName = fName;
    lastName = lName;
    accountBalance = balance;
}
public void setFirstName(String newFirstName){
    firstName = newFirstName;
    }
    public void setLastName(String newLastName){
    lastName = newLastName;
    }
public String getFirstName() {

return firstName;

}

public String getLastName() {

return lastName;

}



public double getAccountBalance() {

return accountBalance;

}
public void withdraw(int amount){
    total=accountBalance-amount;
    accountBalance=total;

}
public void deposit(int amount){
    total=accountBalance+amount;
    accountBalance=total;
}



public void menu(){

        System.out.println("a  Show all records");
        System.out.println("r  Remove the current record");
        System.out.println("f  Change the first name in the current record");
        System.out.println("l  Change the last name in the current record");
        System.out.println("n  Add a new record");
        System.out.println("d  Add a deposit to the current record");
        System.out.println("w  Make a withdrawal from the current record");
        System.out.println("s Select a record from the record list to become the current record");
    }
}

1 个答案:

答案 0 :(得分:0)

程序中有许多错误,但不完整。看起来您正处于开发阶段,并且您已经粘贴了不完整的代码。 通常首先要先构建代码,然后添加注释/待办事项来表明需要完成的工作。 然后逐个填写这些待办事项行以完成您的代码。

如果您在按q选项时没有退出程序的错误,请尝试以下

if (x.equals("q")) {
                System.exit(1);
            }

以下是选项f和l的方法

    if (x.equals("f")) {
        Customer2 currentRecord = bankRecords.getLast();
        currentRecord.setFirstName("Jack");
    }
    if (x.equals("l")) {
        Customer2 currentRecord = bankRecords.getLast();
        currentRecord.setLastName("Kyle");
    }

下面是选项d和w的方法

        if (x.equals("d")) {
            Customer2 currentRecord = bankRecords.getLast();
            currentRecord.deposit(20);
        }
        if (x.equals("w")) {
            Customer2 currentRecord = bankRecords.getLast();
            currentRecord.withdraw(10);
        }

希望它有所帮助。