sum of all transactions?

Gric

New member
Joined
Feb 25, 2023
Messages
1
Programming Experience
Beginner
I'm stuck with Implementing method "TotalProvisionRevenue()" I need sum of all transactions provision . How can I get this data?
C#:
using BankingSystem.Exceptions;
using BankingSystem.Models;
using BankingSystem.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BankingSystem.Services
 {
      public class BankService
     {
          public BankService()
         {
              Accounts = new AccountRepository();
             Transactions = new TransactionRepository();
          }

        private AccountRepository Accounts { get; set; }
        private TransactionRepository Transactions { get; set; }

       

        public void ViewAccount()
        {
            Console.WriteLine("Please enter account number");
            var accountNumber = Console.ReadLine();
            var account = GetAccountByNumber(accountNumber);

            if (account == null)
            {
                Console.WriteLine("account does not exist");
                return;
            }

            account.Print();
            var accountTransactions = GetTransactionForAccount(accountNumber);


            accountTransactions.ForEach(x => x.Print());

          
        }
        private List<Transaction> GetTransactionForAccount(string accountNumber)
        {
            var transactionAccount = Transactions.Getall();
            return transactionAccount
                .Where(x => x.AccountNumberFrom == accountNumber || x.AccountNumberTo == accountNumber)
                .ToList();
        }

        public void TotalProvisionRevenue()
        {
            throw new NotImplementedException();
        }

        public void ViewAllTransactions()
        {
            var transactions = Transactions.Getall();

            transactions.ForEach(x => x.Print());

           
        }

        public void ViewAllAccounts()
        {
            var accounts = Accounts.GetAll();

            accounts.ForEach(x => x.Print());
          
        }

       

        public void CreateTransaction()
        {
            Console.WriteLine("Account from");
            Console.WriteLine("Enter account number ");

            var accountFromNumber = Console.ReadLine();
            var firstAccount = GetAccountByNumber(accountFromNumber);

            if (firstAccount == null)
            {
               throw new BankingSystemException($"Account with number {accountFromNumber} does not exits");
            }

            Console.WriteLine("Account to");
            Console.WriteLine("Enter account number ");
            var accountToNumber = Console.ReadLine();
            var secondAccount = GetAccountByNumber(accountToNumber);

            if (secondAccount == null)
            {
                throw new BankingSystemException($"Account with number {accountToNumber} does not exits");
            }

            Console.WriteLine("Enter amount to transfer ");
            var transferAmount = decimal.Parse(Console.ReadLine());

            if (transferAmount < 0)
            {
                throw new BankingSystemException("Invalid input. Amount can not be negative number");
            }

            var provision = transferAmount * 0.03m;

            firstAccount.DecreaseBalance(transferAmount + provision);
            secondAccount.IncreaseBalance(transferAmount);

            var transaction = new Transaction();
            transaction.AccountNumberFrom = accountFromNumber;
            transaction.AccountNumberTo = accountToNumber;
            transaction.Amount = transferAmount;
            transaction.Provision = provision;
            transaction.EntryDate = DateTime.Now;

            Transactions.AddTransactions(transaction);

          

        }
        public void CreateAccount()
        {
           
                Console.WriteLine("Please enter account number");
                var inputAccountNumber = Console.ReadLine();
             

         

            var newAccount = new Account();
            newAccount.AccountNumber = inputAccountNumber;
            Console.WriteLine("Please enter account holder name");
            var name = Console.ReadLine();
            Console.WriteLine("Please enter account holder surname");
            var surname = Console.ReadLine();
            Console.WriteLine("Please enter account holder balance");
            var balance = Console.ReadLine();

            ValidateAccountDetails(name, surname, balance);

            newAccount.Name = name;
            newAccount.Surname = surname;
            newAccount.IncreaseBalance(decimal.Parse(balance));

            Accounts.Create(newAccount);
        }

        private void ValidateAccountDetails(string name, string surname, string balance)
        {
            if(name.Trim() == "")
            {
                throw new BankingSystemException($"Name: {name} invalid");
            }
        }

        private void AddTransaction(Transaction transaction)
        {
            Transactions.Create(transaction);
        }

        private Account GetAccountByNumber(string accountNumber)
        {
            var accRepo = Accounts.GetAll();
            return accRepo.FirstOrDefault(x => x.AccountNumber == accountNumber);
        }
    }
}
 
Last edited by a moderator:
There's way too much code there. Please narrow it down to only code relevant to your specific issue. You might try providing a more detailed explanation of the problem as well. As it stands, you're expecting us to work out a lot from your code that you could just explain to us.
 
Since you managed to write ViewAllTransactions() which prints out all of the transactions, what is stopping you from just computing the sum of all the provision field values? Do you need to write the code the compute the sum using LINQ extension methods? Or are you free to do it anyway as long as you get the sum?
 

Latest posts

Back
Top Bottom