使用类似的界面对数组进行排序

时间:2013-11-09 22:59:39

标签: java interface comparable

我试图按照克拉的大小对钻石进行排名/分类。如果大小相等,那么我想根据颜色或清晰度对它们进行排名,以最佳排名为准。我无法让它正常工作。它运行,但它只会根据克拉的大小对它们进行排名

Crawford_Diamond[] stones = new Crawford_Diamond[16];

stones[0] = new Crawford_Diamond( "A1023", 1.0, "VS1",  'F', "brilliant");
stones[1] = new Crawford_Diamond( "A5911", 1.1, "VVS2", 'G', "rose");
stones[2] = new Crawford_Diamond( "C5427", 1.0, "VS1",  'D', "princess");
stones[3] = new Crawford_Diamond( "D8307", 1.6, "SI1",  'H', "brilliant");
stones[4] = new Crawford_Diamond( "B4825", 0.3, "I1",   'D', "rose");
stones[5] = new Crawford_Diamond( "A1844", 2.1, "VS2",  'D', "lozenge");
stones[6] = new Crawford_Diamond( "A3747", 3.1, "SI2",  'W', "baguette");
stones[7] = new Crawford_Diamond( "E6393", 2.3, "VS2",  'I', "brilliant");
stones[8] = new Crawford_Diamond( "C5619", 2.8, "VVS1", 'E', "pear");
stones[9] = new Crawford_Diamond( "E8348", 1.4, "VS2",  'G', "brilliant");
stones[10] = new Crawford_Diamond( "D2381", 1.7, "I3",   'G', "brilliant");
stones[11] = new Crawford_Diamond( "C9253", 1.3, "VS2",  'H', "baguette");
stones[12] = new Crawford_Diamond( "G3459", 2.1, "VS2",  'H', "rose");
stones[13] = new Crawford_Diamond( "B3598", 2.4, "VVS2", 'D', "pear");
stones[14] = new Crawford_Diamond( "D9836", 2.8, "IF",   'E', "princess");
stones[15] = new Crawford_Diamond( "E1046", 2.2, "FL",   'E', "rose");

Arrays.sort(stones);

for ( int j=0; j<stones.length; j++)
    System.out.println( stones[j].toString());
