Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Any necessary imports
- import java.util.*;
- public class StockTrader
- {
- public static void main(String[] args)
- {
- // declare and initialize a Scanner
- Scanner in = new Scanner(System.in);
- // get seed value from user and use it to make the random number
- // generator
- System.out.println("Please enter a seed value [integer]:");
- // you may assume the user inputs an int for the seed
- int seed = in.nextInt();
- in.reset();
- Random rng = new Random(seed);
- // declare and initialize any other needed variables
- int pORNG = 100;
- int pMCRO = 100;
- int pGUUG = 100;
- int sORNG = 0;
- int sMCRO = 0;
- int sGUUG = 0;
- int money = 10000;
- int day = 1;
- int tvalue = sORNG * pORNG + sMCRO * pMCRO + sGUUG * pGUUG + money;
- // begin day loop
- while (day < 12)
- {
- // print daily report
- System.out.println("****************************");
- System.out.println("Day " + day + " - You Currently Have:");
- System.out.println("Money: " + money);
- System.out.println("ORNG Shares: " + sORNG);
- System.out.println("MCRO Shares: " + sMCRO);
- System.out.println("GUUG Shares: " + sGUUG);
- System.out.println("Total Value: " + tvalue);
- System.out.println();
- // begin menu loop
- boolean done = false;
- while(!done)
- {
- // prompt user for their decision (buy, sell, or end turn)
- System.out.println("Would you like to:");
- System.out.println("1. Buy");
- System.out.println("2. Sell");
- System.out.println("3. End your trading");
- String userChoice = in.next();
- while (!userChoice.equals("1") && !userChoice.equals("2") && !userChoice.equals("3"))
- {
- System.out.println("Invalid input; enter 1, 2, or 3.");
- System.out.println("Would you like to:");
- System.out.println("1. Buy");
- System.out.println("2. Sell");
- System.out.println("3. End your trading");
- userChoice = in.next();
- }
- // if user chose buy
- if (userChoice.equals("1"))
- {
- // Start buy loop
- // prompt for which stock to buy
- System.out.println("Which stock would you like to buy?");
- System.out.println("1. Orange Inc.");
- System.out.println("2. Macrosoft");
- System.out.println("3. Guugle");
- String iBuy = in.next();
- while (!iBuy.equals("1") && !iBuy.equals("2") && !iBuy.equals("3"))
- {
- System.out.println("Invalid input; enter 1, 2, or 3.");
- System.out.println("Which stock would you like to buy?");
- System.out.println("1. Orange Inc.");
- System.out.println("2. Macrosoft");
- System.out.println("3. Guugle");
- iBuy = in.next();
- }
- // prompt for how many shares to buy
- System.out.println("Enter how many shares you would like to buy:");
- if (in.hasNextInt())
- {
- int sBuy = in.nextInt();
- // update money and shares count
- if (iBuy.equals("1"))
- {
- int nMoney = pORNG * sBuy;
- while (nMoney > money)
- {
- System.out.println("You don't have that much money.");
- System.out.println("Enter how many shares you would like to buy:");
- sBuy = in.nextInt();
- nMoney = pORNG * sBuy;
- }
- money = money - nMoney;
- sORNG = sORNG + sBuy;
- System.out.println("You now have " + money + " money.");
- }
- else if (iBuy.equals("2"))
- {
- int nMoney = pMCRO * sBuy;
- if (nMoney > money)
- {
- System.out.println("You don't have that much money.");
- System.out.println("Enter how many shares you would like to buy:");
- sBuy = in.nextInt();
- }
- else
- {
- money = money - nMoney;
- sMCRO = sMCRO + sBuy;
- }
- System.out.println("You now have " + money + " money.");
- }
- else if (iBuy.equals("3"))
- {
- int nMoney = pGUUG * sBuy;
- if (nMoney > money)
- {
- System.out.println("You don't have that much money.");
- System.out.println("Enter how many shares you would like to buy:");
- sBuy = in.nextInt();
- }
- else
- {
- money = money - nMoney;
- sMCRO = sMCRO + sBuy;
- }
- System.out.println("You now have " + money + " money.");
- }
- }
- else
- {
- System.out.println("Please enter an integer.");
- }
- }
- // End buy loop
- // if user chose sell
- int iSell = 0;
- if (userChoice.equals("2"))
- {
- // Start sell loop
- // prompt for which stock to sell
- System.out.println("Which stock would you like to sell?");
- System.out.println("1. Orange Inc.");
- System.out.println("2. Macrosoft");
- System.out.println("3. Guugle");
- iSell = in.nextInt();
- // prompt for how many shares to sell
- System.out.println("Enter how many shares you would like to sell:");
- int sSell = in.nextInt();
- // update money and shares count
- if (iSell == 1)
- {
- int nMoney = pORNG * sSell;
- money = money + nMoney;
- sORNG = sORNG - sSell;
- System.out.println("You now have " + money + " money.");
- }
- if (iSell == 2)
- {
- int nMoney = pMCRO * sSell;
- money = money + nMoney;
- sMCRO = sMCRO - sSell;
- }
- if (iSell == 3)
- {
- int nMoney = pGUUG * sSell;
- money = money + nMoney;
- sGUUG = sGUUG - sSell;
- }
- else
- {
- System.err.println("Invalid input; enter 1, 2, or 3.");
- System.out.println("Which stock would you like to buy?");
- System.out.println("1. Orange Inc.");
- System.out.println("2. Macrosoft");
- System.out.println("3. Guugle");
- iSell = in.nextInt();
- }
- }
- // end of loop sell loop
- // if user chose to end turn
- if (userChoice.equals("3"))
- {
- done = true;
- }
- }
- // update data such that menu loop will end
- // end menu loop
- // calculate closing prices for Orange Inc., Macrosoft, and Guugle
- int change = rng.nextInt(2);
- int sChange = (int) (Math.random() * 10) +1;
- if (change == 0)
- {
- pORNG = pORNG + sChange;
- //System.out.println("Orange Inc. Increases by " + sChange);
- //System.out.println("New price is: " + pORNG);
- }
- else
- {
- pORNG = pORNG - sChange;
- //System.out.println("Orange Inc. Decreases " + sChange);
- //System.out.println("New price is: " + pORNG);
- }
- int change2 = rng.nextInt(2);
- int sChange2 = (int) (Math.random() * 10) +1;
- // You need to make all the stocks change mang :|
- if (change2 == 0) {
- pMCRO = pMCRO + sChange2;
- //System.out.println("Macrosoft Increases by " + sChange2);
- //System.out.println("New price is: " + pMCRO);
- }
- else
- {
- pMCRO = pMCRO - sChange2;
- //System.out.println("Macrosoft Decreases by " + sChange2);
- //System.out.println("New price is: " + pMCRO);
- }
- int change3 = rng.nextInt(2);
- int sChange3 = (int) (Math.random() * 10) +1;
- if (change3 == 0)
- {
- pGUUG = pGUUG + sChange3;
- //System.out.println("Guugle Increases by " + sChange3);
- //System.out.println("New price is: " + pGUUG);
- }
- else
- {
- pGUUG = pGUUG - sChange3;
- //System.out.println("Guugle Decreases by " + sChange3);
- //System.out.println("New price is: " + pGUUG);
- }
- // print end of day report
- System.out.println("Day " + day + " - Closing Prices:");
- System.out.println("Money: " + money);
- System.out.println("ORNG Price: " + pORNG);
- System.out.println("MCRO Price: " + pMCRO);
- System.out.println("GUUG Price: " + pGUUG);
- System.out.println("");
- // end of day loop
- day++;
- }
- // print end-of-game report
- if (day == 12)
- {
- System.out.println("*******************");
- System.out.println("*******************");
- System.out.println("End of Game Summary");
- System.out.println("Money: " + money);
- System.out.println("ORNG Shares: " + sORNG);
- System.out.println("MCRO Shares: " + sMCRO);
- System.out.println("GUUG Shares: " + sGUUG);
- System.out.println("Total Value: " + tvalue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement