/* Example of a Web robot. Let's write a program that will go to the Web * and read a share price. * We can generalize the program, so it's not always just IBM. */ import java.io.*; import java.net.*; import java.util.*; public class Driver { public static void main(String [] args) throws MalformedURLException, IOException, InterruptedException { // Read our desired portfolio from portfolio.txt // Feel free to change the desired portfolio before running the program! // But please keep it short (~4 stocks). // Each line of portfolio.txt has a stock symbol and number of shares. double totalInvested = 0.00; double totalProceeds = 0.00; ArrayList list = new ArrayList(); BufferedReader inFile = new BufferedReader(new FileReader("portfolio.txt")); // Read portfolio.txt and "buy" the stocks. while(true) { String line = inFile.readLine(); if (line == null) break; // Add code here that tokenizes the line, creates a Stock object, // and "buys" some stock. Also, don't forget to add the stock // object to the ArrayList. We'll need to go thru our portfolio // later. } // Hold ... wait time. For now, it's set to 30 seconds. // Once your program is working, we'll change it to a longer period. Thread.sleep(30 * 1000); // Sell stocks! for (int i = 0; i < list.size(); ++i) { totalProceeds += list.get(i).sellAll(); } // How did we do? for (int i = 0; i < list.size(); ++i) { System.out.println(list.get(i)); } System.out.printf("total cost $ %.2f, total proceeds $ %.2f\n", totalInvested, totalProceeds); double profit = totalProceeds - totalInvested; double percentProfit = 100.0 * (profit / totalInvested); if (percentProfit > 0.0) { System.out.printf("PROFIT = $%.2f or %.1f%% of your investment.\n", profit, percentProfit); } else if (percentProfit < 0.0) { System.out.printf("LOSS = $%.2f or %.1f%% of your investment.\n", -profit, percentProfit); } else { System.out.printf("You broke even on your $%.2f investment.\n", totalInvested); } } }