Java Reference
In-Depth Information
9
10 // first stock
11 System.out.print("First stock's symbol: ");
12 String symbol1 = console.next();
13 Stock stock1 = new Stock(symbol1);
14 double profit1 = makePurchases(stock1, console);
15
16 // second stock
17 System.out.print("Second stock's symbol: ");
18 String symbol2 = console.next();
19 Stock stock2 = new Stock(symbol2);
20 double profit2 = makePurchases(stock2, console);
21
22 // report which stock made more money
23 if (profit1 > profit2) {
24 System.out.println(symbol1 + " was more " +
25 "profitable than" + symbol2 + ".");
26 } else if (profit2 > profit1) {
27 System.out.println(symbol2 + " was more " +
28 "profitable than " + symbol1 + ".");
29 } else { // profit1 == profit2
30 System.out.println(symbol1 + " and " + symbol2 +
31
" are equally profitable.");
32 }
33 }
34
35 // make purchases of stock and return the profit
36 public static double makePurchases(Stock currentStock,
37 Scanner console) {
38 System.out.print("How many purchases did you make? ");
39 int numPurchases = console.nextInt();
40
41 // ask about each purchase
42 for ( int i = 1; i <= numPurchases; i++) {
43 System.out.print(i +
44 ": How many shares, at what price per share? ");
45 int numShares = console.nextInt();
46 double pricePerShare = console.nextDouble();
47
48 // ask the Stock object to record this purchase
49 currentStock.purchase(numShares, pricePerShare);
50 }
51
52 // use the Stock object to compute profit
Search WWH ::




Custom Search