Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need assistance accessing the account class. When I run the program it prompts with the ATM class's WriteLine("Welcome/Enter Account/Exit");. After I enter a number however, the command window just closes. I'm not sure what to do here. I should also mention this is my first program with C Sharp.

Account Class:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Account //Within the Account class, we have balance, withdraw,and deposit
        {
    
            ////An account array to create 3 seperate accounts each with a default balance of $100.00. 
            //int[] myAccount = new int[3];
            Account[] account = new Account[3];
    
    
            public double balance;
    
            public void deposit(double n)
            {
                balance += n;
            }
            public void withdraw(double n)
            {
                balance -= n;
            }
            public void calcInterest(double n)
            {
                //Here is where we calculate the interest!
            }
    
            public void menu()
            {
                {
                    {
    
                        int input = Convert.ToInt32(Console.ReadLine());
                        var currAccount = account[input]; // Not sure what this code is for. 
    
    
                        if (account[input] == null)
                        {
                            account[input] = new Account();
                            account[input].balance = 100; //Set initial balance to $100
                        }
    
                        if (input != 4)
                        {
    
                            Console.WriteLine("1) Deposit");
                            Console.WriteLine("2) Withdraw");
                            Console.WriteLine("3) Get Balance");
                            Console.WriteLine("4) Exit");
                            if(input == 1)
                                {
                                Console.WriteLine("How much would you like to deposit today?");
                                int moneyIn = Convert.ToInt32(Console.ReadLine());
                                account[input].deposit(moneyIn); //access the deposit method and increase balance by the amount entered by user.
                                Console.WriteLine("Here is your current balance:" + account[input].balance);
                            }
    
                                if(input == 2)
                                {
    
                                Console.WriteLine("How much would you like to withdraw today?");
                                int moneyOut = Convert.ToInt32(Console.ReadLine());
                                account[input].withdraw(moneyOut); //Access the withdraw method and decrease balance by the amount entered by user.
                                Console.WriteLine("Here is your current balance:" + account[input].balance);
                            }
    
                            if (input == 3)
                                {
    
                                Console.WriteLine("Here is your current balance:"+account[input].balance);
                                //return account[input].balance;
                            }
    
                            if (input == 4)
                            {
                                //I want to exit the application here. 
                            }                  
                         }
                     }
                }
            }
        }
    }




ATM Class:


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

namespace ConsoleApplication3
{
    class Atm //With the atm class we will have the atm menu
    {
        static void Main(string[] args)
        {
                int input = -1;
            do
            {
                Console.WriteLine("Welcome!");
                Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
                if (Int32.TryParse(Console.ReadLine(), out input))
                {
                    if (input >= 1 && input <= 3)
                    {
                        Console.WriteLine("You have entered " + input);
                        Console.ReadLine();

                        //ConsoleApplication3.Account[input]; // How do I access the account here? 
                    }
                }
            }
            while (input != 4); 
                { 
                                    Console.WriteLine("Goodbye.");
                }

        }
    }
}





















Program Class:


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

namespace ConsoleApplication3
{
    class Program
    {
      //Not really sure what this is for at the moment, or if it is even needed.
    }
}


What I have tried:

I've been searching online for quite some time but can't seem to find anything similar to the problem I'm having. This is my first program I am working on solo for class.
Posted
Updated 16-Sep-16 7:22am
v2
Comments
[no name] 16-Sep-16 11:22am    
Search online for "how to use the debugger"
[no name] 16-Sep-16 11:30am    
It doesn't have to. You say your program does have errors. The debugger will help you find them.

1 solution

Becasue your Main method does very little:
static void Main(string[] args)
    {
    Console.WriteLine("Welcome!");
    Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
    int input = Convert.ToInt32(Console.ReadLine());
    if (input >= 1 && input <= 3)
        {
        Console.WriteLine("You have entered " + input);
        Console.ReadLine();
        //ConsoleApplication3.Account[input]; // How do I access the account here? 
        }
    else if (input == 4)
        {
        Console.WriteLine("Goodbye.");
        //Exit Application
        }
    }
As soon as you enter the number it exits and the window closes so you can't see anything.
So start by changing that and adding a loop:
static void Main(string[] args)
    {
    int input = -1;
    do
        {
        Console.WriteLine("Welcome!");
        Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
        if (int32.TryParse(Console.ReadLine(), out input))
            {
            if (input >= 1 && input <= 3)
                {
                Console.WriteLine("You have entered " + input);
                Console.ReadLine();
                //ConsoleApplication3.Account[input]; // How do I access the account here? 
                }
            }
        } while input != 4)
    Console.WriteLine("Goodbye.");
    }
I've also replaced the Convert with TryParse - which means your app doesn't crash if you type the wrong thing!

You other problem is the "I want to use Account here" bit - it's a bit muddled as to exactly what you need but try:
Account acc = new Account();
acc.menu();
But you shouldn't really be putting the console stuff in the Account class - it should be concerned only with manipulating accounts, not handling user input. You main method should handle user input, and pass data to the Account class, and call methods inside it as the user asks for things to happen.

