|
Practical Learning: Creating the Application
|
|
- Start Microsoft Visual C# Professional or Microsoft Visual C# 2005
Express Edition
- To create a new application, on the main menu, click File -> New
-> Project...
- In the Templates list, click Windows Application
- Set the name to WattsALoan2 and click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type LoanPreparation.cs and press Enter
- To create a new form, on the main menu, click Project -> Add
Windows Form...
- In the Templates list, make sure Windows Form is selected.
Set the Name to NewEmployee and click Add
- Design the form as follows:
 |
| Control |
Text |
Name |
| Label |
Employee #: |
|
| TextBox |
|
txtEmployeeNumber |
| Label |
Employee Name: |
|
| TextBox |
|
txtEmployeeName |
| Button |
Create |
btnCreate |
| Button |
Close |
btnClose |
|
- Double-click the Close button and change the file as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WattsALoan2b
{
public partial class NewEmployee : Form
{
public NewEmployee()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
|
- Return to the NewEmployee form and double-click its Create button
- Implement its Click event as follows:
private void btnCreate_Click(object sender, EventArgs e)
{
string strFilename = @"C:\Watts A Loan\Employees.wal";
FileInfo fiEmployees = new FileInfo(strFilename);
StreamWriter stwEmployees = null;
// Normally, we should have the file already but just in case...
if (!fiEmployees.Exists)
stwEmployees = fiEmployees.CreateText();
else // If the file exists already, then we will only add to it
stwEmployees = fiEmployees.AppendText();
try
{
stwEmployees.WriteLine(txtEmployeeNumber.Text);
stwEmployees.WriteLine(txtEmployeeName.Text);
}
finally
{
stwEmployees.Close();
}
txtEmployeeNumber.Text = "";
txtEmployeeName.Text = "";
txtEmployeeNumber.Focus();
}
|
- To be able to use the Visual Basic library, in the Solution
Explorer, right-click References and click Add Reference...
- In the .NET property page, click Microsoft.VisualBasic
- Click OK
- Display the LoanPreparation form
- In the Common Controls section of the Toolbox, click ToolTip and
click the form
- Design the form as follows:
 |
| Control |
Name |
Text |
ToolTip on toolTip1 |
| Label |
|
If this is a new loan, enter a new account number and the name of the customer who is requesting the loan |
|
| Label |
|
To open a previously prepared loan, enter its account number and press Tab |
|
| Label |
|
Acnt #: |
|
| Label |
|
Customer Name: |
|
| Label |
|
Customer: |
|
| TextBox |
txtAccountNumber |
|
Account number of the customer requesting
the loan |
| TextBox |
txtCustomerName |
|
Name of the customer requesting the loan |
| Label |
|
Empl #: |
|
| Label |
|
Employee Name: |
|
| Label |
|
Prepared By: |
|
| TextBox |
txtEmployeeNumber |
|
Employee number of the clerk preparing the
loan |
| TextBox |
txtEmployeeName |
|
Name of the clerk preparing the loan |
| Button |
btnNewEmployee |
|
Used to add a new employee to the company |
| Button |
btnNewCustomer |
|
Used to create an account for a new
customer |
| Label |
|
Loan Amount: |
|
| TextBox |
txtLoanAmount |
|
Amount of loan the customer is requesting |
| Label |
|
Interest Rate: |
|
| TextBox |
txtInterestRate |
|
Annual percentage rate of the loan |
| Label |
|
% |
|
| Label |
|
Periods |
|
| TextBox |
|
txtPeriods |
The number of months the loan is supposed
to last |
| Button |
btnCalculate |
Calculate |
Used to calculate the monthly payment |
| Label |
|
Monthly Payment: |
|
| TextBox |
txtMonthlyPayment |
|
The minimum amount the customer should pay
every month |
| Button |
btnClose |
Close |
Used to close the form |
|
- Double-click the Calculate button and implement its event as
follows:
private void btnCalculate_Click(object sender, EventArgs e)
{
double LoanAmount = 0.00D, MonthlyPayment = 0.00D;
double InterestRate = 0.00D, Periods = 0.00D;
try
{
LoanAmount = double.Parse(txtLoanAmount.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Loan Amount");
}
try
{
InterestRate = double.Parse(txtInterestRate.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Interest Rate");
}
try
{
Periods = double.Parse(txtPeriods.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Periods Value");
}
try
{
MonthlyPayment =
Microsoft.VisualBasic.Financial.Pmt(
InterestRate / 12 / 100,
Periods,
-LoanAmount,
0,
Microsoft.VisualBasic.DueDate.BegOfPeriod);
txtMonthlyPayment.Text = MonthlyPayment.ToString("F");
}
catch (FormatException)
{
MessageBox.Show("Invalid Periods Value");
}
}
|
- Return to the form and double-click the Close button to implement
its event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
|
- Scroll up completely and, under the other using lines, type using
System.IO;
- Access the LoanPresentation form
- Double-click the New button on the form and implement the event as follows:
private void btnNewEmployee_Click(object sender, EventArgs e)
{
NewEmployee frmNewEmployee = new NewEmployee();
frmNewEmployee.ShowDialog();
}
|
- Return to the form
- On the LoanPreparation form, double-click an unoccupied area of its body
- Implement its Load event as follows:
private void LoanPresentation_Load(object sender, EventArgs e)
{
string strDirectory = @"C:\Watts A Loan";
if (!Directory.Exists(strDirectory))
Directory.CreateDirectory(strDirectory);
string strFilename = strDirectory + @"\Employees.wal";
FileInfo fiEmployees = new FileInfo(strFilename);
// If the employees file was not created already,
// then create it
if (!fiEmployees.Exists)
{
StreamWriter stwEmployees = fiEmployees.CreateText();
// And create a John Doe employee
try
{
stwEmployees.WriteLine("00-000");
stwEmployees.WriteLine("John Doe");
}
finally
{
stwEmployees.Close();
}
}
}
|
- Return to the LoanPreparation form
- In the combo box on top of the Properties window, select
txtAccountNumber
- In the Events section, double-click Leave and implement the event as
follows:
private void txtAccountNumber_Leave(object sender, EventArgs e)
{
string strPath = @"C:\Watts A Loan";
DirectoryInfo diLoans = new DirectoryInfo(strPath);
FileInfo[] aryLoans = diLoans.GetFiles("*",
SearchOption.AllDirectories);
string strFilename = txtAccountNumber.Text + ".wal";
string strFullname = strPath + "none.wal";
bool found = false;
foreach (FileInfo fle in aryLoans)
{
if (fle.Name == strFilename)
{
found = true;
strFullname = fle.FullName;
}
}
if (found == true)
{
FileStream stmLoans =
File.Open(strFullname,
FileMode.Open,
FileAccess.Read);
BinaryReader bnrLoans = new BinaryReader(stmLoans);
txtAccountNumber.Text = bnrLoans.ReadString();
txtCustomerName.Text = bnrLoans.ReadString();
txtEmployeeNumber.Text = bnrLoans.ReadString();
txtEmployeeName.Text = bnrLoans.ReadString();
txtLoanAmount.Text = bnrLoans.ReadString();
txtInterestRate.Text = bnrLoans.ReadString();
txtPeriods.Text = bnrLoans.ReadString();
txtMonthlyPayment.Text = bnrLoans.ReadString();
bnrLoans.Close();
stmLoans.Close();
}
}
|
- In the combo box on top of the Properties window, select
txtEmployeeNumber
- On the Properties window, click the Events button and double-click
Leave
- Implement the event as follows:
private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
string strFilename = @"C:\Watts A Loan\Employees.wal";
FileInfo fiEmployees = new FileInfo(strFilename);
if (fiEmployees.Exists)
{
if (txtEmployeeNumber.Text == "")
{
txtEmployeeName.Text = "";
return;
}
else
{
StreamReader strEmployees = fiEmployees.OpenText();
string strEmployeeNumber, strEmployeeName;
bool found = false;
try
{
while( (strEmployeeNumber = strEmployees.ReadLine()) != null )
{
if (strEmployeeNumber == txtEmployeeNumber.Text)
{
strEmployeeName = strEmployees.ReadLine();
txtEmployeeName.Text = strEmployeeName;
found = true;
}
}
// When the application has finished checking the file
// if there was no employee with that number,
// let the user know
if (found == false)
{
MessageBox.Show("No employee with that number was found");
txtEmployeeName.Text = "";
txtEmployeeNumber.Focus();
}
}
finally
{
strEmployees.Close();
}
}
}
}
|
- Return to the form and double-click the Save button
- Implement the event as follows:
private void btnSave_Click(object sender, EventArgs e)
{
string strPath = @"C:\Watts A Loan\" + txtAccountNumber.Text + ".wal";
FileStream stmLoan = File.Create(strPath);
BinaryWriter bnwLoan = new BinaryWriter(stmLoan);
bnwLoan.Write(txtAccountNumber.Text);
bnwLoan.Write(txtCustomerName.Text);
bnwLoan.Write(txtEmployeeNumber.Text);
bnwLoan.Write(txtEmployeeName.Text);
bnwLoan.Write(txtLoanAmount.Text);
bnwLoan.Write(txtInterestRate.Text);
bnwLoan.Write(txtPeriods.Text);
bnwLoan.Write(txtMonthlyPayment.Text);
txtAccountNumber.Text = "";
txtCustomerName.Text = "";
txtEmployeeNumber.Text = "";
txtEmployeeName.Text = "";
txtLoanAmount.Text = "";
txtInterestRate.Text = "";
txtPeriods.Text = "";
txtMonthlyPayment.Text = "";
txtAccountNumber.Focus();
bnwLoan.Close();
stmLoan.Close();
}
|
- Execute the application to test it
- First create a few employees as follows:
| Employee # |
Employee Name |
| 42-806 |
Patricia Katts |
| 75-148 |
Helene Mukoko |
| 36-222 |
Frank Leandro |
| 42-808 |
Gertrude Monay |
- Process a few loans
- Close the application
|
|