Starting a Collection Class
|
|
The Collection<> class starts as
follows:
public class Collection<T> : IList<T>,
ICollection<T>,
IEnumerable<T>,
IList, ICollection,
IEnumerable
The Collection<> class is used to
create a custom collection class that impliments some needed behaviors. At a
minimum, you can simply derive your class from it. If you do, the
Collection<> class provides some primary functionality such as the ability
to add an object to the collection. It also provides the ability to get the
number of objects in the collection, to check whether the collection
contains a certain object, or to delete an object.
Topic
Applied: Using a Collection Class
|
|
- To create a new form, in the Solution Explorer, right-click CeilInn1
-> Add -> Windows Form...
- Set the name to Customers
- Click Add
- In the Toolbox, click ListView and click the form
- Right-click it and click Edit Columns
- Create the columns as follows:
(Name) |
Text |
Width |
colAccountNumber |
Account # |
70 |
colFirstName |
First Name |
65 |
colLastName |
Last Name |
65 |
colPhoneNumber |
Phone # |
80 |
colEmergencyName |
Emergency Name |
100 |
colEmergencyPhone |
Emergency Phone |
100 |
- Click OK
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
ListView |
|
lvwCustomers |
|
FullRowSelect: True GridLines: True View:
Details |
Button |
|
btnNewCustomer |
New Customer... |
|
Button |
|
btnClose |
Close |
|
|
- Double-click an unoccupied area of the form and make the following
changes:
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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class Customers : Form
{
public Customers()
{
InitializeComponent();
}
private void ShowCustomers()
{
BinaryFormatter bfCustomers = new BinaryFormatter();
Collection<Customer> lstCustomers = new Collection<Customer>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
// Make sure the file exists
if (File.Exists(strFileName) == true)
{
// if so, create a file stream
using( FileStream fsCustomers = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
// If some customers records were created already,
// get them and store them in the collection
lstCustomers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
// First, empty the list view
lvwCustomers.Items.Clear();
// Visit each customer in the collection and add it to the list view
foreach (Customer client in lstCustomers)
{
ListViewItem lviCustomer = new ListViewItem(client.AccountNumber);
lviCustomer.SubItems.Add(client.FirstName);
lviCustomer.SubItems.Add(client.LastName);
lviCustomer.SubItems.Add(client.PhoneNumber);
lviCustomer.SubItems.Add(client.EmergencyName);
lviCustomer.SubItems.Add(client.EmergencyPhone);
lvwCustomers.Items.Add(lviCustomer);
}
}
}
}
private void Customers_Load(object sender, EventArgs e)
{
ShowCustomers();
}
}
}
- Return to the Customers form and double-click the New Customer
button
- Implement its event as follows:
private void btnNewCustomer_Click(object sender, EventArgs e)
{
CustomerEditor editor = new CustomerEditor();
BinaryFormatter bfCustomers = new BinaryFormatter();
Collection<Customer> lstCustomers = new Collection<Customer>();
// Get a reference to the file that holds the customers records
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
// First check if the file was previously created
if (File.Exists(strFileName) == true)
{
// If the list of customers exists already,
// get it and store it in a file stream
using (FileStream fsCustomers = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
// Store the list of customers in the collection
lstCustomers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
} // Close the file stream
}
if (editor.ShowDialog() == DialogResult.OK)
{
Customer client = new Customer();
client.AccountNumber = editor.txtAccountNumber.Text;
client.FirstName = editor.txtFirstName.Text;
client.LastName = editor.txtLastName.Text;
client.PhoneNumber = editor.txtPhoneNumber.Text;
client.EmergencyName = editor.txtEmergencyName.Text;
client.EmergencyPhone = editor.txtEmergencyPhone.Text;
// Add the customer in the collection
lstCustomers.Add(client);
// Create a file stream to hold the list of customers
using (FileStream fsCustomers = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
// Serialize the list of customers
bfCustomers.Serialize(fsCustomers, lstCustomers);
}
// Show the list of properties
ShowCustomers();
}
}
- Return to the Customers form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To create a new form, on the main menu, click PROJECT -> Add Windows
Form...
- Set the name to Employees
- Click Add
- Add a list view to the form and create the columns as follows:
(Name) |
Text |
Width |
colEmployeeNumber |
Employee # |
70 |
colFirstName |
First Name |
80 |
colLastName |
Last Name |
80 |
colTitle |
Title |
120 |
- Click OK
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
ListView |
|
lvwEmployees |
|
FullRowSelect: True GridLines: True View:
Details |
Button |
|
btnNewEmployee |
New Employee... |
|
Button |
|
btnClose |
Close |
|
|
- Double-click an unoccupied area of the form and make the following
changes:
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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
}
private void ShowEmployees()
{
Collection<Employee> lstEmployees;
BinaryFormatter bfEmployees = new BinaryFormatter();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
if (File.Exists(strFileName) == true)
{
using (FileStream fsEmployees = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstEmployees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
lvwEmployees.Items.Clear();
foreach (Employee clerk in lstEmployees)
{
ListViewItem lviEmployee = new ListViewItem(clerk.EmployeeNumber);
lviEmployee.SubItems.Add(clerk.FirstName);
lviEmployee.SubItems.Add(clerk.LastName);
lviEmployee.SubItems.Add(clerk.Title);
lvwCustomers.Items.Add(lviEmployee);
}
}
}
}
private void Employees_Load(object sender, EventArgs e)
{
ShowEmployees();
}
}
}
- Return to the Employees form and double-click the New Employee
button
- Implement its event as follows:
private void btnNewEmployee_Click(object sender, EventArgs e)
{
Collection<Employee> lstEmployees = new Collection<Employee>();
EmployeeEditor editor = new EmployeeEditor();
BinaryFormatter bfEmployees = new BinaryFormatter();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
if (File.Exists(strFileName) == true)
{
using (FileStream fsEmployees = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstEmployees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
}
}
if (editor.ShowDialog() == DialogResult.OK)
{
Employee clerk = new Employee();
clerk.EmployeeNumber = editor.txtEmployeeNumber.Text;
clerk.FirstName = editor.txtFirstName.Text;
clerk.LastName = editor.txtLastName.Text;
clerk.Title = editor.txtTitle.Text;
lstEmployees.Add(clerk);
using (FileStream fsEmployees = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
bfEmployees.Serialize(fsEmployees, lstEmployees);
}
ShowEmployees();
}
}
- Return to the Employees form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To create a new form, on the main menu, click PROJECT -> Add Windows
Form...
- Set the name to Rooms
- Click Add
- Add a list view to the form and create the columns as follows:
(Name) |
Text |
TextAlign |
Width |
colRoomNumber |
Room # |
|
|
colRoomType |
Room Type |
|
100 |
colBedType |
Bed Type |
|
80 |
colRate |
Rate |
Right |
|
colAvailable |
Available? |
Center |
65 |
- Click OK
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
ListView |
|
lvwRooms |
|
FullRowSelect: True GridLines: True View:
Details |
Button |
|
btnNewRoom |
New Room... |
|
Button |
|
btnClose |
Close |
|
|
- Double-click an unoccupied area of the form and make the following
changes:
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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class Rooms : Form
{
public Rooms()
{
InitializeComponent();
}
private void ShowRooms()
{
BinaryFormatter bfRooms = new BinaryFormatter();
Collection<Room> lstRooms = new Collection<Room>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Rooms.rms";
if (File.Exists(strFileName) == true)
{
using (FileStream fsRooms = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstRooms = (Collection<Room>)bfRooms.Deserialize(fsRooms);
lvwRooms.Items.Clear();
foreach (Room rm in lstRooms)
{
ListViewItem lviRoom = new ListViewItem(rm.RoomNumber);
lviRoom.SubItems.Add(rm.RoomType);
lviRoom.SubItems.Add(rm.BedType);
lviRoom.SubItems.Add(rm.Rate.ToString("F"));
lviRoom.SubItems.Add(rm.OccupancyStatus);
lvwRooms.Items.Add(lviRoom);
}
}
}
}
private void Rooms_Load(object sender, EventArgs e)
{
ShowRooms();
}
}
}
- Return to the Rooms form and double-click the New Room button
- Implement its event as follows:
private void btnNewRoom_Click(object sender, EventArgs e)
{
RoomEditor editor = new RoomEditor();
BinaryFormatter bfRooms = new BinaryFormatter();
Collection<Room> lstRooms = new Collection<Room>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Rooms.rms";
if (File.Exists(strFileName) == true)
{
using (FileStream fsRooms = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
// Store the list of customers in the collection
lstRooms = (Collection<Room>)bfRooms.Deserialize(fsRooms);
} // Close the file stream
}
if (editor.ShowDialog() == DialogResult.OK)
{
Room rm = new Room();
rm.RoomNumber = editor.txtRoomNumber.Text;
rm.RoomType = editor.cbxRoomTypes.Text;
rm.BedType = editor.cbxBedTypes.Text;
rm.Rate = double.Parse(editor.txtRate.Text);
rm.OccupancyStatus = editor.cbxOccupanciesStatus.Text;
lstRooms.Add(rm);
using (FileStream fsCustomers = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
// Serialize the list of properties
bfRooms.Serialize(fsCustomers, lstRooms);
}
// Show the list of properties
ShowRooms();
}
}
- Return to the Room form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Display the Occupany Editor dialog box 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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class OccupancyEditor : Form
{
public OccupancyEditor()
{
InitializeComponent();
}
private void OccupancyEditor_Load(object sender, EventArgs e)
{
int iOccupancyNumber = 100000;
BinaryFormatter bfOccupancies = new BinaryFormatter();
Collection<Occupancy> lstOccupancies = new Collection<Occupancy>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Occupancies.ocp";
if (File.Exists(strFileName) == true)
{
using (FileStream fsOccupancies = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstOccupancies = (Collection<Occupancy>)bfOccupancies.Deserialize(fsOccupancies);
foreach (Occupancy order in lstOccupancies)
{
iOccupancyNumber = order.OccupancyNumber;
}
}
}
txtOccupancyNumber.Text = (iOccupancyNumber + 1).ToString();
}
}
}
- Return to the Occupancy Editor and click the Employee # text box
- On the Properties window, click the Events button and double-click
Leave
- Change the document as follows:
private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
Collection<Employee> lstEmployees;
BinaryFormatter bfEmployees = new BinaryFormatter();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
if (File.Exists(strFileName) == true)
{
using (FileStream fsEmployees = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstEmployees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in lstEmployees)
{
if (empl.EmployeeNumber == txtEmployeeNumber.Text)
txtEmployeeName.Text = empl.LastName + ", " + empl.FirstName;
}
}
}
}
- Return to the Occupancy Editor dialog box and click the Customer #
text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtAccountNumber_Leave(object sender, EventArgs e)
{
BinaryFormatter bfCustomers = new BinaryFormatter();
Collection<Customer> lstCustomers = new Collection<Customer>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
if (File.Exists(strFileName) == true)
{
using (FileStream fsCustomers = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstCustomers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
foreach (Customer client in lstCustomers)
{
if( client.AccountNumber == txtAccountNumber.Text )
txtCustomerName.Text = client.LastName + ", " + client.FirstName;
}
}
}
}
- Return to the Occupancy Editor dialog box and click the Room Number
text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtRoomNumber_Leave(object sender, EventArgs e)
{
BinaryFormatter bfRooms = new BinaryFormatter();
Collection<Room> lstRooms = new Collection<Room>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Rooms.rms";
if (File.Exists(strFileName) == true)
{
using (FileStream fsRooms = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstRooms = (Collection<Room>)bfRooms.Deserialize(fsRooms);
foreach (Room rm in lstRooms)
{
if (rm.RoomNumber == txtRoomNumber.Text)
txtRoomDescription.Text = "Room type: " + rm.RoomType +
", Bed type: " + rm.BedType +
", Rate = " + rm.Rate.ToString("F") + "/night";
}
}
}
}
- To create a new form, on the main menu, click PROJECT -> Add Windows
Form...
- Set the name to Occupancies
- Click Add
- Add a list viiew to the form and create the columns as follows:
(Name) |
Text |
TextAlign |
Width |
colOccupancyNumber |
Occupancy # |
|
80 |
colDateOccupied |
Date Occupied |
|
150 |
colProcessedBy |
Processed By |
|
140 |
colProcessedFor |
Processed For |
|
140 |
colRoomOccupied |
Room Occupied |
|
180 |
colRateApplied |
Rate Applied |
Right |
80 |
colPhoneUse |
Phone Use |
Right |
65 |
- Click OK
- Design the form as follows:
- Double-click an unoccupied area of the form and make the following
changes:
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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class Occupancies : Form
{
public Occupancies()
{
InitializeComponent();
}
private void ShowOccupancies()
{
Collection<Employee> lstEmployees;
BinaryFormatter bfRooms = new BinaryFormatter();
Collection<Room> lstRooms = new Collection<Room>();
BinaryFormatter bfEmployees = new BinaryFormatter();
BinaryFormatter bfCustomers = new BinaryFormatter();
BinaryFormatter bfOccupancies = new BinaryFormatter();
string strEmployee = "", strCustomer = "", strRoom = "";
Collection<Customer> lstCustomers = new Collection<Customer>();
Collection<Occupancy> lstOccupancies = new Collection<Occupancy>();
string strRoomsFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Rooms.rms";
string strCustomersFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
string strEmployeesFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
string strOccupanciesFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Occupancies.ocp";
if (File.Exists(strOccupanciesFile) == true)
{
using (FileStream fsOccupancies = new FileStream(strOccupanciesFile,
FileMode.Open,
FileAccess.Read))
{
lstOccupancies = (Collection<Occupancy>)bfOccupancies.Deserialize(fsOccupancies);
lvwOccupancies.Items.Clear();
foreach (Occupancy order in lstOccupancies)
{
ListViewItem lviOccupancy = new ListViewItem(order.OccupancyNumber.ToString());
lviOccupancy.SubItems.Add(order.DateOccupied.ToLongDateString());
using (FileStream fsEmployees = new FileStream(strEmployeesFile,
FileMode.Open,
FileAccess.Read))
{
lstEmployees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee clerk in lstEmployees)
{
if(clerk.EmployeeNumber == order.ProcessedBy)
strEmployee = clerk.EmployeeNumber + ": " +
clerk.FirstName + " " +
clerk.LastName;
}
}
lviOccupancy.SubItems.Add(strEmployee);
using (FileStream fsCustomers = new FileStream(strCustomersFile,
FileMode.Open,
FileAccess.Read))
{
lstCustomers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
foreach (Customer client in lstCustomers)
{
if( client.AccountNumber == order.ProcessedFor )
strCustomer = client.AccountNumber + ": " +
client.FirstName + " " +
client.LastName;
}
}
lviOccupancy.SubItems.Add(strCustomer);
if (File.Exists(strRoomsFile) == true)
{
using (FileStream fsRooms = new FileStream(strRoomsFile,
FileMode.Open,
FileAccess.Read))
{
lstRooms = (Collection<Room>)bfRooms.Deserialize(fsRooms);
foreach (Room rm in lstRooms)
if(rm.RoomNumber == order.RoomOccupied )
strRoom = rm.RoomNumber + ": " +
rm.RoomType + ", " +
rm.BedType + ", " +
rm.Rate.ToString("F") + "/day";
}
}
lviOccupancy.SubItems.Add(strRoom);
lviOccupancy.SubItems.Add(order.RateApplied.ToString("F"));
lviOccupancy.SubItems.Add(order.PhoneUse.ToString("F"));
lvwOccupancies.Items.Add(lviOccupancy);
}
}
}
}
private void Occupancies_Load(object sender, EventArgs e)
{
ShowOccupancies();
}
}
}
- Return to the Occupancies form and double-click the New Occupancy
button
- Implement its event as follows:
private void btnNewOccupancy_Click(object sender, EventArgs e)
{
OccupancyEditor editor = new OccupancyEditor();
BinaryFormatter bfmOccupancies = new BinaryFormatter();
Collection<Occupancy> lstOccupancies = new Collection<Occupancy>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Occupancies.ocp";
if (File.Exists(strFileName) == true)
{
using (FileStream fsOccupancies = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstOccupancies = (Collection<Occupancy>)bfmOccupancies.Deserialize(fsOccupancies);
}
}
if (editor.ShowDialog() == DialogResult.OK)
{
Occupancy occupy = new Occupancy();
occupy.OccupancyNumber = editor.txtOccupancyNumber.Text;
occupy.DateOccupied = editor.dtpDateOccupied.Value;
occupy.ProcessedBy = editor.txtEmployeeNumber.Text;
occupy.ProcessedFor = editor.txtAccountNumber.Text;
occupy.RoomOccupied = editor.cbxRoomsNumbers.Text;
occupy.RateApplied = double.Parse(editor.txtRateApplied.Text);
occupy.PhoneUse = double.Parse(editor.txtPhoneUse.Text);
lstOccupancies.Add(occupy);
using (FileStream fsOccupancies = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
bfmOccupancies.Serialize(fsOccupancies, lstOccupancies);
}
}
ShowOccupancies();
}
- Return to the form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Display the Payment Editor dialog box 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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class PaymentEditor : Form
{
public PaymentEditor()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
// If there is no payment amount, don't do anything
if (string.IsNullOrEmpty(txtAmountCharged.Text))
return;
txtSubTotal.Text = (Convert.ToInt16(txtTotalNights.Text) * Convert.ToDouble(txtAmountCharged.Text)).ToString("F");
txtTaxAmount.Text = (Convert.ToDouble(txtSubTotal.Text) * Convert.ToDouble(txtTaxRate.Text) / 100).ToString("F");
txtAmountPaid.Text = (Convert.ToDouble(txtSubTotal.Text) + Convert.ToDouble(txtTaxAmount.Text)).ToString("F");
}
private void PaymentEditor_Load(object sender, EventArgs e)
{
int iReceiptNumber = 1000;
BinaryFormatter bfPayments = new BinaryFormatter();
Collection<Payment> lstPaymentes = new Collection<Payment>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Payments.pmt";
if (File.Exists(strFileName) == true)
{
using (FileStream fsPayments = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstPaymentes = (Collection<Payment>)bfPayments.Deserialize(fsPayments);
foreach (Payment pmt in lstPaymentes)
{
iReceiptNumber = pmt.ReceiptNumber;
}
}
}
txtReceiptNumber.Text = (iReceiptNumber + 1).ToString();
}
}
}
- Return to the Payment Editor and click the Employee # text box
- On the Properties window, click the Events button and double-click
Leave
- Change the document as follows:
private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
Collection<Employee> lstEmployees;
BinaryFormatter bfEmployees = new BinaryFormatter();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
if (File.Exists(strFileName) == true)
{
using (FileStream fsEmployees = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstEmployees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee empl in lstEmployees)
{
if (empl.EmployeeNumber == txtEmployeeNumber.Text)
txtEmployeeName.Text = empl.LastName + ", " + empl.FirstName;
}
}
}
}
- Return to the Payment Editor dialog box and click the Account # text
box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtAccountNumber_Leave(object sender, EventArgs e)
{
BinaryFormatter bfCustomers = new BinaryFormatter();
Collection<Customer> lstCustomers = new Collection<Customer>();
string strFileName = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
if (File.Exists(strFileName) == true)
{
using (FileStream fsCustomers = new FileStream(strFileName,
FileMode.Open,
FileAccess.Read))
{
lstCustomers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
foreach (Customer client in lstCustomers)
{
if( client.AccountNumber == txtAccountNumber.Text )
txtAccountDetails.Text = client.LastName + ", " + client.FirstName;
}
}
}
}
- To create a new form, on the main menu, click PROJECT -> Add Windows
Form...
- Set the name to Payments
- Click Add
- Add a list viiew to the form and create the columns as follows:
(Name) |
Text |
TextAlign |
Width |
colReceiptNumber |
Receipt # |
|
|
colProcessedBy |
Processed By |
|
140 |
colPaymentDate |
Payment Date |
|
130 |
colAccountNumber |
Processed For |
|
140 |
colFirstDayOccupied |
First Day Occupied |
|
140 |
colLastDayOccupied |
Last Day Occupied |
|
140 |
colTotalNights |
Total Nights |
Right |
70 |
colAmountCharged |
Amt Charged |
Right |
75 |
colPhoneUse |
Phone Use |
Right |
65 |
colSubTotal |
Sub-Total |
Right |
|
colTaxRate |
Tax Rate |
Right |
|
colTaxAmount |
Tax Amt |
Right |
55 |
colTotalAmountPaid |
Amt Paid |
Right |
|
- Click OK
- Design the form as follows:
- Double-click an unoccupied area of the form and make the following
changes:
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.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
namespace CeilInn1
{
public partial class Payments : Form
{
public Payments()
{
InitializeComponent();
}
private void ShowPayments()
{
Collection<Employee> employees;
PaymentEditor editor = new PaymentEditor();
BinaryFormatter bfPayments = new BinaryFormatter();
BinaryFormatter bfEmployees = new BinaryFormatter();
BinaryFormatter bfCustomers = new BinaryFormatter();
string strEmployee = "", strCustomer = "";
Collection<Payment> payments = new Collection<Payment>();
Collection<Customer> customers = new Collection<Customer>();
string strPaymentsFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Payments.pmt";
string strCustomersFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Customers.cst";
string strEmployeesFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Employees.mpl";
if (File.Exists(strPaymentsFile) == true)
{
using (FileStream fsPayments = new FileStream(strPaymentsFile,
FileMode.Open,
FileAccess.Read))
{
payments = (Collection<Payment>)bfPayments.Deserialize(fsPayments);
lvwPayments.Items.Clear();
foreach (Payment pmt in payments)
{
ListViewItem lviPayment = new ListViewItem(pmt.ReceiptNumber.ToString());
using (FileStream fsEmployees = new FileStream(strEmployeesFile,
FileMode.Open,
FileAccess.Read))
{
employees = (Collection<Employee>)bfEmployees.Deserialize(fsEmployees);
foreach (Employee clerk in employees)
{
if (clerk.EmployeeNumber == pmt.EmployeeNumber)
strEmployee = clerk.EmployeeNumber + ": " +
clerk.FirstName + " " +
clerk.LastName;
}
}
lviPayment.SubItems.Add(strEmployee);
lviPayment.SubItems.Add(pmt.PaymentDate.ToLongDateString());
using (FileStream fsCustomers = new FileStream(strCustomersFile,
FileMode.Open,
FileAccess.Read))
{
customers = (Collection<Customer>)bfCustomers.Deserialize(fsCustomers);
foreach (Customer client in customers)
{
if (client.AccountNumber == pmt.AccountNumber)
strCustomer = client.AccountNumber + ": " +
client.FirstName + " " +
client.LastName;
}
}
lviPayment.SubItems.Add(strCustomer);
lviPayment.SubItems.Add(pmt.FirstDayOccupied.ToLongDateString());
lviPayment.SubItems.Add(pmt.LastDayOccupied.ToLongDateString());
lviPayment.SubItems.Add(pmt.TotalNights.ToString());
lviPayment.SubItems.Add(pmt.AmountCharged.ToString("F"));
lviPayment.SubItems.Add(pmt.PhoneUse.ToString("F"));
lviPayment.SubItems.Add(pmt.SubTotal.ToString("F"));
lviPayment.SubItems.Add((pmt.TaxRate / 100).ToString("P"));
lviPayment.SubItems.Add(pmt.TaxAmount.ToString("F"));
lviPayment.SubItems.Add(pmt.TotalAmountPaid.ToString("F"));
lvwPayments.Items.Add(lviPayment);
}
}
}
}
private void Payments_Load(object sender, EventArgs e)
{
ShowPayments();
}
}
}
- Return to the form and double-click the New Payment button
- Implement its event as follows:
private void btnNewPayment_Click(object sender, EventArgs e)
{
PaymentEditor editor = new PaymentEditor();
BinaryFormatter bfPayments = new BinaryFormatter();
Collection<Payment> payments = new Collection<Payment>();
string strPaymentsFile = @"C:\Microsoft Visual C# Application Design\Ceil Inn\Payments.pmt";
if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(strPaymentsFile) == true)
{
using (FileStream fsPayments = new FileStream(strPaymentsFile,
FileMode.Open,
FileAccess.Read))
{
payments = (Collection<Payment>)bfPayments.Deserialize(fsPayments);
}
}
Payment pmt = new Payment();
pmt.ReceiptNumber = int.Parse(editor.txtReceiptNumber.Text);
pmt.EmployeeNumber = editor.txtEmployeeNumber.Text;
pmt.PaymentDate = editor.dtpPaymentDate.Value;
pmt.AccountNumber = editor.txtAccountNumber.Text;
pmt.FirstDayOccupied = editor.dtpFirstDateOccupied.Value;
pmt.LastDayOccupied = editor.dtpLastDateOccupied.Value;
pmt.TotalNights = int.Parse(editor.txtTotalNights.Text);
pmt.AmountCharged = double.Parse(editor.txtAmountCharged.Text);
pmt.PhoneUse = double.Parse(editor.txtPhoneUse.Text);
pmt.SubTotal = double.Parse(editor.txtSubTotal.Text);
pmt.TaxRate = double.Parse(editor.txtTaxRate.Text);
pmt.TaxAmount = double.Parse(editor.txtTaxAmount.Text);
pmt.TotalAmountPaid = double.Parse(editor.txtTotalAmountPaid.Text);
payments.Add(pmt);
using (FileStream fsPayments = new FileStream(strPaymentsFile,
FileMode.Create,
FileAccess.Write))
{
bfPayments.Serialize(fsPayments, payments);
}
}
ShowPayments();
}
- Return to the form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type CeilInn.cs and press Enter twice to display
the form
- Design the form as follows:
|
Control |
(Name) |
Text |
Button |
|
btnCustomers |
Customers... |
Button |
|
btnOcupancies |
Occupancies... |
Button |
|
btnRooms |
Rooms... |
Button |
|
btnPayments |
Payments... |
Button |
|
btnEmployees |
Employees... |
Button |
|
btnClose |
Close |
|
- Double-click an unoccupied area of the body of the form
- 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;
namespace CeilInn2
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CeilInn_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(@"C:\Microsoft Visual C# Application Design\Ceil Inn");
}
}
}
- Return to the Ceil Inn form and double-click the Customers button
- Implement its event as follows:
private void btnCustomers_Click(object sender, EventArgs e)
{
Customers clients = new Customers();
clients.ShowDialog();
}
- Return to the Ceil Inn fform and double-click the Occupancies button
- Implement the event as follows:
private void btnOccupancies_Click(object sender, EventArgs e)
{
Occupancies rentals = new Occupancies();
rentals.ShowDialog();
}
- Return to the Ceil Inn fform and double-click the Rooms button
- Implement the event as follows:
private void btnRooms_Click(object sender, EventArgs e)
{
Rooms rms = new Rooms();
rms.ShowDialog();
}
- Return to the Ceil Inn form and double-click the Payments button
- Implement its event as follows:
private void btnPayments_Click(object sender, EventArgs e)
{
Payments pmts = new Payments();
pmts.Show();
}
- Return to the Ceil Inn form and double-click the Employees button
- Implement its event as follows:
private void btnEmployees_Click(object sender, EventArgs e)
{
Employees staff = new Employees();
staff.ShowDialog();
}
- Return to the form and double-click the Close button
- Implement its event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To execute, press Ctrl + F5
- Click the Customers button
- In the Customers form, click New Customer...
- Create each record as follows and click OK at then end of each:
Account # |
First Name |
Last Name |
Phone # |
Emergency Name |
Emergency Phone |
100752 |
Caroline |
Lomey |
301-652-0700 |
Albert Lomey |
301-412-5055 |
946090 |
Peter |
Carney |
990-585-1886 |
Spencer Miles |
990-750-8666 |
474065 |
Peter |
Carney |
990-585-1886 |
Spencer Miles |
990-750-8666 |
204795 |
Juliette |
Beckins |
410-944-1440 |
Bernard Brodsky |
410-385-2235 |
208405 |
Peter |
Carney |
990-585-1886 |
Spencer Miles |
990-750-8666 |
284085 |
Lucy |
Chen |
425-979-7413 |
Edward Lamb |
425-720-9247 |
294209 |
Doris |
Wilson |
703-416-0934 |
Gabriela Dawson |
703-931-1000 |
383084 |
Peter |
Carney |
990-585-1886 |
Spencer Miles |
990-750-8666 |
902840 |
Daniel |
Peters |
624-802-1686 |
Grace Peters |
877-490-9333 |
608502 |
Caroline |
Lomey |
301-652-0700 |
Albert Lomey |
301-412-5055 |
180204 |
Randy |
Whittaker |
703-631-1200 |
Bryan Rattner |
703-506-9200 |
629305 |
Joan |
Davids |
202-789-0500 |
Rebecca Boiron |
202-399-3600 |
660820 |
Anne |
Sandt |
953-172-9347 |
William Sandt |
953-279-2475 |
260482 |
Caroline |
Lomey |
301-652-0700 |
Albert Lomey |
301-412-5055 |
608208 |
Alfred |
Owens |
804-798-3257 |
Jane Owens |
240-631-1445 |
640800 |
Randy |
Whittaker |
703-631-1200 |
Bryan Rattner |
703-506-9200 |
- Close the Customers form
- Click the Employees button
- Click the New Employee button and create each new employee as
follows (click OK after each):
Employee # |
First Name |
Last Name |
Title |
22958 |
Andrew |
Laskin |
General Manager |
24095 |
Fred |
Barclay |
Accounts Associate |
27049 |
Harriett |
Dovecot |
Accounts Associate |
28405 |
Peggy |
Thompson |
Accounts Associate |
20429 |
Lynda |
Fore |
Shift Manager |
22947 |
Sheryl |
Shegger |
Intern |
- Close the Employees form
- Click the Rooms button
- Continuously click New Room... and create the following records:
Room # |
Room Type |
Bed Type |
Rate |
Room Status |
104 |
Bedroom |
Queen |
75.85 |
Occupied |
105 |
Bedroom |
King |
92.75 |
Available |
106 |
Bedroom |
Queen |
75.85 |
Available |
107 |
Bedroom |
King |
92.75 |
Occupied |
108 |
Bedroom |
Queen |
75.85 |
Available |
110 |
Conference |
|
450.00 |
Available |
112 |
Conference |
|
650.00 |
Available |
114 |
Bedroom |
King |
92.75 |
Available |
115 |
Bedroom |
King |
92.75 |
Available |
116 |
Bedroom |
Queen |
75.85 |
Available |
117 |
Bedroom |
Queen |
75.85 |
Available |
118 |
Bedroom |
King |
85.75 |
Available |
120 |
Studio |
King |
98.95 |
Available |
122 |
Conference |
|
725.00 |
Available |
125 |
Bedroom |
King |
95.50 |
Available |
126 |
Studio |
King |
98.95 |
Available |
127 |
Bedroom |
Double |
79.90 |
Available |
202 |
Studio |
King |
98.95 |
Other |
203 |
Studio |
Queen |
94.50 |
Available |
204 |
Bedroom |
Double |
96.60 |
Available |
205 |
Bedroom |
Queen |
75.85 |
Available |
206 |
Bedroom |
King |
92.75 |
Occupied |
207 |
Bedroom |
Queen |
75.85 |
Available |
208 |
Bedroom |
Queen |
75.85 |
Available |
209 |
Studio |
King |
98.95 |
Available |
210 |
Studio |
Queen |
94.50 |
Available |
211 |
Bedroom |
Double |
79.90 |
Available |
212 |
Bedroom |
Queen |
96.60 |
Available |
213 |
Bedroom |
King |
95.50 |
Occupied |
214 |
Bedroom |
Queen |
96.60 |
Available |
215 |
Bedroom |
Queen |
96.60 |
Available |
216 |
Bedroom |
King |
95.50 |
Available |
217 |
Bedroom |
Queen |
96.60 |
Available |
218 |
Bedroom |
Queen |
96.60 |
Available |
219 |
Bedroom |
Queen |
96.60 |
Available |
220 |
Bedroom |
Queen |
96.60 |
Available |
- Close the Rooms form
- Click Occupancies
- Create the following records:
Occupancy # |
Processed By |
Date Occupied |
Processed For |
Room Occupied |
Rate Applied |
Phone Charge |
100001 |
24095 |
6/16/2014 |
100752 |
106 |
|
|
100002 |
|
6/17/2014 |
100752 |
106 |
75.85 |
|
100003 |
|
6/18/2014 |
100752 |
106 |
75.85 |
|
100004 |
|
6/19/2014 |
100752 |
106 |
75.85 |
|
100005 |
28405 |
6/20/2014 |
100752 |
106 |
75.85 |
|
100006 |
28405 |
6/21/2014 |
946090 |
112 |
|
3.55 |
100007 |
28405 |
6/21/2014 |
474065 |
110 |
450 |
|
100008 |
27049 |
6/21/2014 |
204795 |
104 |
|
|
100009 |
28405 |
6/22/2014 |
946090 |
112 |
98.95 |
28.86 |
100010 |
24095 |
6/22/2014 |
204795 |
104 |
75.85 |
|
100011 |
24095 |
6/23/2014 |
208405 |
203 |
|
|
100012 |
24095 |
6/23/2014 |
284085 |
106 |
|
|
100013 |
24095 |
6/23/2014 |
294209 |
205 |
|
|
100014 |
|
6/24/2014 |
208405 |
203 |
94.5 |
|
100015 |
|
6/24/2014 |
284085 |
106 |
75.85 |
|
100016 |
|
6/24/2014 |
294209 |
205 |
75.85 |
|
100017 |
|
6/25/2014 |
208405 |
203 |
94.5 |
2.25 |
100018 |
|
6/25/2014 |
284085 |
106 |
75.85 |
|
100019 |
|
6/25/2014 |
294209 |
205 |
75.85 |
|
100020 |
|
6/26/2014 |
208405 |
203 |
94.5 |
|
100021 |
|
6/26/2014 |
284085 |
106 |
75.85 |
3.15 |
100022 |
|
6/26/2014 |
294209 |
205 |
75.85 |
|
100023 |
|
6/27/2014 |
208405 |
203 |
94.5 |
4.05 |
100024 |
|
6/27/2014 |
284085 |
106 |
75.85 |
5.52 |
100025 |
|
6/27/2014 |
294209 |
205 |
75.85 |
|
100026 |
28405 |
6/28/2014 |
208405 |
203 |
94.5 |
|
100027 |
20429 |
6/28/2014 |
383084 |
112 |
650 |
22.64 |
100028 |
28405 |
6/28/2014 |
284085 |
106 |
75.85 |
|
100029 |
24095 |
6/28/2014 |
294209 |
205 |
75.85 |
|
100030 |
28405 |
6/28/2014 |
902840 |
107 |
|
|
100031 |
|
6/28/2014 |
608502 |
120 |
|
4.26 |
100032 |
|
6/28/2014 |
180204 |
126 |
|
|
100033 |
|
6/28/2014 |
629305 |
122 |
725 |
|
100034 |
28405 |
6/28/2014 |
660820 |
105 |
|
|
100035 |
27049 |
6/29/2014 |
902840 |
107 |
85.75 |
|
100036 |
27049 |
6/29/2014 |
608502 |
120 |
98.85 |
8.48 |
100037 |
28405 |
6/29/2014 |
180204 |
126 |
98.85 |
|
100038 |
20429 |
6/29/2014 |
260482 |
110 |
450 |
|
100039 |
|
6/29/2014 |
660820 |
105 |
92.75 |
|
100040 |
28405 |
6/30/2014 |
660820 |
105 |
92.75 |
|
100041 |
28405 |
7/2/2014 |
608208 |
114 |
|
|
100042 |
28405 |
7/3/2014 |
608208 |
114 |
92.75 |
6.82 |
100043 |
20429 |
7/4/2014 |
608208 |
114 |
92.75 |
|
100044 |
28405 |
7/4/2014 |
640800 |
204 |
|
|
100045 |
|
7/5/2014 |
640800 |
204 |
96.6 |
|
100046 |
27049 |
7/6/2014 |
640800 |
204 |
96.6 |
|
- Close the Occupancies form
- Click the Payments button and create a few payments as follows:
Processed By |
Pmt Date |
Account # |
Occupied from |
To |
Total Nights |
Amt Charged |
Phone Use |
Tax Rate |
28405 |
6/20/2014 |
100752 |
6/16/2014 |
6/20/2014 |
4 |
75.85 |
0 |
7.70 |
28405 |
6/22/2014 |
946090 |
6/21/2014 |
6/22/2014 |
1 |
98.95 |
32.41 |
7.70 |
20429 |
6/23/2014 |
474065 |
6/21/2014 |
6/21/2014 |
1 |
450 |
0 |
7.70 |
24095 |
6/24/2014 |
204795 |
6/21/2014 |
6/22/2014 |
1 |
75.85 |
0 |
7.70 |
28405 |
6/28/2014 |
208405 |
6/23/2014 |
6/28/2014 |
5 |
94.50 |
2.25 |
7.70 |
28405 |
6/28/2014 |
284085 |
6/23/2014 |
6/28/2014 |
5 |
75.85 |
8.67 |
7.70 |
24095 |
6/28/2014 |
294209 |
6/23/2014 |
6/28/2014 |
5 |
75.85 |
0 |
7.70 |
28405 |
7/2/2014 |
180204 |
6/28/2014 |
6/29/2014 |
1 |
98.85 |
0 |
7.70 |
20429 |
7/5/2014 |
260482 |
6/29/2014 |
6/29/2014 |
1 |
450 |
0 |
7.70 |
20429 |
7/6/2014 |
383084 |
6/28/2014 |
6/28/2014 |
1 |
650 |
22.64 |
7.70 |
27049 |
7/6/2014 |
640800 |
7/4/2014 |
7/6/2014 |
2 |
93.65 |
0 |
7.70 |
20429 |
7/11/2014 |
608208 |
7/2/2014 |
7/4/2014 |
2 |
92.75 |
6.82 |
7.70 |
- Close the forms and return to your programming environment
Application
|
|