2014-03-31 63 views
0

我有一個程序,我想輸入一個假股票代碼,股票數量和每股價格。我希望用戶能夠搜索股票的數組列表,如果找到某個股票,則會顯示最後250股股票的後期平均價格。用戶應該能夠輸入一個股票不止一次,例如搜索鍵值和打印某些值的hashmap

AAPL 50 99.99AAPL 300 50.00

因此,如果用戶在50.00買入99.99 50萬股和300萬股,這應該平均在過去250買股票使用正確的價格。

這裏是我到目前爲止,我在搜索hashmap,然後顯示該特定股票的平均值有麻煩。

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); 


     } 
     choice = 0; 

     if (choice == 2){ 
      Scanner sc3 = new Scanner (System.in); 
      System.out.println("Please enter the symbol of the stock you wish to see: "); 
      String search = sc3.next(); 

     } 


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

回答

0

如果你想遍歷HashMap,你可以在下面做。 Thouhg我不清楚你的問題完全

Map<String, Stocks> stocks = new HashMap<String, Stocks>(); 
for (Object key : stocks.keySet()) { 
System.out.println("Key : " + key.toString() + " Value : " 
     + stocks.get(key)); 
} 

或者你也可以做

Iterator it = mp.entrySet().iterator(); 
while (it.hasNext()) { 
    Map.Entry pairs = (Map.Entry)it.next(); 
    System.out.println(pairs.getKey() + " = " + pairs.getValue()); 
} 

UPDATE

,你可以這樣做下面

Iterator it = mp.entrySet().iterator(); 
    while (it.hasNext()) { 
     Map.Entry pairs = (Map.Entry)it.next(); 
     if(pairs.getKey() != null && pairs.getKey().equals(sym)) { 
      pairs.getValue() // this is the cost for share. write logic here to find the last 259 shares and calculate avg 
     } 
    } 
+0

我希望用戶輸入股票代碼,然後它將搜索hashmap並打印平均值爲該股票購買的最後250股股票。 – user2921899

+0

更新後。請檢查。 –

+0

每當我輸入2到控制檯,程序終止沒有任何錯誤。 – user2921899