Last active
October 20, 2017 19:57
-
-
Save jraczak/aeeef11a29aec6951e48f5dc1dc0e3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Justin Raczak | |
// This program calculates the max one-day increase of a stock price over a 10-day period. | |
import java.util.*; | |
class Increase { | |
public static void main(String [] args) { | |
// Set up the Scanner | |
Scanner scanner = new Scanner(System.in); | |
// Store the price of the low day in the biggest one-day increase | |
int dayOnePrice = 0; | |
// Store the numerical day for the low price | |
int dayOne = 0; | |
// Store the price of the high day in the biggest one-day increase | |
int dayTwoPrice = 0; | |
// Store the numerical day for the high price | |
int dayTwo = 0; | |
// Store the previous new price we saw so we can deterime if it's part of a larger one-day increase | |
int previousDayPrice = 0; | |
// Store the numerical day for the previous day | |
int previousDay = 0; | |
for (int i = 1; i <= 10; i++) { | |
// Ask the user for 10 stock prices | |
System.out.print("Please input the price of the stock on day " + i + " of the desired time period: "); | |
int newPrice = scanner.nextInt(); | |
// If we haven't stored any values yet, store days 1 and 2 without any comparison | |
if (i < 3) { | |
if (i == 1) { | |
dayOnePrice = newPrice; | |
dayOne = i; | |
} else { | |
dayTwoPrice = newPrice; | |
previousDayPrice = newPrice; | |
dayTwo = i; | |
previousDay = i; | |
} | |
// Compare the price changes between the current day and previous day and currently stored first and second days | |
// If the new difference is greater, replace the stored data. If not, stash the new price as the previous day and continue | |
} else { | |
if ((newPrice - previousDayPrice) > (dayTwoPrice - dayOnePrice)) { | |
dayOnePrice = previousDayPrice; | |
dayOne = previousDay; | |
dayTwoPrice = newPrice; | |
dayTwo = i; | |
} else { | |
previousDayPrice = newPrice; | |
previousDay = i; | |
} | |
} | |
} | |
if ((dayTwoPrice - dayOnePrice) == 0) { | |
System.out.print("The price of this stock never increased. (bummer)"); | |
} else { | |
System.out.print("The highest price increase of $" + (dayTwoPrice - dayOnePrice) + " occurred between days " + dayOne + " and " + dayTwo + "."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment