2014-05-16 68 views
2

我的問題的焦點屬於takeInventory()方法。通過對象的ArrayList對象的給定構造函數值排序

您可以假設InventoryDemomain()方法是有效的(不包括執行takeInventory()方法)。

如果你願意,你可能會發現其他類here

我的takeInventory()方法的目標是對我的list進行排序,並報告Product類型的每個唯一實例的整數值。

這是要由專門的名稱區分:

產品(,成本))。

Product類似名稱的s應該組合在一起(不管成本如何)。

輸出應報告如下:

This shows the desired output.

我假設有排序這個數據比我目前的做法更有效的方法。但是,我不知道其中之一。

import java.util.*; 
public class InventoryDemo 
{ 
    public static void main(String [] args) { 
     ArrayList<Product> list = new ArrayList<Product>(); 
     list.add(new Car("Jaguar", 1000000)); 
     list.add(new Car("Neon", 17000)); 
     list.add(new Tool("JigSaw", 149.18)); 
     list.add(new Car("Jaguar", 110000)); 
     list.add(new Car("Neon", 17500)); 
     list.add(new Car("Neon", 17875.32)); 
     list.add(new Truck("RAM", 35700)); 
     list.add(new Tool("CircularSaw", 200)); 
     list.add(new Tool("CircularSaw", 150)); 
     list.add(new Tool("saw1", 200)); 
     list.add(new Tool("saw2", 150)); 

     if(list.get(9).compareTo(list.get(10)) == 0) { 
      System.out.println("\nBoth saws are of equal value."); 
     } else if(list.get(9).compareTo(list.get(10)) > 0) { 
      System.out.println("\nThe first saw is more expensive."); 
     } else { 
      System.out.println("\nThe second saw is more expensive."); 
     } 

     takeInventory(list); 
    } 

    public static void takeInventory(ArrayList<Product> list) { 
     int inventory[] = new int[list.size()]; 
     int counter = 0; 
     for(Product token: list) { 
      for(int x = 0; x < list.size(); x++) { 
       if(token.compareTo(list.get(x)) == 0) { 
        inventory[counter] = 0; 
       } else { 
        counter++; 
       } 
      } 
     } 

     for(int token : inventory) { 
      System.out.println(token); 
     } 
    } 
} 

如果沒有明確清晰:

我要爲我takeInventory()方法補救。這種方法的客觀目的是對對象給定的ArrayList進行排序,並報告其唯一類型值的總和成本。這在輸出中清楚地表明瞭。輸出的最後一個字符串文字由我的main()方法中的條件生成。其餘的將由takeInventory()方法生產。

我確定我目前的takeInventory()不是工作。

+0

不清楚你想要什麼,請解釋 – Sanjeev

+0

你想用takeInventory方法做什麼? – Husman

+0

你確定你目前的方法正在工作嗎?我認爲'takeInventory'總是打印出零。 – johnchen902

回答

3

我會建立一個Map<String, C>其中C是包含數量(int)和成本(double)的幫助類。遍歷產品列表,併爲每個產品:

  • 如果名稱不在地圖中,請將名稱關聯到new C(1, cost)
  • 如果名稱在地圖中,請將與名稱相關的數量增加1,並將與名稱相關的成本增加cost

最後,遍歷地圖並打印結果;那麼你就完成了。

參考:http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

下面是一些代碼:

import java.util.*; 

class Product { 
    private String name; 
    private double cost; 

    public Product (String name, double cost) { 
     this.name = name; 
     this.cost = cost; 
    } 

    public String getName() { 
     return name; 
    } 

    public double getCost() { 
     return cost; 
    } 
} 

class Car extends Product { 
    public Car(String name, double cost) { 
     super(name, cost); 
    } 
} 

class Truck extends Product { 

    public Truck(String name, double cost) { 
     super(name, cost); 
    } 
} 

class Tool extends Product { 

    public Tool(String name, double cost) { 
     super(name, cost); 
    } 
} 



class Entry { 
    private int quantity = 1; 
    private double cost; 

    public int getQuantity() { 
     return quantity; 
    } 

    public double getCost() { 
     return cost; 
    } 

    public Entry(double cost) { 
     this.cost = cost; 
    } 

    public void add (double cost) { 
     quantity++; 
     this.cost += cost; 
    } 

