简体   繁体   中英

PriorityQueue java

I am writing a priorityqueue class that I want to sort and print based on the account balance. It prints values, but the problem is that it prints the hex values of the parameters passed into the constructor. Where in the code am I going wrong?

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;
    }

    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());
    }
}

Override the toString() method of your Account class.

Something like:

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;
    }
}

What you are getting currently is the default implementation of the toString method defined in the Object class, which..

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

in class Account you should overrite "toString" method. Eg like that:

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

This is auto-called function when you are adding your object to string. Eg

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

You'll get this:

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

Override the toString()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM