2012-03-01 11 views
0

私はオブジェクト配列Customerを比較しているクラスの代入を行い、次にユーザーにいくつかのメソッドを提供しています。 「抽象メソッドではない抽象メソッドcompareTo(Customer)をjava.lang.Comparableにオーバーライドしていないというエラーが表示されています。このフォーラムでこれが処理された場合は申し訳ありませんが、この無意味のすべてが。Comparable実装のエラーに関する問題

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.Scanner; 


class prog4 { 
public static void main(String args[]) 
      throws FileNotFoundException 
{ 
Scanner kb=new Scanner(System.in); 

Customer [] A =readArray(); //read data into objects in A 

while(true) 
{ 
    System.out.println(); 
    System.out.println(); 
    System.out.println("Please select one of the follwing actions: "); 
    System.out.println("q - Quit"); 
    System.out.println("a - List the customer records sorted by customer name"); 
    System.out.println("b - Enter a customer name to find the customer's record"); 
    System.out.println("c - List the customer records sorted by purchase in descending order"); 
    System.out.println("Please enter q, a, b, or c: "); 

    String selection=kb.nextLine(); //read user's selection 
    if (selection.equals("")) continue; //break; //if selection is "", show menu again 

    switch (selection.charAt(0)) 
    { 
    /*write code to process the following cases*/ 

    case 'a': 

      break; 

    case 'b': 

      break; 

    case 'c': 

      break; 

    case 'q': 

      return; 

    default: 
    } //end switch 
} //end while 
} //end main(); 


    //the following method uses the data from indata.txt 
    //to create Customer objects of an array 
    //and returns the array 

    private static Customer[] readArray() 
        throws FileNotFoundException 
    { 
    String name; 
    double purchase; 
    double rebate; 

    Scanner infile=new Scanner(new File("indata.txt")); //input file 

    int size=infile.nextInt(); //get number of lines 
    infile.nextLine();   //skips end of line 

    Customer A[]=new Customer[size]; 

    for (int k=0; k<size; k++) 
    { 
     //read a name = 16 characters 
     infile.useDelimiter(""); 
     name=""; 

     for (int i=0; i<16; i++) name=name+infile.next(); 
     infile.reset(); 

     purchase=infile.nextDouble(); 
     rebate=infile.nextDouble(); 
     if (infile.hasNextLine()) infile.nextLine(); //skip end of line 

     A[k]=new Customer(name, purchase, rebate); //create object for A[i] 

    } //end for loop 

    infile.close(); 
    return A; 

    } //end readArray 

    //the method prints the Customer objects of the array A 
    //one customer record per line 
    private static void printArray(Customer [] A) 
    { 
    for (Customer x:A) 
    {System.out.println(x);} 
    } //end printArray 
} //end class prog4 


class Customer implements Comparable<Customer> 
{ 
    String name; 
    double purchase; 
    double rebate; 

    public Customer(String n, double p, double r) 
    { name=n; 
    purchase=p; 
    rebate=r; 
    } //end Constructor 

    public Customer(String n) 
    { 
    name=n; 
    } //end Constructor 

    public String toString() 
    { 
    return String.format("%-20s%10.2f%10.2f", name, purchase, rebate); 
    } 

public int compareTo(Customer a, Customer b) 
{return b.name.compareTo(a.name);} 
    } 
//end class Customer 

class descendingPurchase implements Comparator<Customer> 
{ 
public int compare(Customer a, Customer b) 
    { 
    if(a.purchase<b.purchase) return 1; 
    else if(a.purchase==b.purchase) return 0; 
    else return -1; 
    } 
} 
+0

コンパイルエラーが発生しますか? – lolo

答えて

0

Comparable<T>インタフェースのドキュメントによると、あなたがオーバーライドすべきメソッドは、比較のために第1の目的はthisである

public int compareTo(T o) 

です。

あなたは定義しています

public int compareTo(Customer a, Customer b) 

これは正しい方法を無効にしません。そのため、Javaコンパイラは、CustomerComparable<Customer>を実装していますが、実際には正しいメソッドが見つからないという苦情を申し立てています。

後者は、同じ機能を提供するが「オブジェクトに依存しない」方法であるComparator<Customer>に必要なメソッドです。

1

あなたのクラスCustomer実装Comparable<Customer>

class Customer implements Comparable<Customer> 

次の方法

public int compareTo(Customer that) 

注単一の引数を実装する必要があります。 thisthatを比較すると仮定します。

0

を実装した場合、は、それが提供するメソッドシグネチャを正確に使用する必要があります。インターフェースの実装のポイントは、必要なメソッドを実装したことを他の人に知らせることです。あなたのケースでは、インターフェイスで宣言された同じ名前を持つメソッドがありますが、処理する方法を知っているクラスが期待するものではありませんComparable

他のオブジェクトにcompareToメソッドを呼び出すクラスを作成する必要があるとします。単一のパラメータ(T型)を渡すことを期待し、さまざまな種類のパラメータ(String、int、Comparablesなど)を使用して独自のバージョンを実装し続けます。たとえばcompareTo(boolean b, String s, int minMatches)

compareTo(float f, boolean caseSensitive)

どのように呼び出すのかわかりますか?

関連する問題