带有对象的Java LinkedList
尝试实现一个LinkedList,它模拟由股票对象组成的投资组合。我正在努力找出如何正确地遍历列表并检查每个股票是否包含某些参数。股票价格法是我特别不喜欢的方法,如果有人能帮我,我将非常感激。到目前为止,我所拥有的:带有对象的Java LinkedList,java,Java,尝试实现一个LinkedList,它模拟由股票对象组成的投资组合。我正在努力找出如何正确地遍历列表并检查每个股票是否包含某些参数。股票价格法是我特别不喜欢的方法,如果有人能帮我,我将非常感激。到目前为止,我所拥有的: import java.util.*; public class Portfolio<AnyType> implements Iterable<AnyType> { public int balance, shares; private Stock<
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) {
}
}
import java.util.*;
公共类公文包实现了Iterable{
公开发行股票;
私人股票开始标记、结束标记、临时标记;
LinkedList公文包=新建LinkedList();
java.util.Iterator iter=Portfolio.Iterator();
公共整数兑现(整数x){
平衡=x;
收益余额;
}
公共整数兑现(整数y){
平衡=平衡+(-y);
收益余额;
}
公共整数现金余额(){
收益余额;
}
公共无效购买(字符串t、整数s、浮动pp){
temp=新股票(t,s,pp,pp,0,null,null);
投资组合。添加(临时);
股份=股份+s;
}
公开作废出售(字符串t、整数s、浮动pp){
股份=股份-s;
}
公开无效股价(字符串t,浮动pp)
{
如果(投资组合)包含(股票)
{
}
}
公共无效查询(字符串t){
}
公共投资组合(){
返还股份;
}
公共投资组合(){
}
public java.util.Iterator迭代器(){
返回新的迭代器();
}
私有类迭代器实现java.util.Iterator{
私人股票当前=beginMarker.next;
私有布尔值okToRemove=false;
公共布尔hasNext(){
返回电流!=结束标记;
}
公共AnyType next(){
如果(!hasNext())
抛出新的java.util.NoSuchElementException();
AnyType nextItem=(AnyType)current.getTicker();
当前=当前。下一步;
okToRemove=true;
返回nextItem;
}
公共空间删除(){
如果(!okToRemove)
抛出新的非法状态异常();
组合。本。删除(当前。上一个);
okToRemove=false;
}
}
私有类股票{
公共字符串getTicker(){
退票机;
}
公共无效设置代码(字符串代码){
this.ticker=ticker;
}
公共浮动getPurchasePrice(){
退货价格;
}
公共作废设置采购价格(浮动采购价格){
this.purchasePrice=采购价格;
}
公共浮点数getLatestPrice(){
返回最晚价格;
}
公共无效设定最晚价格(浮动最晚价格){
this.latestPrice=latestPrice;
}
公共浮点数getPctChange(){
返回pctChange;
}
弦乐器;
播种;
浮动购买价格,最新价格;
浮动价格变化=(最新价格-购买价格)/购买价格;
上交所,下交所;
公开股票(字符串股票代码、整数股票代码、浮动购买价格、,
浮动最新价格、浮动价格变动、股票价格变动、,
库存(下一步){
this.ticker=ticker;
this.sharesOwned=sharesOwned;
this.purchasePrice=采购价格;
this.latestPrice=latestPrice;
this.pctChange=pctChange;
this.prev=prev;
this.next=next;
}
公共内部比较(股票价格变化){
返回((可比)this.pctChange)
.compareTo(Stock.getPctChange());
}
}
}
类测试组合{
公共静态void main(字符串[]args){
}
}
前进方向:
while(itr.hasNext())
{
System.out.println(itr.next());
}
反向
while(itr.hasPrevious())
System.out.println(itr.previous());
}
你真的应该用这个。