public ProcessOrder(IPayment payment, Customer customer) { if (payment.GetType().ToString() == "OrderProcessingApplication.Classes.BookPayment") { ISlip slip = ((BookPayment)payment).GenerateSlip(); IPayment commission = ((BookPayment)payment).GenerateCommision(); } if (payment.GetType().ToString() == "OrderProcessingApplication.Classes.MembershipPayment") { bool isSuccess = ((MembershipPayment)payment).ProcessMemberShip(customer); } if (payment.GetType().ToString() == "OrderProcessingApplication.Classes.MembershipPayment") { bool isSuccess = ((MembershipPayment)payment).ProcessMemberShip(customer); } if (payment.GetType().ToString() == "OrderProcessingApplication.Classes.Video") { ISlip videoSlip = ((VideoPAyment)payment).GenerateSlip(customer.VideoText); } }
public bool PaymentExists(IPayment payment) { switch (payment.GetType().Name) { case "Cash": return(availablePayments.OfType <Cash>().Any()); case "Card": return(availablePayments.OfType <Card>().Any()); default: return(availablePayments.OfType <BankTransfer>().Any()); } }
private static void AddPaymentToDB(IPayment payment) { Type paymentType = payment.GetType(); var paymentListType = typeof(List <>).MakeGenericType(paymentType); Type[] typesArray = new Type[] { typeof(int), paymentListType }; var type = typeof(Dictionary <,>).MakeGenericType(typesArray); var totDict = File.ReadAllText(_dbPaymentsFilePath); var dictPay = JsonConvert.DeserializeObject(totDict, type); if (dictPay == null) { var dictCtors = type.GetConstructors(); dictPay = dictCtors[0].Invoke(new object[] { }); } object[] prs = new object[] { (int)payment.ContractId, null }; var getList = type.GetMethod("TryGetValue"); getList?.Invoke(dictPay, prs); var ctors = paymentListType.GetConstructors(); var newList = prs[1] ?? ctors[0].Invoke(new object[] { }); object[] prms = new object[] { payment }; var addMethod = paymentListType.GetMethod("Add"); addMethod?.Invoke(newList, prms); //remove the old item from the dictionary object[] prmsRemove = new object[] { (int)payment.ContractId }; var removeMethod = type.GetMethod("Remove", new[] { type.GetGenericArguments()[0] }); removeMethod?.Invoke(dictPay, prmsRemove); //add new item to the dictionary object[] prmsAdd = new object[] { (int)payment.ContractId, newList }; var addToDictMethod = type.GetMethod("Add"); addToDictMethod?.Invoke(dictPay, prmsAdd); var strDictContract = JsonConvert.SerializeObject(dictPay); File.WriteAllText(_dbPaymentsFilePath, strDictContract); }