Java LinkedList with Object

时间:2012-10-13 06:40:58

标签: java

尝试实现模拟投资组合的LinkedList,包含Stock对象。我正在努力弄清楚如何正确地遍历列表并检查每个库存是否包含某些参数。 SHAREPRICE方法是我特别遇到的问题,如果有人可以提供帮助,我会非常感激。到目前为止我所拥有的:

import java.util.*;

public class Portfolio<AnyType> implements Iterable<AnyType> {

public int balance, shares;
private Stock<AnyType> beginMarker, endMarker, temp;

LinkedList<Stock> Portfolio = new LinkedList<Stock>();
java.util.Iterator<Stock> iter = Portfolio.iterator();

public int CASHIN(int x) {
    balance = x;
    return balance;
}

public int CASHOUT(int y) {
    balance = balance + (-y);
    return balance;
}

public int CASHBALANCE() {
    return balance;
}

public void BUY(String t, int s, float pp) {
    temp = new Stock<AnyType>(t, s, pp, pp, 0, null, null);
    Portfolio.add(temp);
    shares = shares + s;
}

public void SELL(String t, int s, float pp) {
    shares = shares - s;
}

public void SHAREPRICE(String t, float pp)
{
    if(Portfolio.contains(Stock.)
    {

    }


}

public void QUERY(String t) {

}

public int COUNTPORTFOLIO() {
    return shares;
}

public void PRINTPORTFOLIO() {

}

public java.util.Iterator<AnyType> iterator() {
    return new Iterator();
}

private class Iterator implements java.util.Iterator<AnyType> {
    private Stock<AnyType> current = beginMarker.next;
    private boolean okToRemove = false;

    public boolean hasNext() {
        return current != endMarker;
    }

    public AnyType next() {
        if (!hasNext())
            throw new java.util.NoSuchElementException();

        AnyType nextItem = (AnyType) current.getTicker();
        current = current.next;
        okToRemove = true;
        return nextItem;
    }

    public void remove() {
        if (!okToRemove)
            throw new IllegalStateException();

        Portfolio.this.remove(current.prev);
        okToRemove = false;
    }
}

private class Stock<AnyType> implements Comparable<Stock<AnyType>> {

    public String getTicker() {
        return ticker;
    }

    public void setTicker(String ticker) {
        this.ticker = ticker;
    }

    public float getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(float purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    public float getLatestPrice() {
        return latestPrice;
    }

    public void setLatestPrice(float latestPrice) {
        this.latestPrice = latestPrice;
    }

    public float getPctChange() {
        return pctChange;
    }

    String ticker;
    int sharesOwned;
    float purchasePrice, latestPrice;
    float pctChange = (latestPrice - purchasePrice) / purchasePrice;
    Stock<AnyType> prev, next;

    public Stock(String ticker, int sharesOwned, float purchasePrice,
            float latestPrice, float pctChange, Stock<AnyType> prev,
            Stock<AnyType> next) {
        this.ticker = ticker;
        this.sharesOwned = sharesOwned;
        this.purchasePrice = purchasePrice;
        this.latestPrice = latestPrice;
        this.pctChange = pctChange;
        this.prev = prev;
        this.next = next;
    }

    public int compareTo(Stock<AnyType> pctChange) {
        return ((Comparable) this.pctChange)
                .compareTo(Stock.getPctChange());

    }

}

}

class TestPortfolio {
public static void main(String[] args) {

}
}

1 个答案:

答案 0 :(得分:0)

前进方向:

while(itr.hasNext())
{
  System.out.println(itr.next());
}

反向

while(itr.hasPrevious())
  System.out.println(itr.previous());

}