private static void InitialDataImport() { using (var db = new ATMContext()) { var account1 = new CardAccount { CardNumber = "1234567893", CardPin = "1234", CardCash = 5000.00m }; var account2 = new CardAccount { CardNumber = "1234567894", CardPin = "2234", CardCash = 5000.00m }; var account3 = new CardAccount { CardNumber = "1234567895", CardPin = "3234", CardCash = 5000.00m }; db.CardAccounts.Add(account1); db.CardAccounts.Add(account2); db.CardAccounts.Add(account3); db.SaveChanges(); db.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX IX_CardNumber ON CardAccounts (CardNumber)"); } }
/// <summary> /// Saves the transaction to a log. /// </summary> /// <param name="sum">The sum.</param> /// <param name="cardNumber">The card number.</param> private static void LogTransaction(decimal sum, string cardNumber) { using (var atmDbContext = new ATMContext()) { TransactionHistory tranLog = new TransactionHistory { CardNumber = cardNumber, Ammount = sum, TransactionDate = DateTime.Now, }; atmDbContext.TransactionsHistory.Add(tranLog); atmDbContext.SaveChanges(); } }
public static void RetrievesMoney(ATMContext db) { Console.WriteLine("Enter card number:"); string cNumber = Console.ReadLine(); Console.WriteLine("Enter PIN:"); string pin = Console.ReadLine(); Console.WriteLine("Enter money:"); decimal money = decimal.Parse(Console.ReadLine()); using (db) { using (var tran = new TransactionScope()) { var firstCheck = db.CardAccounts.Where(x => x.Number == cNumber).Where(x => x.PIN == pin); if (firstCheck.Count() != 1) { tran.Dispose(); Console.WriteLine("More"); } else { var a = db.CardAccounts.Where(x => x.Number == cNumber).First(); if (a.Cash >= money) { try { a.Cash -= money; db.SaveChanges(); tran.Complete(); Console.WriteLine("OK"); AddToHistory(cNumber, DateTime.Now, money, db); } catch (Exception ex) { Console.WriteLine(ex); tran.Dispose(); Console.WriteLine("No"); } } } } } }
public static bool WithdrawMoney(int PIN, int cardNumber, decimal moneyToWithdraw, ATMContext db) { bool success = true; int cardNumberToRecord = 0; using (var scope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead } )) { var card = (from c in db.CardAccounts where c.CardNumber == cardNumber select c).First(); if (card == null || card.CardPIN != PIN || card.CardCash < moneyToWithdraw) { success = false; } else { card.CardCash -= moneyToWithdraw; cardNumberToRecord = card.CardNumber; scope.Complete(); } } if (success) { RecordWithdrawal(cardNumberToRecord, moneyToWithdraw, db); db.SaveChanges(); return true; } else { return false; } }
static void Main() { var context = new ATMContext(); var acc = context.CardAccounts.Find(1); using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required)) { if (context.CardAccounts.Any(c => c.CardNumber == acc.CardNumber && c.CardPIN == acc.CardPIN)) { if (context.CardAccounts.First(c => c.CardNumber == acc.CardNumber).CardCash >= acc.CardCash && context.CardAccounts.First(c => c.CardNumber == acc.CardNumber).CardCash > 0) { context.TransactionHistories.Add(new TransactionHistory { CardNumber = context.CardAccounts.First(c => c.CardNumber == acc.CardNumber).CardNumber, Amount = acc.CardCash, TransactionDate = DateTime.Now }); context.CardAccounts.First(c => c.CardNumber == acc.CardNumber).CardCash -= acc.CardCash; Console.WriteLine("Transaction complete!"); context.SaveChanges(); tran.Complete(); } else { throw new ArgumentException("You cannot request more money than you have in your account."); } } else { throw new ArgumentException("Invalid credit card number and / or card PIN."); } } }
private static void AddToHistory(string cNumber, DateTime transactionTime, decimal money, ATMContext db) { using (db) { using (var tran = new TransactionScope()) { var history = new TransactionsHistory(cNumber, transactionTime, money); db.TransactionsHistories.Add(history); db.SaveChanges(); tran.Complete(); Console.WriteLine("added"); } } }
public static void AddCardAccount(CardAccount newAccount, ATMContext context) { context.CardAccounts.Add(newAccount); context.SaveChanges(); Console.WriteLine("Correct added!"); }
public static void CashWithdraw(string cardNumber, string cardPin, decimal sum) { using (var db = new ATMContext()) { bool success = false; using (TransactionScope transaction = new TransactionScope()) { try { // get card number and pin var card = db.CardAccounts. Where(c => c.CardNumber == cardNumber). Where(p => p.CardPin == cardPin).First(); if (card.CardCash >= sum) { // withdraw sum card.CardCash -= sum; } else { Console.WriteLine("Insufficient funds!"); } transaction.Complete(); success = true; } catch (Exception ex) { Console.WriteLine("Wrong PIN or Invalid card!"); } } if (success) { // save in history var log = new TransactionsHistory() { CardNumber = cardNumber, TransactionDate = DateTime.Now, Ammount = sum }; db.TransactionsHisory.Add(log); db.SaveChanges(); Console.WriteLine("Transaction successful!"); } } }
/// <summary> /// Retrieves money from the ATM. /// </summary> /// <param name="cardNumber">The card number.</param> /// <param name="PINCode">The pin code.</param> /// <param name="sum">The sum.</param> private static void WithdrawMoney(string cardNumber, string PINCode, decimal sum) { /* 02. Using transactions write a method which retrieves some money (for example $200) from certain account. * The retrieval is successful when the following sequence of sub-operations is completed successfully: * A query checks if the given CardPIN and CardNumber are valid. * The amount on the account (CardCash) is evaluated to see whether it is bigger than the requested sum (more than $200). * The ATM machine pays the required sum (e.g. $200) and stores in the table CardAccounts the new amount (CardCash = CardCash - 200).*/ /* 03. Extend the project from the previous exercise and add a new table TransactionsHistory * with fields (Id, CardNumber, TransactionDate, Ammount) containing information about all money retrievals on all accounts. * Modify the program logic so that it saves historical information (logs) in the new table after each successful money withdrawal. * What should the isolation level be for the transaction?*/ using (var atmDbContext = new ATMContext()) { // We work with money - so we use Repetable read. TransactionOptions tranOptions = new TransactionOptions(); tranOptions.IsolationLevel = IsolationLevel.RepeatableRead; using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required, tranOptions)) { var account = atmDbContext.CardAccounts.Where(ca => ca.CardNumber == cardNumber && ca.CardPIN == PINCode).FirstOrDefault(); if (account == null) { throw new InvalidOperationException("Card does not exists or PIN is wrong!"); } if (sum <= 0) { throw new InvalidOperationException("Invalid sum!"); } if (account.CardCash < sum) { throw new InvalidOperationException("Not enough minerals!"); } // The ATM machine pays the required sum ShowMeTheMoney(sum); // Update the account balance. account.CardCash = account.CardCash - sum; // Saves the transaction to a log. LogTransaction(sum, account.CardNumber); atmDbContext.SaveChanges(); tran.Complete(); } } }