/** Stock.java - Stock class for Day trading program. * We need to store the stock symbol, and have a way of getting the price * from the Web. Would also be nice to keep track of how much we * "bought" the stock for and number of shares? * * It makes sense to have attributes to keep track of the total amount * of money we have invested and received for this stock. I'll call these * attributes totalCost and totalProceeds. It costs money to buy stock * (negative cash balance) and you make money when you sell. Hopefully, * the result after buying and selling is a net positive cash balance, * indicating a profit or capital gain. */ import java.io.*; import java.net.*; import java.util.*; public class Stock { private String symbol; private double latestPrice; private int numShares; private double totalCost; private double totalProceeds; public Stock(String stockSymbol) { symbol = stockSymbol; numShares = 0; totalCost = 0.00; totalProceeds = 0.00; } ////////////////////////////////////////////////////////////////// // We need to write functions buy() and sell() to keep track of our // transactions. // These 2 functions return double. In the case of buy, it tells // us how much our purchase was. For sell, how much $ we got back. ////////////////////////////////////////////////////////////////// public double buy(int howManyShares) throws MalformedURLException, IOException, InterruptedException { } public double sell(int howManyShares) throws MalformedURLException, IOException, InterruptedException { } // Sell all shares public double sellAll() throws MalformedURLException, IOException, InterruptedException { return sell(numShares); } public int getNumShares() { return numShares; } public double getTotalCost() { return totalCost; } public double getTotalProceeds() { return totalProceeds; } public String toString() { return symbol + " " + numShares + " shares, cost $ " + totalCost + ", proceeds $ " + totalProceeds; } /** getLatestPrice() - go to the Web to read the price, set the * latestPrice attribute and return this value */ public double getLatestPrice() throws MalformedURLException, IOException, InterruptedException { String urlString = "http://finance.yahoo.com/q?s=" + symbol; URL url = new URL(urlString); InputStream urlIn = url.openStream(); BufferedReader in = new BufferedReader(new InputStreamReader(urlIn)); String line; // Look for the line from the Web page that says "Last Trade:" while(true) { line = in.readLine(); if (line == null) { System.out.printf("I couldn't find %s's trade data, sorry.", symbol); System.exit(1); } if (line.indexOf("Last Trade:") >= 0) break; } // Okay, it's a very long line, but shortly after the string "Last Trade" // we should see the share price, as in this example: // 8%">Last Trade: // 107.99< int position = line.indexOf("Last Trade:"); line = line.substring(position, position + 100); // Now that we have an extract of the Web page containing the desired // price: // Set up a StringTokenizer to token out the '<' and '>' symbols, and // look for the token that has a '.' in it. StringTokenizer tok = new StringTokenizer(line, "<>"); while(tok.hasMoreTokens()) { String token = tok.nextToken(); if(token.contains(".")) { latestPrice = Double.parseDouble(token); break; } } return latestPrice; } }