2015-03-27 1 views
0

Je reçois ces erreursLa méthode compareTo ne fonctionnera pas?

Java A5-1\SavingsAccountPart1Test.java:74: error: incompatible types: SavingsAccount cannot be converted to double 
     if (savings1.compareTo(savings2) > 0) 

Java A5-1\SavingsAccountPart1Test.java:76: error: incompatible types: SavingsAccount cannot be converted to double 
     else if (savings1.compareTo(savings2) == 0) 

Voici la classe

import java.text.DecimalFormat; 

public class SavingsAccount 

{ 

    private double annualInterestRate; //A double for the yearly interest rate 
    private double balance;   //A double for balance 

    /** 
     SavingsAccount A constructor for the annual interest rate and balance 
    */ 
    public SavingsAccount() 
    { 
     //The balance and interest rate are given default values 
     annualInterestRate = .005; 
     balance = 0.00; 
    } 

    /** 
     SavingsAccount A constructor for the annual interest rate and balance 
     @param rate The annual interest rate 
     @param bal The balance 
    */ 
    public SavingsAccount(double rate, double bal) 
    { 
     //Allowing the client to set the balance and interest rate while checking if the values are valid 
     if (rate < 0) 
     { 
      System.out.println("WARNING: Starting balance must not be negative; setting it to $0.00."); 
      rate = .005; 
     } 
     else 
      annualInterestRate = rate; 

     if (balance < 0) 
     { 
      System.out.println("WARNING: Interest rate must not be negative; setting it to 0.5%."); 
      bal = 0; 
     } 
     else 
      balance = bal; 
    } 

    /** 
     getInterestRate A method that returns the interest rate 
     @return The annual interest rate 
    */ 
    public double getInterestRate() 
    { 
     return annualInterestRate; 
    } 

    /** 
     getBalance A menthod that returns the balance 
     @return The balance 
    */ 
    public double getBalance() 
    { 
     return balance; 
    } 

    /** 
     update A method that updates the balance 
     @param bal The Balance 
    */ 
    public void update(double bal) 
    { 
     //make a money format, update balance by setting balance to passed in bal and 
     //check if balance is overdrawn or not 
     DecimalFormat money = new DecimalFormat("$#,##0.00"); 
     balance += bal; 
     if (balance < 0) 
      System.out.println("WARNING: ACCOUNT OVERDRAWN \n Current balance: " + money.format(balance)); 
    } 

    /** 
     addInterest A method that will add interest to balance 
    */ 
    public void addInterest() 
    { 
     //declare a monthly interest and a sub balance, then add interest if balance is greater than 0 
     double monthlyInterest; 
     double subBalance; 
     if (balance > 0) 
     { 
      monthlyInterest = annualInterestRate/12; 
      subBalance = balance * monthlyInterest; 
      balance += subBalance; 
     } 
    } 

    /** 
     compareTo A method to see if balance is bigger or smaller than savings 
     @param savings The savings 
     @return The result 
    */ 
    public int compareTo(double savings) 
    { 
     int result; 
     if (balance > savings) 
      result = 1; 
     else if (balance == savings) 
      result = 0; 
     else 
      result = -1; 

     return result; 
    } 

    /** 
     toString A method that returns the balance and rate as a string 
     @return The string of balance and rate 
    */ 
    public String toString() 
    { 
     //makes format for money and interest rate 
     //makes a string to hold the balance and rate then returns the string 
     DecimalFormat money = new DecimalFormat("$#,##0.00"); 
     DecimalFormat rate = new DecimalFormat("0.0%"); 
     String balRate = ("Balance: " + money.format(balance) + "\n" + 
          "Annual Interest Rate: " + rate.format(annualInterestRate * 100)); 
     return balRate; 
    } 
} 

Et le code pour tester la classe (pas autorisé à le modifier) ​​

import java.io.*; 

import java.util.Scanner; 

import java.text.DecimalFormat; 

public class SavingsAccountPart1Test 

{ 

    public static void main(String args[]) throws IOException 
    { 
     double interestRate;  // Annual interest rate 
     double balance;    // Balance in the account 
     double interestEarned;  // Interest earned 
     double amount;    // Amount of a deposit or withdrawal 
     double totalUpdates;  // Total amount deposited and withdrawn 

     // Create a DecimalFormat object for formatting output. 
     DecimalFormat df = new DecimalFormat("$#,##0.00"); 

     // Display your name 
     System.out.println("[client] YOUR NAME"); 

     // Welcome message 
     System.out.println("\n[client] Welcome to the Fleesum Bank" 
      + "\n[client] This is the monthly interest calculator"); 

     // Create a Scanner object for keyboard input. 
     Scanner keyboard = new Scanner(System.in); 

     // Create a SavingsAccount object using the no-argument constructor 
     SavingsAccount savings1 = new SavingsAccount(); 

     // Display the initial state of savings1 
     System.out.println("\n[client] First SavingsAccount object"); 
     System.out.println("\n[client] Balance: " + savings1.getBalance()); 
     System.out.println("[client] Interest Rate: " + savings1.getInterestRate()); 
     System.out.println("\n[client] Using toString:\n" + savings1.toString()); 

     // Create a SavingsAccount object using the 2-argument constructor 
     SavingsAccount savings2 = 
      new SavingsAccount(1000, 0.10); 

     // Display the initial state of savings2 
     System.out.println("\n[client] Second SavingsAccount object"); 
     System.out.println("\n[client] Balance: " + savings2.getBalance()); 
     System.out.println("[client] Interest Rate: " + savings2.getInterestRate()); 
     System.out.println("\n[client] Using toString:\n" + savings2.toString()); 

     // Test method addInterest when balance is >= 0 
     savings2.addInterest(); 
     System.out.println("\n[client] Interest computed. New balance: " 
      + savings2.getBalance()); 

     // update the balance: 
     savings2.update(50); 
     System.out.println("\n[client] Fifty dollars deposited. New balance: " 
      + savings2.getBalance()); 
     // update, overdrawing the account 
     savings2.update(-2000); 
     System.out.println("\n[client] Two thousand dollars withdrawn. New balance: " 
      + savings2.getBalance()); 

     // Test method addInterest when balance is < 0 
     savings2.addInterest(); 
     System.out.println("\n[client] Interest computed. New balance: " 
      + savings2.getBalance()); 

     // Show final state of savings2: 
     System.out.println("\n[client] Using toString:\n" + savings2.toString() 
      + "\n"); 

     // Test compareTo method 
     if (savings1.compareTo(savings2) > 0) 
      System.out.println("[client] Savings1 has the larger balance"); 
     else if (savings1.compareTo(savings2) == 0) 
      System.out.println("[client] Savings1 and Savings2 " 
      + "have the same balance"); 
     else 
      System.out.println("[client] Savings2 has the larger balance"); 

     System.out.println(); 
    } 
} 

Répondre

4
public int compareTo(double savings) 

est votre problème , remplacez double par SavingsAccount et modifiez la méthamphétamine od en conséquence.

1

Notez que le test compare deux objets du type SavingsAccount:

savings1.compareTo(savings2) 

Bien que votre mise en œuvre compare un SavingsAccount à un double:

public int compareTo(double savings) 

Idéalement, vous devriez faire SavingsAccount mettre en œuvre le Comparable (see docs), comme ceci:

public class SavingsAccount implements Comparable<SavingsAccount> { ... } 

Et puis il se plaindrait si le compareTo(SavingsAccount a) approprié n'a pas été implémenté. Cela devrait ressembler à ceci:

@Override 
public int compareTo(SavingsAccount that) { ... }