0

Note: I am learning to use the arrays.

The issue with my bool method for the parallel array is that I want to return true or false with a message for balance being empty (equal to 0.00 in double data type) or not (return false). I got the result after running this code:

  • The balance is not empty False
  • The balance is not empty False
  • The balance is not empty False

In short, once I run those codes then it will be looping or will not read whether the user's balance is empty or not because it doesn't check if it is zero or less than a zero. Instead, they simply ignore it and then return a false.

Here is my code:

// declaring three parallel arrays that store name, account number, and balance with data types
string [] customerName = new string [2];
int [] bankAccount = new int [2];
double [] balance = new double [2];

customerName = new string [] { "John Doe", "Jane Doe", "John Smith" };
bankAccount = new int [] { 123456 , 654321 , 987654 };
balance = new double [] {100.00, 200.00, 300.00};

for ( int i = 0; i < customerName.Length; i++)
{
    Console.WriteLine(customerName[i] + "\t" + "Account Number: " + bankAccount[i] + "\t" + "Balance: $" + balance[i]);
}

// declared three different balances for the deposit method since context is necessary for local variables
double balance1 = balance[0];
double balance2 = balance[1];
double balance3 = balance[2];

Console.WriteLine(isEmpty(balance1));
Console.WriteLine(isEmpty(balance2));
Console.WriteLine(isEmpty(balance3));

 bool isEmpty (double balance)
{
    bool balance1 = balance == 0;
    bool balance2 = balance == 0;
    bool balance3 = balance == 0;
    if (balance == 0)
    {
        return true;
    }
    else
    {
        Console.WriteLine("The balance is not empty");
    }
    return balance1 || balance2 || balance3;
}
// hardcoded values for three customers' balances to be updated
double deposit = 200.00;
double deposit1 = 300.00;
double deposit2 = 400.00;

// declared three different customers' names for return with string data type
string firstCustomer = "John Doe";
string secondCustomer = "Jane Doe";
string thirdCustomer = "John Smith";

Console.WriteLine(Deposit(firstCustomer, balance1, deposit));
Console.WriteLine(Deposit(secondCustomer, balance2, deposit1));
Console.WriteLine(Deposit(thirdCustomer, balance3, deposit2));

// string return that will return the three customers with updated balance after the deposit which is string
string Deposit (string customerName, double balance, double deposit)
{
    double newBalance = balance + deposit;
    return customerName + " deposited $" + deposit + " and the new balance is " + newBalance;
}

Therefore, I need to simply check and then return false or true after deciding if the balance is empty or not. There is no error but simply shows some repeating and ignoring any that is return false. Instead, they will always return true no matter what it is.

  • 2
    Maybe im missing something obvious but I read it twice and still dont understand what is the problem / what u are trying to accomplish – mont_ Oct 18 '22 at 21:21
  • Very unclear what you have problem with - clearly you know how to create/pass parameters to method with multiple arguments - `Deposit` is the example, code in the question seem to be syntactically valid... Are you asking https://stackoverflow.com/questions/9784630/function-with-variable-number-of-arguments? – Alexei Levenkov Oct 18 '22 at 21:22
  • @PeterB "parallel arrays" is the concept when people store structured records in separate array per field instead of array of objects with fields/properties - it is exactly what OP is doing (whether it is good idea in general or in this specific case is not a topic for SO) – Alexei Levenkov Oct 18 '22 at 21:24
  • i apologize for making unclear info since I am pretty much a real beginner and just someone recommended me to use this StackOverflow –  Oct 18 '22 at 21:26
  • @TEN - Then read [ask] and then edit your question to make it a better question. :-) – Enigmativity Oct 18 '22 at 21:28
  • Why would you expect any of those three objects to be "empty"? They have the values 100, 200, and 300: `balance = new double [] {100.00, 200.00, 300.00};` – Steve Oct 18 '22 at 22:47

1 Answers1

0

It seems to me, if you are calling IsEmpty with one double, you simply need this:

bool IsEmpty(double balance)
{
    if (balance == 0)
    {
        return true;
    }
    else
    {
        Console.WriteLine("The balance is not empty");
        return false;
    }
}

If you are calling it with an array:

bool IsEmpty(params double[] balances)
{
    if (balances.Any(b => b == 0))
    {
        return true;
    }
    else
    {
        Console.WriteLine("No balance is empty");
        return false;
    }
}

The params keyword allows you to pass the array as separate variables, like IsEmpty(balance1, balance2, balance3).

Please note that I changed the name of the method from isEmpty to IsEmpty to follow standard C# naming conventions.


You're also better off creating one array and using a custom type:

Account[] accounts = new Account[]
{
    new Account() { Customer = "John Doe", Number = 123456, Balance = 100m, },
    new Account() { Customer = "Jane Doe", Number = 654321, Balance = 200m, },
    new Account() { Customer = "John Smith", Number = 987654, Balance = 300m, },
};

public class Account
{
    public string Customer { get; set; }
    public int Number { get; set; }
    public decimal Balance { get; set; }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • However, I understand that if I am calling it with an array or isEmpty with a single double but problem is that I keep getting an error message "No overload for method 'isEmpty' takes 3 arguments" for "Console.WriteLine(isEmpty(balance1, balance2, balance));" –  Oct 18 '22 at 21:36
  • Because isEmpty(balance1, balance2, balance) your are not passing an array, thats a call with multiple double arguments. You simply call isEmpty(balance), balance is already an array in your code – mont_ Oct 18 '22 at 21:40
  • Oh, I see, however, I have tried to use the first code you mentioned and it doesn't print out "The balance is not empty". –  Oct 18 '22 at 21:44
  • @TEN - Because of of your sample data has a balance. – Enigmativity Oct 19 '22 at 06:15