将相同的String值转换为Object

时间:2015-11-04 17:22:15

标签: java reflection casting

我有一个对象分享:

Share tea = new Share("TEA", "Common", 0, 100);

ArrayList<Share> shares = new ArrayList<Share>();
shares.add(tea);

我想做的是,从键盘读取参数,将“茶”字符串直接转换为共享茶对象:

Trade trade = new Trade( tea, Boolean.parseBoolean(buyOrSell), Integer.parseInt(quantity), Double.parseDouble(tradePrice));

我应该放什么而不是茶,因为我的构造函数正在等待Share而不是String。用户正在输入一个字符串,我不需要创建一个新实例,我必须使用已存在的Share对象“tea”。

Share.java类:

public class Share {

private String shareSymbol = "";
private String type = "Common";
private double lastDividend = 0;
private double fixedDividend = 0;
private int parValue = 0;

// Calucul values


public String getShareSymbol() {
    return shareSymbol;
}

public String getType() {
    return type;
}

public double getLastDividend() {
    return lastDividend;
}

public double getFixedDividend() {
    return fixedDividend;
}

public int getParValue() {
    return parValue;
}

// Constructor without Fixed Dividend
public Share(String shareSymbol, String type, int lastDividend, int parValue) {
    this.shareSymbol = shareSymbol;
    this.type = type;
    this.lastDividend = lastDividend;
    this.parValue = parValue;
}

// Constructor with Fixed Dividend
public Share(String shareSymbol, String type, int lastDividend,
        int fixedDividend, int parValue) {
    this(shareSymbol, type, lastDividend, parValue);
    this.fixedDividend = fixedDividend / 100.0;
}
public String toString(){
    String result="";
    result += shareSymbol + "              "+type + "     " + lastDividend + "           " + fixedDividend + "           " + parValue + "\n";
    return result;
}

}

Trade.java类:

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

// The class trade allows the stock, the quantity and the price to be intialised
public class Trade {
Share share;
private int quantity;
double price;
private double dividendYield;
private double pERatio;
private boolean buyOrSell;
private Date tradeDate;

public Trade(Share share, boolean buyOrSell, int quantity, double price) {
    this.share = share;
    this.buyOrSell = buyOrSell;
    this.quantity = quantity;
    this.price = price;
    tradeDate = Calendar.getInstance().getTime();

}



public String toString() {
    String result = "";
    result += "stock symbol : " + share.getShareSymbol() + " \n";
    result += "Buy or Sell : " + buyOrSell + " \n";
    result += "quantity :" + quantity + " \n";
    result += "price : " + price + " \n";
    result += "Dividend Yield : " + dividendYield + " \n";
    result += "P/E Ratio : " + pERatio + " \n";
    result += "tradeDate : " + tradeDate + " \n\n";
    return result;

}

public Share getShare() {
    return share;
}

public int getQuantity() {
    return quantity;
}

public double getPrice() {
    return price;
}

public double getDividendYield() {
    return dividendYield;
}

public double getpERatio() {
    return pERatio;
}

public Date getTradeDate() {
    return tradeDate;
}

public double calcDividendYield() {
    if ("Common".equalsIgnoreCase(share.getType())) {
        dividendYield = share.getLastDividend() / price;
    } else if ("Preferred".equalsIgnoreCase(share.getType())) {
        dividendYield = share.getFixedDividend() * share.getParValue()
                / price;
    }

    return dividendYield;
}

public double calcPERatio() {
    if (dividendYield > 0)
        pERatio = price / dividendYield;
    return pERatio;
}

}

2 个答案:

答案 0 :(得分:0)

您的构造函数正在等待共享。所以给它:

/* Read parameters from keyboard, stock in s1, s2, i1 and i2 variables */    
Trade trade = new Trade( new Share(s1,s2,i1,i2), Boolean.parseBoolean(buyOrSell), Integer.parseInt(quantity), Double.parseDouble(tradePrice));

否则,您可以实现一个为您执行此操作的方法(在Share类中)

public static Share stringToShare(String s1, String s2, int t1, int t2){
   return new Share(s1, s2, t1, t2);
}

然后:

Trade trade = new Trade( Share.stringToShare(...), Integer.parseInt(quantity), Double.parseDouble(tradePrice));

编辑:更好地使用真正的变量名称,而不是s1s2等。

答案 1 :(得分:0)

我通过创建一个新的构造函数来解决这个问题:

        Trade trade = new Trade(shares, stockSymbol, Boolean.parseBoolean(buyOrSell),
                        Integer.parseInt(quantity), Double.parseDouble(tradePrice));

这是新的构造函数:

public Trade(ArrayList<Share> shares, String stockSymbol,
            boolean buyOrSell, int quantity, double price) {
        Iterator<Share> iter = shares.iterator();
        int index = -1;
        while (iter.hasNext()) {
            Share share = iter.next();
            index++;
            if (stockSymbol.equalsIgnoreCase((share.getShareSymbol()))) {
                this.share = shares.get(index);
                this.buyOrSell = buyOrSell;
                this.quantity = quantity;
                this.price = price;
                tradeDate = Calendar.getInstance().getTime();

                break;
            }
        }

    }

有点抱怨,因为我还要创建一个新方法来获取给定String的Share(例如,具有相同的名称)。但那就是我所需要的。 使用Java Reflection创建它会更干净,而不必创建新的构造函数。因此,如果任何人对代码更聪明有所了解,我会很高兴!