1
answer
0
watching
271
views

You are to write a GUI program that will allow a user to buy,sell and view stocks in a stock portfolio. This document willdescribe the minimum expected functions for a grade of 90. Yourmission is to “go and do better.” The meaning of “do better” islargely up to you. For example, you might track the profit or losson the trades. You might allow for sales of partial holdings, orsubsequent purchases to be added to an existing holding. Becreative. Objectives By the end of this project, the student willbe able to • write a GUI program that maintains a cash balance,list of stock holdings, supports buying and selling of stocks anddisplays the current portfolio inventory • demonstrate the abilityto assemble already-written classes into a larger, more complicatedprogram • demonstrate the ability to segregate business logic fromuser interface code. Capabilities At a minimum, the program should• allow a user to buy a stock with a given number of shares andprice per share. • display the current portfolio (stock ticker,number of shares, initial price). • update the portfolio displayfor purchases and sales. • allow the user to sell all of the sharesof a given stock. • give the user an initial cash balance, andupdate and display the balance according to the user's purchasesand sales. • ignore any transaction that causes the cash positionto go below $0. Sample Execution The initial frame has text fieldsfor the stock ticker, number of shares, and price per share. It hasa label for the current cash balance. It also has “buy” and “sell”buttons. Figure 1 Initial Frame Let's buy Apple (ticker AAPL)shares. First we enter the ticker, number of shares and price pershare. Figure 2 Data for Apple Purchase Then we click “Buy.” Noteour cash was reduced accordingly. Our portfolio inventory nowdisplays since it's no longer empty. Figure 3 Click Buy to PurchaseApple Let's buy IBM. We enter the ticker, number of shares andprice per share. Figure 4 Data to Purchase IBM We click “Buy.” Ourcash is reduced appropriately, our portfolio is updated. WidgetViewmoves things around. We won't worry about that. We also won't worryabout the not-quite-right truncation of the last penny of the cashbalance. Figure 5 Click Buy to Purchase IBM Let's buy Microsoft(MSFT). We'll just skip to the completed transaction. Figure 6Purchase of Microsoft We're losing faith in IBM's CEO; let's sellit. We need to tell our portfolio manager which stock we want tosell, and the selling price per share. Since we're selling all ofour shares, we don't need to fill in that field. Figure 7 Data toSell IBM We click “Sell,” and the portfolio listing and cash areupdated. Figure 8 IBM is Sold Hints The StockHolding andPortfolioList classes from earlier Lessons (Writing Classes Better)should be directly usable in this project. The sample screens abovewere generated with a version of that code that had slightlydifferent toString methods, but that's not significant to thefunction of the program. using these classes public classPortFolioList { ArrayList portFolio = new ArrayList(); publicPortFolioList(){ } public void add(StockHolding stock){portFolio.add(stock); } public void remove(String ticker){for(StockHolding str: portFolio){if(str.getTicker().equals(ticker)){ portFolio.remove(str); } break;} } public StockHolding find(String ticker) { for(StockHolding str:portFolio){ if(str.getTicker().equals(ticker)){ return str; } }return null; } public String toString() { String string = "";for(StockHolding str: portFolio){ string = "\n"+str +str.toString(); } return string; } } and the second class publicclass StockHolding { private String ticker; private intnumberShares; private double initialPrice; private doublecurrentPrice; StockHolding( String ticker,int numberShares,doubleinitialPrice){ this.ticker=ticker; this.numberShares=numberShares;this.initialPrice=initialPrice; } public String getTicker() {return ticker; } public int getShares(){ return numberShares; }public double getInitialSharePrice(){ return initialPrice; } publicdouble getCurrentSharePrice(){ return currentPrice; } public doublegetInitialCost(){ return numberShares*initialPrice; } public doublegetCurrentValue() { return numberShares*currentPrice; } doublegetCurrentProfit(){ returnnumberShares*(currentPrice-initialPrice); } voidsetCurrentSharePrice(double sharePrice){ currentPrice=sharePrice; }public String toString(){ String str="stock"+ticker+","+numberShares+" shares bought at "+initialPrice+",current price "+currentPrice; return str; } }

For unlimited access to Homework Help, a Homework+ subscription is required.

Sixta Kovacek
Sixta KovacekLv2
28 Sep 2019

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in

Weekly leaderboard

Start filling in the gaps now
Log in