简体   繁体   中英

How to return true or false with message inside the bool method for parallel array C#?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM