【问题标题】:Average objects in an arraylist数组列表中的平均对象
【发布时间】:2014-05-10 04:54:45
【问题描述】:

我有一个程序,用户将股票对象输入到数组列表中。股票对象由股票代码、股票数量和每股成本组成。只要用户继续选择第一个选项,就会提示他们将库存对象添加到不断增长的数组列表中。我希望这样当用户输入 2 时,我希望显示最后输入的 250 个对象的 LIFO 平均价格。如何显示对象数组列表的平均成本?例如,如果用户输入

AAPL 200 15 

AAPL 300 20

,现在他们以15 的价格购买了200 的股票,并以20 的价格购买了更多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);




           System.out.println(getAvg250());

         }
         choice = 0;

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

【问题讨论】:

  • 你到底想做什么?您的清单可以包含不同种类的股票,还是只有一种?
  • 它可以有不同的种类,稍后我将添加一个搜索功能,它将显示用户搜索的股票的平均成本
  • 您是否需要为股票代码的每个条目保持原始成本,还是只是一个运行平均值?
  • 用户输入2时会计算平均值,所以最近的250个分享会被平均。

标签: java


【解决方案1】:

如果您希望能够轻松计算一个股票代码的平均值,我会稍微不同地设计数据结构。您可以为每个股票代码保留一份股票购买清单。代码看起来像这样(填写你的 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;
}

由于您要添加搜索功能,请将所有 Stock 对象添加到由股票代码索引的 HashMap 中,以进行 O(1) 检索。这在添加购买时也有帮助,因为您需要检索相应的 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);

【讨论】:

  • 使用这个方法可以同样的方式将对象输入到列表中吗?
  • 是的,我已经更新了答案,包括了想要的内容。
  • 我以前从未使用过 hashmap,所以我试图理解它。 hashmap 声明中的 来自哪里?
  • Map 是参数类型,就像列表一样。它有两个类型参数,一个用于键和值。对于我们的例子,股票代码 (String) 是键,Stock 对象是值。您可以在此处阅读有关Map 的更多信息 - docs.oracle.com/javase/7/docs/api/java/util/Map.html
  • 我似乎只能将元素添加到 hashmap 而不是整个 stock 对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2022-01-16
  • 2016-02-19
  • 2012-06-03
相关资源
最近更新 更多