6 Replies - 483 Views - Last Post: 02 March 2012 - 10:25 PM Rate Topic: -----

#1 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 11

C# program for Web Application course- bank account error

Posted 27 February 2012 - 03:09 AM

For my Web Application course, I need to write a c# program the can keep track to up to 20 bank accounts using an array. Each account (and array element) consists of a 5-digit account number (long) and a balance (float). This program is menu driven and also includes an Account class that has an Account constructor, a getter and setter method for both data members(account number and balance), a Withdraw method and a Deposit method. Withdrawals and deposits must change the balance values in the array. The menu for the program is as follows:

A - Add
D - Deposit
W - Withdraw
S - Show all accounts
Q - Quit

When the 'A' key is pressed at the menu screen, the user must enter the account number, then a beginning balance. If there are already 20 accounts added, then an error message pops up and the menu is shown again. If the account number is not 5 digits, the user must re-enter the account number.

When the 'D' key is pressed at the menu screen, the user is prompted to enter an existing account number. If the typed account number doesn't exist, an error message pops up and the user re-enters the account number. Otherwise, it prompts the user for the amount of money to deposit. If the amount entered is negative, an error message pops up and the user re-enters the amount to deposit.

When the 'W' key is pressed at the menu screen, the user is prompted to enter an existing account number. If the typed account number doesn't exist, an error message pops up and the user re-enters the account number. Otherwise, it prompts the user for the amount of money to withdraw. If the amount entered is negative, an error message pops up and the user re-enters the amount to withdraw.

When the 'S' key is pressed at the menu screen, the user is shown the existing account numbers along with their current balances, sorted by account number. If there are no accounts, then an error message pops up and the menu is shown again.

When the 'Q' key is pressed, an exit message prints, then the program closes once a key is pressed.

My questions are: first of all, how can I implement my menu so that the 'Q' key will exit the application instead of pressing any key other than A, D, W, or S? Secondly I'm getting a couple errors when I compile the two files (Program.cs and Account.cs) in Visual Studio 2010:

Program.cs(79,47): error CS0118: Operator '!=' cannot be applied to operands of type 'long' and 'Program3.Account'
Program.cs(116,50): Operator '!=' cannot be applied to operands of type 'long' and 'Program3.Account'

