2014-10-01 122 views
0

我一直在編碼幾個小時,試圖完成我的任務。現在我已經完成了,我想添加一種方法來分類客戶的價值。因此,客戶2排在前5位。我怎麼做?如何對我的對象列表進行排序

下面是第一類的代碼:

public class Customer { 

    private int customerNumber; 
    private int purchases; 
    private int shippingCost; 
    private int productPrice; 

    public int getProductPrice() { 
     return productPrice; 
    } 

    public void setProductPrice(int productPrice) { 
     this.productPrice = productPrice; 
    } 

    public int getPurchases() { 
     return purchases; 
    } 

    public Customer(int purchases, int shippingCost, int productPrice) { 
     this.purchases = purchases; 
     this.shippingCost = shippingCost; 
     this.productPrice = productPrice; 
    } 

    public void setPurchases(int purchases) { 
     this.purchases = purchases; 
    } 

    public int getShippingCost() { 
     return shippingCost; 
    } 

    public void setShippingCost(int shippingCost) { 
     this.shippingCost = shippingCost; 
    } 

    protected int calculateValue() { 
     int value=purchases*(productPrice-shippingCost); 
     return value; 
    } 

    public int getCustomerNumber() { 
     return customerNumber; 
    } 

    public void setCustomerNumber(int customerNumber) { 
     this.customerNumber = customerNumber; 
    } 

第二類:

public class Subscriber extends Customer{ 

    private int years; 
    private int fee; 

    public Subscriber(int purchases, int shippingCost, int productPrice) { 
     super(purchases, shippingCost, productPrice); 
    } 

    public Subscriber(int purchases, int shippingCost, 
      int productPrice,int _years,int _fee) { 
     super(purchases, shippingCost, productPrice); 
     years=_years; 
     fee=_fee; 
    } 

    public int getYears() { 
     return years; 
    } 

    public void setYears(int years) { 
     this.years = years; 
    } 

    public int getFee() { 
     return fee; 
    } 

    public void setFee(int fee) { 
     this.fee = fee; 
    } 

    protected int calculateSubscriptionValue() { 
     return years*fee; 
    } 

    protected int calculateValue(){ 
     int value=super.calculateValue(); 
     value+=calculateSubscriptionValue(); 
     return value; 
    } 

第三類:

public class BusinessSubscriber extends Subscriber{ 

    public BusinessSubscriber(int purchases, int shippingCost, int productPrice, int _years, int _fee) { 
     super(purchases, shippingCost, productPrice, _years, _fee); 
    } 
    private double supportCost; 
    private double supportTime; 

    public double getSupportCost() { 
     return supportCost; 
    } 
    public void setSupportCost(double supportCost) { 
     this.supportCost = supportCost; 
    } 
    public double getSupportTime() { 
     return supportTime; 
    } 
    public void setSupportTime(double supportTime) { 
     this.supportTime = supportTime; 
    } 

    //override method: // skriv över den // problem med denna metod. uträkningsfel 
    protected int calculateValue(){ 
     int value=super.calculateValue(); 
     value+=calculateSubscriptionValue() - calculateSubscriptionValue(); 
     return value; 
    } 
} 

最後和主類:

public class printValue { 

