0

Stock オブジェクトで構成される Portfolio をシミュレートする LinkedList を実装しようとしています。リストを適切に反復処理し、各株式に特定のパラメーターが含まれているかどうかを確認する方法を理解するのに苦労しています。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) {

}
}
4

1 に答える 1

0

順方向:

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

逆方向

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

}
于 2012-10-13T06:45:20.063 に答える