Untitled

 avatar
unknown
csharp
9 months ago
3.0 kB
16
Indexable
namespace Lab1_Task.ConsoleApp
{
    public class BankAccount
    {
        // Pola prywatne
        private string _accountNumber;
        private string _ownerName;
        private decimal _balance;
        private string _currency;
        private bool _isActive;
        private DateTime _creationDate;

        // Właściwości publiczne
        public string AccountNumber
        {
            get => _accountNumber;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("Account number cannot be empty.");
                _accountNumber = value;
            }
        }

        public string OwnerName
        {
            get => _ownerName;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("Owner name cannot be empty.");
                _ownerName = value;
            }
        }

        public decimal Balance
        {
            get => _balance;
            private set => _balance = value;
        }

        public string Currency
        {
            get => _currency;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("Currency cannot be empty.");
                _currency = value;
            }
        }

        public bool IsActive
        {
            get => _isActive;
            set => _isActive = value;
        }

        public DateTime CreationDate
        {
            get => _creationDate;
            private set => _creationDate = value;
        }

        // Konstruktory
        public BankAccount(string accountNumber, string ownerName, decimal initialBalance, string currency)
        {
            AccountNumber = accountNumber;
            OwnerName = ownerName;
            Balance = initialBalance >= 0 ? initialBalance : 0;
            Currency = currency;
            IsActive = true;
            CreationDate = DateTime.Now;
        }

        public BankAccount() : this("000000", "Unknown", 0, "PLN")
        {
            // Empty body.
        }

        // Metody
        public void Deposit(decimal amount)
        {
            if (amount > 0 && IsActive)
                Balance += amount;
        }

        public void Withdraw(decimal amount)
        {
            if (amount > 0 && amount <= Balance && IsActive)
                Balance -= amount;
        }

        public void TransferTo(BankAccount target, decimal amount)
        {
            if (target == null || amount <= 0 || !IsActive)
                return;

            if (amount <= Balance)
            {
                Withdraw(amount);
                target.Deposit(amount);
            }
        }

        public void Close()
        {
            IsActive = false;
        }
    }
}
Editor is loading...
Leave a Comment