private static void StartProcess() { //Console.WriteLine("Entry Point: Start"); //Console.WriteLine("Entry Point: Start installing the model time to the default value (for test only)"); var defaultDate = DateTime.Parse("10-Dec-15 00:00:00"); var context = new LalkaBankDabaseModelContainer(); var time = context.Table.FirstOrDefault(); if (time == null) { context.Table.AddOrUpdate(new Table() { Date = defaultDate }); } else { defaultDate = time.Date; } context.SaveChanges(); context.Dispose(); Console.WriteLine("Entry Point: Default value of model time - {0} (for test only)", defaultDate); Console.WriteLine("Entry Point: End installing the model time to the default value (for test only)"); Console.WriteLine("Entry Point: Start Credit Handler"); var creditHandler = new CreditHandler(_day); creditHandler.Init(); creditHandler.Start(); while (true) { var input = Console.ReadKey(); if (input.Key == ConsoleKey.Escape) { Console.WriteLine("Entry Point: Stop"); creditHandler.Stop(); Console.WriteLine("Entry Point: Continue? Press 'Y' for continue, any other key - for exit"); input = Console.ReadKey(); if (input.Key == ConsoleKey.Y) { creditHandler.Start(); continue; } break; } } }
public void Process(object obj) { if (_mut.WaitOne()) { var context = new LalkaBankDabaseModelContainer(); var credit = (Credit) obj; Console.WriteLine("Annuity Credit Thread {0}: Start working", _number); List<CreditHistory> history = context.CreditHistory.Where(x => x.CreditId == credit.Id).ToList(); history = history.OrderBy(x => x.Month).ToList(); var currentDate = context.Table.First().Date; if (currentDate < credit.DateStart) { Console.WriteLine("Annuity Credit Thread {0}: Credit in future? Really?", _number); _mut.ReleaseMutex(); return; } var currentMouth = ((currentDate.Year - credit.DateStart.Year) * 12) + currentDate.Month - credit.DateStart.Month; if (currentDate.Day - credit.DateStart.Day > 0) { currentMouth++; } Console.WriteLine("Annuity Credit Thread {0}: Current month for payment - {1}", _number, currentMouth); var previosHistory = history.Where(x => x.Month < currentMouth).ToList(); decimal arrears = 0; Console.WriteLine("Annuity Credit Thread {0}: Count of previos months - {1}", _number, previosHistory.Count); for (int i = 0; i < previosHistory.Count; i++) { Console.WriteLine("Annuity Credit Thread {0}: Start calculating arrears for the {1} month", _number, i + 1); var monthArrears = previosHistory[i].TotalPayment - previosHistory[i].Paid; if (monthArrears <= 0) { Console.WriteLine("Annuity Credit Thread {0}: Arrears on the credit does not exist", _number); continue; } Console.WriteLine("Annuity Credit Thread {0}: Main Arrears on the credit does exist - {1}", _number, monthArrears); previosHistory[i].Arrears = monthArrears; Console.WriteLine("Annuity Credit Thread {0}: Start calculating surcharge", _number); var diffDay = (currentDate - credit.DateStart.AddMonths(i + 1)).Days; var surchargeAlready = previosHistory[i].FinePayment; Console.WriteLine("Annuity Credit Thread {0}: Days of delay - {1}", _number, diffDay); Console.WriteLine("Annuity Credit Thread {0}: Already paid for surcharge - {1}", _number, surchargeAlready); var surcharge = monthArrears * (decimal)0.01 * diffDay - surchargeAlready; previosHistory[i].Fine = surcharge; Console.WriteLine("Annuity Credit Thread {0}: Current surcharge - {1}", _number, surcharge); Console.WriteLine("Annuity Credit Thread {0}: End calculating surcharge", _number); Console.WriteLine("Annuity Credit Thread {0}: End calculating arrears for the {1} month", _number, i + 1); arrears += monthArrears + surcharge; } if (arrears > 0) { context.Persons.Where(person => person.Credits.Any(x => x.Id == credit.Id)).FirstOrDefault().CreditHistoryIndex++; } Console.WriteLine("Annuity Credit Thread {0}: Whole arrears - {1}", _number, arrears); var bankBook = context.BankBooks.Where( account => account.CreditId == credit.Id).FirstOrDefault(); if (bankBook.cache <= 0) { Console.WriteLine("Annuity Credit Thread {0}: The account is empty", _number); context.SaveChanges(); context.Dispose(); _mut.ReleaseMutex(); return; } Console.WriteLine("Annuity Credit Thread {0}: The account balance - {1}", _number, bankBook.cache); if (arrears > 0) { for (int i = 0; i < previosHistory.Count; i++) { Console.WriteLine("Annuity Credit Thread {0}: Start calculating payments for arrears for the {1} month", _number, i + 1); var surcharge = previosHistory[i].Fine; if (bankBook.cache < surcharge) { surcharge = (int)bankBook.cache; previosHistory[i].FinePayment += (int)Math.Ceiling(surcharge); Console.WriteLine("Annuity Credit Thread {0}: It can be pay only - {1} for surcharge for {2} mouth (partial payment)", _number, surcharge, i + 1); } previosHistory[i].Fine -= surcharge; context.BankAaccount.FirstOrDefault().Amount += (int)Math.Ceiling(surcharge); bankBook.cache -= (int)Math.Ceiling(surcharge); Console.WriteLine("Annuity Credit Thread {0}: Payment done, Account balance - {1}, Remaining surcharge - {2}", _number, bankBook.cache, previosHistory[i].Fine); Console.WriteLine("Annuity Credit Thread {0}: End calculating payments for surcharge", _number); Console.WriteLine("Annuity Credit Thread {0}: Start calculating payments for main arrears for the {1} month", _number, i + 1); var mouthArrears = previosHistory[i].Arrears; if (bankBook.cache < mouthArrears) { mouthArrears = (int)bankBook.cache; Console.WriteLine("Annuity Credit Thread {0}: It can be pay only - {1} for main arrears for {2} mouth (partial payment)", _number, mouthArrears, i + 1); } previosHistory[i].Arrears -= mouthArrears; previosHistory[i].Paid += (int)Math.Ceiling(mouthArrears); context.BankAaccount.FirstOrDefault().Amount += (int)Math.Ceiling(mouthArrears); bankBook.cache -= (int)Math.Ceiling(mouthArrears); if (mouthArrears > 0) { previosHistory[i].FinePayment = 0; } Console.WriteLine("Annuity Credit Thread {0}: Payment done, Account balance - {1}, Remaining main arrears - {2}", _number, bankBook.cache, previosHistory[i].Arrears); Console.WriteLine("Annuity Credit Thread {0}: End calculating payments for main arrears for the {1} month", _number, i + 1); arrears = arrears - mouthArrears - surcharge; } } else { Console.WriteLine("Annuity Credit Thread {0}: Arrears on the credit does not exist", _number); } var curentHistory = history.Where(x => x.Month == currentMouth).FirstOrDefault(); if (arrears <= 0 && ((currentMouth > credit.PayCount) || (currentMouth == credit.PayCount && curentHistory.TotalPayment - curentHistory.Paid <= 0))) { context.Credits.FirstOrDefault(x => credit.Id == x.Id).Status = "1"; Console.WriteLine("Annuity Credit Thread {0}: Close credit", _number); context.SaveChanges(); context.Dispose(); _mut.ReleaseMutex(); return; } if (currentMouth <= credit.PayCount) { if (curentHistory.TotalPayment - curentHistory.Paid <= 0) { Console.WriteLine("Annuity Credit Thread {0}: Payment don't need for this month", _number); _mut.ReleaseMutex(); context.SaveChanges(); context.Dispose(); return; } Console.WriteLine("Annuity Credit Thread {0}: Payment need for this month", _number); var pay = (int) Math.Ceiling(curentHistory.TotalPayment - curentHistory.Paid); Console.WriteLine("Annuity Credit Thread {0}: Remaining payment for this month - {1}", _number, pay); if (bankBook.cache < pay) { pay = (int) bankBook.cache; Console.WriteLine("Annuity Credit Thread {0}: It can be pay only - {1} (partial payment)", _number, pay); } bankBook.cache -= pay; context.BankAaccount.FirstOrDefault().Amount += (int)Math.Ceiling((decimal)pay); curentHistory.Paid += pay; Console.WriteLine( "Annuity Credit Thread {0}: Payment done, Account balance - {1}, Remaining payment for mounth - {2}", _number, bankBook.cache, curentHistory.TotalPayment - curentHistory.Paid < 0 ? 0 : (int)Math.Ceiling(curentHistory.TotalPayment - curentHistory.Paid)); } if (arrears <= 0 && ((currentMouth > credit.PayCount) || (currentMouth == credit.PayCount && curentHistory.TotalPayment - curentHistory.Paid <= 0))) { context.Credits.FirstOrDefault(x => credit.Id == x.Id).Status = "1"; Console.WriteLine("Annuity Credit Thread {0}: Close credit", _number); context.SaveChanges(); context.Dispose(); _mut.ReleaseMutex(); return; } context.SaveChanges(); context.Dispose(); } _mut.ReleaseMutex(); }
private void TimerElapsed(object sender, ElapsedEventArgs e) { LalkaBankDabaseModelContainer context = new LalkaBankDabaseModelContainer(); Console.WriteLine("Credit Handler: Start processing life circle spiral"); Console.WriteLine("Credit Handler: Start updating current model time (for test only)"); var newDate = context.Table.First().Date.AddDays(1); context.Table.RemoveRange(context.Table); context.Table.Add(new Table() { Date = newDate }); context.SaveChanges(); Console.WriteLine("Credit Handler: Current model time - {0} (for test only)", newDate); Console.WriteLine("Credit Handler: End updating current model time (for test only)"); //All bank account into systems List<Credit> credits = context.Credits.Where(credit => credit.Status == "0").ToList(); if (credits.Count > 0) { int counter = 1; Console.WriteLine("Credit Handler: Start proccesing credits"); foreach (var credit in credits) { Console.WriteLine("Credit Handler: Start proccesing {0} credit", counter); StartThreadForCreditType(counter, credit); counter++; } } credits = null; context.Dispose(); }
static void Process() { var context = new LalkaBankDabaseModelContainer(); Console.WriteLine("Terminal: Enter Login"); string name = Console.ReadLine(); if (context.Persons.Any(person => person.Login == name) == false) { Console.WriteLine("Terminal: User with such login does not exist"); return; } Console.WriteLine("Terminal: Active user credits"); List<Credit> creditList = context.Credits.Where(credit => credit.Status == "0" && credit.Persons.Login == name).ToList(); if (creditList.Count == 0) Console.WriteLine("Terminal: Active user credits do not exist"); else { Console.WriteLine("Terminal: Active credits numbers"); int counter = 1; foreach (var credit in creditList) { Console.WriteLine("{0}. Credit with number {1}", counter, credit.Number); counter++; } Console.WriteLine("Terminal: Press 'P' to pay, other - for exit"); var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.P: { Console.WriteLine("Terminal: Enter credit's number"); var key1 = Console.ReadLine(); int number; int.TryParse(key1, out number); Credit credit = context.Credits.FirstOrDefault(x => x.Number == number); if (credit == null) { Console.WriteLine("Terminal: Wrong credit number"); } else { Console.WriteLine("Terminal: Enter the amount for payment"); string key2 = Console.ReadLine(); if (Validator(key2) == false) { Console.WriteLine("Terminal: Wrong credit amount"); return; } int pay = 0; int.TryParse(key2, out pay); Payments payment = new Payments { Id = Guid.NewGuid(), CreditId = credit.Id, Payment = key2, Time = context.Table.FirstOrDefault().Date }; context.Payments.Add(payment); BankBook book = credit.BankBooks.FirstOrDefault(x => x.CreditId == credit.Id); book.cache = (long)(book.cache + pay); Console.WriteLine("Terminal: Payment - {0}", payment.Payment); } break; } } } context.SaveChanges(); context.Dispose(); }