2012-10-13 88 views
0

試圖實現一個模擬包含股票對象的投資組合的LinkedList。我正在努力弄清楚如何正確迭代列表並檢查每個股票是否包含某些參數。 SHAREPRICE方法是我特別遇到的問題,如果有人能夠幫助解決這個問題,我會非常感激。我有什麼至今:Java LinkedList與對象

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

您應該_really_使用[Java命名約定(http://www.oracle。 COM/technetwork/JAVA/JavaSE的/文檔/ codeconvtoc-136057.html)。 –

回答

0

正向:

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

反方向

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

}