Objective: Create a simple stock market simulator in Java that allows users to buy and sell stocks, view their portfolio, and track the historical prices of a limited set of stocks. Starter code is given. Figure out what is missing and complete the project. Some of the requirements have been done.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class StockMarketSimulator {
static final int NUM_STOCKS = 6;
static String[] stockNames = new String[NUM_STOCKS];
static double[] stockPrices = new double[NUM_STOCKS];
static int[] userShares = new int[NUM_STOCKS];
static double userBalance = 10000.0;
public static void main(String[] args) {
initializeStockData();
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
displayStocks();
break;
case 2:
buyStock(scanner);
break;
case 3:
sellStock(scanner);
break;
case 4:
displayPortfolio();
break;
case 5:
savePortfolio();
System.out.println("Portfolio saved successfully.");
break;
case 6:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
updateStockPrices();
}
}
public static void initializeStockData() {
stockNames[0] = "Apple";
stockNames[1] = "Google";
stockNames[2] = "Amazon";
stockNames[3] = "Meta";
stockNames[4] = "Netflix";
stockNames[5] = "Tesla";
try (Scanner fileScanner = new Scanner(new
File("initial_stock_prices.txt"))) {
for (int i = 0; i < NUM_STOCKS; i++) {
stockPrices[i] = Double.parseDouble(fileScanner.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
public static void displayMenu() {
System.out.println("Stock Market Simulator");
System.out.println("1. View Stocks");
System.out.println("2. Buy Stocks");
System.out.println("3. Sell Stocks");
System.out.println("4. View Portfolio");
System.out.println("5. Save Portfolio");
System.out.println("6. Exit");
System.out.print("Select an option: ");
}
public static void displayStocks() {
System.out.println("Stocks:");
for (int i = 0; i < NUM_STOCKS; i++) {
System.out.println((i + 1) + ". " + stockNames[i] + " - $" +
stockPrices[i]);
}
}
public static void buyStock(Scanner scanner) {
displayStocks();
System.out.print("Select a stock to buy: ");
int stockIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
System.out.print("Enter the number of shares to buy: ");
int numShares = scanner.nextInt();
scanner.nextLine(); // Consume newline
//Your code goes here
}
public static void sellStock(Scanner scanner) {
displayPortfolio();
System.out.print("Select a stock to sell: ");
int stockIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
System.out.print("Enter the number of shares to sell: ");
int numShares = scanner.nextInt();
scanner.nextLine(); // Consume newline
//Your code goes here
}
public static void displayPortfolio() {
//Your code goes here
}
public static void savePortfolio() {
// Save portfolio to "portfolio.txt" file
//Your code goes here
}
public static void updateStockPrices() {
Random random = new Random();
for (int i = 0; i < NUM_STOCKS; i++) {
double variation = (random.nextDouble() - 0.5) * 10; // Simulate price
fluctuation
stockPrices[i] += variation;
if (stockPrices[i] < 1.0) {
stockPrices[i] = 1.0; // Minimum price
}
}
}
}

portfolio.txt -
Apple,100
Google,0
Amazon,0
Balance,8630.45447144116

To complete the project, the following code should be added:

1. In the `buyStock` method, add the following code to check if the user has enough balance to buy the specified number of shares at the current stock price:
```java
double totalPrice = stockPrices[stockIndex] * numShares;
if (totalPrice > userBalance) {
System.out.println("Insufficient balance. Cannot buy the specified number of shares.");
return;
}
```
If the user has enough balance, deduct the total price from the user's balance and update the user's share count for the specified stock:
```java
userShares[stockIndex] += numShares;
userBalance -= totalPrice;
```

2. In the `sellStock` method, add the following code to check if the user has enough shares to sell the specified number of shares:
```java
if (numShares > userShares[stockIndex]) {
System.out.println("Insufficient shares. Cannot sell the specified number of shares.");
return;
}
```
If the user has enough shares, calculate the total amount to be added to the user's balance and update the user's share count for the specified stock:
```java
double totalAmount = stockPrices[stockIndex] * numShares;
userShares[stockIndex] -= numShares;
userBalance += totalAmount;
```

3. In the `displayPortfolio` method, add the following code to print the user's current portfolio:
```java
System.out.println("Portfolio:");
for (int i = 0; i < NUM_STOCKS; i++) {
System.out.println(stockNames[i] + " - " + userShares[i] + " shares");
}
System.out.println("Balance: $" + userBalance);
```

4. In the `savePortfolio` method, add the following code to save the user's portfolio to the "portfolio.txt" file:
```java
try (FileWriter writer = new FileWriter("portfolio.txt")) {
for (int i = 0; i < NUM_STOCKS; i++) {
writer.write(stockNames[i] + "," + userShares[i] + "\n");
}
writer.write("Balance," + userBalance);
} catch (IOException e) {
System.out.println("Error saving portfolio: " + e.getMessage());
}
```