summaryrefslogtreecommitdiff
path: root/java/Stonks.java
diff options
context:
space:
mode:
authorpants <niklasjo2004@gmail.com>2026-09-21 14:17:15 -0700
committerpants <niklasjo2004@gmail.com>2026-09-21 14:17:15 -0700
commit3813621611945a0be23384c0c4d395b4c4a614ad (patch)
tree1812fd440839272e7dd16f5635e180769a66f5f1 /java/Stonks.java
Diffstat (limited to 'java/Stonks.java')
-rw-r--r--java/Stonks.java169
1 files changed, 169 insertions, 0 deletions
diff --git a/java/Stonks.java b/java/Stonks.java
new file mode 100644
index 0000000..58fd703
--- /dev/null
+++ b/java/Stonks.java
@@ -0,0 +1,169 @@
+// Nik Johnson
+// 1-25-2024
+// TA: Andy Ruan
+// modified attempt for resubmission 1
+
+import java.util.*;
+import java.io.*;
+
+// stock simulator
+// stock market sandbox which reads fake market data from "stonks.tsv", and allows users to:
+// buy and sell stocks, and save their portfolio to a file.
+// upon quitting, the value of the users portfolio will be calculated and printed.
+
+public class Stonks {
+ public static final String STOCKS_FILE_NAME = "stonks.tsv";
+
+ public static void main(String[] args) throws FileNotFoundException {
+ Scanner console = new Scanner(System.in);
+ File stonks = new File(STOCKS_FILE_NAME);
+ Scanner stonklines = new Scanner(stonks);
+ int numStocks = Integer.parseInt(stonklines.nextLine());
+
+ String[] tickers = new String[numStocks];
+ Double[] prices = new Double[numStocks];
+ Double[] portfolio = new Double[numStocks];
+
+ // fill portfolio with zeros to prevent null exceptions
+ // without using Arrays.fill()
+ for (int n = 0; n < numStocks; n++) {
+ portfolio[n] = 0.0;
+ }
+
+ String info = stonklines.nextLine();
+
+ // this runs once.
+ // functionally decomposing it would add lines of code, but no functionality.
+ int index = 0;
+ while (stonklines.hasNextLine()) {
+ String nextline = stonklines.nextLine();
+ Scanner token = new Scanner(nextline);
+ tickers[index] = token.next();
+ prices[index] = token.nextDouble();
+ index++;
+ }
+
+ System.out.println("Welcome to the CSE 122 Stocks Simulator!");
+ System.out.println("There are " + numStocks + " stocks on the market:");
+
+ for (int i = 0; i < numStocks; i++) {
+ System.out.println(tickers[i] + ": " + prices[i]);
+
+ }
+
+ System.out.println();
+ String next = navigator(console);
+
+ while (!next.equals("quit")) {
+ if (next.equals("buy")) {
+ portfolio = buy(console, tickers, prices, portfolio);
+ } else if (next.equals("sell")) {
+ portfolio = sell(console, tickers, prices, portfolio);
+ } else if (next.equals("save")) {
+ save(console, tickers, portfolio);
+ } else {
+ System.out.println("Invalid choice: " + next);
+ System.out.println("Please try again");
+ System.out.println();
+ }
+ next = navigator(console);
+ }
+
+ Double value = quit(prices, portfolio);
+ System.out.println("Your portfolio is currently valued at: $" + value);
+ }
+
+ // menu method, called for inital prompt and then after using any other methods
+ // returns String which decides which function to use
+
+ public static String navigator(Scanner console) {
+ System.out.println("Menu: buy, sell, save, quit");
+ System.out.print("Enter your choice: ");
+ String destination = console.next();
+ return destination;
+ }
+
+ // add (buy) to portfolio according to user input, no lower than $5.0
+ // takes console scanner, and all data arrays defined in main lines 24-26
+ // return modified portfolio with MORE shares than before
+
+ public static Double[] buy(Scanner console, String[] tickers,
+ Double[] prices, Double[] portfolio) {
+ System.out.print("Enter the stock ticker: ");
+ String order = console.next();
+ System.out.print("Enter your budget: ");
+ Double budget = console.nextDouble();
+
+ if (budget >= 5.0) {
+ for (int i = 0; i < tickers.length; i++) {
+ if (order.equals(tickers[i])) {
+ portfolio[i] += (budget / prices[i]);
+ System.out.println("You successfully bought " + tickers[i] + ".");
+ System.out.println();
+ }
+ }
+ } else {
+ System.out.println("Budget must be at least $5");
+ System.out.println();
+ }
+ return portfolio;
+ }
+
+ // subtract (sell) from portfolio according to user input, if they have enough to sell
+ // takes console scanner, and all data arrays defined in main lines 24-26
+ // return modified portfolio with LESS shares than before
+
+ public static Double[] sell(Scanner console, String[] tickers,
+ Double[] prices, Double[] portfolio) {
+ System.out.print("Enter the stock ticker: ");
+ String ticker = console.next();
+ System.out.print("Enter the number of shares to sell: ");
+ Double sellAmount = console.nextDouble();
+ for (int i = 0; i < tickers.length; i++) {
+ if (ticker.equals(tickers[i])) {
+ if (sellAmount > portfolio[i]) {
+ System.out.println("You do not have enough shares of " + ticker + " to sell " + sellAmount + " shares.");
+ System.out.println();
+ } else if (sellAmount <= portfolio[i]) {
+ portfolio[i] = portfolio[i] - sellAmount;
+ System.out.println("You successfully sold " + sellAmount + " shares of " + ticker + ".");
+ System.out.println();
+ }
+ }
+ }
+ return portfolio;
+ }
+
+ // write current portfolio to file named according to user input
+ // takes console scanner, array of tickers and the users current portfolio
+
+ public static void save(Scanner console, String[] tickers, Double[] portfolio) throws FileNotFoundException {
+ System.out.print("Enter new portfolio file name: ");
+ String fileName = console.next();
+ System.out.println();
+ File saveData = new File(fileName);
+ PrintStream savePortfolio = new PrintStream(saveData);
+ for (int i = 0; i < tickers.length; i++) {
+ if (portfolio[i] != 0.0) {
+ savePortfolio.println(tickers[i] + " " + portfolio[i]);
+ }
+ }
+ }
+
+ // upon quitting, calculate total portfolio value using-
+ // -values in portfolio and current market prices
+ // return total value for printing
+
+ public static double quit(Double[] prices, Double[] portfolio) {
+ System.out.println();
+ Double portfolioValue = 0.0;
+ for (int i = 0; i < prices.length; i++) {
+ if (portfolio[i] != null) {
+ Double currValue = prices[i]*portfolio[i];
+ portfolioValue += currValue;
+ } else portfolioValue += 0;
+ }
+ return portfolioValue;
+ }
+
+} \ No newline at end of file