2012-03-01 3 views
0

나는 객체 배열 Customer을 비교하는 클래스에 대한 과제를 수행 한 다음 사용자에게 여러 가지 방법을 제공합니다. "고객이 추상적이 아니며 추상적 인 메소드 compareTo (Customer)를 java.lang.Comparable에 겹쳐 쓰지 않습니다.이 문제가이 포럼에서 처리되었지만 유감스럽게도 답변을 찾지 못했습니다. 이 sensical의 모든.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> 인터페이스의 문서에 따르면, 당신은 오버라이드 (override) 할 방법은

public int compareTo(T o) 

이있는 비교를 위해 첫 번째 개체가 this입니다.

정의하고 있습니다

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) 

참고 하나의 인자를 구현해야합니다. 과 that을 비교합니다.

0

인터페이스를 구현할 때 은 반드시이 제공하는 메소드 서명을 사용해야합니다. 인터페이스 구현의 요점은 필요한 메서드를 구현했다는 것을 다른 사람들에게 알리는 것입니다. 귀하의 경우 인터페이스에서 선언 된 동일한 이름을 사용하는 메서드가 있지만 Comparable을 처리하는 방법을 알고있는 클래스에서 기대하는 메서드는 아닙니다.

다른 개체에 compareTo 메서드를 호출하는 클래스를 작성해야한다고 가정 해보십시오. 예를 들어

과 같이 모든 종류의 param 유형 (Strings, ints, Comparables 등)을 사용하여 단일 유형의 매개 변수 (T 유형)를 전달하고 사람들은 자체 버전을 계속 구현합니다. compareTo(float f, boolean caseSensitive)

어떻게 전화를 걸 수 있습니까?