    public static void main(String[] args) { 
     System.out.println("Unsorted version without DataStructure"); 
     System.out.println("-----------------------"); 
     BusinessSubscriber num1 = new BusinessSubscriber(3, 33, 99, 8, 78); 

     num1.setCustomerNumber(1); 

     num1.setPurchases(3); 
     num1.setShippingCost(33); 
     num1.setYears(8); 
     num1.setSupportTime(2); 
     num1.setProductPrice(99); 
     num1.setFee(78); 
     num1.setSupportCost(25); 
     System.out.println("Customer number: " + num1.getCustomerNumber() + " is worth " + num1.calculateValue() + "!"); 

     BusinessSubscriber num2 = new BusinessSubscriber (0, 0, 99, 18, 78); 

     num2.setCustomerNumber(2); 

     num2.setPurchases(0); 
     num2.setShippingCost(0); 
     num2.setYears(18); 
     num2.setSupportTime(7); 

     num2.setProductPrice(99); 
     num2.setFee(78); 
     num2.setSupportCost(25); 

     System.out.println("Customer number: " + num2.getCustomerNumber() + " is worth " + num2.calculateValue() + "!"); 

     BusinessSubscriber num3 = new BusinessSubscriber (0, 0, 99, 8, 78); 

     num3.setCustomerNumber(3); 

     num3.setPurchases(0); 
     num3.setShippingCost(0); 
     num3.setYears(8); 
     num3.setSupportTime(0); 

     num2.setProductPrice(99); 
     num2.setFee(78); 
     num2.setSupportCost(25); 

     System.out.println("Customer number: " + num3.getCustomerNumber() + " is worth " + num3.calculateValue() + "!"); 

     // int purchases, int shippingCost, int productPrice, int _years, int _fee 

     Subscriber num4 = new Subscriber (12, 33, 99, 5, 78); 

     num4.setCustomerNumber(4); 

     num4.setPurchases(12); 
     num4.setShippingCost(33); 
     num4.setYears(5); 

     num4.setProductPrice(99); 
     num4.setFee(78); 

     System.out.println("Customer number: " + num4.getCustomerNumber() + " is worth " + num4.calculateValue() + "!"); 

     Customer num5 = new Customer (8, 33, 99); 

     num5.setCustomerNumber(5); 

     num5.setPurchases(8); 
     num5.setShippingCost(33); 
     num5.setProductPrice(99); 

     System.out.println("Customer number: " + num5.getCustomerNumber() + " is worth " + num5.calculateValue() + "!"); 

     // sortera efter calculatevalue metoden i businesssubsriber 
} 

代碼的輸出:

Customer number: 1 is worth 822! 
Customer number: 2 is worth 1404! 
Customer number: 3 is worth 624! 
Customer number: 4 is worth 1182! 
Customer number: 5 is worth 528! 

我想客戶號2是在上面,最後5個,即對​​它們進行排序。我怎麼做?謝謝。

我試過這個,但那夠了嗎?

Integer [] sort = {num1.calculateValue(), num2.calculateValue(), num3.calculateValue(), num4.calculateValue(), num5.calculateValue()}; 

List<Integer> l1 = Arrays.asList(sort); 

Collections.sort(l1); 
System.out.println("Values sorted: " + l1); 

但更有效的方法是把它放在地圖中,循環並打印出來。我怎麼做?

此外,我試圖儘可能地編輯文本,所以它看起來不亂。

回答

0

如果要排序基於calculateValue您的客戶()可以B可以這樣做:

Map sortedCustomer= new TreeMap(); 
sortedCustomer.put(num1.calculateValue(),num1); 
sortedCustomer.put(num2.calculateValue(),num2); 
sortedCustomer.put(num3.calculateValue(),num3); 
sortedCustomer.put(num4.calculateValue(),num4); 
sortedCustomer.put(num5.calculateValue(),num5); 
//sortedCustomer is in ascending order...reverse it 
sortedCustomerDescending =(TreeSet)sortedCustomer.descendingSet(); 
//get entrySet 
    Set set = sortedCustomerDescending.entrySet(); 
    // Get an iterator 
    Iterator i = set.iterator(); 
    // Display elements 
    while(i.hasNext()) { 
    Map.Entry me = (Map.Entry)i.next(); 
    //print the entries 
    System.out.print(me.getKey() + ": "); 
    System.out.println(me.getValue()); 
    } 
0

我假設你想在降序排序。

您需要通過執行器Comaprator並將其傳遞給Collections#sort方法以及包含您要排序的對象的List。

東西liek這應該這樣做

public void testSort() { 
    List<Customer> cL = Arrays.asList(new Customer(1, 822), 
      new Customer(2, 1404), 
      new Customer(3, 624), 
      new Customer(4, 1182), 
      new Customer(5, 528)); 
    Collections.sort(cL, new Comparator<Customer>() { 
     @Override 
     public int compare(Customer o1, Customer o2) { 
      return o2.getCalc() - o1.getCalc(); 
     } 
    }); 

    for(Customer c : cL) { 
     System.out.println("Customer number: " + c.getCustomerNumber() 
       + " is worth " + c.getCalc() + "!"); 
    } 
} 

輸出:

Customer number: 2 is worth 1404! 
Customer number: 4 is worth 1182! 
Customer number: 1 is worth 822! 
Customer number: 3 is worth 624! 
Customer number: 5 is worth 528! 

爲了簡單起見,我已經加入Customer一提起calc本身,你可能想要實現比較爲BusinessSubscriber類作爲你正在尋找的排序值來自它。

0

你的客戶類應該實現Comparable接口

public class Customer implements Comparable<Customer>{ 

你應該重寫它的compareTo方法

@Override 
public int compareTo(Customer cus) { 
int calculatedValue = cus.calculateValue(); 
return calculatedValue - this.calculateValue(); 
} 

最後,在你的PRINTVALUE類,添加這些客戶ArrayList和使用集合。排序

ArrayList<Customer> custList = new ArrayList<>(); 
custList.add(num1); 
custList.add(num2); 
custList.add(num3); 
custList.add(num4); 
custList.add(num5); 

Collections.sort(custList); 

for (Iterator iterator = custList.iterator(); iterator.hasNext();) { 
    Customer customer = (Customer) iterator.next(); 
    System.out.println("Customer number: " + customer.getCustomerNumber() + " is worth " + customer.calculateValue() + "!"); 
} 
0

如果你有對象的列表,你可以用Collections.sort排序。

愚蠢的例子(沒有編譯或運行):

Collections.sort(listOfUsers, new Comparator<User>() { 
    @Override 
    public int compare(User u1, User u2) { 
     return Integer.compare(u1.getAge(), u2.getAge()); 
    } 
}); 

這種比較可能不會恰好是你需要做什麼,但你的想法:-)

相關問題