Question

C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private...

C#

Step 1: Create a Windows Forms Application.

Step 2: Create a BankAccount class. Include:

  • Private data fields to store the account holder's name and the account balance
  • A constructor with 0 arguments
  • A constructor with 1 argument (account holder's name)
  • A constructor with 2 arguments(account holder's name and account balance)
  • Public properties for the account holder's name and the account balance. Do not use auto-implemented properties.
  • A method to increase the balance (deposit)
  • A method to decrease the balance (withdraw)

Step 3: Create a custom NegativeException class that derives from ApplicationException.

Step 4: Design the form (there will be only one). Include:

  • A textbox to input/display the account holders name
  • A textbox to input a dollar amount
  • A label to display the current balance
  • A button to create an account (instantiate a BankAccount object)
  • A button to deposit a dollar amount
  • A button to withdraw a dollar amount
  • Any additional identifying labels

Form behavior:

When the form loads, hide the deposit and withdraw buttons, as well as the labels to display the current balance. These should only be visible after the account is created.  When the program starts, the user should be able to type in a name, and a dollar amount that represents the starting balance. They then click a button to create an account. If this is successful, then hide the create account button, make the name textbox read-only, and show the deposit and withdraw buttons. After each deposit and withdraw, update the BankAccount object's balance and display the balance into the appropriate label on the form. Also, after each deposit or withdraw, clear the dollar amount text box and give it focus.  

Exception handling:

In the form, include exception handling in the windows form code. Do not allow a negative number to be passed to any BankAccount methods (including set methods for properties). Also, handle any other exceptions that may occur. Display exception messages in a message box.

Throw exceptions in the BankAccount class preventing any negative numbers from being stored in the account balance. Prevent negative numbers from being used to increase or decrease the account balance. Vary the exception messages so that they are descriptive of the problem that occurred. Example: "Cannot open an account with a negative balance."

Homework Answers

Answer #1

Here is the answer for your question in C# Programming Language.

CODE :

BankAccount.cs

using System;
using System.Collections.Generic;

namespace BankAccount
{
public class BankAccount
{
private string holderName;
private double balance;
//Constructor with 0 arguments
public BankAccount(){
}
//Construtor with 1 argument
public BankAccount(string name){
holderName = name;
balance = 0;
}
//Constructor with 2 arguments
public BankAccount(string name,double bal){
holderName = name;
if (bal < 0)
throw new NegativeException("Exception : Cannot open an account with a negative balance.");
balance = bal;
}
//Public properties
public string HolderName { get => holderName; }
public double Balance { get => balance; }
//Deposit
public void deposit(double amount){
if (amount < 0)
throw new NegativeException("Exception : Cannot deposit negative amount to account");
balance += amount;
}
//Withdrawal
public void withdraw(double amount) {
if (amount < 0)
throw new NegativeException("Exception : Cannot withdraw negative amount from account");
if (amount > balance)
Console.WriteLine("Insufficient balance..");
else
balance -= amount;
}
}
}

NegativeException.cs

using System;

namespace BankAccount
{
public class NegativeException : ApplicationException
{
string exception;
public NegativeException(string exceptionType) : base() {
exception = exceptionType;
}
public override string ToString(){
return exception;
}
}
}

Form1.cs

using System;
using System.Windows.Forms;

namespace BankAccount
{
public partial class Form1 : Form
{
public BankAccount account;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
depositBtn.Hide();
withdrawBtn.Hide();
}

private void createAccount(object sender, EventArgs e)
{
try
{
string name = holderNameTxt.Text;
if (name.Equals(""))
throw new Exception("Exception : Account holder name cannot be empty.");
double bal = double.Parse(amountTxt.Text);
if (bal == 0) {
account = new BankAccount(name);
balanceTxt.Text = "Rs. " + account.Balance.ToString() + "/-";
}
else {
account = new BankAccount(name, bal);
balanceTxt.Text = "Rs. " + account.Balance.ToString() + "/-";
}
depositBtn.Show();
withdrawBtn.Show();
createAccountBtn.Hide();
amountTxt.Text = "";
holderNameTxt.ReadOnly = true;
}
catch(NegativeException n){
MessageBox.Show(n.ToString());
}
catch(FormatException ex){
MessageBox.Show("Invalid input given...Try again");
}
}

private void deposit(object sender, EventArgs e) {
try {
double amount = double.Parse(amountTxt.Text);
account.deposit(amount);
amountTxt.Text = "";
}
catch(NegativeException n) { MessageBox.Show(n.ToString()); }
catch(FormatException ){ MessageBox.Show("Invalid input given...Try again"); }
balanceTxt.Text = "Rs. " + account.Balance.ToString() + "/-";
}

private void withdraw(object sender, EventArgs e){
try{
double amount = double.Parse(amountTxt.Text);
account.withdraw(amount);
amountTxt.Text = "";
}
catch (NegativeException n){ MessageBox.Show(n.ToString()); }
catch (FormatException ){ MessageBox.Show("Invalid input given...Try again"); }
balanceTxt.Text = "Rs. " + account.Balance.ToString() + "/-";
}
}
}

Form1.cs[Design]

namespace BankAccount
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.holderNameLbl = new System.Windows.Forms.Label();
this.amountLbl = new System.Windows.Forms.Label();
this.balanceLbl = new System.Windows.Forms.Label();
this.holderNameTxt = new System.Windows.Forms.TextBox();
this.amountTxt = new System.Windows.Forms.TextBox();
this.balanceTxt = new System.Windows.Forms.Label();
this.createAccountBtn = new System.Windows.Forms.Button();
this.depositBtn = new System.Windows.Forms.Button();
this.withdrawBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// holderNameLbl
//
this.holderNameLbl.AutoSize = true;
this.holderNameLbl.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.holderNameLbl.Location = new System.Drawing.Point(12, 27);
this.holderNameLbl.Name = "holderNameLbl";
this.holderNameLbl.Size = new System.Drawing.Size(199, 21);
this.holderNameLbl.TabIndex = 0;
this.holderNameLbl.Text = "Account Holder\'s Name :";
//
// amountLbl
//
this.amountLbl.AutoSize = true;
this.amountLbl.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.amountLbl.Location = new System.Drawing.Point(12, 67);
this.amountLbl.Name = "amountLbl";
this.amountLbl.Size = new System.Drawing.Size(123, 21);
this.amountLbl.TabIndex = 1;
this.amountLbl.Text = "Enter amount : ";
//
// balanceLbl
//
this.balanceLbl.AutoSize = true;
this.balanceLbl.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.balanceLbl.Location = new System.Drawing.Point(12, 112);
this.balanceLbl.Name = "balanceLbl";
this.balanceLbl.Size = new System.Drawing.Size(143, 21);
this.balanceLbl.TabIndex = 2;
this.balanceLbl.Text = "Current Balance : ";
//
// holderNameTxt
//
this.holderNameTxt.Location = new System.Drawing.Point(218, 27);
this.holderNameTxt.Name = "holderNameTxt";
this.holderNameTxt.Size = new System.Drawing.Size(222, 20);
this.holderNameTxt.TabIndex = 3;
//
// amountTxt
//
this.amountTxt.Location = new System.Drawing.Point(218, 69);
this.amountTxt.Name = "amountTxt";
this.amountTxt.Size = new System.Drawing.Size(222, 20);
this.amountTxt.TabIndex = 4;
//
// balanceTxt
//
this.balanceTxt.AutoSize = true;
this.balanceTxt.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.balanceTxt.Location = new System.Drawing.Point(214, 112);
this.balanceTxt.Name = "balanceTxt";
this.balanceTxt.Size = new System.Drawing.Size(66, 21);
this.balanceTxt.TabIndex = 5;
this.balanceTxt.Text = "Rs. 0 /-";
//
// createAccountBtn
//
this.createAccountBtn.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.createAccountBtn.Location = new System.Drawing.Point(16, 183);
this.createAccountBtn.Name = "createAccountBtn";
this.createAccountBtn.Size = new System.Drawing.Size(119, 38);
this.createAccountBtn.TabIndex = 6;
this.createAccountBtn.Text = "Create Account";
this.createAccountBtn.UseVisualStyleBackColor = true;
this.createAccountBtn.Click += new System.EventHandler(this.createAccount);
//
// depositBtn
//
this.depositBtn.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.depositBtn.Location = new System.Drawing.Point(218, 183);
this.depositBtn.Name = "depositBtn";
this.depositBtn.Size = new System.Drawing.Size(119, 38);
this.depositBtn.TabIndex = 7;
this.depositBtn.Text = "Deposit";
this.depositBtn.UseVisualStyleBackColor = true;
this.depositBtn.Click += new System.EventHandler(this.deposit);
//
// withdrawBtn
//
this.withdrawBtn.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.withdrawBtn.Location = new System.Drawing.Point(370, 183);
this.withdrawBtn.Name = "withdrawBtn";
this.withdrawBtn.Size = new System.Drawing.Size(119, 38);
this.withdrawBtn.TabIndex = 8;
this.withdrawBtn.Text = "Withdraw";
this.withdrawBtn.UseVisualStyleBackColor = true;
this.withdrawBtn.Click += new System.EventHandler(this.withdraw);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(575, 274);
this.Controls.Add(this.withdrawBtn);
this.Controls.Add(this.depositBtn);
this.Controls.Add(this.createAccountBtn);
this.Controls.Add(this.balanceTxt);
this.Controls.Add(this.amountTxt);
this.Controls.Add(this.holderNameTxt);
this.Controls.Add(this.balanceLbl);
this.Controls.Add(this.amountLbl);
this.Controls.Add(this.holderNameLbl);
this.Name = "Form1";
this.Text = "ABC Bank";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label holderNameLbl;
private System.Windows.Forms.Label amountLbl;
private System.Windows.Forms.Label balanceLbl;
private System.Windows.Forms.TextBox holderNameTxt;
private System.Windows.Forms.TextBox amountTxt;
private System.Windows.Forms.Label balanceTxt;
private System.Windows.Forms.Button createAccountBtn;
private System.Windows.Forms.Button depositBtn;
private System.Windows.Forms.Button withdrawBtn;
}
}

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

