0

I Have the first part of my code pretty complete and I got the gist of the rest, I'm just not sure how to put it all together. Here is the first part

using System;
namespace ConsoleApplication1
{
    class Program
    {
        public class AccountInfo
        {
            public int Number { get; set; }
            public double Balance { get; set; }
            public string LastName { get; set; }
        }

        static void Main(string[] args)
        {
            List<AccountInfo> accounts = new List<AccountInfo>();

            for (int index = 1; index < 6; index++)
            {
                AccountInfo acc = new AccountInfo();

                Console.Write("Enter account number: " + index.ToString() + ": ");
                acc.Number = int.Parse(Console.ReadLine());
                Console.Write("Enter the account balance: ");
                acc.Balance = double.Parse(Console.ReadLine());
                Console.Write("Enter the account holder last name: ");
                acc.LastName = Console.ReadLine();

                accounts.Add(acc);
            }

        }
    }
}

The second part is to ask the user what they want to do with the arrays


enter an a or A to search account numbers enter a b or B to average the accounts enter an x or X to exit program


the search part I can use something like this:

public void search Accounts()
{
    for(int x = 0; x < validValues.Lenth; ++x)
    {
        if(acctsearched == validValues[x])
        {
            isValidItem = true;
            acctNumber = Number[x];
        }
        }

And I can use a while loop with a bool true/false to close out.

I'm not sure how to get the average balance. I keep getting errors like "can't implicitly change int[] to int"

Any help with this would very much appreciated.

2 Answers 2

2

Sounds like you have an List of AccountInfo. Are you able to use LINQ?

To get the average, you can do this with LINQ:

double avg = accounts.Select(x=>x.Balance).Average(); 

To search for a given acct, you can do something like this:

var foundAcct = accounts.SingleOrDefault(x=>x.Number==someSearchNum);

For this to work(and create methods for these 2 actions), you'd need to move the List<AccountInfo> accounts out of the Main and be declared in the class.

With LINQ, you'll required .NET 3.5 or greater.

Sign up to request clarification or add additional context in comments.

3 Comments

@Musi: nice! Hadn't realize they'd gone to that extent. The Albahari guys are amazing.
@MusiGenesis - OMG best link ever
I've never used it myself - I googled "linq in .net 2.0" and found (as usual) a StackOverflow answer that mentioned LinqBridge. Maybe we should all get in the habit of regularly googling for stuff we wish was available - otherwise we'll miss it when it finally turns up. :)
0

using System; namespace AssignmentWeek3 { class Accounts { // private class members, creating new arrays private int [] AcctNumber = new int[5]; private double [] AcctBalance = new double[5]; private string [] LastName = new string[5];

    public void fillAccounts()//fill arrays with user input
    {
        int x = 0;
        for (int i = 0; i < AcctNumber.Length; i++)
        {
            Console.Write("Enter account number: ");              
            AcctNumber[x] = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter account balance: ");
            AcctBalance[x] = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter account holder last name: ");
            LastName[x] = Convert.ToString(Console.ReadLine());
            x++;
        } 

    }

    public void searchAccounts() //search account method to be called later in main()
    {
        int accountNum = 0;
        bool isValid = false;
        int x = 0;

        Console.Write("Please enter account number to search for: ");
        accountNum = Convert.ToInt32(Console.ReadLine());


        while (x < AcctNumber.Length && accountNum != AcctNumber[x])
            ++x;
        if(x != AcctNumber.Length)
        {
        isValid = true;
        }
        if(isValid){
        Console.WriteLine("AcctNumber: {0}      Balance: {1:c}  Acct Holder: {2}", AcctNumber[x], AcctBalance[x], LastName[x]);
        }
        else{  
            Console.WriteLine("You entered an invalid account number"); 
        }                    
    }

    public void averageAccounts()//averageAccounts method to be store for later use
    {
        // compute and display average of all 5 bal as currency use length.
        int x = 0;
        double balanceSum = 0;
        double Avg = 0;

        for (x = 0; x < 5; x++ )
        {
            balanceSum = balanceSum + AcctBalance[x];
        }
        Avg = (balanceSum / x);
        Console.WriteLine("The average balance for all accounts is {0:c}", Avg);
    } 

} //end public class Accounts

class week3_assignment //Main class { static void Main() { char entry = '0';

       //instantiate one new Accounts object
       Accounts accounts = new Accounts();

       //call class methods to fill Accounts
       accounts.fillAccounts();

       //menu with input options
       bool Exit = false;
       while (!Exit)   
       {
       while (entry != 'x' && entry != 'X')
       {


           Console.WriteLine("*****************************************");
           Console.WriteLine("enter an a or A to search account numbers");
           Console.WriteLine("enter a b or B to average the accounts");
           Console.WriteLine("enter an x or X to exit program");
           Console.WriteLine("*****************************************");


          entry = Convert.ToChar(Console.ReadLine());

               switch (entry)  //set switch
               {
                   case 'a':
                   case 'A':
                       accounts.searchAccounts();//calls search accounts method
                       break;
                   case 'b':
                   case 'B':
                       accounts.averageAccounts();//calls average accounts method
                       break;
                   case 'x':
                   case 'X':
                       (Exit) = true;
                       break;
                   default:
                        Console.WriteLine("That is not a valid input");
                       break;
               }
           }  
       }    
   } 

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.