Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I am trying to access a method from another class but on the second thought I could return a variable instead.

I have 2 options here. I can call "myBank.Deposit(cash)" method in my User class GiveCash method or I could simply return a variable/value by using the method.

I think, in order to access deposit method in BankAccount class I would have to initialize I mean create an object of that class and that wouldn't make any sense since its already done in my main class/code.

So, I am just seeking a little bit of advice what is the best thing to do here and why?

C#
// Developed by : xxx xxx

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

namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            short choice;
            decimal money;
            string input;

            BankAccount myBank = new BankAccount("AIB BankAccount No.123", 1000);
            User me = new User("Michal", 200);

            me.Print();
            myBank.Print();

            Console.WriteLine("\nOptions:");
            Console.WriteLine("1. - Deposit");
            Console.WriteLine("2. - Withdraw");

            Console.WriteLine("\nPlease choose the number of your action!");
            input = Console.ReadLine();
            choice = Convert.ToSByte(input);

            if (choice == 1)
            {
                Console.Write("Deposit the value : ");
                input = Console.ReadLine();
                money = Convert.ToDecimal(input);

                me.GiveCash(money);
                myBank.Deposit(money);
                
            }
            else
            {
                Console.Write("Withdraw the value : ");
                input = Console.ReadLine();
                money = Convert.ToDecimal(input);

                myBank.Withdraw(money);
            }

            Console.Read();
        }
    }
}

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

namespace MyApplication
{
    class User
    {
        string name;
        decimal cash;

        public User() : this("Default name", 0) { }

        public User(string Name, decimal Cash)
        {
            name = Name;
            cash = Cash;
        }

        public decimal GiveCash(decimal value)
        {
            if (cash <= value)
            {
                cash -= value;

                return cash;
            }
            else
            {
                Console.WriteLine("DAMN! I dont have enought money in my pocket!");
            }
        }

        public void Print()
        {
            Console.WriteLine("Hello {0} you have {1} cash in your pocket.", name, cash);
        }
    }
}



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

namespace MyApplication
{
    class BankAccount
    {
        // public fields are not recommended due to security
        string name;
        decimal balance;

        // providing default constructor
        public BankAccount() : this("default name", 0) { }

        // constructor is used to initialize data fields, we can pass variables to a constructor but it doesnt return a value
        public BankAccount(string Name, decimal Balance)
        {
            balance = Balance;
            name = Name;
        }

        public void Withdraw(decimal value)
        {
            balance -= value;
            Console.WriteLine("Current balance is {0}.", balance);
        }

        public void Deposit(decimal value)
        {
            balance += value;
            Console.WriteLine("Current balance is {0}.", balance);
        }

        public void Print()
        {
            Console.WriteLine("Current balance of {0} is {1}.", name, balance);
        }

        // no need for destructor, its done automaticaly in garbage collector
    }
}


Any comments or suggestions are welcome. Thank you.
Posted

1 solution

Since a Deposit is logically impossible without an Account to deposit it in, you need to have an instance of the BankAccount class ready to receive the money. So the way you have it is already correct:
C#
myBank.Deposit(money);

The confusion comes when you try to deposit money via a user, not because you "have to create an object of that class" but because you haven't established a link between the User and the Account.

Think about it: when you (User) goto the bank with a fist full of your hard-earned money, the first thing they want to know is "What account?" - if only because (like me and many others) you may have more than one account with the same bank: Checking and Deposit accounts for example.

So it isn't that you want to create and instance of the account, but rather that you need to find out the instance of the account that the user is planning to deposit into: and he may be depositing the money into a number of accounts at the same time - 50% here, 20% there and 30% other there please!

The next fun is that should "Michal" be allowed to deposit or withdraw money on that account? :laugh:

So think about how you need to operate in your Main method - work out what happens with a real bank, and try to emulate that.
 
Share this answer
 

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