搜索关键字的hashmap并打印某些值

时间:2014-03-31 02:52:41

标签: java

我有一个程序,我想输入一个假的股票代码,股票数量和每股价格。我希望用户能够搜索股票的arraylists,如果找到某个股票,则显示最后买入的250只股票的LIFO平均价格。用户应该能够多次输入股票,例如

AAPL 50 99.99AAPL 300 50.00

因此,如果用户在50.00买入50股99.99股和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);
         }
         }
       }
     }

1 个答案:

答案 0 :(得分:0)

如果你想迭代HashMap,你可以在下面做。我完全不清楚你的问题

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

<强>更新

你可以做类似下面的事情

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
        }
    }