Ad
import java.util.HashMap;
import java.util.Map;

public class BankingSystem {

    private Map<String, Double> accountBalances;

    public BankingSystem() {
        this.accountBalances = new HashMap<>();
    }

    public void createAccount(String accountNumber, double initialBalance) {
        if (accountBalances.containsKey(accountNumber)) {
            throw new IllegalArgumentException("Account already exists");
        }
        accountBalances.put(accountNumber, initialBalance);
    }

    public double getAccountBalance(String accountNumber) {
        if (!accountBalances.containsKey(accountNumber)) {
            throw new IllegalArgumentException("Account does not exist");
        }
        return accountBalances.get(accountNumber);
    }

    public void deposit(String accountNumber, double amount) {
        if (!accountBalances.containsKey(accountNumber)) {
            throw new IllegalArgumentException("Account does not exist");
        }
        double currentBalance = accountBalances.get(accountNumber);
        accountBalances.put(accountNumber, currentBalance + amount);
    }

    public void withdraw(String accountNumber, double amount) {
        if (!accountBalances.containsKey(accountNumber)) {
            throw new IllegalArgumentException("Account does not exist");
        }
        double currentBalance = accountBalances.get(accountNumber);
        if (amount > currentBalance) {
            throw new IllegalArgumentException("Insufficient funds");
        }
        accountBalances.put(accountNumber, currentBalance - amount);
    }
}
Fundamentals
Logic

The method checks if the items array is null or empty. If it is, it throws an InvalidOperationException with an error message.

The method initializes a variable max with the first element of the items array.

The method iterates over the items array starting from the second element, and uses the CompareTo method of each element to compare it to max. If an element is greater than max, it updates max with that element using a ternary operator.

After iterating over all the elements in the items array, the method returns the final value of max.

I added a check for null values in the array of items to avoid a potential NullReferenceException

I added a more informative error message to the InvalidOperationException in case no items were provided

I changed the variable names to use camelCase for better readability

Code
Diff
  • using System;
    public static class Math
    {
        public static T Max<T>(params T[] items) where T : IComparable<T>
        {
            if (items == null || items.Length == 0)
                throw new InvalidOperationException("No items were provided to find the maximum value.");
    
            T max = items[0];
            for (int i = 1; i < items.Length; i++)
            {
                max = items[i].CompareTo(max) > 0 ? items[i] : max;
            }
    
            return max;
        }
    }
    
    • using System;
    • public static class Math
    • {
    • public static T Max<T>(params T[] items) where T : IComparable<T>
    • {
    • if (items.Length <= 0)
    • throw new InvalidOperationException();
    • if (items == null || items.Length == 0)
    • throw new InvalidOperationException("No items were provided to find the maximum value.");
    • var max = items[0];
    • for (var i = 1; i < items.Length; ++i)
    • if (max.CompareTo(items[i]) < 0)
    • max = items[i];
    • T max = items[0];
    • for (int i = 1; i < items.Length; i++)
    • {
    • max = items[i].CompareTo(max) > 0 ? items[i] : max;
    • }
    • return max;
    • }
    • }