"The account is concerned with the interface because it needs to know what method to call. The ATM needs to be able to call the account and then later choose an action, by pressing the key. So I need to move the interface to the ATM section because it contains the main method?"

The ATM is "an interface" in the same way that login via the internet is - but you don't expect the two to be the same just because they access the same Account, do you? For one thing, where are you going to insert the bank card in your computer? :laugh:
The ATM constructs an instance of the account (which has the right account number) by asking the Account List (or whatever) for the Account itself and passing it the account number.
Disclaimer: you may not want to use this code (in fact I'd recommend you don't) but it may explain what I mean:
public class Account
    {
    private static Account[] accounts = new Account[3];
    private decimal balance = 0.0M;
    public Account(decimal openingDeposit, int accountNumber)
        {
        if (accountNumber >= 0 && accountNumber < accounts.Length)
            {
            balance = openingDeposit;
            accounts[accountNumber] = this;
            }
        }
    public Account GetAccount(int index)
        {
        if (index >= 0 && index < accounts.Length)
            {
            return accounts[index];
            }
        return null;
        }
    public decimal MakeDeposit(decimal deposit)
        {
        balance += deposit;
        return balance;
        }
    public decimal MakeWithdrawl(decimal deposit)
        {
        balance -= deposit;
        return balance;
        }
    public decimal GetBalance()
        {
        return balance;
        }
    }

Now, your Main method can "fill in the blanks":
public static void Main(string[] args)
    {
    Account a = new Account(123.88M, 0);
    Account b = new Account(456.99M, 1);
    Account c = new Account(1.24M, 2);
    ...
    if (int.TryParse(Console.ReadLine(), out input))
        {
        if (input >= 1 && input <= 3)
            {
            Account acc = Account.GetAccount(input - 1);
            Console.WriteLine("The account balance is: {0}", acc.GetBalance());
            }
        }
    }
Here, the Account is dealing with itself, which means it "knows" which account it's got to access - this is called an instance of the Account class, and each instance is separate from each other. They have different account numbers, different balances - and one account can't affect another.
There's probably quite a lot there that doesn't make much sense, but try to gloss over it for a bit and get a "feel" for what happens.

The ATM (your Main method) knows how to deal with the user via a screen and a couple of keys, but it doesn't affect your account itself - it asks your bank's main systems to do that and that "real world" separation is reflected in the software.

Have a think, and see if your get what I mean - I'm off now as Herself is insisting I "get a life" and the consequences for disobedience here could get serious! :laugh:
 
Share this answer
 
v2
Comments
Member 12743888 16-Sep-16 13:05pm    
Okay I changed it to the way you wrote the code. I'm glad to know about the TryParse, thank you for that knowledge. Why are we starting with input = -1? And about the muddled part. I'm trying to figure out how to call the account menu. It's in the account class, So in theory you get prompted with a greet and asked to enter the account number, after doing so It will ask you if you want to deposit, withdraw, check balance, or exit.
OriginalGriff 16-Sep-16 14:10pm    
Why the -1? To give it a value that won't match any of your choices. It's a good idea to explicitly assign a value, as your code won't compile if the system sees any possible route through your code that means a variable can be "read" without being "written" - even if in practice it's impossible that it will happen.

It's a "bit muddled" because you wrote it that way! Please don't be offended, but it reads like you started coding before you started thinking, and have got to a point where you aren't sure what does what any more. And that's normal - everybody does that at the start and it takes time to learn that it's a lot quicker to stop and think, plan, design before you ever touch the keyboard!

Your "greet and get the account no" stuff: why is that part of the account? Is it part of your bank? Or does it vary depending on how you access the account? If you walk into a branch, it's not the same interface as using the phone, and again that's different from using the internet. But it's the same Account that is behind all of those, the same code that checks account numbers, balances, and so forth, and serves it back to the particular interface you are using at that moment, yes?
So why is your Account concerned with the interface, with the details of which key to press?

Let the file that contains the Main method deal with the interface - by all means add methods there that handle the user choices - and pass them the Account instance that he selected. Then let them deal with the user selections: 1 for balance, 2 for deposit, and so one, then call the appropriate method on the Account instance to deal with it.
Have a think about it for a while, and see if you can understand what I mean. It takes a little getting your head round to start with.
Member 12743888 16-Sep-16 14:28pm    
Okay I understand the -1 aspect now. Thank you, I am not offended I enjoy the constructive criticism. The account is concerned with the interface because it needs to know what method to call. The ATM needs to be able to call the account and then later choose an action, by pressing the key.

So I need to move the interface to the ATM section because it contains the main method?
OriginalGriff 16-Sep-16 14:57pm    
Answer updated.
Member 12743888 18-Sep-16 11:07am    
Yeah the account needs to be instantiated on the fly. I'll just wait until I have time to speak with my teacher about it. I understand most of the code and how it works. You've helped me a ton, I really really appreciate all the time and effort. So I need to fix the code to make what ever account is entered upon entering, not before with the a,b,c. Which I can just use the same input variable somehow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900