A collection-based application starts from a
collection class. To illustrate the ability to iterate through a list, we
will create our own collection class. The class will have methods to add an
object to the list and to remove an object from the list. Our collection class
will also have an indexed property that can be used to locate an object and
change its values.
Practical
Learning: Enumerating a Collection
|
|
- To create a new class, in the Solution Explorer, right-click
LambdaSquareApartments1 -> Add -> Class...
- Set the Name to Collector and click Add
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LambdaSquareApartments1
{
[Serializable]
public class Collector<T>
{
T[] items;
int size;
public Collector()
{
size = 0;
items = new T[1];
}
public int Count
{
get
{
return size;
}
}
public T this[int index]
{
get
{
return items[index];
}
set
{
items[index] = value;
size++;
}
}
public static void IncreaseArray(ref T[] values, int increment)
{
T[] array = new T[values.Length + increment];
values.CopyTo(array, 0);
values = array;
}
public void Add(T str)
{
items[size] = str;
size++;
IncreaseArray(ref items, 1);
}
public int IndexOf(T item)
{
int index = -1;
if (size > 0)
{
for (int i = 0; i < Count; i++)
{
if (items[i].Equals(item))
{
index = i;
break;
}
}
return index;
}
else
return -1;
}
public void RemoveAt(int index)
{
if ((index >= 0) && (index < Count))
{
for (int i = index; i < Count - 1; i++)
items[i] = items[i + 1];
size--;
}
}
public void Remove(T item)
{
int index = IndexOf(item);
if (index >= 0)
{
RemoveAt(index);
}
}
public IEnumerator<T> GetEnumerator()
{
int counter = 0;
while (counter < Count)
{
yield return items[counter];
counter++;
}
}
}
}
- Save all
Our application is used to manage rent transactions
for an apartment building. An apartment has one or more bedrooms and
bathrooms. Since this is a rental building, the information about an
apartment must also include the amount to pay every month. A customer must
also pay a security deposit to acquire the apartment.
Practical
Learning: Creating Apartments Records
|
|
- Display the Apartments form and double-click the New Apartment
button
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class Apartments : Form
{
public Apartments()
{
InitializeComponent();
}
private void ShowApartments()
{
}
private void btnNewApartment_Click(object sender, EventArgs e)
{
Apartment apart = new Apartment();
NewApartment na = new NewApartment();
BinaryFormatter bfApartments = new BinaryFormatter();
Collector<Apartment> apartments = new Collector<Apartment>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.apt";
if (na.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(na.txtUnitCode.Text))
{
MessageBox.Show("You must specify the unit code.",
"Lambda Square Apartments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (File.Exists(strFileName))
{
using (FileStream fsApartments = new FileStream(strFileName, FileMode.Open))
{
apartments = (Collector<Apartment>)bfApartments.Deserialize(fsApartments);
}
}
apart.UnitCode = na.txtUnitCode.Text;
apart.ApartmentNumber = na.txtApartmentNumber.Text;
apart.Bedrooms = int.Parse(na.txtBedrooms.Text);
apart.Bathrooms = float.Parse(na.txtBathrooms.Text);
apart.MonthlyRate = double.Parse(na.txtMonthlyRate.Text);
apart.SecurityDeposit = double.Parse(na.txtSecurityDeposit.Text);
apart.OccupancyStatus = na.cbxOccupanciesStatus.Text;
apartments.Add(apart);
using (FileStream fsApartments = new FileStream(strFileName, FileMode.Create))
{
bfApartments.Serialize(fsApartments, apartments);
}
}
}
ShowApartments();
}
}
- Return to the Apartments form and double-click an unoccupied area of
the form
- Implement the event as follows:
private void ShowApartments()
{
BinaryFormatter bfApartments = new BinaryFormatter();
Collector<Apartment> apartments = new Collector<Apartment>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.apt";
if (File.Exists(strFileName))
{
lvwApartments.Items.Clear();
using (FileStream fsApartments = new FileStream(strFileName, FileMode.Open))
{
apartments = (Collector<Apartment>)bfApartments.Deserialize(fsApartments);
foreach (Apartment apart in apartments)
{
ListViewItem lviApartment = new ListViewItem(apart.UnitCode);
lviApartment.SubItems.Add(apart.ApartmentNumber);
lviApartment.SubItems.Add(apart.Bedrooms.ToString());
lviApartment.SubItems.Add(apart.Bathrooms.ToString());
lviApartment.SubItems.Add(apart.MonthlyRate.ToString());
lviApartment.SubItems.Add(apart.SecurityDeposit.ToString());
lviApartment.SubItems.Add(apart.OccupancyStatus);
lvwApartments.Items.Add(lviApartment);
}
}
}
}
private void Apartments_Load(object sender, EventArgs e)
{
ShowApartments();
}
- Return to the Apartments form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Save all
The employees are in charge of creating records (for
apartments and customers) and managing the transactions (such as rent
payments). For a simple application, we will keep information to a
minimum.
Practical
Learning: Creating Employees Records
|
|
- Display the Employees form and double-click an unoccupied area of
its body
- Return to the Employees form and double-click the New Employee
button
- Return to the Employees form and double-click the Close button
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
}
private void ShowEmployees()
{
BinaryFormatter bfEmployees = new BinaryFormatter();
Collector<Employee> employees = new Collector<Employee>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.apt";
if (File.Exists(strFileName))
{
lvwEmployees.Items.Clear();
using (FileStream fsEmployees = new FileStream(strFileName, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in employees)
{
ListViewItem lviEmployee = new ListViewItem(empl.EmployeeNumber);
lviEmployee.SubItems.Add(empl.FirstName);
lviEmployee.SubItems.Add(empl.LastName);
lviEmployee.SubItems.Add(empl.Title);
lvwEmployees.Items.Add(lviEmployee);
}
}
}
}
private void Employees_Load(object sender, EventArgs e)
{
ShowEmployees();
}
private void btnNewEmployee_Click(object sender, EventArgs e)
{
Employee empl = new Employee();
NewEmployee ne = new NewEmployee();
BinaryFormatter bfEmployees = new BinaryFormatter();
Collector<Employee> employees = new Collector<Employee>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.apt";
if (ne.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(ne.txtEmployeeNumber.Text))
{
MessageBox.Show("You must enter the employee number.",
"Lambda Square Apartments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (File.Exists(strFileName))
{
using (FileStream fsEmployees = new FileStream(strFileName, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
}
}
empl.EmployeeNumber = ne.txtEmployeeNumber.Text;
empl.FirstName = ne.txtFirstName.Text;
empl.LastName = ne.txtLastName.Text;
empl.Title = ne.txtTitle.Text;
employees.Add(empl);
using (FileStream fsEmployees = new FileStream(strFileName, FileMode.Create))
{
bfEmployees.Serialize(fsEmployees, employees);
}
}
ShowEmployees();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
- Save all
To rent an apartment, a customer, also called a
tenant, must fill an application to have an account created. The tenant
must specify the type of desired apartment. The management would check the
available apartments and allocate one.
Practical
Learning: Registering Tenants
|
|
- Display the New Tenant Registration form and double-click an
unoccupied area of its body
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class NewTenantRegistration : Form
{
public NewTenantRegistration()
{
InitializeComponent();
}
private void InitializeTenantRegistration()
{
int iRegistrationNumber = 1000;
BinaryFormatter bfTenantRegistrations = new BinaryFormatter();
Collector<TenantRegistration> registrations = new Collector<TenantRegistration>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.apt";
if (File.Exists(strFileName))
{
using (FileStream fsApartments = new FileStream(strFileName, FileMode.Open))
{
registrations = (Collector<TenantRegistration>)bfTenantRegistrations.Deserialize(fsApartments);
foreach (TenantRegistration tr in registrations)
{
iRegistrationNumber = tr.RegistrationNumber;
}
}
}
txtRegistrationNumber.Text = (iRegistrationNumber + 1).ToString();
}
private void NewTenantRegistration_Load(object sender, EventArgs e)
{
InitializeTenantRegistration();
}
}
}
- Return to the New Tenant Registration form and click the Employee #
text box
- 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)
{
BinaryFormatter bfEmployees = new BinaryFormatter();
Collector<Employee> employees = new Collector<Employee>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.apt";
if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
return;
if (File.Exists(strFileName))
{
using (FileStream fsEmployees = new FileStream(strFileName, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in employees)
{
if (txtEmployeeNumber.Text == empl.EmployeeNumber)
{
txtEmployeeName.Text = empl.FirstName + " " + empl.LastName;
break;
}
}
}
}
}
- Return to the New Tenant Registration form and click the Unit Code
text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtUnitCode_Leave(object sender, EventArgs e)
{
BinaryFormatter bfApartments = new BinaryFormatter();
Collector<Apartment> apartments = new Collector<Apartment>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.apt";
if (string.IsNullOrEmpty(txtUnitCode.Text))
return;
if (File.Exists(strFileName))
{
using (FileStream fsApartments = new FileStream(strFileName, FileMode.Open))
{
apartments = (Collector<Apartment>)bfApartments.Deserialize(fsApartments);
foreach(Apartment apart in apartments)
{
if (apart.UnitCode == txtUnitCode.Text)
{
txtApartmentDetails.Text = "Bedroom(s):\t" + apart.Bedrooms.ToString() + Environment.NewLine +
"Bathroom(s):\t" + apart.Bathrooms.ToString("F") + Environment.NewLine +
"Monthly Rent:\t" + apart.MonthlyRate.ToString() + "\\month\n" + Environment.NewLine +
"Security Deposit:\t " + apart.SecurityDeposit.ToString();
break;
}
}
}
}
}
- Return to the New Tenant Registration form and double-click the
Submit button
- Implement the event as follows:
private void btnSubmit_Click(object sender, EventArgs e)
{
TenantRegistration tr = new TenantRegistration();
BinaryFormatter bfTenantsRegistrations = new BinaryFormatter();
Collector<TenantRegistration> registrations = new Collector<TenantRegistration>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.trs";
if (string.IsNullOrEmpty(txtRegistrationNumber.Text))
{
MessageBox.Show("You must specify the registration number.",
"Lambda Square Apartments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (File.Exists(strFileName))
{
using (FileStream fsTenantsRegistrations = new FileStream(strFileName, FileMode.Open))
{
registrations = (Collector<TenantRegistration>)bfTenantsRegistrations.Deserialize(fsTenantsRegistrations);
}
}
tr.RegistrationNumber = int.Parse(txtRegistrationNumber.Text);
tr.RegistrationDate = dtpRegistrationDate.Value;
tr.EmployeeNumber = txtEmployeeNumber.Text;
tr.TenantCode = txtTenantCode.Text;
tr.FirstName = txtFirstName.Text;
tr.LastName = txtLastName.Text;
tr.MaritalStatus = cbxMaritalsStatus.Text;
tr.NumberOfChildren = ushort.Parse(txtNumberOfChildren.Text);
tr.PhoneNumber = txtPhoneNumber.Text;
tr.EmailAddress = txtEmailAddress.Text;
tr.UnitCode = txtUnitCode.Text;
tr.RentStartDate = dtpRentStartDate.Value;
registrations.Add(tr);
using (FileStream fsTenantsRegistrations = new FileStream(strFileName, FileMode.Create))
{
bfTenantsRegistrations.Serialize(fsTenantsRegistrations, registrations);
}
Close();
}
- Return to the New Tenant Registration form and click the Close
button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Display the Tenants Registrations form and double-click an
unoccupied area of its body
- Return to the Tenants Registrations form and double-click the New
Registration button
- Return to the Tenants Registrations form and double-click the Close
button
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class TenantsRegistrations : Form
{
public TenantsRegistrations()
{
InitializeComponent();
}
private void ShowRegistrations()
{
lvwRegistrations.Items.Clear();
string strEmployee = "", strApartment = "";
BinaryFormatter bfEmployees = new BinaryFormatter();
BinaryFormatter bfApartments = new BinaryFormatter();
Collector<Employee> employees = new Collector<Employee>();
Collector<Apartment> apartments = new Collector<Apartment>();
BinaryFormatter bfTenantsRegistrations = new BinaryFormatter();
Collector<TenantRegistration> registrations = new Collector<TenantRegistration>();
string strEmployeesFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.mpl";
string strApartmentsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.apt";
string strTenantsRegistrationsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.trs";
if (File.Exists(strTenantsRegistrationsFile))
{
using (FileStream fsTenantsRegistrations = new FileStream(strTenantsRegistrationsFile, FileMode.Open))
{
registrations = (Collector<TenantRegistration>)bfTenantsRegistrations.Deserialize(fsTenantsRegistrations);
foreach (TenantRegistration tr in registrations)
{
ListViewItem lviRegistration = new ListViewItem(tr.RegistrationNumber.ToString());
lviRegistration.SubItems.Add(tr.RegistrationDate.ToShortDateString());
using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in employees)
{
if (empl.EmployeeNumber == tr.EmployeeNumber)
{
strEmployee = empl.EmployeeNumber + ": " + empl.FirstName + " " + empl.LastName;
break;
}
}
}
lviRegistration.SubItems.Add(strEmployee);
lviRegistration.SubItems.Add(tr.TenantCode);
lviRegistration.SubItems.Add(tr.FirstName);
lviRegistration.SubItems.Add(tr.LastName);
lviRegistration.SubItems.Add(tr.MaritalStatus);
lviRegistration.SubItems.Add(tr.NumberOfChildren.ToString());
lviRegistration.SubItems.Add(tr.PhoneNumber);
lviRegistration.SubItems.Add(tr.EmailAddress);
using (FileStream fsApartments = new FileStream(strApartmentsFile, FileMode.Open))
{
apartments = (Collector<Apartment>)bfApartments.Deserialize(fsApartments);
foreach (Apartment apart in apartments)
{
if (apart.UnitCode == tr.UnitCode)
{
strApartment = tr.TenantCode + ": " + apart.Bedrooms.ToString() + " bedroom(s), " +
apart.Bathrooms.ToString() + " bathroom(s), " +
"Rent = " + apart.MonthlyRate.ToString() + "\\month, " +
"Deposit = " + apart.SecurityDeposit.ToString();
break;
}
}
}
lviRegistration.SubItems.Add(strApartment);
lviRegistration.SubItems.Add(tr.RentStartDate.ToShortDateString());
lvwRegistrations.Items.Add(lviRegistration);
}
}
}
}
private void TenantsRegistrations_Load(object sender, EventArgs e)
{
ShowRegistrations();
}
private void btnNewRegistration_Click(object sender, EventArgs e)
{
NewTenantRegistration ntr = new NewTenantRegistration();
ntr.ShowDialog();
ShowRegistrations();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
- Save all
There are various types of payments a tenant must
make:
- In the begining, when acquiring an apartment, a customer must pay a
security deposit
- Every month, usually at the end (of the month), a tenant must pay the
fixed monthly rate for occupying the apartment
- Some time to time, a customer may have to pay a fine. There are various
reasons. The most common reason is when rent is paid late
Practical
Learning: Making Payments
|
|
- Display the New Payment form and double-click an unoccupied area of
its body
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class NewPayment : Form
{
public NewPayment()
{
InitializeComponent();
}
private void InitializePayment()
{
int iReceiptNumber = 100000;
BinaryFormatter bfPayments = new BinaryFormatter();
Collector payments = new Collector();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Payments.pmt";
if (File.Exists(strFileName))
{
using (FileStream fsPayments = new FileStream(strFileName, FileMode.Open))
{
payments = (Collector)bfPayments.Deserialize(fsPayments);
foreach (Payment pmts in payments)
{
iReceiptNumber = pmts.ReceiptNumber;
}
}
}
txtReceiptNumber.Text = (iReceiptNumber + 1).ToString();
}
private void NewPayment_Load(object sender, EventArgs e)
{
InitializePayment();
}
}
}
- Return to the New Payment form and click the Employee # text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
BinaryFormatter bfEmployees = new BinaryFormatter();
Collector<Employee> employees = new Collector<Employee>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.mpl";
if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
return;
if (File.Exists(strFileName))
{
using (FileStream fsEmployees = new FileStream(strFileName, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in employees)
{
if (txtEmployeeNumber.Text == empl.EmployeeNumber)
{
txtEmployeeName.Text = empl.FirstName + " " + empl.LastName;
break;
}
}
}
}
}
- Return to the New Tenant Registration form and click the
Registration # text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtRegistrationNumber_Leave(object sender, EventArgs e)
{
BinaryFormatter bfApartments = new BinaryFormatter();
BinaryFormatter bfRegistrations = new BinaryFormatter();
Collector<Apartment> apartments = new Collector<Apartment>();
Collector<TenantRegistration> registrations = new Collector<TenantRegistration>();
string strApartmentsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.apt";
string strRegistrationsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.trs";
if (string.IsNullOrEmpty(txtRegistrationNumber.Text))
return;
if (File.Exists(strRegistrationsFile))
{
using (FileStream fsRegistrations = new FileStream(strRegistrationsFile, FileMode.Open))
{
registrations = (Collector<TenantRegistration>)bfRegistrations.Deserialize(fsRegistrations);
foreach (TenantRegistration regist in registrations)
{
if (regist.RegistrationNumber == int.Parse(txtRegistrationNumber.Text) )
{
txtDateRegistered.Text = regist.RegistrationDate.ToShortDateString();
txtRentStartDate.Text = regist.RentStartDate.ToShortDateString();
txtTenantInformation.Text = regist.TenantCode + ": " +
regist.FirstName + " " + regist.LastName + ", " +
regist.MaritalStatus + ", " +
regist.NumberOfChildren.ToString() + " child(ren)";
}
using (FileStream fsApartments = new FileStream(strApartmentsFile, FileMode.Open))
{
apartments = (Collector<Apartment>)bfApartments.Deserialize(fsApartments);
foreach (Apartment apart in apartments)
{
if( apart.UnitCode == regist.UnitCode )
{
txtApartmentInformation.Text = apart.UnitCode + ": Apart #: " +
apart.ApartmentNumber + ", " +
apart.Bedrooms.ToString() + " bedroom(s), " +
apart.Bathrooms.ToString() + " bathroom(s), " +
apart.MonthlyRate.ToString() + "/month, " +
" deposit = " + apart.SecurityDeposit.ToString();
}
}
}
}
}
}
}
- Return to the New Payment form and double-click the Submit button
- Implement the event as follows:
private void btnSubmit_Click(object sender, EventArgs e)
{
Payment pmt = new Payment();
BinaryFormatter bfPayments = new BinaryFormatter();
Collector<Payment> payments = new Collector<Payment>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Payments.pmt";
if (string.IsNullOrEmpty(txtReceiptNumber.Text))
{
MessageBox.Show("You must specify the receipt number.",
"Lambda Square Apartments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (File.Exists(strFileName))
{
using (FileStream fsPayments = new FileStream(strFileName, FileMode.Open))
{
payments = (Collector<Payment>)bfPayments.Deserialize(fsPayments);
}
}
pmt.ReceiptNumber = Convert.ToInt32(txtReceiptNumber.Text);
pmt.PaymentDate = dtpPaymentDate.Value;
pmt.EmployeeNumber = txtEmployeeNumber.Text;
pmt.RegistrationNumber = int.Parse(txtRegistrationNumber.Text);
pmt.PaymentAmount = Convert.ToDouble(txtPaymentAmount.Text);
pmt.Notes = txtNotes.Text;
payments.Add(pmt);
using (FileStream fsPayments = new FileStream(strFileName, FileMode.Create))
{
bfPayments.Serialize(fsPayments, payments);
}
Close();
}
- Return to the New Payment form and click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Display the Payments form and double-click an unoccupied area of its
body
- Return to the Payments form and double-click the New Payment button
- Return to the Payments form and double-click the Close button
- Change the document as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LambdaSquareApartments1
{
public partial class Payments : Form
{
public Payments()
{
InitializeComponent();
}
private void ShowPayments()
{
string strEmployee = "", strRegistration = "";
BinaryFormatter bfPayments = new BinaryFormatter();
BinaryFormatter bfEmployees = new BinaryFormatter();
Collector<Payment> payments = new Collector<Payment>();
Collector<Employee> employees = new Collector<Employee>();
BinaryFormatter bfTenantsRegistrations = new BinaryFormatter();
Collector<TenantRegistration> registrations = new Collector<TenantRegistration>();
string strPaymentsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Payments.pmt";
string strEmployeesFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.mpl";
string strTenantsRegistrationsFile = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.trs";
if (File.Exists(strPaymentsFile))
{
lvwPayments.Items.Clear();
using (FileStream fsPayments = new FileStream(strPaymentsFile, FileMode.Open))
{
payments = (Collector<Payment>)bfPayments.Deserialize(fsPayments);
foreach (Payment pmt in payments)
{
ListViewItem lviPayment = new ListViewItem(pmt.ReceiptNumber.ToString());
lviPayment.SubItems.Add(pmt.PaymentDate.ToShortDateString());
using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open))
{
employees = (Collector<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in employees)
{
if (empl.EmployeeNumber == pmt.EmployeeNumber)
{
strEmployee = empl.EmployeeNumber + ": " + empl.FirstName + " " + empl.LastName;
break;
}
}
}
lviPayment.SubItems.Add(strEmployee);
using (FileStream fsTenantsRegistrations = new FileStream(strTenantsRegistrationsFile, FileMode.Open))
{
registrations = (Collector<TenantRegistration>)bfTenantsRegistrations.Deserialize(fsTenantsRegistrations);
foreach (TenantRegistration tr in registrations)
{
if (tr.RegistrationNumber == pmt.RegistrationNumber)
{
strRegistration = "Regist #: " + tr.RegistrationNumber.ToString() +
" for " + tr.FirstName + " " + tr.LastName + ", " +
tr.MaritalStatus + ", " + tr.NumberOfChildren + " child(ren)";
}
}
}
lviPayment.SubItems.Add(strRegistration);
lviPayment.SubItems.Add(pmt.PaymentAmount.ToString("F"));
lviPayment.SubItems.Add(pmt.Notes);
lvwPayments.Items.Add(lviPayment);
}
}
}
}
private void Payments_Load(object sender, EventArgs e)
{
ShowPayments();
}
private void btnNewPayment_Click(object sender, EventArgs e)
{
NewPayment np = new NewPayment();
np.ShowDialog();
ShowPayments();
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
}
- Execute the application
- Create the aparments as follows:
Unit Code |
Aprt # |
Bedrooms |
Bathrooms |
Monthly Rate |
Security Deposit |
Occupancy Status |
399475 |
101 |
2 |
2 |
1150 |
650 |
Available |
508293 |
102 |
1 |
1 |
950 |
500 |
Needs Repair |
729397 |
103 |
1 |
1 |
925 |
500 |
Available |
928364 |
104 |
3 |
2 |
1350 |
850 |
Available |
297297 |
105 |
2 |
1 |
1150 |
550 |
Available |
492739 |
106 |
3 |
2 |
1350 |
850 |
Available |
692797 |
107 |
3 |
2 |
1285 |
850 |
Not Ready |
829475 |
108 |
1 |
1 |
885 |
500 |
Available |
139749 |
109 |
2 |
2 |
1150 |
650 |
Available |
369294 |
110 |
1 |
1 |
895 |
500 |
Available |
502084 |
111 |
2 |
2 |
1145 |
650 |
Available |
829397 |
112 |
2 |
1 |
1085 |
600 |
Available |
292739 |
201 |
2 |
1 |
1185 |
650 |
Available |
496055 |
202 |
1 |
1 |
895 |
500 |
Available |
939595 |
203 |
1 |
1 |
925 |
500 |
Available |
384068 |
204 |
3 |
2 |
1250 |
850 |
Available |
824850 |
205 |
2 |
1 |
1100 |
600 |
Available |
620485 |
206 |
3 |
2 |
1300 |
850 |
Available |
294940 |
207 |
3 |
2 |
1350 |
850 |
Available |
602048 |
208 |
1 |
1 |
920 |
500 |
Available |
829479 |
209 |
2 |
2 |
1150 |
650 |
Available |
280484 |
210 |
1 |
1 |
895 |
500 |
Available |
602408 |
211 |
2 |
2 |
1175 |
650 |
Available |
384086 |
212 |
2 |
1 |
1075 |
600 |
Available |
397493 |
301 |
2 |
2 |
1175 |
650 |
Available |
625941 |
302 |
1 |
1 |
950 |
500 |
Available |
404950 |
303 |
1 |
1 |
925 |
500 |
Available |
304806 |
304 |
3 |
2 |
1250 |
850 |
Available |
844850 |
305 |
2 |
1 |
1100 |
600 |
Needs Repair |
596305 |
306 |
3 |
2 |
1300 |
850 |
Available |
138408 |
307 |
3 |
2 |
1350 |
850 |
Available |
305860 |
308 |
1 |
1 |
920 |
500 |
Available |
847584 |
309 |
2 |
2 |
1150 |
650 |
Available |
746959 |
310 |
1 |
1 |
935 |
500 |
Available |
359405 |
311 |
2 |
2 |
1175 |
650 |
Available |
308505 |
312 |
2 |
1 |
1075 |
600 |
Available |
- Create employees records as follows:
Employee # |
First Name |
Last Name |
Title |
93947 |
Leonard |
Goulet |
Owner - General Manager |
40685 |
Justine |
Sandt |
Rent Manager |
73048 |
Raymond |
Wilkinson |
|
60949 |
Mark |
Reason |
Maintenance Technician |
38408 |
Marc |
Knights |
Rent Associate |
20448 |
Nancy |
Longhorn |
Rent Associate |
- Create the registrations records as follows:
Regist # |
Registration Date |
Processed By |
Tenant Code |
First Name |
Last Name |
Marital Status |
Children |
Phone Number |
Email Address |
Tenancy Status |
Unit Code |
Rent Start Date |
1001 |
6/12/2014 |
38408 |
29-485-05 |
Ann |
Sanders |
Married |
1 |
(240) 524 -2831 |
annsanders@emailcity.com |
Active |
139749 |
7/1/2014 |
1002 |
6/16/2014 |
20448 |
83-400-85 |
Mahty |
Shaoul |
Married |
2 |
202-729-1574 |
mshaoulman@gmail.com |
Active |
928364 |
9/1/2014 |
1003 |
6/24/2014 |
40685 |
48-602-73 |
Frank |
Ulm |
Single |
0 |
(301) 882-0704 |
fulm112244@yaho.com |
Active |
729397 |
7/1/2014 |
1004 |
6/24/2014 |
93947 |
24-385-30 |
Elise |
Provoski |
Separated |
1 |
(443) 974-9631 |
eprevalence@yahoo.com |
Active |
844850 |
8/1/2014 |
1005 |
7/23/2014 |
93947 |
92-048-11 |
Grace |
Curryan |
Married |
1 |
(240) 927-0993 |
gcarrier@gmail.com |
Active |
297297 |
9/1/2014 |
1006 |
7/25/2014 |
38408 |
51-304-58 |
Tracy |
Warrens |
Divorced |
2 |
202-793-6924 |
twarrior12@hotmail.coom |
Active |
492739 |
8/1/2014 |
1007 |
8/1/2014 |
38408 |
72-384-04 |
Paul |
Yamo |
Married |
3 |
(410-792-7045 |
pyamo@hr.umd.edu |
Active |
384068 |
10/1/2014 |
1008 |
8/10/2014 |
40685 |
62-405-29 |
Nancy |
Shermann |
Single |
1 |
(703) 338-2973 |
nsherre@emailcity.com |
Active |
829475 |
9/1/2014 |
1009 |
9/12/2014 |
20448 |
72-484-04 |
Michael |
Tiernan |
Single |
0 |
301-274-9285 |
resdev.globelan.net |
Active |
829479 |
11/1/2014 |
1010 |
10/5/2014 |
38408 |
60-285-83 |
Phillippe |
Anderson |
Single |
0 |
202-729-1574 |
philanders@gmail.com |
Active |
496055 |
11/1/2014 |
- Create payments records as follows:
Receipt # |
Payment Date |
Empl # |
Regist # |
Amount |
Notes |
100001 |
6/12/2014 |
38408 |
1001 |
650 |
This is the payment for the security deposit. |
100002 |
6/20/2014 |
40685 |
1003 |
500 |
Security Deposit |
100003 |
7/27/2014 |
38408 |
1003 |
925 |
|
100004 |
7/28/2014 |
38408 |
1001 |
1150 |
|
100005 |
8/1/2014 |
40685 |
1006 |
850 |
Security Deposit |
100006 |
8/8/2014 |
40685 |
1007 |
850 |
Security Deposit |
100007 |
8/8/2014 |
40685 |
1008 |
500 |
Security Deposit |
100008 |
8/13/2014 |
40685 |
1004 |
600 |
Security Deposit |
100009 |
8/14/2014 |
20448 |
1002 |
850 |
Payment for security deposit |
100010 |
8/25/2014 |
38408 |
1001 |
1150 |
|
100011 |
8/25/2014 |
38408 |
1002 |
1350 |
|
100012 |
8/26/2014 |
20448 |
1003 |
925 |
|
100013 |
8/27/2014 |
40685 |
1004 |
1100 |
|
100014 |
8/30/2014 |
38408 |
1006 |
1350 |
|
100015 |
9/17/2014 |
40685 |
1009 |
650 |
Security Deposit |
100016 |
9/18/2014 |
20448 |
1005 |
550 |
Security Deposit |
100017 |
9/25/2014 |
20448 |
1004 |
1100 |
|
100018 |
9/25/2014 |
20448 |
1006 |
1350 |
|
100019 |
9/25/2014 |
20448 |
1008 |
885 |
|
100020 |
9/28/2014 |
20448 |
1001 |
1150 |
|
100021 |
9/28/2014 |
40685 |
1002 |
1350 |
|
100022 |
9/28/2014 |
40685 |
1005 |
1150 |
|
100023 |
10/5/2014 |
38408 |
1003 |
925 |
|
100024 |
10/8/2014 |
40685 |
1010 |
500 |
Security Deposit |
100025 |
10/24/2014 |
38408 |
1004 |
1100 |
|
100026 |
10/24/2014 |
38408 |
1005 |
1150 |
|
100027 |
10/25/2014 |
40685 |
1006 |
1350 |
|
100028 |
10/25/2014 |
40685 |
1007 |
1250 |
|
100029 |
10/27/2014 |
93947 |
1001 |
1150 |
|
100030 |
10/29/2014 |
93947 |
1008 |
885 |
|
100031 |
10/30/2014 |
38408 |
1002 |
1350 |
|
100032 |
10/31/2014 |
40685 |
1003 |
925 |
|
100033 |
11/26/2014 |
38408 |
1002 |
1350 |
|
100034 |
11/26/2014 |
38408 |
1008 |
885 |
|
100035 |
11/27/2014 |
38408 |
1006 |
1350 |
|
100036 |
11/28/2014 |
20448 |
1004 |
1100 |
|
100037 |
11/28/2014 |
38408 |
1005 |
1150 |
|
100038 |
11/28/2014 |
38408 |
1007 |
1250 |
|
100039 |
11/29/2014 |
93947 |
1001 |
1150 |
|
100040 |
11/30/2014 |
38408 |
1003 |
925 |
|
100041 |
11/30/2014 |
20448 |
1009 |
1150 |
|
100042 |
11/30/2014 |
20448 |
1010 |
895 |
|
100043 |
12/25/2014 |
38408 |
1006 |
1350 |
|
100044 |
12/25/2014 |
38408 |
1007 |
1250 |
|
100045 |
12/27/2014 |
20448 |
1009 |
1150 |
|
100046 |
12/28/2014 |
20448 |
1001 |
1150 |
|
100047 |
12/28/2014 |
38408 |
1004 |
1100 |
|
100048 |
12/28/2014 |
38408 |
1005 |
1150 |
|
100049 |
12/28/2014 |
38408 |
1010 |
895 |
|
100050 |
12/30/2014 |
20448 |
1003 |
925 |
|
100051 |
12/31/2014 |
38408 |
1002 |
1350 |
|
100052 |
12/31/2014 |
20448 |
1008 |
885 |
|
100053 |
1/23/2015 |
20448 |
1005 |
1150 |
|
100054 |
1/26/2015 |
38408 |
1001 |
1150 |
|
100055 |
1/28/2015 |
93947 |
1003 |
925 |
|
100056 |
1/29/2015 |
93947 |
1002 |
1350 |
|
100057 |
2/10/2015 |
20448 |
1004 |
100 |
This is a fee for late payment. |
100058 |
2/10/2015 |
20448 |
1004 |
1100 |
|
100059 |
2/20/2015 |
20448 |
1001 |
1150 |
|
100060 |
2/25/2015 |
20448 |
1005 |
1150 |
|
100061 |
2/26/2015 |
38408 |
1002 |
1350 |
|
100062 |
3/1/2015 |
38408 |
1004 |
1100 |
|
100063 |
3/3/2015 |
40685 |
1003 |
925 |
|
- Close the forms and return to your programming environment
Application
|
|