如何对链接列表中的名称进行排序?

时间:2016-03-13 20:35:57

标签: java sorting linked-list

我试图创建一个不断重复的银行记录,直到用户输入命令Q退出。我的while循环看起来很好但是它只重复两次然后停止。此外,我正在尝试编写一个打印列表,根据姓氏组织名称。我尝试按照姓氏打印它,但它不会根据姓氏打印。

我的主要课程代码:

public static void main(String args[]){     
    LinkedList<Customer2> bankRecords = new LinkedList<>(); 
    Scanner input=new Scanner(System.in);
    System.out.println("Enter Command");
    String x=input.nextLine();


    while(true){
        if(x.equals("a")){
            if(bankRecords.isEmpty()){
                System.out.println("Records are empty");
                System.out.println("Enter a command");
                x=input.nextLine();

            }else {
                printList(bankRecords);

                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");
                System.out.println("Enter a command");
            }
        }

        if(x.equals("r")){
            bankRecords.removeLast();
            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");
            System.out.println("Enter a command");
            x=input.nextLine();
        }
        if (x.equals("f")) {
            Customer2 currentRecord = bankRecords.getLast();
            System.out.print("Enter Firstname: ");
            String fname=input.nextLine();
            currentRecord.setFirstName(fname);
            currentRecord.menu();
            System.out.println("Enter a command");
            x=input.nextLine();

        }
        if (x.equals("l")) {
            Customer2 currentRecord = bankRecords.getLast();
            System.out.print("Enter Last Name: ");
            String lname=input.nextLine();
            currentRecord.setLastName(lname);
            currentRecord.menu();
            System.out.println("Enter a command");
            x=input.nextLine();
        }
        if(x.equals("d")){
            Customer2 currentRecord = bankRecords.getLast();
            System.out.print("Enter deposited amount");
            int deposit=input.nextInt();
            currentRecord.deposit(deposit);
            currentRecord.menu();
            System.out.println("Enter a command");
            x=input.nextLine();

        }
        if(x.equals("w")){
            Customer2 currentRecord = bankRecords.getLast();
            System.out.print("Enter Withdrawn amount");
            int withdraw=input.nextInt();
            currentRecord.withdraw(withdraw);
            currentRecord.menu();
            System.out.println("Enter a command");
            x=input.nextLine();

        }

        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));
            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");
            System.out.println("Enter a command");
            x=input.nextLine();

        }
        if(x.equals("q")){
            return;
        }
        if(x.equals("s")){
            System.out.println("Enter first name");
            String fname=input.nextLine();
            for(int i=0;i<bankRecords.size();i++){
                if(bankRecords.contains(fname)){
                    Customer2 cur=bankRecords.getLast();
                }}}}}
public static void printList(LinkedList<Customer2> list) {

    for (Customer2 data : list) {

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

    }

}

我的第二堂课:

public class Customer2 {
    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)

你的第一部分非常混乱,难以理解。它甚至没有运行两个周期,我的特殊情况是它运行到你的电脑。如果您可以清除代码并使其可理解,则可以回答。对于第二部分,您需要基于姓氏的排序列表。为此,您需要为您的Customer2类实现Comparable。

public class Customer2 implements Comparable<Customer2>{
    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");
        }

    @Override
    public int compareTo(Customer2 o) {
        // TODO Auto-generated method stub
        return o.lastName.compareTo(lastName);

    }
    }

然后,在您的主类中,您可以使用Collection对列表进行排序。我还附上了修改后的部分

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

    Collections.sort(list);
    System.out.println(list);

}