Coder Social home page Coder Social logo

danieldotwav / stock-profit-maximizer Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 3 KB

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.

Java 100.00%
algorithms complexity-analysis control-structures exception-handling stream-api test-case-development

stock-profit-maximizer's Introduction

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);
    }
}

stock-profit-maximizer's People

Contributors

danieldotwav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.