Here is my code for Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program3
{
    class Program
    {
        static void Main(string[] args)
        {
            long number;
            float balance;
            float amount;
            int accountCounter = 0;
            Account[] myAccounts = new Account[20];
            char menuChoice;

            while (true)
            {

                Console.WriteLine("Please select an option\n");
                Console.WriteLine(" A - Add    D - Deposit     W-Withdraw    S-Show    Any other character to Quit\n");

                menuChoice = (char)Console.Read();    // reads the menu character entered by the User

                switch (menuChoice)
                {
                    // If menu option 1 is selected, call the initialize function.
                    case 'a':
                        {
                            if (accountCounter < 20)
                            {
                                Console.WriteLine("Account number to add: ");
                                number = Console.Read();

                                while (number < 10000 || number > 99999)
                                {
                                    Console.WriteLine("Error. Number must be 5 digits: ");
                                    Console.Write("Account number to add: ");
                                    number = Console.Read();
                                }

                                Console.WriteLine("Beginning balance: ");
                                balance = Console.Read();

                                while (balance < 0)
                                {
                                    Console.WriteLine("Error: Balance must be non-negative");
                                    Console.Write("Beginning balance: ");
                                    balance = Console.Read();
                                }


                                myAccounts[accountCounter] = new Account(number, balance);
                                Console.WriteLine("Account " + number + " added");

                                accountCounter++;
                            }
                            else
                            {
                                Console.WriteLine("There is no more room for accounts:");
                            }
                        }
                        break;

                    case 'd':
                        {
                            if (accountCounter == 0)
                                Console.WriteLine("There are no accounts to process");
                            break;

                            Console.WriteLine("Please enter an existing account number: ");
                            number = Console.Read();


                            for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
                            {
                                if (number != myAccounts[accountCounter])
                                {
                                    Console.WriteLine("Account does not exist.");
                                    Console.Write("Please enter an existing account number: ");
                                    number = Console.Read();
                                }
                                
                            }


                            Console.Write("Amount to add: ");
                            amount = Console.Read();
                            for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
                            {
                                if (amount < balance)
                                {
                                    Console.WriteLine("Amount is less than current balance.");
                                    Console.Write("Please enter an existing account number: ");
                                }
                                balance = Console.Read();

                            }
                            myAccounts[accountCounter].Deposit(amount);
                        }

                    case 'w':
                        {
                            if (accountCounter == 0)
                                Console.WriteLine("There are no accounts to process");
                            break;

                            Console.WriteLine("Please enter an existing account number: ");
                            number = Console.Read();


                            for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
                            {
                                while (number != Accounts[accountCounter])
                                    Console.WriteLine("Account does not exist.");
                                Console.Write("Please enter an existing account number: ");
                                number = Console.Read();
                            }


                            Console.Write("Amount to withdraw: ");
                            amount = Console.Read();
                            for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
                            {
                                while (amount < balance)
                                    Console.WriteLine("Amount is less than current balance.");
                                Console.Write("Amount to withdraw: ");
                                balance = Console.Read();
                            }
                            myAccounts[accountCounter].Withdraw(amount);
                        }


                    case 's':
                        {
                            Console.WriteLine("Account      Deposit");
                            Console.WriteLine("--------------------");
                            for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
                            {

                                Console.Write("{0:2c}       {0:2c}", myAccounts[accountCounter].getNumber(), myAccounts[accountCounter].getBalance());
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("Bye-bye");

                }
            }
        }
    }
}



And here is my Account.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program3
{
    public class Account
    {
        private long number;
        private float balance;

        public Account(long num, float bal)
        {
            number = num;
            balance = bal;
        }

        public void setNumber(long num)
        {
            number = num;
        }

        public long getNumber()
        {
            return number;
        }

        public void setBalance(float bal)
        {
            balance = bal;
        }

        public float getBalance()
        {
            return balance;
        }

        public void Deposit(float amount)
        {
            balance += amount;
        }

        public void Withdraw(float amount)
        {
            balance -= amount;
        }
    }
}



How can I fix these errors? I've tried using Accounts[accountCounter] instead, but then these are the errors that are detected:

Program.cs(79,47): error CS0118: 'Program3.Account' is a 'type' but is used like a 'variable'
Program.cs(116,50): error CS0118: 'Program3.Account' is a 'type' but is used like a 'variable'

How can I fix the errors so that my program executes correctly?

Is This A Good Question/Topic? 0
  • +

Replies To: C# program for Web Application course- bank account error

#2 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 206
  • View blog
  • Posts: 436
  • Joined: 07-July 10

Re: C# program for Web Application course- bank account error

Posted 27 February 2012 - 05:42 AM

Quote

how can I implement my menu so that the 'Q' key will exit the application instead of pressing any key other than A, D, W, or S?

So you wrote this to recognize those four keys but can't figure out how to recognize the 'Q' key? What help do you need with this?


Quote

while (number != Accounts[accountCounter])

You are asking if number is equal to this object. You might as well be telling it, "while 12 is not equal to baseball".

You should break this code into parts. It will help everyone including yourself debug what is happening. Try to break it into multiple methods like this:
static void Main(string[] args)
{
   while (true)
   {
      DrawMenu();
      menuChoice = (char)Console.Read();
      switch(menuChoice)
      {
         case 'A':
            AddAccount();
         case 'D':
            Deposit();
         etc...
      }
   }
}

This post has been edited by Robin19: 27 February 2012 - 05:43 AM

Was This Post Helpful? 3
  • +
  • -

#3 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 11

Re: C# program for Web Application course- bank account error

Posted 02 March 2012 - 01:05 AM

View PostRobin19, on 27 February 2012 - 05:42 AM, said:

Quote

how can I implement my menu so that the 'Q' key will exit the application instead of pressing any key other than A, D, W, or S?

So you wrote this to recognize those four keys but can't figure out how to recognize the 'Q' key? What help do you need with this?


Quote

while (number != Accounts[accountCounter])

You are asking if number is equal to this object. You might as well be telling it, "while 12 is not equal to baseball".

You should break this code into parts. It will help everyone including yourself debug what is happening. Try to break it into multiple methods like this:
static void Main(string[] args)
{
   while (true)
   {
      DrawMenu();
      menuChoice = (char)Console.Read();
      switch(menuChoice)
      {
         case 'A':
            AddAccount();
         case 'D':
            Deposit();
         etc...
      }
   }
}


I have fixed up my AccountTest class. This is what it looks like now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Program3
{
    class AccountTest
    {   
        static void Main(string[] args)
        {
            AccountTest myAccounts = new AccountTest();
            int minBound = 0;
            
            myAccounts.DrawMenu(minBound);
        }



        public void DrawMenu(int minBound)
        {
            AccountTest myAccounts = new AccountTest();
            string myChoice;
            char choice;
            int maxBound = 20;

            while(true)
            {
                Console.Write("Command (Add     Deposit     Withdraw    Show    Quit):    ");
                myChoice = Console.ReadLine();
                choice = Char.Parse(myChoice);

                switch (choice)
                {
                    case 'a':
                    case 'A':
                        myAccounts.AddAccount(minBound, maxBound);
                        break;
                    case 'd':
                    case 'D':
                        myAccounts.DepositMoney(minBound, maxBound);
                        break;
                    case 'w':
                    case 'W':
                        myAccounts.WithdrawMoney(minBound, maxBound);
                        break;
                    case 's':
                    case 'S':
                        myAccounts.ShowAccounts(minBound, maxBound);
                        break;
                    case 'q':
                    case 'Q':
                        myAccounts.QuitProgram();
                        break;
                    default:
                        myAccounts.PrintEntryError();
                        break;
                }
            } 
        }

           

            

        public void AddAccount(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[maxBound];
            String myNum, myBal;
            long number;
            float balance;

            if (minBound < maxBound)
            {
                
                Console.Write("Account number to add: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);

                if (number < 10000 || number > 99999)
                {
                    Console.WriteLine("Error. Number must be 5 digits\n");
                }
                else
                {
                    
                    for (int counter = 0; counter <= minBound; counter++)
                    {
                        if (number == (myAccounts[counter].getNumber()))
                        {
                            Console.WriteLine("Account already exists.");
                        }
                        else
                        {
                            myAccounts[counter].setNumber(number);
                            Console.WriteLine("Beginning balance: ");
                            myBal = Console.ReadLine();
                            balance = Single.Parse(myBal);


                            while (balance <= 0)
                            {
                                Console.WriteLine("Error: Balance must be positive");
                                Console.Write("Beginning balance: ");
                                myBal = Console.ReadLine();
                                balance = Single.Parse(myBal);
                            }

                            myAccounts[counter].setBalance(balance);
                            myAccounts[counter] = new Account(number, balance);

                            Console.WriteLine("Account " + number + " added.");
                            minBound++;
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("There is no more room for accounts:");
            }
        }

        public void DepositMoney(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[maxBound];
            String myNum, myAmt;
            long number;
            float amount = 0;

            if (minBound == 0)
                Console.WriteLine("There are no accounts to deposit money for");
            else
            {
                Console.WriteLine("Please enter an existing account number: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);

                for (int counter = 0; counter < minBound; counter++)
                {
                    if (number != (myAccounts[counter].getNumber()))
                    {
                        Console.WriteLine("Account does not exist.");
                    }

                    else
                    {
                        Console.Write("Amount to add: ");
                        myAmt = Console.ReadLine();
                        amount = Single.Parse(myAmt);

                        while (amount <= 0)
                        {
                            Console.Write("Amount to add: ");
                            myAmt = Console.ReadLine();
                            amount = Single.Parse(myAmt);
                        }
                        myAccounts[counter].Deposit(amount);
                    }
                    
                }
            }
        }

        public void WithdrawMoney(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[maxBound];
            String myNum, myAmt;
            long number;
            float amount = 0;

            if (minBound == 0)
                Console.WriteLine("There are no accounts to withdraw money from");
            else
            {
                Console.WriteLine("Please enter an existing account number: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);

                for (int counter = 0; counter < minBound; counter++)
                {
                    if (number != (myAccounts[counter].getNumber()))
                    {
                        Console.WriteLine("Account does not exist.");
                    }

                    else
                    {
                        Console.Write("Amount to wd: ");
                        myAmt = Console.ReadLine();
                        amount = Single.Parse(myAmt);

                        while (amount <= 0)
                        {
                            Console.Write("Amount to wd: ");
                            myAmt = Console.ReadLine();
                            amount = Single.Parse(myAmt);
                        }

                        for (counter = 0; counter < minBound; counter++)
                        {
                            if (amount < (myAccounts[minBound].getBalance()))
                            {
                                Console.WriteLine("Amount is less than current balance.");
                            }

                        }
                        myAccounts[counter].Withdraw(amount);
                    }

                }
            }
        }

        public void ShowAccounts(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[maxBound];

            if (minBound == 0)
                Console.WriteLine("There are no accounts to show");
            else
            {
                Console.WriteLine("Account      Balance");
                Console.WriteLine("--------------------");
                for (int counter = 0; counter < minBound; counter++)
                    {
                        Console.Write("{0:2c}       {0:2c}\n", myAccounts[counter].getNumber(), myAccounts[counter].getBalance());
                    }
            }
        }
        
        public void QuitProgram()
        {
            Console.WriteLine("Quitting the Program");
        }
        

        
        public void PrintEntryError()
        {
            Console.WriteLine("You have entered an illegal character");
        }
        
    }
}



Assuming that no accounts have been added, the methods DepositMoney, WithdrawMoney, and ShowMoney seem to work perfectly, but whenever I choose the AddAccount method, it crashes immediately after adding any 5-digit account, then pressing Enter.

I am attaching a screenshot showing the process that occurs while trying to debug this code.
What is wrong with my logic in my AccountTest code?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,949
  • Joined: 04-October 09

Re: C# program for Web Application course- bank account error

Posted 02 March 2012 - 02:04 AM

First, line 69 is going to erase any accounts you've already added. What you are doing there is allocating space to hold maxBound number of accounts. 2nd time through, it will lose the previous space and allocate new space.

Second, in line 90, you access the *space* where the Account class would be stored, but no where do you actually *create* an Account object.
Was This Post Helpful? 0
  • +
  • -

#5 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 11

Re: C# program for Web Application course- bank account error

Posted 02 March 2012 - 03:01 PM

View PostMomerath, on 02 March 2012 - 02:04 AM, said:

First, line 69 is going to erase any accounts you've already added. What you are doing there is allocating space to hold maxBound number of accounts. 2nd time through, it will lose the previous space and allocate new space.

Second, in line 90, you access the *space* where the Account class would be stored, but no where do you actually *create* an Account object.


I edited the array so that the minBound is now in the brackets. See below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Program3
{
    class AccountTest
    {   
        static void Main(string[] args)
        {
            AccountTest myAccounts = new AccountTest();
            int minBound = 0;
            
            myAccounts.DrawMenu(minBound);
        }



        public void DrawMenu(int minBound)
        {
            AccountTest myAccounts = new AccountTest();
            string myChoice;
            char choice;
            int maxBound = 20;

            while(true)
            {
                Console.Write("Command (Add     Deposit     Withdraw    Show    Quit):    ");
                myChoice = Console.ReadLine();
                choice = Char.Parse(myChoice);

                switch (choice)
                {
                    case 'a':
                    case 'A':
                        myAccounts.AddAccount(minBound, maxBound);
                        break;
                    case 'd':
                    case 'D':
                        myAccounts.DepositMoney(minBound, maxBound);
                        break;
                    case 'w':
                    case 'W':
                        myAccounts.WithdrawMoney(minBound, maxBound);
                        break;
                    case 's':
                    case 'S':
                        myAccounts.ShowAccounts(minBound, maxBound);
                        break;
                    case 'q':
                    case 'Q':
                        myAccounts.QuitProgram();
                        break;
                    default:
                        myAccounts.PrintEntryError();
                        break;
                }
            } 
        }

        public void AddAccount(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[minBound];
            String myNum, myBal;
            long number;
            float balance;

            if (minBound >= maxBound)
            {
                Console.WriteLine("There is no more room for accounts:");
            }
            else
            {
                Console.Write("Account number to add: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);

                while (number < 10000 || number > 99999)
                {
                    Console.WriteLine("Error. Number must be 5 digits\n");
                    Console.Write("Account number to add: ");
                    myNum = Console.ReadLine();
                    number = Int64.Parse(myNum);
                }
                      
                Console.WriteLine("Beginning balance: ");
                myBal = Console.ReadLine();
                balance = Single.Parse(myBal);

                while (balance <= 0)
                {
                    Console.WriteLine("Error: Balance must be positive");
                    Console.Write("Beginning balance: ");
                    myBal = Console.ReadLine();
                    balance = Single.Parse(myBal);
                }

                for (int counter = 0; counter < minBound; counter++)
                {
                     if (number == (myAccounts[counter].getNumber()) )
                     {
                          Console.WriteLine("Account already exists.");
                     }
                      
                     else
                     {
                          myAccounts[minBound].setNumber(number);
                          myAccounts[minBound].setBalance(balance);
                          myAccounts[minBound] = new Account(number, balance);

                          Console.WriteLine("Account " + number + " added.");
                          minBound++;
                     }
                }
            }        
        }

        public void DepositMoney(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[minBound];
            String myNum, myAmt;
            long number;
            float amount = 0;

            if (minBound == 0)
                Console.WriteLine("There are no accounts to deposit money for");
            else
            {
                Console.WriteLine("Please enter an existing account number: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);
                
             
                for (int counter = 0; counter < minBound; counter++)
                {
                    if (number != (myAccounts[counter].getNumber()))
                    {
                        Console.WriteLine("Account does not exist.");
                    }

                    else
                    {
                        Console.Write("Amount to add: ");
                        myAmt = Console.ReadLine();
                        amount = Single.Parse(myAmt);

                        while (amount <= 0)
                        {
                            Console.WriteLine("Error: Amount entered must be positive");
                            Console.Write("Amount to add: ");
                            myAmt = Console.ReadLine();
                            amount = Single.Parse(myAmt);
                        }

                        myAccounts[minBound].Deposit(amount);
                        Console.WriteLine("{0:2c} deposited from account {0}.  New Balance: {0:2c}",
                               amount, myAccounts[minBound].getNumber(), myAccounts[minBound].getBalance() );
                        }
                    }    
               }
        }
        

        public void WithdrawMoney(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[minBound];
            String myNum, myAmt;
            long number;
            float amount = 0;

            if (minBound == 0)
                Console.WriteLine("There are no accounts to withdraw money for");
            else
            {
                Console.WriteLine("Please enter an existing account number: ");
                myNum = Console.ReadLine();
                number = Int64.Parse(myNum);

                if (minBound >= 1)
                {
                    for (int counter = 0; counter < minBound; counter++)
                    {
                        if (number != (myAccounts[counter].getNumber()))
                        {
                            Console.WriteLine("Account does not exist.");
                        }

                        else
                        {
                            Console.Write("Amount to withdraw: ");
                            myAmt = Console.ReadLine();
                            amount = Single.Parse(myAmt);

                            while (amount <= 0)
                            {
                                Console.WriteLine("Error: Amount entered must be positive");
                                Console.Write("Amount to withdraw: ");
                                myAmt = Console.ReadLine();
                                amount = Single.Parse(myAmt);
                            }

                      
                            if (amount > (myAccounts[minBound].getBalance()))
                            {
                                Console.WriteLine("{0:2c} is greater than current balance ({0:2c}) in the account", 
                                    amount, myAccounts[minBound].getBalance());
                            }
                            else
                            {
                                myAccounts[minBound].Withdraw(amount);
                                Console.WriteLine("{0:2c} withdrawn from account {0}.  New Balance: {0:2c}",
                                    amount, myAccounts[minBound].getNumber(), myAccounts[minBound].getBalance() );
                            }             
                        }
                    }
                }
            }
        }
        public void ShowAccounts(int minBound, int maxBound)
        {
            Account[] myAccounts = new Account[minBound];

            if (minBound == 0)
                Console.WriteLine("There are no accounts to show");
            else
            {
                Console.WriteLine("Account      Balance");
                Console.WriteLine("--------------------");
                for (int counter = 0; counter < minBound; counter++)
                    {
                        Console.Write("{0:2c}       {0:2c}\n", myAccounts[counter].getNumber(), myAccounts[counter].getBalance());
                    }
            }
        }
        
        public void QuitProgram()
        {
            Console.WriteLine("Quitting the Program");
        }
        

        
        public void PrintEntryError()
        {
            Console.WriteLine("You have entered an illegal character");
        }
        
    }
}



When I run the program and add an account, it reads the account number, then reads the balance entered. But then, instead of adding the account to the array, it just presents the menu options again (see attachment). Notice that the maxBound variable has been initialized to 20 only in the DrawMenu() method.

This is my Account.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program3
{
    public class Account
    {
        private long number;
        private float balance;

        public Account(long num, float bal)
        {
            number = num;
            balance = bal;
        }

        public void setNumber(long num)
        {
            number = num;
        }

        public long getNumber()
        {
            return number;
        }

        public void setBalance(float bal)
        {
            balance = bal;
        }

        public float getBalance()
        {
            return balance;
        }

        public void Deposit(float amount)
        {
            balance += amount;
        }

        public void Withdraw(float amount)
        {
            balance -= amount;
        }
    }
}



Should I rewrite my Account constructor like this?

public Account(long num, float bal)
{
     setNumber(num);
     setBalance(bal);
}



As for creating an account object, if I type 12345 for account number and 34.45 in the AddAccount() function, wouldn't that create the following object for element 0?

myAccounts[0] = new Account(12345, 34.45); 



Is there something wrong with my array declaration in any of my methods in the driver class?

This post has been edited by gutchman84: 02 March 2012 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,949
  • Joined: 04-October 09

Re: C# program for Web Application course- bank account error

Posted 02 March 2012 - 03:09 PM

Again, in line 65 this time, you are erasing your accounts by creating a new array to hold them all. You need to create the array once in your program and never again create it. So you should only have one line where you say "new Account[xxx]" with the square brackets. This will create the *space* to hold the accounts, but not the accounts themselves. Every other time it should be "new Account()" which does create the account, one of them, which you then store in the Account array.

Pretty much every program follows these steps:
1. Initialize data -> This is where we create data structures that we'll need to use throughout the program and populate them if possible.
2. Process data -> This is where we do work, making changes to values.
3. End Program -> This is where we save any data we'll need for the next time it runs.

You need to think about what you need to have in step 1 (and you don't have a step 3 as you aren't saving anything, yet).
Was This Post Helpful? 0
  • +
  • -

#7 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 11

Re: C# program for Web Application course- bank account error

Posted 02 March 2012 - 10:25 PM

View PostMomerath, on 02 March 2012 - 03:09 PM, said:

Again, in line 65 this time, you are erasing your accounts by creating a new array to hold them all. You need to create the array once in your program and never again create it. So you should only have one line where you say "new Account[xxx]" with the square brackets. This will create the *space* to hold the accounts, but not the accounts themselves. Every other time it should be "new Account()" which does create the account, one of them, which you then store in the Account array.

Pretty much every program follows these steps:
1. Initialize data -> This is where we create data structures that we'll need to use throughout the program and populate them if possible.
2. Process data -> This is where we do work, making changes to values.
3. End Program -> This is where we save any data we'll need for the next time it runs.

You need to think about what you need to have in step 1 (and you don't have a step 3 as you aren't saving anything, yet).


When I create the array, should I only declare it in only the main method like this?
int minBound = 1;
int maxBound = 20;
Account[] myAccounts = new Account[maxBound];


And then have the Add, Deposit, and Withdraw funcitons take in minBound, maxBound, and the Account array as parameters?

This is a piece of code from my Add method (after reading the number and balance inputs by the user)
                for (int counter = 0; counter < minBound; counter++)
                {
                    if (number == (myAccounts[counter].getNumber()))
                    {
                        Console.WriteLine("Account already exists.");
                    }

                    else
                    {
                        myAccounts[minBound].setNumber(number);
                        myAccounts[minBound].setBalance(balance);
                        myAccounts[minBound] = new Account(number, balance);

                        Console.WriteLine("Account " + number + " added.");
                        minBound++;
                    }
                }


When taking in the Account array as a parameter for all the methods to implement all menu choices, except Quit, should I declare the Account array as a float? Are there other ways I can declare my Account array?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1