1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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;
}
}
|