PriorityQueue java

时间:2015-07-29 13:37:38

标签: java priority-queue

我正在编写一个我想根据帐户余额进行排序和打印的优先级。它打印值,但问题是它打印传递给构造函数的参数的十六进制值。我在代码中哪里出错了?

帐户:

public class Account implements Comparable<Account> {

    private String firstName;
    private String lastName;
    private double balance;
    private int accountNumber;

    public Account(String firstName, String lastName, double balance, int accountNumber){

        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
        this.accountNumber = accountNumber;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber) {
        this.accountNumber = accountNumber;
    }
    public boolean equals(Account x){
        return firstName.equals(x.firstName);
    }

    @Override
    public int compareTo(Account o) {

        return(int) (this.balance - o.balance);

       // Account other = (Account)o;

        /*if(balance<other.balance)
            return -1;
        if(balance==other.balance)
            return 0;
        return 1;*/

       /* int c = this.firstName.compareTo(o.firstName);

        if(c < 0){
            return -1;
        }else if(c == 0){
            if(this.balance < 0 && o.balance < 0){
                if(this.balance < o.balance){
                    return 1;
                }
            }

        }
        return 1;*/


    }

}

AccountApp:

package account;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;

/**
 *
 * @author saner20
 */
public class AccountApp {

    public static void main(String []args){

        Account account1 = new Account("billy", "bob", 10.00, 1);
        Account account2 = new Account("tom","sawyer", 20.00, 2);
        //Account account3 = new Account("bob","builder", 30, 3);

        PriorityQueue<Account> account = new PriorityQueue<>();

        account.offer(account1);
        account.add(account2);
        //account.add(account3);

        while(!account.isEmpty())
        {
            System.out.println("Print queue: " + account.remove());
            //time.remove();
        }
        //Arrays.sort(account.toArray());
    }
}

3 个答案:

答案 0 :(得分:1)

覆盖toString()课程的Account方法。

类似的东西:

public class Account implements Comparable<Account> {

    private String firstName;
    private String lastName;
    private double balance;
    private int accountNumber;

    public Account(String firstName, String lastName, double balance, int accountNumber){

        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
        this.accountNumber = accountNumber;
    }

    // ... other methods

    @Override
    public String toString() {
        return "First name: " + firstName + ", Last name: " + lastName +
            ", Account number: " + accountNumber + ", Balance: " + balance;
    }
}

您目前获得的是Object类中定义的toString方法的default implementation,其中包含..

  

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”和对象的哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于值的字符串:

     

getClass()。getName()+'@'+ Integer.toHexString(hashCode())

答案 1 :(得分:0)

在类帐户中

你应该覆盖“toString”方法。例如。那样:

@Override
public string toString() {
    return "Account{owner=" + firstName + " " + lastName + "; balance=" + balance + "; accountNumber=" + accountNumber + "}";
}

将对象添加到字符串时,这是自动调用的函数。 E.g

System.out.print(new Account("Josh", "Sad", 10, 10) + " is cool");

你会得到这个:

Account{owner=Josh Sad; balance=10; accountNumber=10} is cool

答案 2 :(得分:0)

覆盖toString()

@Override
public string toString() {
    return "Name: " + lastName + ", " + firstName + "\nbalance: " + balance + "\n accountNumber: " + accountNumber;
}