2014-03-31 112 views
0

我有一個程序,用戶在股票對象中輸入一個數組列表。股票對象由股票代碼,股數和每股成本組成。只要用戶不斷選擇第一個選項,就會提示他們將股票對象添加到不斷增長的數組列表中。我希望它能夠在用戶輸入2時顯示最後輸入的250個對象的LIFO平均價格。如何顯示對象的數組列表的平均成本?例如,如果用戶輸入陣列列表中的平均對象

AAPL 200 15 

AAPL 300 20 

,現在他們已經在每個15300更在每個20200股,但我只想第一250的平均值。這裏是我的代碼:

package stocks; 
import java.util.*; 


public class Stocks { 
    private String sym; 
    private List<Purchase> purchases; 

    public Stocks(final String symbol) { 
     this.sym = symbol; 
     purchases = new ArrayList<Purchase>(); 
    } 

    public void addPurchase(final int amt, final double cost){ 
     purchases.add(new Purchase(amt,cost)); 
    } 

    public String getSym(){ 
     return sym; 
    } 

    public void setSym(){ 
     this.sym = sym; 
    } 

    public double getAvg250() { 
     int i = 0; 
     int total = 0; 
     int shares = 0; 
     while (i < purchases.size()) { 
      Purchase p = purchases.get(i); 
      if (shares + p.getAmt() >= 250) { 
       total += (250 - shares) * p.getCost(); 
       shares = 250; 
       break; 
      } 
      shares += p.getAmt(); 
      i++; 
     } 
     return total * 1.0/shares; 
    } 


class Purchase { 
    private int amt; 
    private int cost; 

    public Purchase(int amt, double cost){ 

    } 

    public int getAmt() { 
    return amt; 
} 

public void setAmt(int amt) { 
    this.amt = amt; 
} 

public int getCost() { 
    return cost; 
} 

public void setCost(int cost) { 
    this.cost = cost; 
} 

public static void main(String[] args) { 

     int choice = 0; 

     while (choice == 0){ 
     System.out.println("Enter 1 to input a new stock, or 2 to query a stock's price, 3 to quit: "); 
     Scanner sc1 = new Scanner (System.in); 
     choice = sc1.nextInt(); 



     if(choice==1){ 
      ArrayList<Stocks> StocksList = new ArrayList<Stocks>(); 
      Scanner sc2 = new Scanner (System.in); 
      System.out.println("Please enter the stock symbol: "); 
      String sym = sc2.next(); 
      System.out.println("Please enter the number of shares: "); 
      int amt = sc2.nextInt(); 
      System.out.println("Please enter the price per share: "); 
      double cost = sc2.nextDouble(); 


      Map<String, Stocks> stocks = new HashMap<String, Stocks>(); 

      Stocks s = stocks.get(sym); 
      if (s == null) { 
       s = new Stocks(sym); 
       stocks.put(sym, s); 
      } 
      s.addPurchase(amt, cost); 
      StocksList.add(s); 




      System.out.println(getAvg250()); 

     } 
     choice = 0; 

     if(choice==3){ 
      System.exit(0); 
     } 
     } 
    } 
    } 
} 
+0

您準備做什麼?你的名單可以有不同種類的股票,或只有一種? – spinlok

+0

它可以有不同的類型,稍後我將添加一個搜索功能,該功能將顯示用戶搜索的股票的平均成本 – user2921899

+0

您是否需要維護股票行情的每個條目的原始成本,或者只是一個平均運行? –

回答

0

我將設計數據結構略有不同,如果你希望能夠很容易地計算出平均一個股票代碼。您可以保留每個股票代碼的股票購買清單。該代碼會是這個樣子(在您的getter/setter方法填寫):

public class Stock { 
    private String symbol; 
    private List<Purchase> purchases; 

    public Stock(final String symbol) { 
     this.symbol = symbol; 
     purchases = new ArrayList<Purchase>(); 
    } 

    public void addPurchase(final int amt, final int cost) { 
     purchases.add(new Purchase(amt, cost)); 
    } 
} 

public class Purchase { 
    private int amt; 
    private int cost; 
} 

這樣你就可以計算出第一個250股(最大值)這樣的平均成本:

public float getAvg250() { 
    int i = 0; 
    int total = 0; 
    int shares = 0; 
    while (i < purchases.size()) { 
     Purchase p = purchases.get(i); 
     if (shares + p.getAmt() >= 250) { 
      total += (250 - shares) * p.getCost(); 
      shares = 250; 
      break; 
     } 
     shares += p.getAmt(); 
     i++; 
    } 
    return total * 1.0/shares; 
} 

由於要添加搜索功能,請將所有對象添加到由O(1)檢索的股票代碼索引的HashMap中。這在添加購買時也有幫助,因爲您需要檢索相應的Stock對象以更新購買清單:

Map<String, Stock> stocks = new HashMap<String, Stock>(); 
// Get input from the user 
//...... 
Stock s = stocks.get(sym); 
if (s == null) { 
    s = new Stock(sym); 
    stocks.put(sym, s); 
} 
s.addPurchase(amt, cost); 
+0

使用這種方法可以以相同的方式將對象輸入到列表中嗎? – user2921899

+0

是的,我已經更新了答案,以包含想要的框架。 – spinlok

+0

我從來沒有用過hashmap,所以我試圖去理解它。股票>來自hashmap聲明在哪裏? – user2921899