Skip to content

A simple Java program that implements an algorithm to calculate the maximum profit from buying and selling stocks given daily price fluctuations. It includes various test cases to validate the algorithm's performance in different market scenarios.

Notifications You must be signed in to change notification settings

danieldotwav/Stock-Profit-Maximizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This Java project includes a method, maxProfit, designed to calculate the maximum profit from a series of daily stock prices. It simulates buying and selling stocks to maximize profit, considering stock price changes day-to-day.

Algorithms

1. Stock Profit Calculation

Logic

  • The algorithm iterates through an array of stock prices. For each day, if the stock price is higher than the previous day, it calculates the profit from a hypothetical buy-and-sell transaction and adds it to the total profit.

Complexity Analysis

  • Time Complexity: O(n), where n is the number of days (length of the prices array).
  • Space Complexity: O(1), as it uses a constant amount of space.

Test Cases

The project includes a variety of test cases to validate the functionality of the maxProfit method under different scenarios:

  1. Increasing Stock Prices: Simulates a market scenario where stock prices increase steadily each day.
  2. Decreasing Stock Prices: Tests the algorithm in a situation where stock prices are falling daily.
  3. Mixed Stock Prices: Checks algorithm performance in a volatile market with both increasing and decreasing prices.
  4. Constant Stock Prices: Ensures that the algorithm handles scenarios where stock prices remain constant.
  5. Empty Array: Verifies the method's robustness when provided with an empty array of prices.
  6. Single Price: Tests the method's behavior with only one price in the array.
  7. Prices with Larger Variations: Challenges the algorithm with a more complex market scenario including significant price variations.

Each test case prints the array of stock prices and the calculated maximum profit, allowing for easy verification and understanding of the algorithm's performance in different market conditions.

Code Snippet

import java.util.stream.Collectors;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Test cases for different stock price scenarios
        Integer[] prices1 = new Integer[] { 1, 2, 3, 4, 5 };
        getResults(prices1, maxProfit(prices1));
        // Additional test cases...
    }

    static int maxProfit(Integer[] prices) {
        int maxProfit = 0;
        for (int i = 1; i < prices.length; ++i) {
            if (prices[i] > prices[i - 1]) {
                maxProfit += prices[i] - prices[i - 1];
            }
        }
        return maxProfit;
    }

    static void getResults(Integer[] prices, int maxProfit) {
        String resultString = (prices != null)
            ? Arrays.stream(prices).map(String::valueOf).collect(Collectors.joining(", ", "[ ", " ]"))
            : "Null Container";
        
        System.out.println("\nPrice Array: " + resultString + "\nMax Profit: " + maxProfit);
    }
}

About

A simple Java program that implements an algorithm to calculate the maximum profit from buying and selling stocks given daily price fluctuations. It includes various test cases to validate the algorithm's performance in different market scenarios.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages