morqe
morqe

Reputation: 11

How to get value from object in C#

I'm writing a simple ATM program in C# and it is suppose to be Object Oriented. I created a public class ATM and inside it a another public class ACC (account). How can i create method inside ATM class to write how much money is in ATM.ACC.ACCSaldo?

public class ACC
{
    public int ACCNr;       //accaunt number
    public int ACCPIN;      //PIN number
    public double ACCSaldo; //How much money is in account

    //constr
    public ACC(int p, int p2, double p3)
    {
        ACCNr = p;
        ACCPIN = p2;
        ACCSaldo = p3;
    }
}

My main part of code looks like this:

static void Main(string[] args)
{
    ATM ATM01 = new ATM();

    ATM.ACC acc1 = new ATM.ACC(3333, 1234, 3000.53);

    acc1.ACCNr = ATM01.czytnik();

    Console.WriteLine("ACCNr: {0}", acc1.ACCNr);
    Console.WriteLine("PIN: {0}", acc1.ACCPIN);
    Console.WriteLine("Saldo: {0}", acc1.ACCSaldo);

    ATM.tstPIN tstPIN1 = new ATM.tstPIN(acc1.ACCNr, acc1.ACCPIN ,acc1.ACCSaldo);
    tstPIN1.test();

    ATM.menu();

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}   

Upvotes: 0

Views: 132

Answers (3)

Tony XU
Tony XU

Reputation: 11

If there is only one Account in the ATM, you can declare a ACC instance (e.g. _acc) in the ATM; You need to initialise in your main method, then you can do sth like:

public void DisplayAccountDetails()
{
   Console.Write(String.Format("Account Balance:{0}", _acc.ACCSaldo));
}

I hope this helps.

Upvotes: 1

Kurorion
Kurorion

Reputation: 108

Assuming there is only one account in the ATM class and it looks similar to the follow:

public class ATM
{
    public ACC Acc;
}

You can add a method inside the ATM class like the following:

public void WriteSaldo()
{
    Console.WriteLine("Saldo: {0}", Acc.ACCSaldo);
}

Then in your Main method you can use ATM01.WriteSaldo();

Upvotes: 0

Nicholas Ramsay
Nicholas Ramsay

Reputation: 475

You could add get and set properties. public double ACCSaldo{ get; set; }

Upvotes: 1

Related Questions