Vitaliy Atamanyuk
Vitaliy Atamanyuk

Reputation: 3

Is there a way to transfer a value of an int from one object to another within the same class via method in c#

class Bank
{
    private String accoutNumber;
    private double credit;

    public String AccountNumber
    {
        get { return accoutNumber; }
        set { accoutNumber = value; }
    }

    public double Credit
    {
        get { return credit; }
        set { credit = value; }
    }
        
    public Bank() { }

    public Bank(String accoutNumber)
    {
        this.accoutNumber = accoutNumber;
    }

    public Bank(String accoutNumber, int credit)
    {
        this.accoutNumber = accoutNumber;
        this.credit = credit;
    }

    public void addBalance(int amount) { credit += amount; }

    public void vyber(int amount)
    {
        if (amount > credit)
            return;

        credit -= amount;
    }

    public void transfer(int amount, String accoutNumber)
    {
        if (amount > credit)
            return;

        //else transfer from one object to another
    }
}

(in another file i am using the transfer method to transfer credit from one object to another, i just don't know how to do that, also i am not using any files for database, it is as simple as it could possible be)

Bank object1 = new Bank("1234567890/1234", 10000);
Bank object2 = new Bank("7845213154/1448", 7000);
object1.transfer("7845213154/1448", 2000)
//object1's credit = 8000
//object2's credit = 9000

Upvotes: 0

Views: 191

Answers (2)

SammuelMiranda
SammuelMiranda

Reputation: 460

Would go something like this (refine some rules here and there about validation of account numbers and such).

public class Bank
{
    private static global::System.Collections.Generic.Dictionary<string, global::Bank> accounts = new System.Collections.Generic.Dictionary<string, global::Bank>(10000);
    private string accountNumber;
    private decimal credit;
    public string AccountNumber { get { return this.accountNumber; } }
    public decimal Credit { get { return this.credit; } }
    public void addBalance(decimal amount) { this.credit += amount; }
    public void vyber(int amount) { if (amount <= credit) { this.credit -= amount; } }

    public static global::Bank GetAccount(string number)
    {
        if (string.IsNullOrEmpty(number)) { return null; }
        else
        {
            global::Bank bank = global::Bank.accounts[number];
            if (bank == null) { bank = new global::Bank(number); }
            return bank;
        }
    }

    public void transfer(decimal amount, string number)
    {
        if (amount <= credit && number != this.accountNumber)
        {
            global::Bank bank = global::Bank.GetAccount(number);
            if (bank == null) { throw new global::System.ArgumentException("Not Found"); }
            else
            {
                this.credit -= amount;
                bank.addBalance(amount);
            }
        }
    }

    private Bank(string number) { this.accountNumber = number; }
}

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063964

It sounds like what you're trying to do here is:

  • you have multiple objects of some type (perhaps called Account), each of which has an identity defined by accountNumber
  • when transferring funds, you want to look up a different account by number, and access that

Now: there is no automatic pre-build index of objects by their accountNumber. That is something you would need to add separately. For example, you might have a Dictionary<string, Account>, that you add each instance to:

var foo = new Account { accoutNumber = "12391", Credit = 420 };
var bar = new Account { accoutNumber = "58u98:a24", Credit = 9000 };

var accounts = new Dictionary<string, Account>();
accounts.Add(foo.accoutNumber , foo);
accounts.Add(bar.accoutNumber , bar);
// etc

Now, we can obtain accounts by their identity:

if (!accounts.TryGetValue(someAccountId, out var someAccount))
{
    throw new KeyNotFoundException("Target bank account not found");
}
// here, someAccount is the one we want by someAccountId
someAccount.DoSomething();

This, however, presents a problem; you wouldn't normally expect each individual Account object to keep hold of the entire set of accounts; you could have some account management type (which maintains the dictionary) perform both lookups and perform both deltas:

if (!accounts.TryGetValue(fromId, out var from))
{
    throw new KeyNotFoundException("Source bank account not found");
}
if (!accounts.TryGetValue(toId, out var to))
{
    throw new KeyNotFoundException("Destination bank account not found");
}
if (from.Balance < amount)
{
    throw new InvalidOperationException("Insufficient funds in source account");
}
from.AddBalance(-amount);
to.AddBalance(amount);

Note that this is not thread-safe. I'm guessing threading isn't a concern for what you're doing.

The alternative would be to pass in the accounts lookup to your method. This could the dictionary itself, or some helper service:

object1.Transfer("7845213154/1448", 2000, accounts);

and have the Transfer method do the lookup internally, after whatever validation you need:

public void Transfer(decimal amount, string accountNumber, Dictionary<string, Account> accounts)
{
    if (!accounts.TryGetValue(accountNumber, out var to))
    {
        throw new KeyNotFoundException("Destination bank account not found");
    }
    if (amount > credit)
    {
        throw new InvalidOperationException("Insufficient funds in source account");
    }
    AddBalance(-amount);
    to.AddBalance(amount);
}

Again, this is not thread-safe in any way, and is not transactional etc.

Upvotes: 1

Related Questions