我一直在編碼幾個小時,試圖完成我的任務。現在我已經完成了,我想添加一種方法來分類客戶的價值。因此,客戶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);
但更有效的方法是把它放在地圖中,循環並打印出來。我怎麼做?
此外,我試圖儘可能地編輯文本,所以它看起來不亂。