public class Crawford_Diamond implements Comparable<Crawford_Diamond>
{

private String stockNumber; //diamond stock number
private double carot; //carrot size
private String clarity;  
private char color; //color of the diamond. D=Best Z=Worst
private String cut; //cut of the diamond 
private int diamondColor;
private int diamondClarity;

public Crawford_Diamond(String sN, double car, String clar, char col, String cutType)
{
    stockNumber = sN; 
    carot = car; 
    clarity = clar; 
    color = col; 
    cut = cutType;

}
//gets the stock number of the diamond
public String getStock(){return stockNumber; }

//gets the carrot size of the diamond
public double getCarot(){return carot;}

//gets the clarity of the diamond
public String getClarity(){return clarity;}

//gets the color of the diamond
public char getColor()
{

    return color;
}

//gets the cut of the diamond 
public String getCut() {return cut;}
public int compareClarity(String getClarity)
{
    int diamondClarity=0; 
    if (getClarity.equals("Fl"))
        diamondClarity = 10; 
    else if (getClarity.equals("IF"))
        diamondClarity = 9; 
    else if (getClarity.equals("VVS1"))
        diamondClarity = 8; 
    else if (getClarity.equals("VVS2"))
        diamondClarity = 7; 
    else if (getClarity.equals("VS1"))
        diamondClarity = 6; 
    else if (getClarity.equals("VS2"))
        diamondClarity = 5; 
    else if (getClarity.equals("SI1"))
        diamondClarity = 4; 
    else if (getClarity.equals("SI2"))
        diamondClarity = 3; 
    else if (getClarity.equals("I1"))
        diamondClarity = 2; 
    else if (getClarity.equals("I2"))
        diamondClarity = 1; 
    else if (getClarity.equals("I3"))
        diamondClarity = 0; 
    return diamondClarity;
}

public int getRankD1( )
{
int rankD1=0;
if (this.diamondColor > this.diamondClarity)
    rankD1 = diamondColor;  
else if (this.diamondColor < this.diamondClarity)
    rankD1 = diamondClarity;
return rankD1; 
}
public int getRankD2()
{
int rankD2=0;
if (this.diamondColor > this.diamondClarity)
rankD2 = diamondColor; 
else if (this.diamondColor < this.diamondClarity)
rankD2 = diamondClarity;
return rankD2;
}

public int compareColor(char getColor)
{
    int diamondColor=0; 
    if (getColor=='D' || getColor=='E')
        diamondColor = 10; 
    else if (getColor=='F' || getColor=='G')
        diamondColor = 9; 
    else if (getColor=='H' || getColor=='I')
        diamondColor = 8; 
    else if (getColor=='J' || getColor=='K')
        diamondColor = 7; 
    else if (getColor=='L' || getColor=='M')
        diamondColor = 6; 
    else if (getColor=='N' || getColor=='O')
        diamondColor = 5; 
    else if (getColor=='P' || getColor=='Q')
        diamondColor = 4; 
    else if (getColor=='R' || getColor=='S')
        diamondColor = 3; 
    else if (getColor=='T' || getColor=='U')
        diamondColor = 2; 
    else if (getColor=='V' || getColor=='W')
        diamondColor = 1; 
    else if (getColor=='X' || getColor=='Y')
        diamondColor = 0; 
    return diamondColor;
}

public int compareTo(Crawford_Diamond other)
{

    if (this.carot > other.getCarot())
    {
        return -1;
    }
    else if (this.carot < other.getCarot())
    {
        return 1;

    }
    else if(this.getRankD1() > other.getRankD2())
    {
        return -1;
    }
    else if(this.getRankD1() < other.getRankD2())
    {
        return 1;
    }
    else 
        return 0;}



public String toString()
{
    return "{stockNumber :: " +getStock() + " carot :: " +getCarot() + " clarity :: " +getClarity()+ " color :: " +getColor() + " cut :: " +getCut()+"}";
}
}   

3 个答案:

答案 0 :(得分:0)

尝试好,因为他们是双打而不是整理......

public int compareTo(Crawford_Diamond other)
{  
    int value = 0;
    if (this.carot > other.getCarot())
    {
        value = -1;
    }
    else if (this.carot < other.getCarot())
    {
        value = 1;
    }       
    if (value == 0){
       value = getRankD1() - other.getRankD1();           
    }
    return value;
}  

答案 1 :(得分:0)

在这种情况下,我发现更容易给出绝对分数......

首先,我会更改您的函数以相反的顺序返回值(其中Color D的得分为0,而颜色X的得分为10.同样,最好的清晰度('fl'已经是高分......)。

使用将最佳属性排名最高的分数。你的等级从0到10。

所以,我会在Crawford_Diamond上有一个名为getScore()的方法:

public int getScore() {
    int score = (int)(carat * 100000); // carat of 2.2 will become 220000
    int colorscore = getColorScore(color);
    int clarityscore = getClarityScore(clarity);
    if (color > clarity) {
        // color of D is best, give it score 1000 and add the clarity score
        score = score + (100 * color) + clarity;
    } else {
        score = score + (100 * clarity) + color;
    }
    return score;
}

然后,您的compareTo方法变为:

public int compareTo(Crawford_Diamond other) {
    return other.getScore() - getScore();
}

(注意,它是克拉,而不是carot(或插入符号))

答案 2 :(得分:0)

你几乎是对的。如果您只是在构造函数中添加两行,那么您的代码将完全按照您的意愿执行。

public Crawford_Diamond(String sN, double car, String clar, char col, String cutType)
{
    stockNumber = sN; 
    carot = car; 
    clarity = clar; 
    color = col; 
    cut = cutType;

    diamondColor = compareColor(color);
    diamondClarity = compareClarity(clarity);
}

您还可以执行其他操作来清理代码。您将compareClarity重命名为getClarityValue,因为您只是将字符串转换为整数。颜色也一样。

您也不需要两个版本的排名方法。由于始终在给定的菱形对象上调用该方法,因此单个getRank()方法可以很好地代替getRankD1()getRankD2()