import java.util.Random; import java.text.NumberFormat; public class CompanyStockSolution { private int index; private double record[]; private Random random; private int shares; private final static int STOCKS_PER_DAY = 20; private boolean bankrupt = false; public CompanyStockSolution (int numDays) { random = new Random(); record = new double[numDays*STOCKS_PER_DAY+1]; record[0] = random.nextInt(201) + 100; shares = random.nextInt(5) + 50; index = 1; } public void monitorDay () { for (int i=0; iSTOCKS_PER_DAY)?(index-STOCKS_PER_DAY-1):0; if (record[index-1]-MARGIN>record[minimum]) trend = "rising"; else if (record[index-1]STOCKS_PER_DAY)?(index-STOCKS_PER_DAY-1):0; // This formats the output into monetary form; it was not required. NumberFormat moneyMaker = NumberFormat.getCurrencyInstance(); double priceChange = (record[index-1]-record[minimum]); String dayPrice = moneyMaker.format(Math.abs(priceChange)); result += "; the price changed by " + dayPrice + ".; Total value " + moneyMaker.format(record[index-1]);; return result; } public void prospers (int priceRise) { if (!bankrupt) record[index] = record[index-1] + priceRise; else record[index] = 0; index++; } public void slumps (int priceDrop) { if (!bankrupt) { record[index] = record[index-1] - priceDrop; if (record[index] <= 0) { record[index] = 0; bankrupt = true; } } else record[index] = 0; index++; } }