私は現在、3 年目のプログラミング プロジェクトとフォリオ トラッカーを行っています。:/ Stock_API および Portfolio_API インターフェイス (およびそれらの実装) と、インスタンス化されたときに 2 つのパラメーターを受け取る GUI クラスを作成しました。
public GUI(Portfolio_API p, Stock s){
tempPort = p;
tempStock = s;
}
このコンストラクターは、実装を GUI に公開することなく、これらのインターフェイスの実装を GUI に取得する方法として使用します (これは、このプロジェクトの主な目的の 1 つです)。ポートフォリオ オブジェクトには名前 (文字列) と ArrayList があります。株式オブジェクトには、ティッカー シンボル (文字列)、株式名 (文字列)、株式の値 (float)、株式の数 (int)、および保有の値 (float) があります。
GUI には、portCollection_API タイプのオブジェクトを保持する portCollection 配列リストがあります。これは、システムが複数のポートフォリオを追跡できるようにするためです。また、上記のコード ブロックで述べたように、tempStock および tempPort オブジェクトがあります。
プログラムについて詳しく説明して申し訳ありませんが、コンテキストを理解できるようにするのが最善だと思いました. とにかく目の前の問題。GUI を使用してティッカー シンボル、株式名、および株式数を取得し、開いている現在のポートフォリオに株式を追加する方法があります (各ポートフォリオには独自のタブがあります)。メソッドは次のようになります。
public void addStock() {
int num_shares = 0;
float dailyChange = 0.0f;
float stockValue = 0.0f;
boolean succeed = true;
// GUI gets information of stock from user
String ticker = JOptionPane.showInputDialog(frame,
"Enter the ticker symbol:");
String stockName = JOptionPane.showInputDialog(frame,
"Enter the Stock name:");
try {
num_shares = Integer.parseInt(JOptionPane.showInputDialog(frame,
"Enter the number of shares:"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame,
"Number of shares was not an integer. Try again");
succeed = false;
}
// If parsing was successful...
if (succeed) {
tempStock.setTicker(ticker);
tempStock.setNumberOfShares(num_shares);
tempStock.setStockName(stockName);
// Fetches newest value using the current ticker symbol
boolean succeedUpdate = tempStock.updateShareValue();
if (succeedUpdate) {
tempStock.calculateValueOfHolding();
// Adds to the current portfolio...
String tabName = tabbedPane.getTitleAt(tabbedPane
.getSelectedIndex());
System.out.println(tabName);
findPortfolio(tabName).addStock(tempStock);
findPortfolio(tabName).sort();
// ...Then adds it to the table
JPanel j = (JPanel) tabbedPane.getSelectedComponent()
.getComponentAt(0, 0);
JViewport v = ((JScrollPane) j.getComponent(0)).getViewport();
JTable table = (JTable) v.getComponent(0);
float currentTotal = findPortfolio(tabName).getTotal();
// Updates the total label
((JLabel) j.getComponent(1)).setText("Total: " + currentTotal);
Object[] newStock = { tempStock.getTicker(),
tempStock.getStockName(),
tempStock.getNumberOfShares(),
tempStock.getShareValue(),
tempStock.getValueOfHolding() };
((DefaultTableModel) table.getModel()).addRow(newStock);
}
}
}
複数の在庫を追加すると、新しい在庫が古い在庫に取って代わり、効果的に上書きされます。それを行っているのはtempStockの再利用だと思います。変数を配列リストに追加すると、その配列リストの一部になり、tempStock変数との関連付けが不要になるのはなぜですか?
上記の arraylists で使用されるメソッド:
private Portfolio_API findPortfolio(String name) {
Portfolio_API p = null;
for (int i = 0; i < portCollection.size(); i++) {
if (portCollection.get(i).getName() == name) {
p = portCollection.get(i);
}
}
これら 2 つは Portfolio クラスにあります。
@Override
public boolean addStock(Stock_API s) {
if (!doesExist(s)) {
portfolio.add(s);
return true;
} else {
return false;
}
}
@Override
public boolean doesExist(Stock_API s) {
boolean found = false;
for (int i = 0; i < portfolio.size(); i++) {
if (portfolio.get(i).getTicker() == s.getTicker()) {
found = true;
}
}
return found;
}
助けを求めてここに来たのは、壁にぶち当たり、本当に助けが必要だからです。誰かが私に何か提案をしてくれたら、私は永遠にあなたに借りがあるでしょう.
ありがとう、クリス