private void btnAdd_Button_Click(object sender, RoutedEventArgs e) { CreditCustomer creditCustomer = new CreditCustomer(); if (txtCustomerName.Text == null || string.IsNullOrWhiteSpace(txtCustomerName.Text)) { MessageBox.Show("Customer Name is a Mandotory field! Please enter the customer name", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } creditCustomer.customerName = txtCustomerName.Text; creditCustomer.phoneNumber = (txtPhoneNumber.Text == null || string.IsNullOrWhiteSpace(txtPhoneNumber.Text)) ? "" : txtPhoneNumber.Text; creditCustomer.creditAmount = (txtCreditAmount.Text == null || string.IsNullOrWhiteSpace(txtCreditAmount.Text)) ? (float)0 : float.Parse(txtCreditAmount.Text); creditCustomer.addedDate = DateTime.Now; bool success = false; MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Add Credit Customer Confirmation", System.Windows.MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { success = creditCustomerDAL.AddCreditCustomer(creditCustomer); if (success == true) { clear(); MessageBox.Show("Credit Customer Added Successfully"); } else { MessageBox.Show("Failed To Add Credit Customer!"); } } }
static void Main(string[] args) { // instantiate an array of 5 CreditCustomer objects const int NUM_OF_CUSTS = 1; CreditCustomer[] creditCustomer = new CreditCustomer[NUM_OF_CUSTS]; // get user input for (int i = 0; i < creditCustomer.Length; i++) { Console.Write("Enter customer number: > "); int number = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter name: > "); string name = Convert.ToString(Console.ReadLine()); Console.Write("Enter amount due: > "); double due = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter interest rate: > "); double rate = Convert.ToDouble(Console.ReadLine()); creditCustomer[i] = new CreditCustomer(number, name, due, rate); } // Print to Console Summary Section Console.WriteLine("\nSummary\n"); double totalDue = 0.00; for (int count = 0; count < creditCustomer.Length; count++) { Console.WriteLine(creditCustomer[count].ToString()); totalDue += creditCustomer[count].amountDue; } Console.WriteLine("\nAmountDue for all Customers is {0}", totalDue.ToString("C2")); // Print to Console Payment Section Console.WriteLine("\nPayment Information\n"); double monthlyDue = 0.00; for (int count = 0; count < creditCustomer.Length; count++) { double payAmt = Common.getPaymentAmount(creditCustomer[count].amountDue); Console.WriteLine(creditCustomer[count].ToString()); monthlyDue += creditCustomer[count].amountDue / 24; } Console.WriteLine("\nMonthly payments for all Customers is {0}", monthlyDue.ToString("C2")); Console.ReadKey(); } // end Main
public bool AddCreditCustomer(CreditCustomer creditCustomer) { bool success = false; string sql = @"insert into CreditCustomer(customer_name,phone_number,credit_amount,added_date,active) values (@fullName,@phoneNumber,@creditAmount,@addedDate,1) "; SqlCommand cmd = new SqlCommand(sql, DbClass.con); cmd.Parameters.AddWithValue("@fullName", creditCustomer.customerName); cmd.Parameters.AddWithValue("@phoneNumber", creditCustomer.phoneNumber); cmd.Parameters.AddWithValue("@creditAmount", creditCustomer.creditAmount); cmd.Parameters.AddWithValue("@addedDate", creditCustomer.addedDate); try { DbClass.openConnection(); int rows = cmd.ExecuteNonQuery(); if (rows > 0) { success = true; } else { success = false; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } finally { DbClass.closeConnection(); } return(success); }