【问题标题】:Search hashmap for key and print certain values在 hashmap 中搜索键并打印某些值
【发布时间】:2014-03-31 11:58:13
【问题描述】:

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

AAPL 50 99.99AAPL 300 50.00

因此,如果用户在 99.99 买入 50 股,在 50.00 买入 300 股,则应该使用正确的价格平均最后购买的 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);


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

【问题讨论】:

    标签: java


    【解决方案1】:

    如果你想遍历 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
            }
        }
    

    【讨论】:

    • 我希望用户输入一个股票代码,然后它将搜索哈希图并打印为该股票购买的最后 250 股的平均值。
    • 每当我在控制台中输入 2 时,程序就会终止而没有任何错误。
    • 请调试。我确定您使用的是 eclipse 或 netbeans 或其他一些 IDE。请调试。我们只能建议您必须编写代码并给我们确切的问题。
    • pairs.getValue() // this is the cost for share. write logic here to find the last 259 shares and calculate avg你能解释一下这条线吗?
    • 在这一行中,您将获得特定代码的股票价格。然后您可以编写自己的逻辑来计算该股票的平均值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多