    @Override 
    public String toString() { 
     return ("Quantity = " + quantity + ", Total cost = " + cost); 
    } 
} 


public class Inventory { 

    static void takeInventory(List<Product> list) { 
     Map<String, Entry> map = new HashMap<>(); 

     for (Product p : list) { 
      Entry e = map.get(p.getName()); 
      if (e == null) { 
       map.put(p.getName(), new Entry(p.getCost())); 
      } else { 
       e.add(p.getCost()); 
      } 
     } 

     for (String s : map.keySet()) { 
      System.out.print(s); 
      Entry e = map.get(s); 
      System.out.println(" " + e);    
     } 
    } 

    public static void main(String [] args) { 
     ArrayList<Product> list = new ArrayList<Product>(); 
     list.add(new Car("Jaguar", 100000)); 
     list.add(new Car("Neon", 17000)); 
     list.add(new Tool("JigSaw", 149.18)); 
     list.add(new Car("Jaguar", 110000)); 
     list.add(new Car("Neon", 17500)); 
     list.add(new Car("Neon", 17875.32)); 
     list.add(new Truck("RAM", 35700)); 
     list.add(new Tool("CircularSaw", 200)); 
     list.add(new Tool("CircularSaw", 150)); 
     list.add(new Tool("saw1", 200)); 
     list.add(new Tool("saw2", 150)); 

     takeInventory(list); 
    } 
} 

輸出:

saw1 Quantity = 1, Total cost = 200.0 
saw2 Quantity = 1, Total cost = 150.0 
CircularSaw Quantity = 2, Total cost = 350.0 
RAM Quantity = 1, Total cost = 35700.0 
JigSaw Quantity = 1, Total cost = 149.18 
Jaguar Quantity = 2, Total cost = 210000.0 
Neon Quantity = 3, Total cost = 52375.32 
+0

您能否提供代碼示例部分?我從來沒有使用過Map()。 – Alex

+0

編輯答案給出一些代碼。 – Zoyd

+0

這是一個有效的修復。我對修復的概念還是有點不穩定,但我只需要多讀一點就可以了。我感謝幫助! – Alex

1

類似@ Zoyd的答案,你可以有一個Map<String, List<Product>>,然後只需添加每個產品進入這樣的地圖:

Map<String, List<Product>> productMap = new HashMap<>(); 
for(Product product : list) { 
    if (!productMap.containsKey(product.getName())) { 
     productMap.put(product.getName(), new ArrayList<Product>()); 
    } 
    productMap.get(product.getName()).add(product); 
} 

然後在您的打印方法,你可以通過地圖鍵集迭代,並在這樣每個列表中添加了所有的產品成本:

for(String productName : productMap.keySet()) { 
    List<Product> products = productMap.get(productName); 
    int quantity = products.size(); 
    double totalCost = 0.0; 
    for (Product product : products) { 
     totalCost += product.getCost(); 
    } 
    System.out.println(String.format("%s: Quantity = %s, Total cost = %s", productName, quantity, totalCost)); 
} 
+0

如何在給定的圖形中生成輸出?我不熟悉這些方法及其迭代。 – Alex

+0

你在if條件後錯過了''''。 – johnchen902

+0

我會使用'LinkedHashMap'來使輸出順序更合理。我更喜歡單個語句: 'productMap.forEach((a,b) - > System.out.println(a +「Quantity =」 + b.size()+「,Total cost =」 + b。 。流()mapToDouble(產品:: getCost)的.sum()));' – johnchen902

0

那麼幹淨的方式做到這一點是利用番石榴的FluentIterable之一,這是根據某些規則過濾集合的Google類。首先做一個謂詞來定義排序:

class sorter implements Predicate<Product> { 
    private final String name; 
    sorter(String name){ 
     this.name=name 
    } 
    @Override 
    boolean apply(Product input){ 
     return (input.name == this.name) 
    } 
} 

如果input.name相同給予謂詞名稱:這將返回true。這適用於Guava的fluentIterable類,用於返回謂詞爲true的所有元素。例如。

List<Product> cars = FluentIterable.from(list).filter(new sorter("car")) 

將產生名稱爲「car」的所有元素的列表。

由於您將爲每個名稱迭代一次,因此它可能會在大集合上出現性能問題,另一方面,因爲它的全部只讀而不會改變列表,所以您可以輕鬆地在本地多線程化它。

雖然它很容易閱讀/維護。

相關問題