1 // Student: Pat Moss
2 // Instructor: Dave Hammer
3 // CIS162AD C# 5832 - Worksheet #7 - Banking Application
4 // 04/12/2006 Rev. 01a
5
6 // III. Programming Exercise:
7 // Write source code for the following.
8 // Create a base banking account. Decide what characteristics are
9 // common for savings and checking accounts, and include these
10 // characteristics in the base class. Define subclasses for checking
11 // and savings. Include a presentation class to test your design.
12 // Turn in your source code for this program.
13
14 using System;
15 using System.Drawing;
16 using System.Collections;
17 using System.ComponentModel;
18 using System.Windows.Forms;
19 using System.Data;
20
21 namespace worksheet07
22
23 // frmTeller.cs
24 {
25 ///
26 /// Summary description for frmTeller.
27 ///
28 public class frmTeller : System.Windows.Forms.Form
29 {
30 // Declare Checking and Savings accounts.
31 // Derived from the base class Account.
32 private CheckingAccount oCheckingAccount = new CheckingAccount();
33 private SavingsAccount oSavingsAccount = new SavingsAccount();
34
35 private System.Windows.Forms.Label label1;
36 private System.Windows.Forms.Label label2;
37 private System.Windows.Forms.Label label3;
38 private System.Windows.Forms.TextBox txtAccountNumber;
39 private System.Windows.Forms.TextBox txtBalance;
40 private System.Windows.Forms.TextBox txtAmount;
41 private System.Windows.Forms.GroupBox groupBox1;
42 private System.Windows.Forms.RadioButton rdbChecking;
43 private System.Windows.Forms.RadioButton rdbSavings;
44 private System.Windows.Forms.Button btnGetBalance;
45 private System.Windows.Forms.Button btnDeposit;
46 private System.Windows.Forms.Button btnWithdraw;
47 private System.Windows.Forms.Button btnHelp;
48 private System.Windows.Forms.Button btnExit;
49 ///
50 /// Required designer variable.
51 ///
52 private System.ComponentModel.Container components = null;
53
54 // Default constructor for the frmTeller form
55 public frmTeller()
56 {
57 //
58 // Required for Windows Form Designer support
59 //
60 InitializeComponent();
61
62 //
63 // TODO: Add any constructor code after InitializeComponent call
64 //
65 } // frmTeller
66
67 ///
68 /// Clean up any resources being used.
69 ///
70 protected override void Dispose( bool disposing )
71 {
72 if( disposing )
73 {
74 if (components != null)
75 {
76 components.Dispose();
77 }
78 }
79 base.Dispose( disposing );
80 } // Dispose
81
82 #region Windows Form Designer generated code
83 ///
84 /// Required method for Designer support - do not modify
85 /// the contents of this method with the code editor.
86 ///
87 private void InitializeComponent()
88 {
89 this.label1 = new System.Windows.Forms.Label();
90 this.label2 = new System.Windows.Forms.Label();
91 this.label3 = new System.Windows.Forms.Label();
92 this.txtAccountNumber = new System.Windows.Forms.TextBox();
93 this.txtBalance = new System.Windows.Forms.TextBox();
94 this.txtAmount = new System.Windows.Forms.TextBox();
95 this.groupBox1 = new System.Windows.Forms.GroupBox();
96 this.rdbSavings = new System.Windows.Forms.RadioButton();
97 this.rdbChecking = new System.Windows.Forms.RadioButton();
98 this.btnGetBalance = new System.Windows.Forms.Button();
99 this.btnDeposit = new System.Windows.Forms.Button();
100 this.btnWithdraw = new System.Windows.Forms.Button();
101 this.btnHelp = new System.Windows.Forms.Button();
102 this.btnExit = new System.Windows.Forms.Button();
103 this.groupBox1.SuspendLayout();
104 this.SuspendLayout();
105 //
106 // label1
107 //
108 this.label1.Location = new System.Drawing.Point(16, 16);
109 this.label1.Name = "label1";
110 this.label1.Size = new System.Drawing.Size(120, 24);
111 this.label1.TabIndex = 0;
112 this.label1.Text = "Account Number:";
113 //
114 // label2
115 //
116 this.label2.Location = new System.Drawing.Point(16, 40);
117 this.label2.Name = "label2";
118 this.label2.Size = new System.Drawing.Size(120, 24);
119 this.label2.TabIndex = 1;
120 this.label2.Text = "Balance:";
121 //
122 // label3
123 //
124 this.label3.Location = new System.Drawing.Point(16, 72);
125 this.label3.Name = "label3";
126 this.label3.Size = new System.Drawing.Size(120, 24);
127 this.label3.TabIndex = 2;
128 this.label3.Text = "Amount:";
129 //
130 // txtAccountNumber
131 //
132 this.txtAccountNumber.Location = new System.Drawing.Point(144, 8);
133 this.txtAccountNumber.Name = "txtAccountNumber";
134 this.txtAccountNumber.Size = new System.Drawing.Size(168, 22);
135 this.txtAccountNumber.TabIndex = 3;
136 this.txtAccountNumber.Text = "";
137 //
138 // txtBalance
139 //
140 this.txtBalance.Location = new System.Drawing.Point(144, 40);
141 this.txtBalance.Name = "txtBalance";
142 this.txtBalance.ReadOnly = true;
143 this.txtBalance.Size = new System.Drawing.Size(168, 22);
144 this.txtBalance.TabIndex = 4;
145 this.txtBalance.Text = "";
146 //
147 // txtAmount
148 //
149 this.txtAmount.Location = new System.Drawing.Point(144, 72);
150 this.txtAmount.Name = "txtAmount";
151 this.txtAmount.Size = new System.Drawing.Size(168, 22);
152 this.txtAmount.TabIndex = 5;
153 this.txtAmount.Text = "";
154 //
155 // groupBox1
156 //
157 this.groupBox1.Controls.Add(this.rdbSavings);
158 this.groupBox1.Controls.Add(this.rdbChecking);
159 this.groupBox1.Location = new System.Drawing.Point(16, 112);
160 this.groupBox1.Name = "groupBox1";
161 this.groupBox1.Size = new System.Drawing.Size(104, 72);
162 this.groupBox1.TabIndex = 6;
163 this.groupBox1.TabStop = false;
164 //
165 // rdbSavings
166 //
167 this.rdbSavings.Location = new System.Drawing.Point(8, 40);
168 this.rdbSavings.Name = "rdbSavings";
169 this.rdbSavings.Size = new System.Drawing.Size(88, 24);
170 this.rdbSavings.TabIndex = 1;
171 this.rdbSavings.Text = "Savings";
172 //
173 // rdbChecking
174 //
175 this.rdbChecking.Location = new System.Drawing.Point(8, 16);
176 this.rdbChecking.Name = "rdbChecking";
177 this.rdbChecking.Size = new System.Drawing.Size(88, 24);
178 this.rdbChecking.TabIndex = 0;
179 this.rdbChecking.Text = "Checking";
180 //
181 // btnGetBalance
182 //
183 this.btnGetBalance.Location = new System.Drawing.Point(144, 104);
184 this.btnGetBalance.Name = "btnGetBalance";
185 this.btnGetBalance.Size = new System.Drawing.Size(168, 24);
186 this.btnGetBalance.TabIndex = 7;
187 this.btnGetBalance.Text = "Get Balance";
188 this.btnGetBalance.Click += new System.EventHandler(this.btnGetBalance_Click);
189 //
190 // btnDeposit
191 //
192 this.btnDeposit.Location = new System.Drawing.Point(144, 136);
193 this.btnDeposit.Name = "btnDeposit";
194 this.btnDeposit.Size = new System.Drawing.Size(168, 24);
195 this.btnDeposit.TabIndex = 8;
196 this.btnDeposit.Text = "Deposit";
197 this.btnDeposit.Click += new System.EventHandler(this.btnDeposit_Click);
198 //
199 // btnWithdraw
200 //
201 this.btnWithdraw.Location = new System.Drawing.Point(144, 168);
202 this.btnWithdraw.Name = "btnWithdraw";
203 this.btnWithdraw.Size = new System.Drawing.Size(168, 24);
204 this.btnWithdraw.TabIndex = 9;
205 this.btnWithdraw.Text = "Withdraw";
206 this.btnWithdraw.Click += new System.EventHandler(this.btnWithdraw_Click);
207 //
208 // btnHelp
209 //
210 this.btnHelp.Location = new System.Drawing.Point(144, 200);
211 this.btnHelp.Name = "btnHelp";
212 this.btnHelp.Size = new System.Drawing.Size(168, 24);
213 this.btnHelp.TabIndex = 10;
214 this.btnHelp.Text = "Help";
215 this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
216 //
217 // btnExit
218 //
219 this.btnExit.Location = new System.Drawing.Point(144, 232);
220 this.btnExit.Name = "btnExit";
221 this.btnExit.Size = new System.Drawing.Size(168, 24);
222 this.btnExit.TabIndex = 11;
223 this.btnExit.Text = "Exit";
224 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
225 //
226 // frmTeller
227 //
228 this.AutoScaleBaseSize = new System.Drawing.Size(7, 15);
229 this.ClientSize = new System.Drawing.Size(328, 270);
230 this.Controls.Add(this.btnExit);
231 this.Controls.Add(this.btnHelp);
232 this.Controls.Add(this.btnWithdraw);
233 this.Controls.Add(this.btnDeposit);
234 this.Controls.Add(this.btnGetBalance);
235 this.Controls.Add(this.groupBox1);
236 this.Controls.Add(this.txtAmount);
237 this.Controls.Add(this.txtBalance);
238 this.Controls.Add(this.txtAccountNumber);
239 this.Controls.Add(this.label3);
240 this.Controls.Add(this.label2);
241 this.Controls.Add(this.label1);
242 this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
243 this.Name = "frmTeller";
244 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
245 this.Text = "Teller Entry";
246 this.Load += new System.EventHandler(this.frmTeller_Load);
247 this.groupBox1.ResumeLayout(false);
248 this.ResumeLayout(false);
249
250 } // InitializeComponent()
251 #endregion
252
253 ///
254 /// The main entry point for the application.
255 ///
256 [STAThread]
257 static void Main()
258 {
259 Application.Run(new frmTeller());
260 } // Main
261
262 // frmTeller form load event
263 private void frmTeller_Load(object sender, System.EventArgs e)
264 {
265 rdbChecking.Checked = true;
266 txtAccountNumber.Focus();
267 } // frmTeller_Load
268
269 // Validate the Account Number and radio buttons
270 // If they are ok, then
271 // Get the current Balance amount for this Account Number
272 private bool GetBalance()
273 {
274 txtBalance.Text = "";
275
276 if (!ValidateAcctNo(txtAccountNumber.Text))
277 {
278 txtAccountNumber.Focus();
279 return false;
280 }
281
282 if (!ValidateCkSav(rdbChecking.Checked, rdbSavings.Checked))
283 {
284 rdbChecking.Focus();
285 return false;
286 }
287
288 try
289 {
290 if (rdbChecking.Checked)
291 {
292 oCheckingAccount.SetAccountNumber(Convert.ToInt32(txtAccountNumber.Text));
293 txtBalance.Text = oCheckingAccount.GetBalance().ToString("0.00");
294 }
295 else if (rdbSavings.Checked)
296 {
297 oSavingsAccount.SetAccountNumber(Convert.ToInt32(txtAccountNumber.Text));
298 txtBalance.Text = oSavingsAccount.GetBalance().ToString("0.00");
299 }
300 }
301 catch (Exception eException)
302 {
303 MessageBox.Show(eException.Message);
304 return false;
305 }
306
307 return true;
308 } // GetBalance
309
310 // Process the GetBalance button click event
311 private void btnGetBalance_Click(object sender, System.EventArgs e)
312 {
313 bool BalanceOk = GetBalance();
314 } // btnGetBalance_Click
315
316 // Process the Deposit button event
317 private void btnDeposit_Click(object sender, System.EventArgs e)
318 {
319 bool BalanceOk = GetBalance();
320 if (!BalanceOk)
321 {
322 return;
323 }
324
325 if (!ValidateAmt(txtAmount.Text))
326 {
327 txtAmount.Focus();
328 return;
329 }
330 else
331 {
332 txtAmount.Text = Convert.ToDouble(txtAmount.Text).ToString("0.00");
333 }
334
335 try
336 {
337 if (rdbChecking.Checked)
338 {
339 txtBalance.Text = oCheckingAccount.Deposit(Convert.ToDouble(txtAmount.Text)).ToString("0.00");
340 // double dbl1 = Convert.ToDouble(txtAmount.Text);
341 // string str1 = Convert.ToString(oCheckingAccount.Deposit(dbl1));
342 // txtBalance.Text = str1;
343 }
344 else if (rdbSavings.Checked)
345 {
346 txtBalance.Text = oSavingsAccount.Deposit(Convert.ToDouble(txtAmount.Text)).ToString("0.00");
347 }
348 }
349 catch (Exception eException)
350 {
351 MessageBox.Show(eException.Message);
352 }
353 } // btnDeposit_Click
354
355 // Process the Withdraw button click event
356 private void btnWithdraw_Click(object sender, System.EventArgs e)
357 {
358 bool BalanceOk = GetBalance();
359 if (!BalanceOk)
360 {
361 return;
362 }
363
364 if (!ValidateAmt(txtAmount.Text))
365 {
366 txtAmount.Focus();
367 return;
368 }
369 else
370 {
371 txtAmount.Text = Convert.ToDouble(txtAmount.Text).ToString("0.00");
372 }
373
374 try
375 {
376 if (rdbChecking.Checked)
377 {
378 txtBalance.Text = oCheckingAccount.Withdraw(Convert.ToDouble(txtAmount.Text)).ToString("0.00");
379 }
380 else if (rdbSavings.Checked)
381 {
382 txtBalance.Text = oSavingsAccount.Withdraw(Convert.ToDouble(txtAmount.Text)).ToString("0.00");
383 }
384 }
385 catch (Exception eException)
386 {
387 MessageBox.Show(eException.Message);
388 }
389 } // btnWithdraw_Click
390
391 // Process the Help button click event
392 private void btnHelp_Click(object sender, System.EventArgs e)
393 {
394 string helpmsg = "";
395 helpmsg += " 1. Click on Checking or Savings.\n";
396 helpmsg += " 2. Enter an Account Number (1, 2, 3, 4, or 5).\n";
397 helpmsg += " 3. Click on Get Balance to view current balance.\n";
398 helpmsg += " 4. Enter an Amount and click on Deposit\n";
399 helpmsg += " to make a Deposit.\n";
400 helpmsg += " 5. Enter an Amount and click on Withdraw\n";
401 helpmsg += " to make a Withdrawal.\n";
402 helpmsg += " 6. Valid Account Numbers are 1, 2, 3, 4, and 5.\n";
403 helpmsg += " 7. Minimum balance for Checking is 0.00.\n";
404 helpmsg += " 8. Minimum balance for Savings is 50.00.\n";
405 helpmsg += " 9. Checking balances for this test implementation\n";
406 helpmsg += " are stored in a five element array in RAM.\n";
407 helpmsg += "10. Savings balances for this test implementation\n";
408 helpmsg += " are stored in a five element array in RAM.\n";
409 helpmsg += "11. Checking initial balances are 1000.00 delta 1000.00\n";
410 helpmsg += " (i.e., Acct 1: 1000.00, ..., Acct 5: 5000.00).\n";
411 helpmsg += "12. Savings initial balances are 10000.00 delta 10000.00\n";
412 helpmsg += " (i.e., Acct 1: 10000.00, ..., Acct 5: 50000.00).\n";
413
414 MessageBox.Show(helpmsg, "User Help",
415 MessageBoxButtons.OK, MessageBoxIcon.Information,
416 MessageBoxDefaultButton.Button1);
417 } // btnHelp_Click
418
419 // Process the Exit button click event
420 private void btnExit_Click(object sender, System.EventArgs e)
421 {
422 // this.Close();
423 Application.Exit();
424 } // btnExit_Click
425
426 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
427
428 // Utility functions
429
430 // Validate the input Account Number as:
431 // a. Non-blank
432 // b. Numeric
433 // c. Integer
434 private bool ValidateAcctNo(string txtAcctNo)
435 {
436 try
437 {
438 if (txtAcctNo == "")
439 {
440 throw new Exception("Please enter an Account Number.");
441 }
442 }
443 catch (Exception eException)
444 {
445 MessageBox.Show(eException.Message);
446 return false;
447 }
448
449 try
450 {
451 if (!IsNumeric(txtAcctNo))
452 {
453 throw new Exception("Please enter a numeric Account Number.");
454 }
455 }
456 catch (Exception eException)
457 {
458 MessageBox.Show(eException.Message);
459 return false;
460 }
461
462 try
463 {
464 if (!IsInteger(txtAcctNo))
465 {
466 throw new Exception("Please enter an integer Account Number.");
467 }
468 }
469 catch (Exception eException)
470 {
471 MessageBox.Show(eException.Message);
472 return false;
473 }
474
475 return true;
476 } // ValidateAcctNo
477
478 // Validate the input Checkboxes for Checking/Savings
479 // One check box must be selected
480 private bool ValidateCkSav(bool rdbCk, bool rdbSav)
481 {
482 try
483 {
484 if (!rdbCk && !rdbSav)
485 {
486 throw new Exception("Please choose Checking or Savings.");
487 }
488 }
489 catch (Exception eException)
490 {
491 MessageBox.Show(eException.Message);
492 return false;
493 }
494
495 return true;
496 } // ValidateCkSav
497
498 // Validate the input Amount value as:
499 // a. Non-blank
500 // b. Numeric
501 private bool ValidateAmt(string txtAmount)
502 {
503 try
504 {
505 if (txtAmount == "")
506 {
507 throw new Exception("Please enter an Amount.");
508 }
509 }
510 catch (Exception eException)
511 {
512 MessageBox.Show(eException.Message);
513 return false;
514 }
515
516 try
517 {
518 if (!IsNumeric(txtAmount))
519 {
520 throw new Exception("Please enter a numeric Amount.");
521 }
522 }
523 catch (Exception eException)
524 {
525 MessageBox.Show(eException.Message);
526 return false;
527 }
528
529 try
530 {
531 if (Convert.ToDouble(txtAmount) < 0.00)
532 {
533 throw new Exception("Please enter a positive Amount.");
534 }
535 }
536 catch (Exception eException)
537 {
538 MessageBox.Show(eException.Message);
539 return false;
540 }
541
542 return true;
543 } // ValidateAmt
544
545 // Utility function "IsNumeric()"
546 public bool IsNumeric(string theValue)
547 {
548 try
549 {
550 Convert.ToDouble(theValue);
551 return true;
552 }
553 catch
554 {
555 return false;
556 }
557 } // IsNumeric
558
559 // Utility function "IsInteger()"
560 public bool IsInteger(string theValue)
561 {
562 try
563 {
564 Convert.ToInt32(theValue);
565 return true;
566 }
567 catch
568 {
569 return false;
570 }
571 } // IsInteger
572 } // frmTeller
573
574 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
575
576 // BankingClasses.cs
577
578 public abstract class Account
579 {
580 // Declare five initial balances for five Accounts.
581 // Note: Data is normally retrieved from a database.
582 // Hard coded in a five-element array for demo testing only.
583 private double [ ] AcctBalTbl =
584 new double [ ] { 0.00, 1000.00, 2000.00, 3000.00, 4000.00, 5000.00 };
585 private int AcctMin = 1;
586 private int AcctMax = 5;
587 private double MinBalance = 0.00;
588 private int m_intAcctNo = 0;
589
590 public Account() // Constructor with zero arguments
591 {
592 // Initialize five initial balances for five Accounts
593 for (int n = AcctMin; n<=AcctMax; n++)
594 {
595 AcctBalTbl [n] = n * 1000.00;
596 }
597 } // Account default constructor
598
599 public Account(double dblDelta) // Constructor with one argument
600 {
601 // Initialize five initial balances for five Accounts
602 for (int n = AcctMin; n<=AcctMax; n++)
603 {
604 AcctBalTbl [n] = n * dblDelta;
605 }
606 } // Account one-argument constructor
607
608 // AccountNumber property
609 protected int AccountNumber
610 {
611 get
612 {
613 return m_intAcctNo;
614 }
615 set
616 {
617 m_intAcctNo = value;
618 }
619 } // AccountNumber
620
621 // Balance property
622 protected double Balance
623 {
624 get
625 {
626 return AcctBalTbl [AccountNumber];
627 }
628 set
629 {
630 AcctBalTbl [AccountNumber] = value;
631 }
632 } // Balance
633
634 // Set the Account Number property
635 public virtual bool SetAccountNumber (int intAcctNo)
636 {
637 try
638 {
639 if ((intAcctNo >= AcctMin) && (intAcctNo <= AcctMax))
640 {
641 AccountNumber = intAcctNo;
642 return true;
643 }
644 else
645 {
646 AccountNumber = 0;
647 throw new Exception("This Account Number is undefined in the database.");
648 }
649 }
650 catch (Exception eException)
651 {
652 MessageBox.Show(eException.Message);
653 AccountNumber = 0;
654 return false;
655 }
656 } // SetAccountNumber
657
658 public virtual double GetBalance()
659 {
660 return Balance;
661 } // GetBalance
662
663 public virtual double GetMinimumBalance()
664 {
665 return MinBalance;
666 } // GetMinimumBalance
667
668 public virtual double Deposit(double dblAmount)
669 {
670 Balance += dblAmount;
671 return Balance;
672 } // Deposit
673
674 public virtual double Withdraw(double dblAmount)
675 {
676 try
677 {
678 if (Balance >= (dblAmount + GetMinimumBalance()))
679 {
680 Balance -= dblAmount;
681 return Balance;
682 }
683 else
684 {
685 throw new Exception("Not enough funds.");
686 }
687 }
688 catch (Exception eException)
689 {
690 MessageBox.Show(eException.Message);
691 return Balance;
692 }
693 } // Withdraw
694 } // public abstract class Account
695
696 public class CheckingAccount : Account
697 {
698 private double CkMinBalance = 0.00;
699
700 public CheckingAccount() // Constructor with zero arguments
701 : base(1000.00) // Call base constructor with one argument
702 {
703 } // CheckingAccount default constructor
704
705 public override double GetMinimumBalance()
706 {
707 return CkMinBalance;
708 } // GetMinimumBalance
709
710 public override double Withdraw (double dblAmount)
711 {
712 try
713 {
714 if (Balance >= (dblAmount + GetMinimumBalance()))
715 {
716 Balance -= dblAmount;
717 return Balance;
718 }
719 else
720 {
721 throw new Exception("Not enough funds.");
722 }
723 }
724 catch (Exception eException)
725 {
726 MessageBox.Show(eException.Message);
727 return Balance;
728 }
729 } // Withdraw
730 } // CheckingAccount
731
732 public class SavingsAccount : Account
733 {
734 private double SavMinBalance = 50.00;
735
736 public SavingsAccount() // Constructor with zero arguments
737 : base(10000.00) // Call base constructor with one argument
738 {
739 } // SavingsAccount default constructor
740
741 public override double GetMinimumBalance()
742 {
743 return SavMinBalance;
744 } // GetMinimumBalance
745
746 public override double Withdraw (double dblAmount)
747 {
748 try
749 {
750 if (Balance >= (dblAmount + GetMinimumBalance()))
751 {
752 Balance -= dblAmount;
753 return Balance;
754 }
755 else
756 {
757 throw new Exception("Not enough funds.");
758 }
759 }
760 catch (Exception eException)
761 {
762 MessageBox.Show(eException.Message);
763 return Balance;
764 }
765 } // Withdraw
766 } // SavingsAccount
767 } // namespace worksheet07