2013-10-29 1 views
-1

BankAccounts의 배열 목록에 비교 가능한 인터페이스를 구현하려고했지만, 주요 오류가 발생했습니다. 특히, Collections.sort (목록)에 대한 테스터 클래스의 main 메소드를 컴파일하고 실행할 때) 선. 이 온라인과 자바 독을 통해 syntax..looked와 내가 잘못 어디 찾을 수없는 인식하지 않는 것을 말하는 ..Comparable interface sorting method not working

public class BankAccount implements Comparable { //QUESTION 2.1 
    /** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
    private int accountNumber; 
    private double balance; 
    /** 
    Constructs a bank account with a zero balance 
    @param anAccountNumber the account number for this account 
    */ 
    public BankAccount(int anAccountNumber) 
    { 
    accountNumber = anAccountNumber; 
    balance = 0; 
    } 

    /** 
    Constructs a bank account with a given balance 
    @param anAccountNumber the account number for this account 
    @param initialBalance the initial balance 

*/ 공공는 BankAccount (INT의 anAccountNumber, 더블 initialBalance) {
accountNumber = anAccountNumber; balance = initialBalance; }

/** 
    Gets the account number of this bank account. 
    @return the account number 
    */ 
    public int getAccountNumber() 
    { 
    return accountNumber; 
    } 

    /** 
    Deposits money into the bank account. 
    @param amount the amount to deposit 
    */ 
    public void deposit(double amount) 
    { 
    double newBalance = balance + amount; 
    balance = newBalance; 
    } 

    /** 
    Withdraws money from the bank account. 
    @param amount the amount to withdraw 
    */ 
    public void withdraw(double amount) 
    { 
    double newBalance = balance - amount; 
    balance = newBalance; 
    } 

    /** 
    Gets the current balance of the bank account. 
    @return the current balance 
    */ 
    public double getBalance() 
    { 
    return balance; 
    } 


    public int compareTo (BankAccount temp) { 


    if (balance<temp.balance) 
     return -1; 
    if (balance==temp.balance) 
     return 0; 
    return 1; 
    } 

} 

public class TestSortedBankAccounts { 

    public static void main(String[] args) { 
     // Put bank accounts into a list 
     ArrayList<BankAccount> list = new ArrayList<BankAccount>(); 

     BankAccount ba1 = new BankAccount(100, 500); //Constructor acctNumber and balance 
     BankAccount ba2 = new BankAccount(200, 10000); 
     BankAccount ba3 = new BankAccount(300, 400); 
     BankAccount ba4 = new BankAccount(600, 0); 
     BankAccount ba5 = new BankAccount(800, 50); 

     list.add(ba1); 
     list.add(ba2); 
     list.add(ba3); 
     list.add(ba4); 
     list.add(ba5); 

     // Call the library sort method 
     Collections.sort(list); 

     // Print out the sorted list 
     for (int i = 0; i < list.size(); i++) { 
      BankAccount b = list.get(i); 
      System.out.println(b.getBalance()); 
     } 
    } 
} 

UPDATE : TestSortedBankAccounts.java:26 오류 : 정렬 (ArrayList를) 은, Collections.sort (목록) 찾지 적합한 방법; ^ 메서드 Collections.sort (List, Comparator)는 적용 할 수 없습니다. (실제 및 형식 인수 목록의 길이가 다르기 때문에 인수를 인스턴스화 할 수 없습니다.) 메서드 Collections.sort (목록)를 사용할 수 없습니다. (유추 된 형식이 선언 바인딩 (들) 추론 : BankAccount가 (들) 바인딩 : Comparable를) T # 1, T # 2는 타입 변수 : T # 1 오브젝트 방법 정렬 (목록, 비교기) T # 2에 선언 연장 메서드 sort (List)에 선언 된 Comparable 1 개의 오류

+0

이 도움이됩니다. ** 경고 ** FP 반올림 오류로 인해 부동 소수점 값에 ==를 사용하지 마십시오. – Darien

+2

대신에 'balance - temp.balance'와 같은 것을 사용할 수 있습니다 - 예제가 컴파일되지 않는다는 사실을 분별하지 마십시오) – MadProgrammer

+0

오류 메시지가 @Darien보다 위에 있습니다 – user2809437

답변

4

의 원시 버전을 구현하고 있습니다.. 당신은 원시 형태를 구현하는 경우

public class BankAccount implements Comparable<BankAccount> { 

는 다음 compareTo의 매개 변수 유형은 Object 될 것이다 : 당신은 Comparable의 일반적인 형태를 구현해야합니다. 일반 형식을 사용하면 generic 형식 매개 변수를 이미 가지고있는 것처럼 compareTo의 매개 변수로 제공 할 수 있습니다. (나는 시도하고 작동하는지)

+0

여전히 오류 메시지가 나타나면 위 편집을 참조하십시오 – user2809437

+0

코드를 직접 실행했습니다. (필요한 2-arg'BankAccount' 생성자를 직접 제공) 작동합니다.방금 제공 한 오류 메시지를 기반으로 제공하지 않는 다른 코드가 있어야합니다. rgettman 거기에 전체 코드가 – rgettman

+0

있습니다. – user2809437

0

내 대답 : 당신이 오류 메시지를 게시 할 경우

public class BankAccount implements Comparable<Object> { // <-- 
    /** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
    private int accountNumber; 
    private double balance; 
    /** 
    Constructs a bank account with a zero balance 
    @param anAccountNumber the account number for this account 
    */ 
    public BankAccount(int anAccountNumber) 
    { 
    accountNumber = anAccountNumber; 
    balance = 0; 
    } 

    /** 
    Constructs a bank account with a given balance 
    @param anAccountNumber the account number for this account 
    @param initialBalance the initial balance 
    */ 
    public BankAccount(int anAccountNumber, double initialBalance) { 
     accountNumber = anAccountNumber; balance = initialBalance; } 

    /** 
    Gets the account number of this bank account. 
    @return the account number 
    */ 
    public int getAccountNumber() 
    { 
    return accountNumber; 
    } 

    /** 
    Deposits money into the bank account. 
    @param amount the amount to deposit 
    */ 
    public void deposit(double amount) 
    { 
    double newBalance = balance + amount; 
    balance = newBalance; 
    } 

    /** 
    Withdraws money from the bank account. 
    @param amount the amount to withdraw 
    */ 
    public void withdraw(double amount) 
    { 
    double newBalance = balance - amount; 
    balance = newBalance; 
    } 

    /** 
    Gets the current balance of the bank account. 
    @return the current balance 
    */ 
    public double getBalance() 
    { 
    return balance; 
    } 


    // public int compareTo (BankAccount temp) { // <-- WRONG 
    public int compareTo(Object temp){ // <-- RIGHT 

    BankAccount other = (BankAccount)temp; // <-- Cast to BankAccount 
    if (balance<other.balance) 
     return -1; 
    if (balance==other.balance) 
     return 0; 
    return 1; 
    } 

}