Beispiel #1
0
        /*method save the transactions into file*/
        public bool saveTransactionFile(Transaction_BO transaction, string fileName)
        {
            bool result = false;

            try
            {
                string filePath = Path.Combine(Environment.CurrentDirectory, fileName);
                if (!File.Exists(filePath))
                {
                    FileStream fs = File.Create(filePath);
                    fs.Close();
                }
                string       jsonObject = JsonSerializer.Serialize(transaction);
                StreamWriter sw         = new StreamWriter(filePath, append: true);
                sw.WriteLine(jsonObject);
                sw.Close();
                result = true;
                return(result);
            }
            catch (Exception)
            {
                result = false;
                return(result);
            }
        }
Beispiel #2
0
        /*Helper method to add balance in account*/
        private bool addCash(Customer_BO customer, int amount, string transType)
        {
            bool               result       = false;
            Transaction_BO     transaction  = new Transaction_BO();
            Customer_DAL       customer_DAL = new Customer_DAL();
            List <Customer_BO> list         = customer_DAL.getAccountList();

            foreach (Customer_BO cus in list)
            {
                if (cus.Account_No == customer.Account_No)
                {
                    customer.Balance = customer.Balance + amount;
                    cus.Balance      = cus.Balance + amount;
                    break;
                }
            }
            DateTime dateTime = DateTime.UtcNow;

            dateTime    = dateTime.Date;
            transaction = createTransactionObj(amount, transType, customer.userName, customer.holderName, dateTime);
            customer_DAL.saveTransaction(transaction);
            bool isSaved = customer_DAL.saveAccounList(list);

            if (isSaved == true)
            {
                result = true;
            }
            return(result);
        }
Beispiel #3
0
        /*Method read the transaction file and return as a list*/
        public List <Transaction_BO> readTransactionFile(string fileName)
        {
            List <Transaction_BO> list = new List <Transaction_BO>();

            try
            {
                Transaction_BO transaction = null;
                string         filePath    = Path.Combine(Environment.CurrentDirectory, fileName);
                if (File.Exists(filePath))
                {
                    StreamReader sr        = new StreamReader(filePath);
                    string       jsoninput = sr.ReadLine();
                    while (jsoninput != null)
                    {
                        transaction = JsonSerializer.Deserialize <Transaction_BO>(jsoninput);
                        list.Add(transaction);
                        jsoninput = sr.ReadLine();
                    }
                    sr.Close();
                }
                return(list);
            }
            catch (Exception)
            {
                list = null;
                return(list);
            }
        }
Beispiel #4
0
        /*Method transder money from one account and add to other account*/
        public bool Transfer(Customer_BO depositor, Customer_BO customer, int amount)
        {
            bool               result       = false;
            Transaction_BO     transaction  = new Transaction_BO();
            Customer_DAL       customer_DAL = new Customer_DAL();
            List <Customer_BO> list         = customer_DAL.getAccountList();

            foreach (Customer_BO cus in list)
            {
                if (cus.Account_No == customer.Account_No)
                {
                    customer.Balance  = customer.Balance + amount;
                    cus.Balance       = customer.Balance + amount;
                    depositor.Balance = depositor.Balance - amount;
                    Customer_BO obj = list.FirstOrDefault(obj1 => obj1.Account_No == depositor.Account_No);
                    if (obj != null)
                    {
                        obj.Balance = depositor.Balance - amount;
                    }
                    break;
                }
            }
            bool isSaved = customer_DAL.saveAccounList(list);

            if (isSaved == true)
            {
                result = true;
            }
            string   transType = "Cash Transfer";
            DateTime dateTime  = DateTime.UtcNow.Date;

            transaction = createTransactionObj(amount, transType, depositor.userName, depositor.holderName, dateTime);
            customer_DAL.saveTransaction(transaction);
            return(result);
        }
Beispiel #5
0
        /*method actullay withdraw money from user account*/
        public (bool, bool, bool) cashWithdraw(Customer_BO customer, int money)
        {
            bool                  result = false; bool daily_transaction = false; bool low_balance = false;
            int                   previousTransaction = 0; string transType = "Cash WithDrawal";
            Transaction_BO        transaction  = new Transaction_BO();
            DateTime              dateTime     = DateTime.UtcNow.Date;
            Customer_DAL          customer_DAL = new Customer_DAL();
            List <Customer_BO>    list         = customer_DAL.getAccountList();
            List <Transaction_BO> t_list       = customer_DAL.getTransactionList();

            foreach (Transaction_BO tr in t_list)
            {
                if (customer.userName == tr.user_id && tr.date == dateTime && tr.type == "Cash WithDrawal")
                {
                    previousTransaction = tr.amount;
                }
            }
            foreach (Customer_BO cus in list)
            {
                if (customer.Account_No == cus.Account_No)
                {
                    int temp     = money;
                    int previous = temp + previousTransaction;
                    if (customer.Balance > money && money < Customer_BO.maxWithdraw && previous < Customer_BO
                        .maxWithdraw)
                    {
                        customer.Balance = customer.Balance - money;
                        cus.Balance      = cus.Balance - money;
                        result           = true;
                        transaction      = createTransactionObj(money, transType, customer.userName, customer.holderName, dateTime);
                        customer_DAL.saveTransaction(transaction);
                        break;
                    }
                    if (customer.Balance > money && previous > Customer_BO.maxWithdraw)
                    {
                        daily_transaction = true;
                    }
                    if (customer.Balance < money)
                    {
                        low_balance = true;
                    }
                    else
                    {
                        result = false;
                        break;
                    }
                }
            }
            bool isSaved = customer_DAL.saveAccounList(list);

            if (isSaved != true)
            {
                result = false;
            }
            return(result, daily_transaction, low_balance);
        }
Beispiel #6
0
        /*helper method to create transaction object*/
        private Transaction_BO createTransactionObj(int money, string transType, string userName, string holderName, DateTime date)
        {
            Transaction_BO obj = new Transaction_BO();

            obj.amount     = money;
            obj.type       = transType;
            obj.user_id    = userName;
            obj.holderName = holderName;
            obj.date       = date;
            return(obj);
        }
Beispiel #7
0
        /// <summary>
        /// Convert string to Transaction_BO
        /// </summary>
        /// <param name="text">text</param>
        /// <returns>Transaction_BO</returns>
        private Transaction_BO stringTOTransactionObject(string text)
        {
            if (text == null)
            {
                return(null);
            }
            string[]       data = text.Split(',');
            Transaction_BO t    = new Transaction_BO
            {
                transactionType = data[0],
                doerUserId      = int.Parse(data[1]),
                doerName        = data[2],
                amount          = int.Parse(data[3]),
                date            = DateTime.Parse(data[4]),
                receiverUserId  = int.Parse(data[5]),
                receiverName    = data[6]
            };

            return(t);
        }
Beispiel #8
0
 /*method save transactions list back to file*/
 public bool saveTransaction(Transaction_BO transaction)
 {
     return(saveTransactionFile(transaction, "Transaction.txt"));
 }