NegativeException.cs

Form1.cs

OUTPUT :

After clicking "Create Account",

After clicking "Deposit",

After withdrawing "250/-"

If any negative value is entered ,

Any doubts regarding this can be explained with pleasure :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions:...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions: 1. Fully parametrized Constructor (to initialize class variables) 2. Getter (getBalance()) and Setter (setBalance()) for the float balance, must be greater than zero. In Account.java Methods: checkBalance( ) to print CURRENT BALANCE. deposit(int added_amount) should add add_amount to balance withdraw(int withdraw_amount) should deduct withdraw_amount balance display( ) will print all the information (account number, name, balance). Class 2: AccountTest.java Create an object account1 and...
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file....
Create a client for your BankAccount classcalled BankAccountClient.java.    This a separate file from the BankAccount file. Both files must be in the same directory to compile. Display all dollar amounts using the DecimalFormat class. Create an account Ask the user for the type of account, the bank account number, the amount that they will deposit to start the account. Set interest earned to 0. Print the account information using an implicit or explicit call to toString Update an account Use...
Create Transaction form to manage account balance. Use GUI. Have file, edit and about menu bar....
Create Transaction form to manage account balance. Use GUI. Have file, edit and about menu bar. Add summary, transaction and exit items to file menu. create 3 radio buttons for deposit, check and service charge. have a calculate button and exit button. 1. Create transaction Options: Deposits add to balance Checks subtract from balance if balance > check then subtract check from balance else Display Insufficient Funds Message and Subtract Service Charge ($10) from balance Service Charge ($10) subtract from...