Stellar Water Point - JSON Serialization
Stellar Water Point - JSON Serialization
Application Setup
Introduction
.
Practical Learning: Introducing the Application
namespace StellarWaterPoint4.Models { internal readonly record struct AccountType { public readonly string? TypeCode { get; init; } public readonly string? TypeDecription { get; init; } } }
The Main Form of the Application
.
Practical Learning: Preparing the Main Form of the Application
namespace StellarWaterPoint4
{
public partial class WaterDistribution : Form
{
public WaterDistribution()
{
InitializeComponent();
}
private void WaterDistribution_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(@"E:\Stellar Water Point2");
Repository.CreateAccountsTypes();
}
}
}
Water Meters
Introduction
.
Practical Learning: Displaying Water Meters
namespace StellarWaterPoint4.Models { internal record WaterMeter { public string? MeterNumber { get; set; } public string? Make { get; set; } public string? Model { get; set; } public string? MeterSize { get; set; } } }
Displaying Water Meters
To let the user see a list of the water meters in the database, we will use a form equipped with a list view.
Practical Learning: Displaying Water Meters
(Name) | Text | Width | TextAlign |
colWaterMeterId | Id | 40 | |
colMeterNumber | Meter # | 150 | Center |
colMake | Make | 300 | |
colModel | Model | 150 | |
colMeterSize | Meter Size | 150 |
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwWaterMeters | FullRowSelect: True GridLines: True View: Details |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterMeters { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowWaterMeters() { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { using (TextReader trWaterMeters = new StreamReader(fiWaterMeters.FullName)) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; int counter = 1; lvwWaterMeters.Items.Clear(); foreach (WaterMeter wm in waterMeters) { ListViewItem lviWaterMeter = new ListViewItem(counter++.ToString()); lviWaterMeter.SubItems.Add(wm.MeterNumber); lviWaterMeter.SubItems.Add(wm.Make); lviWaterMeter.SubItems.Add(wm.Model); lviWaterMeter.SubItems.Add(wm.MeterSize); lvwWaterMeters.Items.Add(lviWaterMeter); } } } } private void Central_Load(object sender, EventArgs e) { ShowWaterMeters(); } } }
Control | (Name) | Text | Font | |
Button | ![]() |
btnWaterMeters | &Water Meters... | Times New Roman, 24pt, style=Bold |
namespace StellarWaterPoint3
{
public partial class WaterDistribution : Form
{
public WaterDistribution()
{
InitializeComponent();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new WaterMeters.Central();
central.ShowDialog();
}
}
}
A Water Meter Record
.
Practical Learning: Creating a Water Meter Record
Control | (Name) | Text | Other Properties | |
Label | ![]() |
&Meter #: | ||
MaskedTextBox | ![]() |
mtbMeterNumber | Masked: 000-000-000 | |
Label | ![]() |
M&ake: | ||
TextBox | ![]() |
txtMake | Modifiers: Public | |
Label | ![]() |
M&odel: | ||
TextBox | ![]() |
txtModel | Modifiers: Public | |
Label | ![]() |
Me&ter Size: | ||
TextBox | ![]() |
txtMeterSize | Modifiers: Public | |
Button | ![]() |
btnOK | &OK | DialogResult: OK |
Button | ![]() |
btnCancel | &Cancel | DialogResult: Cancel |
FormBorderStyle: FixedDialog Text: Stellar Water Point - Water Meter Setup StartPosition: CenterScreen AcceptButton: btnOK CancelButton: btnCancel
Control | (Name) | Other Properties | |
ListView | ![]() |
No Change | No Change |
Button | ![]() |
btnNewWaterMeter | Text: &New Water Meter... |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
Create create = new();
if (create.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
{
MessageBox.Show("You must type a meter number. " +
"Otherwise, the water meter cannot be set up.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
}
WaterMeter wm = new WaterMeter()
{
MeterNumber = create.mtbMeterNumber.Text,
Make = create.txtMake.Text,
Model = create.txtModel.Text,
MeterSize = create.txtMeterSize.Text
};
waterMeters.Add(wm);
JsonSerializerOptions options = new JsonSerializerOptions();
options.WriteIndented = true;
string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options);
File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters);
}
ShowWaterMeters();
}
}
}
Meter # | Make | Model | Meter Size |
392-494-572 | Constance Technologies | TG-4822 | 5/8 Inches |
938-705-869 | Stan Wood | 66-G | 1 Inch |
588-279-663 | Estellano | NCF-226 | 3/4 Inches |
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4
{
public partial class WaterDistribution : Form
{
public WaterDistribution()
{
InitializeComponent();
}
private void WaterDistribution_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(@"E:\Stellar Water Point2");
// Repository.CreateAccountsTypes();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
}
}
Water Meter Details
.
Practical Learning: Creating a Water Meter Record
Control | (Name) | Text | Enabled | Modifiers | Other Properties | |
Label | ![]() |
&Meter #: | ||||
MaskedTextBox | ![]() |
mtbMeterNumber | False | Public | Masked: 000-000-000 | |
Button | ![]() |
btnFindWaterMeter | &Find Water Meter | |||
Label | ![]() |
Make: | ||||
TextBox | ![]() |
txtMake | False | Public | ||
Label | ![]() |
Model: | ||||
TextBox | ![]() |
txtModel | False | Public | ||
Label | ![]() |
Meter Size: | ||||
TextBox | ![]() |
txtMeterSize | False | Public | ||
Button | ![]() |
btnClose | &Close |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterMeters { public partial class Details : Form { public Details() { InitializeComponent(); } private void btnFindWateMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; foreach (WaterMeter meter in waterMeters) { if (meter.MeterNumber == mtbMeterNumber.Text) { txtMake.Text = meter.Make; txtModel.Text = meter.Model; txtMeterSize.Text = meter.MeterSize; } } } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
No Change | No Change |
Button | ![]() |
No Change | No Change |
Button | ![]() |
btnViewWaterMeter | &View Water Meter... |
using System.Data;
using System.json;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint30.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
. . .
}
private void lvwWaterMeters_DoubleClick(object sender, EventArgs e)
{
. . .
}
private void btnViewWaterMeter_Click(object sender, EventArgs e)
{
Details view = new();
view.ShowDialog();
ShowWaterMeters();
}
}
}
Updating a Water Meter Details
One of the routine operations performed on a database is to change the details of a record. To support this operation for a water meter, we will create a form that can be used to update the information of a water meter.
Practical Learning: Updating a Water Meter
Control | (Name) | Text | Enabled | Other Properties | ||
Label | ![]() |
&Meter #: | ||||
MaskedTextBox | ![]() |
mtbMeterNumber | False | Masked: 000-000-000 | ||
Button | ![]() |
btnFindWaterMeter | &Find Water Meter | |||
Label | ![]() |
Make: | ||||
TextBox | ![]() |
txtMake | False | |||
Label | ![]() |
Model: | ||||
TextBox | ![]() |
txtModel | False | |||
Label | ![]() |
Meter Size: | ||||
TextBox | ![]() |
txtMeterSize | False | |||
Button | ![]() |
btnFindWateMeter | &Find Wate Meter | |||
Button | ![]() |
btnClose | &Close |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterMeters
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbMeterNumber.Text))
{
MessageBox.Show("You must type a valid meter number, " +
"and then click the Find Water Meter button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!;
txtMake.Text = meter.Make;
txtModel.Text = meter.Model;
txtMeterSize.Text = meter.MeterSize;
}
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterMeters { public partial class Editor : Form { public Editor() { InitializeComponent(); } private void btnFindWateMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!; txtMake.Text = meter.Make; txtModel.Text = meter.Model; txtMeterSize.Text = meter.MeterSize; } } private void btnUpdateWaterMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; WaterMeter? meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text); if (meter is not null) { meter.Make = txtMake.Text; meter.Model = txtModel.Text; meter.MeterSize = txtMeterSize.Text; JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options); File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters); } } Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
No Change | No Change |
Button | ![]() |
No Change | No Change |
Button | ![]() |
No Change | No Change |
Button | ![]() |
btnEditWaterMeter | &Edit Water Meter... Anchor: Bottom, Right |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterMeters
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterMeters()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterMeters();
}
private void btnNewWaterMeter_Click(object sender, EventArgs e)
{
. . .
}
private void btnViewWaterMeter_Click(object sender, EventArgs e)
{
Details view = new();
view.ShowDialog();
ShowWaterMeters();
}
private void btnEditWaterMeter_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowWaterMeters();
}
}
}
Make: | Stanford Trend | Model: | 266G |
Meter Size: | 1 1/2 Inches |
Removing a Water Meter from the Database
If a record is not necessary anymore on a database, the user may want to remove it. To assist the user with this operation, we will create a form withe necessary buttons.
Practical Learning: Deleting a Water Meter Record
Control | (Name) | Text | Enabled | Other Properties | |
Label | ![]() |
&Meter #: | |||
MaskedTextBox | ![]() |
mtbMeterNumber | False | Masked: 000-000-000 | |
Label | ![]() |
Make: | |||
TextBox | ![]() |
txtMake | False | ||
Label | ![]() |
Model: | |||
TextBox | ![]() |
txtModel | False | ||
Label | ![]() |
Meter Size: | |||
TextBox | ![]() |
txtMeterSize | False | ||
Button | ![]() |
btnClose | &Close |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint30.WaterMeters
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
using (SqlConnection scWaterDistribution = new SqlConnection("Data Source=(local);" +
"Initial Catalog='StellarWaterPoint30';" +
"TrustServerCertificate=True;" +
"Integrated Security=True;"))
{
SqlCommand cmdWaterMeters = new SqlCommand("SELECT Make, " +
" Model, " +
" MeterSize " +
"FROM Management.WaterMeters " +
"WHERE MeterNumber = N'" + mtbMeterNumber.Text + "';",
scWaterDistribution);
scWaterDistribution.Open();
SqlDataAdapter sdaWaterMeters = new SqlDataAdapter(cmdWaterMeters);
DataSet dsWaterMeters = new("WaterMetersSet");
sdaWaterMeters.Fill(dsWaterMeters);
foreach (DataRow drWaterMeter in dsWaterMeters.Tables[0].Rows!)
{
txtMake.Text = drWaterMeter[0].ToString();
txtModel.Text = drWaterMeter[1].ToString();
txtMeterSize.Text = drWaterMeter[2].ToString();
}
}
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterMeters { public partial class Delete : Form { public Delete() { InitializeComponent(); } private void btnFindWateMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!; txtMake.Text = meter.Make; txtModel.Text = meter.Model; txtMeterSize.Text = meter.MeterSize; } } private void btnDeleteWaterMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; WaterMeter meter = waterMeters.Find(mtr => mtr.MeterNumber == mtbMeterNumber.Text)!; if (meter is not null) { if (MessageBox.Show("Are you sure you want to delete this water meter " + "(you cannot undo the action)?", "Stellar Water Point", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { waterMeters.Remove(meter); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options); File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters); MessageBox.Show("The water meter has been removed from our database.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Text | Anchor | |
ListView | ![]() |
lvwWaterMeters | Top, Bottom, Left, Right | |
Button | ![]() |
btnNewWaterMeter | &New Water Meter... | Bottom, Right |
Button | ![]() |
btnViewWaterMeter | &View Water Meter... | Bottom, Right |
Button | ![]() |
btnEditWaterMeter | &Edit Water Meter... | Bottom, Right |
Button | ![]() |
btnDeleteWateMeter | &Delete Water Meter... | Bottom, Right |
Button | ![]() |
btnClose | &Close | Bottom, Right |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterMeters { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowWaterMeters() { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { using (TextReader trWaterMeters = new StreamReader(fiWaterMeters.FullName)) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; int counter = 1; lvwWaterMeters.Items.Clear(); foreach (WaterMeter meter in waterMeters) { ListViewItem lviWaterMeter = new ListViewItem(counter++.ToString()); lviWaterMeter.SubItems.Add(meter.MeterNumber); lviWaterMeter.SubItems.Add(meter.Make); lviWaterMeter.SubItems.Add(meter.Model); lviWaterMeter.SubItems.Add(meter.MeterSize); lvwWaterMeters.Items.Add(lviWaterMeter); } } } } private void Central_Load(object sender, EventArgs e) { ShowWaterMeters(); } private void btnNewWaterMeter_Click(object sender, EventArgs e) { Create create = new(); if (create.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(create.mtbMeterNumber.Text)) { MessageBox.Show("You must type a meter number. " + "Otherwise, the water meter cannot be set up.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; } WaterMeter meter = new WaterMeter() { MeterNumber = create.mtbMeterNumber.Text, Make = create.txtMake.Text, Model = create.txtModel.Text, MeterSize = create.txtMeterSize.Text }; waterMeters.Add(meter); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options); File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters); } ShowWaterMeters(); } private void btnViewWaterMeter_Click(object sender, EventArgs e) { Details view = new(); view.ShowDialog(); ShowWaterMeters(); } private void btnEditWaterMeter_Click(object sender, EventArgs e) { Editor editor = new(); editor.ShowDialog(); ShowWaterMeters(); } private void btnDeleteWateMeter_Click(object sender, EventArgs e) { Delete delete = new(); delete.ShowDialog(); ShowWaterMeters(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
using StellarWaterPoint4.WaterMeters; using System.Text.Json; namespace StellarWaterPoint4.Models { public static class Repository { public static void CreateWaterMeters() { List<WaterMeter> waterMeters = new List<WaterMeter>(); waterMeters!.Add(new WaterMeter() { MeterNumber = "392-494-572", Make = "Constance Technologies", Model = "TG-4822", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "938-725-869", Make = "Stanford Trend", Model = "266G", MeterSize = "1 1/2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "588-279-663", Make = "Estellano", Model = "NCF-226", MeterSize = "4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "186-962-805", Make = "Lansome", Model = "2800", MeterSize = "1 1/2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "379-386-979", Make = "Planetra", Model = "P-2020", MeterSize = "4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "580-742-825", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "849-351-444", Make = "Raynes Energica", Model = "a1088", MeterSize = "2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "208-428-308", Make = "Constance Technologies", Model = "808D", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "738-588-249", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "496-813-794", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "862-715-006", Make = "Warrington", Model = "W-4040", MeterSize = "1/2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "649-358-184", Make = "Raynes Energica", Model = "b1700", MeterSize = "1 1/2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "928-317-924", Make = "Gongola", Model = "GN1000", MeterSize = "2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "595-753-147", Make = "Grass Grill", Model = "CRC-1000", MeterSize = "1 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "799-528-461", Make = "Kensa Sons", Model = "K-584-L", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "386-468-057", Make = "Estellano", Model = "NCF-226", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "938-275-294", Make = "Constance Technologies", Model = "TT-8822", MeterSize = "4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "288-427-585", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "394-835-297", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "847-252-246", Make = "Master Stream", Model = "2000-MS", MeterSize = "1 1/2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "349-725-848", Make = "Planetra", Model = "P-8000", MeterSize = "4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "713-942-058", Make = "Master Stream", Model = "3366-MS", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "747-581-379", Make = "Warrington", Model = "W4242", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "582-755-263", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "827-260-758", Make = "Raynes Energica", Model = "a1088", MeterSize = "1-1/4 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "837-806-836", Make = "Lansome", Model = "7400", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "207-964-835", Make = "Constance Technologies", Model = "TG-6220", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "296-837-495", Make = "Raynes Energica", Model = "QG505", MeterSize = "4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "468-359-486", Make = "Grass Grill", Model = "KLP-8822", MeterSize = "1-1/4 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "931-486-003", Make = "Planetra", Model = "P-2020", MeterSize = "1/2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "483-770-648", Make = "Warren", Model = "WWW", MeterSize = "0.1 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "592-824-957", Make = "Kensa Sons", Model = "D-497-H", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "293-835-704", Make = "Gongola", Model = "GOL1000", MeterSize = "1/2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "739-777-749", Make = "Warrington", Model = "W2200W", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "374-886-284", Make = "Raynes Energica", Model = "i2022", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "186-959-757", Make = "Kensa Sons", Model = "M-686-G", MeterSize = "1 1/2 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "594-827-359", Make = "Planetra", Model = "P-8000", MeterSize = "1 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "394-739-242", Make = "Master Stream", Model = "9393-TT", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "529-283-752", Make = "Constance Technologies", Model = "404T", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "295-770-695", Make = "Warrington", Model = "W-2286", MeterSize = "1-1/4 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "739-749-737", Make = "Kensa Sons", Model = "KS2000A", MeterSize = "1 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "947-528-317", Make = "Gondola", Model = "GDL-5000", MeterSize = "1 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "630-207-055", Make = "Lansome", Model = "2800", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "827-508-248", Make = "Standard Trend", Model = "428T", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "293-924-869", Make = "Grass Grill", Model = "CRC-2020", MeterSize = "1/2 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "928-247-580", Make = "Gondola", Model = "GOL2000", MeterSize = "0.34 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "682-537-380", Make = "Planetra", Model = "P-2020", MeterSize = "1-1/4 Inch" }); waterMeters.Add(new WaterMeter() { MeterNumber = "470-628-850", Make = "Estellano", Model = "WRT-482", MeterSize = "3/4 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "649-373-505", Make = "Constance Technologies", Model = "BD-7000", MeterSize = "5/8 Inches" }); waterMeters.Add(new WaterMeter() { MeterNumber = "306-842-497", Make = "Lansome", Model = "9000", MeterSize = "3/4 Inches" }); string strWaterMeters = string.Empty; string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); string jsWaterMeters = JsonSerializer.Serialize(waterMeters, typeof(List<WaterMeter>), options); File.WriteAllText(fiWaterMeters.FullName, jsWaterMeters); } } }
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4
{
public partial class WaterDistribution : Form
{
public WaterDistribution()
{
InitializeComponent();
}
private void WaterDistribution_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(@"E:\Stellar Water Point2");
// Repository.CreateAccountsTypes();
Repository.CreateWaterMeters();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
}
}
Customers
Introduction
.
Practical Learning: Introducing Customers
namespace StellarWaterPoint4.Models { public class Customer { public string? AccountNumber { get; set; } public string? AccountName { get; set; } public string? MeterNumber { get; set; } public string? AccountType { get; set; } public string? Address { get; set; } public string? City { get; set; } public string? County { get; set; } public string? State { get; set; } public string? ZIPCode { get; set; } } }
Displaying Customers
.
Practical Learning: Displaying Customers
(Name) | Text | TextAlign | Width |
colCustomerId | Id | 40 | |
colAccountNumber | Account # | Center | 150 |
colAccountName | Account Name | 200 | |
colMeterNumber | Meter # | Center | 100 |
colAccountType | Account Type | 200 | |
colAddress | Address | 250 | |
colCity | City | 125 | |
colCounty | County | 125 | |
colState | State | Center | |
colZIPCode | ZIP-Code | Center | 125 |
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwCustomers | FullRowSelect: True GridLines: True View: Details |
using StellarWaterPoint4.Models;
using System.Text.Json;
namespace StellarWaterPoint4.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowCustomers();
}
}
}
Control | (Name) | Text | Font | |
Button | ![]() |
btnCustomers | C&ustomers... | Times New Roman, 24pt, style=Bold |
using StellarWaterPoint4.Models; namespace StellarWaterPoint4 { public partial class WaterDistribution : Form { public WaterDistribution() { InitializeComponent(); } private void WaterDistribution_Load(object sender, EventArgs e) { Directory.CreateDirectory(@"E:\Stellar Water Point2"); // Repository.CreateAccountsTypes(); // Repository.CreateWaterMeters(); } private void btnCustomers_Click(object sender, EventArgs e) { Customers.Central central = new(); central.Show(); } private void btnWaterMeters_Click(object sender, EventArgs e) { WaterMeters.Central central = new(); central.Show(); } } }
A New Customer Account
.
Practical Learning: Creating a Customer Account
Control | (Name) | Text | Other Properties | |
Label | ![]() |
&Account #: | ||
MaskedTextBox | ![]() |
mtbAccountNumber | Masked: 0000-000-0000 | |
Label | ![]() |
&Account Name: | ||
TextBox | ![]() |
txtAccountName | ||
Label | ![]() |
&Meter #: | ||
MaskedTextBox | ![]() |
mtbMeterNumber | Masked: 000-000-000 | |
Button | ![]() |
btnFindWaterMeter | &Find Water Meter | |
Label | ![]() |
Meter &Details: | ||
TextBox | ![]() |
txtMeterDetails | Enabled: False | |
Label | ![]() |
&Account Type: | ||
ComboBox | ![]() |
cbxAccountsTypes | ||
Label | ![]() |
&Address: | ||
TextBox | ![]() |
txtAddress | ||
Label | ![]() |
C&ity: | ||
TextBox | ![]() |
txtCity | ||
Label | ![]() |
C&ounty: | ||
TextBox | ![]() |
txtCounty | ||
Label | ![]() |
&State: | ||
TextBox | ![]() |
txtState | ||
Label | ![]() |
&ZIP-Code: | ||
MaskedTextBox | ![]() |
mtbZIPCode | Masked: Zip-Code | |
Button | ![]() |
btnSaveCustomerAccount | S&ave Customer Account | DialogResult: OK |
Button | ![]() |
btnClose | &Close | DialogResult: Cancel |
FormBorderStyle: FixedDialog Text: Stellar Water Point - Create Customer Account StartPosition: CenterScreen AcceptButton: btnCreateCustomerAccount CancelButton: btnCancel
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void Create_Load(object sender, EventArgs e)
{
string strAccountsTypes = string.Empty;
List<AccountType> types = new List<AccountType>();
string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
if (fiAccountsTypes.Exists == true)
{
strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
foreach (AccountType type in types)
{
cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
}
}
}
}
}
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void Create_Load(object sender, EventArgs e)
{
string strAccountsTypes = string.Empty;
List<AccountType> types = new List<AccountType>();
string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
if (fiAccountsTypes.Exists == true)
{
strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
foreach (AccountType type in types)
{
cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
}
}
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbMeterNumber.Text))
{
MessageBox.Show("You must type a valid meter number, " +
"and then click the Find Water Meter button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
foreach (WaterMeter meter in waterMeters)
{
if (meter.MeterNumber == mtbMeterNumber.Text)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwCustomers | FullRowSelect: True GridLines: True View: Details |
Button | ![]() |
btnNewCustomerAccount | &New Customer Account... |
using StellarWaterPoint4.Models;
using System.Text.Json;
namespace StellarWaterPoint4.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
Create create = new();
if (create.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(create.mtbAccountNumber.Text))
{
MessageBox.Show("You must provide an account number for a new customer. " +
"Otherwise, the account cannot be created.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
if (string.IsNullOrEmpty(create.mtbMeterNumber.Text))
{
MessageBox.Show("You must type a valid meter number to associate " +
"a water meter to a customer's account. After providing " +
"a meter number, click the Find Water Meter button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
if (fiCustomers.Exists == true)
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
}
Customer client = new Customer()
{
AccountNumber = create.mtbAccountNumber.Text,
AccountName = create.txtAccountName.Text,
MeterNumber = create.mtbMeterNumber.Text,
AccountType = create.cbxAccountsTypes.Text,
Address = create.txtAddress.Text,
City = create.txtCity.Text,
County = create.txtCounty.Text,
State = create.txtState.Text,
ZIPCode = create.mtbZIPCode.Text
};
clients.Add(client);
JsonSerializerOptions options = new JsonSerializerOptions();
options.WriteIndented = true;
string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options);
File.WriteAllText(fiCustomers.FullName, jsCustomers);
}
ShowCustomers();
}
}
}
Account #: 9279-570-8394 Account Name: Thomas Stones Meter #: 799-528-461 Account Type: RES - Residential Household Address: 10252 Broward Ave #D4 City: Frederick County: Frederick State: MD ZIP-Code: 21703-4422
Account # | Account Name | Meter # | Account Type | Address | City | County | State | ZIP-Code |
4086-938-4783 | Hernola Dough | 594-827-359 | UUO - Unidentified or Unclassified Type of Organization | 10 10 Hexagonal Drv | Winston | Yoke | Penn | 11402-4411 |
7080-583-5947 | Sunny Yard | 827-508-248 | WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc | 663 Sherry Wood East Street | Shimpstown | Franklin | PA | 17236-2626 |
Customer Account Details
.
Practical Learning: Showing Customer Account
Control | (Name) | Text | Other Properties | |
Label | ![]() |
&Account #: | ||
MaskedTextBox | ![]() |
mtbAccountNumber | Masked: 0000-000-0000 | |
Label | ![]() |
&Account Name: | ||
TextBox | ![]() |
txtAccountName | Enabled: False | |
Label | ![]() |
Meter &Details: | ||
TextBox | ![]() |
txtMeterDetails | Enabled: False | |
Label | ![]() |
&Account Type: | ||
TextBox | ![]() |
txtAccountsTypes | Enabled: False | |
Label | ![]() |
&Address: | ||
TextBox | ![]() |
txtAddress | Enabled: False | |
Label | ![]() |
C&ity: | ||
TextBox | ![]() |
txtCity | Enabled: False | |
Label | ![]() |
C&ounty: | ||
TextBox | ![]() |
txtCounty | Enabled: False | |
Label | ![]() |
&State: | ||
TextBox | ![]() |
txtState | Enabled: False | |
Label | ![]() |
&ZIP-Code: | ||
TextBox | ![]() |
txtZIPCode | Enabled: False | |
Button | ![]() |
btnClose | &Close |
using StellarWaterPoint4.Models; using System.Text.Json; namespace StellarWaterPoint4.Customers { public partial class Details : Form { public Details() { InitializeComponent(); } private void btnFindCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("You must type a valid account number of a customer, " + "and then click the Find Customer Account button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); string? strMeterNumber = string.Empty; if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = from consumer in clients where consumer.AccountNumber == mtbAccountNumber.Text select consumer; foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; strMeterNumber = client.MeterNumber; txtAccountType.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; txtZIPCode.Text = client.ZIPCode; } } } if (strMeterNumber!.Length > 0) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; IEnumerable<WaterMeter> meters = from measure in waterMeters where measure.MeterNumber == strMeterNumber select measure; foreach (WaterMeter meter in waterMeters) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwWaterMeters | No Change |
Button | ![]() |
No Change | No Change |
Button | ![]() |
btnCustomerAccountDetails | Customer Account &Details... Anchor: Bottom, Right |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void btnCustomerAccountDetails_Click(object sender, EventArgs e)
{
Details details = new();
details.ShowDialog();
ShowCustomers();
}
}
}
Editing a Customer Account
.
Practical Learning: Editing a Customer Account
Control | (Name) | Text | Other Properties | |
Label | ![]() |
&Account #: | ||
MaskedTextBox | ![]() |
mtbAccountNumber | Masked: 0000-000-0000 | |
Button | ![]() |
btnFindCustomerAccount | &Find Customer Account | |
Label | ![]() |
&Account Name: | ||
TextBox | ![]() |
txtAccountName | ||
Label | ![]() |
&Meter #: | ||
MaskedTextBox | ![]() |
mtbMeterNumber | Masked: 000-000-000 | |
Button | ![]() |
btnFindWaterMeter | Find &Water Meter | |
Label | ![]() |
Meter &Details: | ||
TextBox | ![]() |
txtMeterDetails | Enabled: False | |
Label | ![]() |
&Account Type: | ||
ComboBox | ![]() |
cbxAccountsTypes | ||
Label | ![]() |
&Address: | ||
TextBox | ![]() |
txtAddress | ||
Label | ![]() |
C&ity: | ||
TextBox | ![]() |
txtCity | ||
Label | ![]() |
C&ounty: | ||
TextBox | ![]() |
txtCounty | ||
Label | ![]() |
&State: | ||
TextBox | ![]() |
txtState | ||
Label | ![]() |
&ZIP-Code: | ||
MaskedTextBox | ![]() |
mtbZIPCode | Masked: Zip-Code | |
Button | ![]() |
btnUpdateCustomerAccount | &Update Customer Account | DialogResult: OK |
Button | ![]() |
btnClose | &Close | DialogResult: Cancel |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void Editor_Load(object sender, EventArgs e)
{
string strAccountsTypes = string.Empty;
List<AccountType> types = new List<AccountType>();
string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json";
FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes);
if (fiAccountsTypes.Exists == true)
{
strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName);
types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!;
foreach (AccountType type in types)
{
cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription);
}
}
}
}
}
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void Editor_Load(object sender, EventArgs e)
{
. . .
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = from consumer
in clients
where consumer.AccountNumber == mtbAccountNumber.Text
select consumer;
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
mtbMeterNumber.Text = client.MeterNumber;
cbxAccountsTypes.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
mtbZIPCode.Text = client.ZIPCode;
}
}
}
if( !string.IsNullOrEmpty(mtbMeterNumber.Text) )
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
foreach (WaterMeter meter in waterMeters)
{
if (meter.MeterNumber == mtbMeterNumber.Text)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
}
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void Editor_Load(object sender, EventArgs e)
{
. . .
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void btnFindWateMeter_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbMeterNumber.Text))
{
MessageBox.Show("You must type a valid meter number, " +
"and then click the Find Water Meter button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
foreach (WaterMeter meter in waterMeters)
{
if (meter.MeterNumber == mtbMeterNumber.Text)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; using StellarWaterPoint4.WaterMeters; namespace StellarWaterPoint4.Customers { public partial class Editor : Form { public Editor() { InitializeComponent(); } private void Editor_Load(object sender, EventArgs e) { string strAccountsTypes = string.Empty; List<AccountType> types = new List<AccountType>(); string fileAccountsTypes = @"E:\Stellar Water Point2\AccountsTypes.json"; FileInfo fiAccountsTypes = new FileInfo(fileAccountsTypes); if (fiAccountsTypes.Exists == true) { strAccountsTypes = File.ReadAllText(fiAccountsTypes.FullName); types = JsonSerializer.Deserialize<List<AccountType>>(strAccountsTypes)!; foreach (AccountType type in types) { cbxAccountsTypes.Items.Add(type.TypeCode + " - " + type.TypeDecription); } } } private void btnFindCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("Please an account number of an existing customer. " + "Then click the Find Customer Account button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = from consumer in clients where consumer.AccountNumber == mtbAccountNumber.Text select consumer; foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; mtbMeterNumber.Text = client.MeterNumber; cbxAccountsTypes.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; mtbZIPCode.Text = client.ZIPCode; } } } if (!string.IsNullOrEmpty(mtbMeterNumber.Text)) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; foreach (WaterMeter meter in waterMeters) { if (meter.MeterNumber == mtbMeterNumber.Text) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } } private void btnFindWateMeter_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; foreach (WaterMeter meter in waterMeters) { if (meter.MeterNumber == mtbMeterNumber.Text) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void btnUpdateCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("Please enter an account number for a customer. " + "You can then click the Find Customer Account button " + "to indicate the customer whose account you want o update.", "Stellar Water Point", MessageBoxButtons.OK); return; } if (string.IsNullOrEmpty(mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number, " + "and then click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); if (fiCustomers.Exists == true) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; Customer? customer = clients.Find(cust => cust.AccountNumber == mtbAccountNumber.Text); if(customer is not null) { customer.AccountName = txtAccountName.Text; customer.MeterNumber = mtbMeterNumber.Text; customer.AccountType = cbxAccountsTypes.Text; customer.Address = txtAddress.Text; customer.City = txtCity.Text; customer.County = txtCounty.Text; customer.State = txtState.Text; customer.ZIPCode = mtbZIPCode.Text; JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options); File.WriteAllText(fiCustomers.FullName, jsCustomers); }; } Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwCustomers | No Change |
Button | ![]() |
btnNewCustomerAccount | No Change |
Button | ![]() |
btnCustomerAccountDetails | No Change |
Button | ![]() |
btnUpdateCustomerAccount | &Update Customer Account... Anchor: Bottom, Right |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.Customers
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowCustomers()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowCustomers();
}
private void btnCreateCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void btnCustomerAccountDetails_Click(object sender, EventArgs e)
{
Details details = new();
details.ShowDialog();
ShowCustomers();
}
private void btnEditCustomerAccount_Click(object sender, EventArgs e)
{
Editor editor = new();
editor.ShowDialog();
ShowCustomers();
}
}
}
Account Name: Bernotte Doughnuts Meter #: 580-742-825 and click Find Water Meter Account Type: BUS - General Business Address: 10103 Hexagon Drive City: Winterstown County: York State: PA ZIP-Code: 17402-8828
Deleting a Customer Account from the Database
.
Practical Learning: Deleting a Customer Account
Control | (Name) | Text | Other Properties | |
Label | ![]() |
&Account #: | ||
MaskedTextBox | ![]() |
mtbAccountNumber | Masked: 0000-000-0000 | |
Button | ![]() |
btnFindCustomerAccount | &Find Customer Account | |
Label | ![]() |
&Account Name: | ||
TextBox | ![]() |
txtAccountName | Enabled: False | |
Label | ![]() |
Meter &Details: | ||
TextBox | ![]() |
txtMeterDetails | Enabled: False | |
Label | ![]() |
&Account Type: | ||
TextBox | ![]() |
txtAccountsTypes | Enabled: False | |
Label | ![]() |
&Address: | ||
TextBox | ![]() |
txtAddress | Enabled: False | |
Label | ![]() |
C&ity: | ||
TextBox | ![]() |
txtCity | Enabled: False | |
Label | ![]() |
C&ounty: | ||
TextBox | ![]() |
txtCounty | Enabled: False | |
Label | ![]() |
&State: | ||
TextBox | ![]() |
txtState | Enabled: False | |
Label | ![]() |
&ZIP-Code: | ||
TextBox | ![]() |
txtZIPCode | Enabled: False | |
Button | ![]() |
btnDeleteCustomerAccount | &Delete Customer Account | |
Button | ![]() |
btnClose | &Close |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.Customers
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbAccountNumber.Text))
{
MessageBox.Show("You must type a valid account number of a customer, " +
"and then click the Find Customer Account button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
string? strMeterNumber = string.Empty;
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = from consumer
in clients
where consumer.AccountNumber == mtbAccountNumber.Text
select consumer;
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
}
}
}
if (strMeterNumber!.Length > 0)
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
IEnumerable<WaterMeter> meters = from measure
in waterMeters
where measure.MeterNumber == strMeterNumber
select measure;
foreach (WaterMeter meter in waterMeters)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.Customers { public partial class Delete : Form { public Delete() { InitializeComponent(); } private void btnFindCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("You must type a valid account number of a customer, " + "and then click the Find Customer Account button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); string? strMeterNumber = string.Empty; if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = from consumer in clients where consumer.AccountNumber == mtbAccountNumber.Text select consumer; foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; strMeterNumber = client.MeterNumber; txtAccountType.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; txtZIPCode.Text = client.ZIPCode; } } } if (strMeterNumber!.Length > 0) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; IEnumerable<WaterMeter> meters = from measure in waterMeters where measure.MeterNumber == strMeterNumber select measure; foreach (WaterMeter meter in waterMeters) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void btnDeleteCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("You must type a valid account number of a customer, " + "and then click the Find Customer Account button. " + "Only then can you delete an customer account.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); if (fiCustomers.Exists == true) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; Customer client = clients.Find(acnt => acnt.AccountNumber == mtbAccountNumber.Text)!; if (client is not null) { if (MessageBox.Show("Are you sure you want to delete this customer's account " + "(you cannot undo the action)?", "Stellar Water Point", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { clients.Remove(client); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options); File.WriteAllText(fiCustomers.FullName, jsCustomers); MessageBox.Show("The customer's account has been deleted from our system.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwCustomers | FullRowSelect: True GridLines: True View: Details Anchor: Top, Bottom, Left, Right |
Button | ![]() |
btnCreateCustomerAccount | Create Customer &Account.... Anchor: Bottom, Right |
Button | ![]() |
btnCustomerAccountDetails | Customer Account &Details... Anchor: Bottom, Right |
Button | ![]() |
btnEditCustomerAccount | &Edit Customer Account... Anchor: Bottom, Right |
Button | ![]() |
btnDeleteCustomerAccount | &Delete Customer Account... Anchor: Bottom, Right |
Button | ![]() |
btnClose | &Close Anchor: Bottom, Right |
using StellarWaterPoint4.Models; using System.Text.Json; namespace StellarWaterPoint4.Customers { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowCustomers() { string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; int counter = 1; lvwCustomers.Items.Clear(); foreach (Customer client in clients) { ListViewItem lviCustomer = new ListViewItem(counter++.ToString()); lviCustomer.SubItems.Add(client.AccountNumber); lviCustomer.SubItems.Add(client.AccountName); lviCustomer.SubItems.Add(client.MeterNumber); lviCustomer.SubItems.Add(client.AccountType); lviCustomer.SubItems.Add(client.Address); lviCustomer.SubItems.Add(client.City); lviCustomer.SubItems.Add(client.County); lviCustomer.SubItems.Add(client.State); lviCustomer.SubItems.Add(client.ZIPCode); lvwCustomers.Items.Add(lviCustomer); } } } } private void Central_Load(object sender, EventArgs e) { ShowCustomers(); } private void btnCreateCustomerAccount_Click(object sender, EventArgs e) { Create create = new(); if (create.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(create.mtbAccountNumber.Text)) { MessageBox.Show("You must provide an account number for a new customer. " + "Otherwise, the account cannot be created.", "Stellar Water Point", MessageBoxButtons.OK); return; } if (string.IsNullOrEmpty(create.mtbMeterNumber.Text)) { MessageBox.Show("You must type a valid meter number to associate " + "a water meter to a customer's account. After providing " + "a meter number, click the Find Water Meter button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); if (fiCustomers.Exists == true) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; } Customer client = new Customer() { AccountNumber = create.mtbAccountNumber.Text, AccountName = create.txtAccountName.Text, MeterNumber = create.mtbMeterNumber.Text, AccountType = create.cbxAccountsTypes.Text, Address = create.txtAddress.Text, City = create.txtCity.Text, County = create.txtCounty.Text, State = create.txtState.Text, ZIPCode = create.mtbZIPCode.Text }; clients.Add(client); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsCustomers = JsonSerializer.Serialize(clients, typeof(List<Customer>), options); File.WriteAllText(fiCustomers.FullName, jsCustomers); } ShowCustomers(); } private void btnCustomerAccountDetails_Click(object sender, EventArgs e) { Details details = new(); details.Show(); } private void btnEditCustomerAccount_Click(object sender, EventArgs e) { Editor editor = new(); editor.ShowDialog(); ShowCustomers(); } private void btnDeleteCustomerAccount_Click(object sender, EventArgs e) { Delete delete = new(); delete.ShowDialog(); ShowCustomers(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
using System.Text.Json;
namespace StellarWaterPoint4.Models
{
public static class Repository
{
public static void CreateAccountsTypes()
{
. . .
}
public static void CreateWaterMeters()
{
. . .
}
public static void CreateCustomers()
{
List<Customer> customers = new List<Customer>();
customers!.Add(new Customer() { AccountNumber = "9279-570-8394", AccountName = "Thomas Stones", MeterNumber = "799-528-461", AccountType = "RES - Residential Household", Address = "10252 Broward Ave #D4", City = "Frederick", County = "Frederick", State = "MD", ZIPCode = "21703-4422" });
customers.Add(new Customer() { AccountNumber = "4086-938-4783", AccountName = "Bernotte's Doughnuts", MeterNumber = "580-742-825", AccountType = "BUS - General Business", Address = "10103 Hexagon Drv", City = "Winterstown", County = "York", State = "PA", ZIPCode = "17402-8818" });
customers.Add(new Customer() { AccountNumber = "2068-258-9486", AccountName = "Yollanda Training", MeterNumber = "186-962-805", AccountType = "UUO - Unidentified or Unclassified Type of Organization", Address = "4819 East Munk Street", City = "Whitehall", County = "Fulton", State = "PA", ZIPCode = "17340-1188" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "First Methodist Congregation", MeterNumber = "", AccountType = "", Address = "7702 Charles Road", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "6986-829-3741", AccountName = "Eyes Wide", MeterNumber = "208-428-308", AccountType = "BUS - General Business", Address = "12087 Avencia Court #4D1", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-2288" });
customers.Add(new Customer() { AccountNumber = "4293-802-8506", AccountName = "Kelly Davids", MeterNumber = "496-813-794", AccountType = "RES - Residential Household", Address = "938 East Panchot Str", City = "Greenwood", County = "Sussex", State = "DE", ZIPCode = "19950-4242" });
customers.Add(new Customer() { AccountNumber = "9947-374-2648", AccountName = "Marianne Harrington", MeterNumber = "862-715-006", AccountType = "RES - Residential Household", Address = "708 Correta Drv", City = "Arlington", County = "", State = "VA", ZIPCode = "22222-6060" });
customers.Add(new Customer() { AccountNumber = "7928-131-4850", AccountName = "", MeterNumber = "", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "Washington", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "1386-949-2058", AccountName = "Watson Country Buffet", MeterNumber = "296-837-495", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "4862 Wellington Street", City = "Hammonton", County = "Atlantic ", State = "NJ", ZIPCode = "08037-2828" });
customers.Add(new Customer() { AccountNumber = "7943-686-9786", AccountName = "Angel Bulzaides", MeterNumber = "394-835-297", AccountType = "RES - Residential Household", Address = "10227 Old Harbor Drv", City = "Elkview", County = "Kanawha", State = "WV", ZIPCode = "25071-5858" });
customers.Add(new Customer() { AccountNumber = "4820-375-2842", AccountName = "Sun Communal", MeterNumber = "392-494-572", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "748 Red Hills Rd", City = "Roanoke", County = "", State = "VA", ZIPCode = "24012-4824" });
customers.Add(new Customer() { AccountNumber = "9618-579-2577", AccountName = "Gerald Place", MeterNumber = "847-252-246", AccountType = "UUO - Unidentified or Unclassified Type of Organization", Address = "3666 Hanchor Drv", City = "Granville", County = "Licking", State = "OH", ZIPCode = "43023-2777" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Astral Sequence", MeterNumber = "", AccountType = "BUS - General Business", Address = "12715 Eastern Gateway", City = "Catonsville", County = "Baltimore County", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "6003-386-3955", AccountName = "Mandiakandara Marmoudi", MeterNumber = "374-886-284", AccountType = "OTH - Other", Address = "539 Avalon Court", City = "Greenwood", County = "Sussex", State = "DE", ZIPCode = "19950-5550" });
customers.Add(new Customer() { AccountNumber = "5294-859-7513", AccountName = "Jeannette Schiller", MeterNumber = "713-942-058", AccountType = "RES - Residential Household", Address = "10110 Winslow Ave", City = "Mercerville", County = "Mercer", State = "NJ", ZIPCode = "08619-7472" });
customers.Add(new Customer() { AccountNumber = "9249-379-6848", AccountName = "Country West Eatery", MeterNumber = "588-279-663", AccountType = "BUS - General Business", Address = "8280 Sligo North Way", City = "Albright", County = "Preston", State = "WV", ZIPCode = "26519-6620" });
customers.Add(new Customer() { AccountNumber = "5252-757-9595", AccountName = "", MeterNumber = "379-386-979", AccountType = "", Address = "4992 Preston Street", City = "", County = "", State = "OH", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "7080-583-5947", AccountName = "Sunny Yard", MeterNumber = "827-508-248", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "663 Sherry Wood East Street", City = "Shimpstown", County = "Franklin", State = "PA", ZIPCode = "17236-2626" });
customers.Add(new Customer() { AccountNumber = "8027-304-6829", AccountName = "Anthony Clarcksons", MeterNumber = "837-806-836", AccountType = "RES - Residential Household", Address = "904 Augusta Drive", City = "Blackbird", County = "New Castle", State = "DE", ZIPCode = "19734-8822" });
customers.Add(new Customer() { AccountNumber = "6699-396-2905", AccountName = "Spencer Reuter", MeterNumber = "649-373-505", AccountType = "RES - Residential Household", Address = "2850 Burnsweak Avenue", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-4044" });
customers.Add(new Customer() { AccountNumber = "1827-395-0203", AccountName = "Sathyavanthara Khooni", MeterNumber = "470-628-850", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "10331 Chryswell Road", City = "Washington", County = "", State = "DC", ZIPCode = "20008-2426" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Eastern Cage", MeterNumber = "", AccountType = "", Address = "", City = "Hammonton", County = "", State = "NJ", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "3947-957-4958", AccountName = "Patsil Industries", MeterNumber = "747-581-379", AccountType = "BUS - General Business", Address = "10348 Larrens Drive", City = "Baltimore", County = "Baltimore", State = "MD", ZIPCode = "21215-2222" });
customers.Add(new Customer() { AccountNumber = "2836-485-9699", AccountName = "Red Oak High School", MeterNumber = "379-386-979", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "442 Donham Road", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-8822" });
customers.Add(new Customer() { AccountNumber = "5938-074-5293", AccountName = "Park and Roll", MeterNumber = "592-824-957", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "582G Dunhill Avenue", City = "Lanham", County = "Prince Georges", State = "MD", ZIPCode = "20706-8284" });
customers.Add(new Customer() { AccountNumber = "3028-502-9418", AccountName = "Spencer Kershaw", MeterNumber = "186-959-757", AccountType = "RES - Residential Household", Address = "338C Grayson Street", City = "Gatchellville", County = "York", State = "PA", ZIPCode = "17352-6464" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "", MeterNumber = "928-317-924", AccountType = "BUS - General Business", Address = "", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "2974-972-8139", AccountName = "Paul Arnette", MeterNumber = "295-770-695", AccountType = "OTH - Other", Address = "8127 Bledsoe Str", City = "Hyattsville", County = "Prince Georges", State = "MD", ZIPCode = "20783-5858" });
customers.Add(new Customer() { AccountNumber = "2758-493-7249", AccountName = "Hervey Smile", MeterNumber = "293-924-869", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "12973 Sonaa Street #E42", City = "Silver Spring", County = "Montgomery", State = "MD", ZIPCode = "20910-4488" });
customers.Add(new Customer() { AccountNumber = "9337-947-3664", AccountName = "", MeterNumber = "649-358-184", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "Bellefontaine", County = "", State = "OH", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "7518-302-6895", AccountName = "Grace Brenner", MeterNumber = "207-964-835", AccountType = "BUS - General Business", Address = "4299 Peachtree Court", City = "Rockville", County = "Montgomery", State = "MD", ZIPCode = "20853-1888" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Jeffrey Maney", MeterNumber = "", AccountType = "RES - Residential Household", Address = "", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "7028-405-9381", AccountName = "Valley Services", MeterNumber = "306-842-497", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "613 Meadowhill Road", City = "Alonzaville", County = "Shenandoah", State = "VA", ZIPCode = "22664-8080" });
customers.Add(new Customer() { AccountNumber = "5293-957-3395", AccountName = "Wellway Community Center", MeterNumber = "386-468-057", AccountType = "RES - Residential Household", Address = "10484 Greenway Avenue", City = "Mt Storm", County = "Grant", State = "WV", ZIPCode = "26739-7700" });
customers.Add(new Customer() { AccountNumber = "2038-413-9680", AccountName = "Eastern Friandise", MeterNumber = "938-725-869", AccountType = "BUS - General Business", Address = "2075 Rose Hills Avenue", City = "Washington", County = "", State = "DC", ZIPCode = "20004-2626" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Amidou Gomah", MeterNumber = "", AccountType = "RES - Residential Household", Address = "14118 Yellow Burrough Blvd", City = "Philadelphia", County = "", State = "PA", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "3792-853-6885", AccountName = "Department of Public Affairs", MeterNumber = "595-753-147", AccountType = "", Address = "", City = "Upper Marlboro", County = "Prince George County", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "8282-777-8282", AccountName = "Garland Hotel", MeterNumber = "938-275-294", AccountType = "BUS - General Business", Address = "4222 Extell Ave", City = "Cambridge", County = "", State = "MD", ZIPCode = "21613-2288" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Single Connection", MeterNumber = "288-427-585", AccountType = "BUS - General Business", Address = "", City = "Mansfield", County = "", State = "OH", ZIPCode = "44903-3030" });
customers.Add(new Customer() { AccountNumber = "2499-636-4444", AccountName = "Bryanna Spencer", MeterNumber = "", AccountType = "RES - Residential Household", Address = "6282 Sheppherd Str", City = "", County = "Anne Arundel", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "2842-585-7260", AccountName = "District Community Reserves", MeterNumber = "349-725-848", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "3280 Hopewell Street, NE", City = "Washington", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "9282-794-7937", AccountName = "Yashua Yáñés", MeterNumber = "392-494-572", AccountType = "RES - Residential Household", Address = "10214 Monroe Ave", City = "Easton", County = "", State = "MD", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Miguel Altieri", MeterNumber = "", AccountType = "RES - Residential Household", Address = "10941 Patriot Blvd", City = "Crenshaw", County = "Jefferson", State = "PA", ZIPCode = "15824-6628" });
customers.Add(new Customer() { AccountNumber = "2847-597-2829", AccountName = "Jameson", MeterNumber = "379-386-979", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "7373 Gold Town Rd", City = "", County = "", State = "WV", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "6381-748-2222", AccountName = "Up Eyes", MeterNumber = "", AccountType = "BUS - General Business", Address = "4149 Deerfield Str", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "Annette Wald", MeterNumber = "", AccountType = "RES - Residential Household", Address = "11441 Eastern Friendshi Rd", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "8384-708-2941", AccountName = "Department of Environment Affairs", MeterNumber = "", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "", City = "", County = "", State = "OH", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "3728-138-2947", AccountName = "Marie Rath", MeterNumber = "", AccountType = "", Address = "8802 Atlantic Ave", City = "", County = "", State = "PA", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "1793-857-9413", AccountName = "Body Care", MeterNumber = "", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "", City = "Ocean City", County = "", State = "NJ", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "6028-695-2068", AccountName = "Ronald Glassman", MeterNumber = "468-359-486", AccountType = "BUS - General Business", Address = "", City = "", County = "", State = "", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "", AccountName = "", MeterNumber = "", AccountType = "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc", Address = "8812 Lawrence Ave", City = "", County = "", State = "NW", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "9616-283-7249", AccountName = "", MeterNumber = "", AccountType = "SGO - Social/Government/Non-Profit Organization", Address = "13006 Blueberry Ave", City = "", County = "", State = "MD", ZIPCode = "" });
customers.Add(new Customer() { AccountNumber = "2829-516-8353", AccountName = "Richard Eghert", MeterNumber = "", AccountType = "RES - Residential Household", Address = "662 Placido Road", City = "", County = "", State = "", ZIPCode = "" });
string strCustomers = string.Empty;
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
JsonSerializerOptions options = new JsonSerializerOptions();
options.WriteIndented = true;
FileInfo fiCustomers = new FileInfo(fileCustomers);
string jsCustomers = JsonSerializer.Serialize(customers, typeof(List<Customer>), options);
File.WriteAllText(fiCustomers.FullName, jsCustomers);
}
}
}
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4
{
public partial class WaterDistribution : Form
{
public WaterDistribution()
{
InitializeComponent();
}
private void WaterDistribution_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(@"E:\Stellar Water Point2");
// Repository.CreateAccountsTypes();
// Repository.CreateWaterMeters();
Repository.CreateCustomers();
}
private void btnCustomers_Click(object sender, EventArgs e)
{
Customers.Central central = new();
central.Show();
}
private void btnWaterMeters_Click(object sender, EventArgs e)
{
WaterMeters.Central central = new();
central.Show();
}
}
}
Water Bills
Introduction
.
Practical Learning: Introducing Water Bills
namespace StellarWaterPoint4.Models { public class WaterBill { public int BillNumber { get; set; } public string? AccountNumber { get; set; } public DateTime MeterReadingStartDate { get; set; } public DateTime MeterReadingEndDate { get; set; } public int BillingDays { get; set; } public int CounterReadingStart { get; set; } public int CounterReadingEnd { get; set; } public int TotalHCF { get; set; } public int TotalGallons { get; set; } public double FirstTierConsumption { get; set; } public double SecondTierConsumption { get; set; } public double LastTierConsumption { get; set; } public double WaterCharges { get; set; } public double SewerCharges { get; set; } public double EnvironmentCharges { get; set; } public double ServiceCharges { get; set; } public double TotalCharges { get; set; } public double LocalTaxes { get; set; } public double StateTaxes { get; set; } public DateTime PaymentDueDate { get; set; } public double AmountDue { get; set; } public DateTime LatePaymentDueDate { get; set; } public DateTime LateAmountDue { get; set; } } }
namespace StellarWaterPoint31.Models { internal class WaterBillManager { internal (double a, double b, double c) CalculateTiers(string acnt, double total) { (double tier1, double tier2, double tier3) results = (0.00, 0.00, 0.00); if (acnt == "RES - Residential Household") { results.tier1 = total * 41.50 / 10000.00; results.tier2 = total * 32.50 / 10000.00; results.tier3 = total * 26.00 / 10000.00; } else if (acnt == "SGO - Social/Government/Non-Profit Organization") { results.tier1 = total * 46.00 / 10000.00; results.tier2 = total * 50.00 / 10000.00; results.tier3 = total * 4.00 / 10000.00; } else if (acnt == "BUS - General Business") { results.tier1 = total * 45.00 / 10000.00; results.tier2 = total * 30.00 / 10000.00; results.tier3 = total * 25.00 / 10000.00; } else if (acnt == "UUO - Unidentified or Unclassified Type of Organization") { results.tier1 = total * 25.00 / 10000.00; results.tier2 = total * 35.00 / 10000.00; results.tier3 = total * 40.00 / 10000.00; } else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc") { results.tier1 = total * 50.00 / 10000.00; results.tier2 = total * 40.00 / 10000.00; results.tier3 = total * 10.00 / 10000.00; } else { results.tier1 = total * (48.00 / 10000.00); results.tier2 = total * (32.00 / 10000.00); results.tier3 = total * (20.00 / 10000.00); } return results; } internal double CalculateSewerCharges(string acnt, double total) { double result; if (acnt == "RES - Residential Household") { result = total * 6.826941 / 100.00; } else if (acnt == "SGO - Social/Government/Non-Profit Organization") { result = total * 4.162522 / 100.00; } else if (acnt == "BUS - General Business") { result = total * 8.315136 / 100.00; } else if (acnt == "UUO - Unidentified or Unclassified Type of Organization") { result = total * 10.626147 / 100.00; } else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc") { result = total * 12.025135 / 100.00; } else // if (acnt == "OTH - Other") { result = total * 9.202615 / 100.00; } return result; } internal double CalculateEnvironmentCharges(string acnt, double total) { double result; switch (acnt) { case "RES - Residential Household": result = total * 0.022724; break; case "SGO - Social/Government/Non-Profit Organization": result = total * 0.118242; break; case "BUS - General Business": result = total * 0.161369; break; case "UUO - Unidentified or Unclassified Type of Organization": result = total * 0.082477; break; case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc": result = total * 0.413574; break; default: result = total * 0.221842; break; } return result; } internal double CalculateServiceCharges(string acnt, double total) { switch (acnt) { case "RES - Residential Household": return total * 0.145748; case "SGO - Social/Government/Non-Profit Organization": return total * 0.102246; case "BUS - General Business": return total * 0.242627; case "UUO - Unidentified or Unclassified Type of Organization": return total * 0.186692; case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc": return total * 0.412628; default: return total * 0.210248; } } internal double CalculateLocalTaxes(string acnt, double total) => acnt switch { "RES - Residential Household" => total * 0.031574, "SGO - Social/Government/Non-Profit Organization" => total * 0.035026, "BUS - General Business" => total * 0.122517, "UUO - Unidentified or Unclassified Type of Organization" => total * 0.105737, "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => total * 0.153248, _ => total * 0.125148 }; internal double CalculateStateTaxes(string acnt, double total) => acnt switch { "RES - Residential Household" => total * 0.017242, "SGO - Social/Government/Non-Profit Organization" => total * 0.008779, "BUS - General Business" => total * 0.042448, "UUO - Unidentified or Unclassified Type of Organization" => total * 0.067958, "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => total * 0.081622, _ => total * 0.013746 }; internal DateTime SetPaymentDueDate(string acnt, DateTime date) { TimeSpan tsPaymentDueDate = new TimeSpan(1, 0, 0, 0); if (acnt == "RES - Residential Household") { tsPaymentDueDate = new TimeSpan(15, 0, 0, 0); } else if (acnt == "SGO - Social/Government/Non-Profit Organization") { tsPaymentDueDate = new TimeSpan(20, 0, 0, 0); } else if (acnt == "BUS - General Business") { tsPaymentDueDate = new TimeSpan(30, 0, 0, 0); } else if (acnt == "UUO - Unidentified or Unclassified Type of Organization") { tsPaymentDueDate = new TimeSpan(25, 0, 0, 0); } else if (acnt == "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc") { tsPaymentDueDate = new TimeSpan(40, 0, 0, 0); } else { tsPaymentDueDate = new TimeSpan(35, 0, 0, 0); } return date + tsPaymentDueDate; } internal DateTime SetLatePaymentDueDate(string acnt, DateTime date) { switch (acnt) { case "RES - Residential Household": return date + new TimeSpan(30, 0, 0, 0); case "SGO - Social/Government/Non-Profit Organization": return date + new TimeSpan(40, 0, 0, 0); case "BUS - General Business": return date + new TimeSpan(50, 0, 0, 0); case "UUO - Unidentified or Unclassified Type of Organization": return date + new TimeSpan(60, 0, 0, 0); case "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc": return date + new TimeSpan(65, 0, 0, 0); default: return date + new TimeSpan(45, 0, 0, 0); } } internal double CalculateLateAmountDue(string acnt, double amt) => acnt switch { "RES - Residential Household" => amt + 8.95, "SGO - Social/Government/Non-Profit Organization" => amt + (amt / 4.575), "BUS - General Business" => amt + (amt / 12.315), "UUO - Unidentified or Unclassified Type of Organization" => amt + (amt / 7.425), "WAT - Water Intensive Business(Laudromat, Hair Salon, Restaurant, etc" => amt + (amt / 15.225), _ => amt + (amt / 6.735) }; } }
Showing Water Bills
.
Practical Learning: Showing Water Bills
(Name) | Text | TextAlign | Width |
colWaterBillId | Id | 40 | |
colBillNumber | Bill # | Center | 80 |
colAccountSummary | Account Summary | Center | 550 |
colStartDate | Start Date | Center | 150 |
colEndDate | End Date | Center | 150 |
colBillingDays | Days | Center | |
colCounterStart | Counter Start | Right | 125 |
colCounterEnd | Counter End | Right | 125 |
colTotalHCF | Total HCF | Right | 100 |
colGallons | Gallons | Right | 95 |
colPaymentDueDate | Pmt Due Date | Center | 125 |
colAmountDue | Amt Due | Right | 90 |
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwWaterBills | FullRowSelect: True GridLines: True View: Details Anchor: Top, Bottom, Left, Right |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterBills { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowWaterBills() { // We will need the list of customers to display an account summary in the list view // Declare a strCustomers variable that will hold the records from the JSON file string strCustomers = string.Empty; // Prepare a list to hold the records of customers List<Customer> clients = new List<Customer>(); // Specify the file that holds the records of customers string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; // Create a FileInfo object for the records of customers FileInfo fiCustomers = new FileInfo(fileCustomers); // Check if a file that holds the records of customers was created already if (fiCustomers.Exists == true) { // If that file exists, create a TextReader object to get those records using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { // Read the records and store them in the strCustomers variable strCustomers = File.ReadAllText(fiCustomers.FullName); // Use JSON deserialization to get the records of customers clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; } } // Declare a strWaterBills variable that will hold the records from the JSON file string strWaterBills = string.Empty; // Prepare a bills list to hold the records of water bills List<WaterBill> bills = new List<WaterBill>(); // Specify the file that holds the records of water bills string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; // Create a FileInfo object for the records of water bills FileInfo fiWaterBills = new FileInfo(fileWaterBills); // Find out whether a file that holds the records of water bills exists already if (fiWaterBills.Exists == true) { // If that file exists, create a TextReader object to read the file using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName)) { /* Read the contents of the file that holds the water bills. * Store the read text in the strWaterBills variable that was declared. */ strWaterBills = File.ReadAllText(fiWaterBills.FullName); // Get the water bills and store them in the bills list variable bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; // This variable will be used to keep count of the water bills records int counter = 1; /* We are about to display the water bills records in the list view. * Before proceeding, first remove any record in the list view. */ lvwWaterBills.Items.Clear(); // Visit each record of the water bills foreach (WaterBill client in bills) { /* Prepare a ListViewItem object for each record. * Display the record counter in the first column of this object. */ ListViewItem lviWaterBill = new ListViewItem(counter++.ToString()); // Display the water bill number of the current record lviWaterBill.SubItems.Add(client.BillNumber.ToString()); /* Refer to the list of records of the customers (that list was prepared earlier). * Use LINQ to get the customer whose account number is * the same as the account number of the current record. */ IEnumerable<Customer> customer = clients.Where(cust => cust.AccountNumber == client.AccountNumber); // Now that we have located the customer record, display some details about it foreach (Customer cust in customer) { lviWaterBill.SubItems.Add(client.AccountNumber + " - " + cust.AccountName + ", Type: " + cust.AccountType![..3] + ", (Mtr #: " + cust.MeterNumber + ")"); } /* Continue displaying some other parts of the water bill. * Before there is not enough space for the whole water bill, we display only some values. */ lviWaterBill.SubItems.Add(client.MeterReadingStartDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.MeterReadingEndDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.BillingDays.ToString()); lviWaterBill.SubItems.Add(client.CounterReadingStart.ToString()); lviWaterBill.SubItems.Add(client.CounterReadingEnd.ToString()); lviWaterBill.SubItems.Add(client.TotalHCF.ToString()); lviWaterBill.SubItems.Add(client.TotalGallons.ToString()); lviWaterBill.SubItems.Add(client.PaymentDueDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.AmountDue.ToString()); lvwWaterBills.Items.Add(lviWaterBill); } } } } private void Central_Load(object sender, EventArgs e) { ShowWaterBills(); } } }
Control | (Name) | Text | Font | |
Button | ![]() |
btnWaterBills | C&Water Bills... | Times New Roman, 24pt, style=Bold |
Button | ![]() |
btnClose | &Close | Times New Roman, 24pt, style=Bold |
namespace StellarWaterPoint31 { public partial class WaterDistribution : Form { public WaterDistribution() { InitializeComponent(); } private void btnWaterBills_Click(object o, EventArgs e) { WaterBills.Central central = new(); central.Show(); } private void btnCustomers_Click(object o, EventArgs e) { Customers.Central clients = new(); clients.ShowDialog(); } private void btnWaterMeters_Click(object o, EventArgs e) { WaterMeters.Central central = new WaterMeters.Central(); central.ShowDialog(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
A New Water Bill
.
Practical Learning: Processing a Water Bill
Control | Text | Name | Other Properties | |
Label | ![]() |
&Water Bill #: | ||
TextBox | ![]() |
txtBillNumber | ||
GroupBox | ![]() |
Customer Information | ||
Label | ![]() |
&Account #: | ||
MaskedTextBox | ![]() |
mtbAccountNumber | Mask: 0000-000-0000 | |
Button | ![]() |
Find Customer &Account | btnFindCustomerAccount | |
Label | ![]() |
Account Name: | ||
TextBox | ![]() |
txtAccountName | ||
Label | ![]() |
Account Type: | ||
TextBox | ![]() |
txtAccountType | ||
Label | ![]() |
Address: | ||
TextBox | ![]() |
txtAddress | ||
TextBox | ![]() |
txtCity | ||
TextBox | ![]() |
txtCounty | ||
TextBox | ![]() |
txtState | ||
TextBox | ![]() |
txtZIPCode | ||
Label | ![]() |
_________________________________________________ | ||
Label | ![]() |
Meter Details: | ||
TextBox | ![]() |
txtMeterDetails | ||
GroupBox | ![]() |
Meter Reading | ||
Label | ![]() |
Meter &Reading Start Date: | ||
Date Time Picker | ![]() |
dtpMeterReadingStartDate | ||
Label | ![]() |
Meter Reading &End Date: | ||
Date Time Picker | ![]() |
dtpMeterReadingEndDate | ||
Label | ![]() |
Coun&ter Reading Start: | ||
TextBox | ![]() |
txtCounterReadingStart | ||
Label | ![]() |
Counter Readi&ng End: | ||
TextBox | ![]() |
txtCounterReadingEnd | ||
Button | ![]() |
&Evaluate Water Bill | btnEvaluateWaterBill | Times New Roman, 24pt, style=Bold |
GroupBox | ![]() |
Meter Result | ||
Label | ![]() |
Billing Days: | ||
TextBox | ![]() |
txtBillingDays | ||
Label | ![]() |
Total HCF: | ||
TextBox | ![]() |
txtTotalHCF | ||
Label | ![]() |
Total Gallons: | ||
TextBox | ![]() |
txtTotalGallons | ||
Label | ![]() |
First Tier Consumption: | ||
TextBox | ![]() |
txtFirstTierConsumption | ||
Label | ![]() |
Second Tier: | ||
TextBox | ![]() |
txtSecondTierConsumption | ||
Label | ![]() |
Last Tier: | ||
TextBox | ![]() |
txtLastTierConsumption | ||
GroupBox | ![]() |
Consumption Charges | ||
Label | ![]() |
Water Charges: | ||
TextBox | ![]() |
txtWaterCharges | ||
Label | ![]() |
Sewer Charges: | ||
TextBox | ![]() |
txtSewerCharges | ||
Label | ![]() |
Environment Charges: | ||
TextBox | ![]() |
txtEnvironmentCharges | ||
Label | ![]() |
Service Charges: | ||
TextBox | ![]() |
txtServiceCharges | ||
Label | ![]() |
Total Charges: | ||
TextBox | ![]() |
txtTotalCharges | ||
GroupBox | ![]() |
Taxes | ||
Label | ![]() |
Local Taxes: | ||
TextBox | ![]() |
txtLocalTaxes | ||
Label | ![]() |
State Taxes: | ||
TextBox | ![]() |
txtStateTaxes | ||
GroupBox | ![]() |
Water Bill Payment | ||
Label | ![]() |
Payment Due Date: | ||
Date Time Picker | ![]() |
dtpPaymentDueDate | ||
Label | ![]() |
Amount Due: | ||
TextBox | ![]() |
txtAmountDue | ||
Label | ![]() |
Late Payment Due Date: | ||
Date Time Picker | ![]() |
dtpLatePaymentDueDate | ||
Label | ![]() |
&Late Amount Due: | ||
TextBox | ![]() |
txtLateAmountDue | ||
Button | ![]() |
Save Water Bill | btnSaveWaterBill | |
Button | ![]() |
&Close | btnClose |
Form Properties
Form Property | Value |
FormBorderStyle | FixedDialog |
Text | Stellar Water Point - Water Bill Processing |
StartPosition | CenterScreen |
MaximizeBox | False |
MinimizeBox | False |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterBills
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbAccountNumber.Text))
{
MessageBox.Show("You must type a valid account number of a customer, " +
"and then click the Find Customer Account button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
string? strMeterNumber = string.Empty;
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text);
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
}
}
}
if (strMeterNumber!.Length > 0)
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber);
foreach (WaterMeter meter in meters)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.WaterBills
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
}
}
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterBills
{
public partial class Create : Form
{
public Create()
{
InitializeComponent();
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MessageBox.Show("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MessageBox.Show("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterBills { public partial class Create : Form { public Create() { InitializeComponent(); } private void btnFindCustomerAccount_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("You must type a valid account number of a customer, " + "and then click the Find Customer Account button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); string? strMeterNumber = string.Empty; if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text); foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; strMeterNumber = client.MeterNumber; txtAccountType.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; txtZIPCode.Text = client.ZIPCode; } } } if (strMeterNumber!.Length > 0) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber); foreach (WaterMeter meter in meters) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e) { TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value; txtBillingDays.Text = (tsDays.Days + 1).ToString(); } private void btnEvaluateWaterBill_Click(object sender, EventArgs e) { double counterStart = 0, counterEnd = 0; try { counterStart = double.Parse(txtCounterReadingStart.Text); } catch (FormatException feCRStart) { MessageBox.Show("You must enter a valid value in the Counter Reading Start text box. " + "The error produced is: " + feCRStart.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } try { counterEnd = double.Parse(txtCounterReadingEnd.Text); } catch (FormatException feCREnd) { MessageBox.Show("You must enter a valid value in the Counter Reading End text box. " + "The error produced is: " + feCREnd.Message, "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } double consumption = counterEnd - counterStart; double gallons = consumption * 748.05; string strAccountType = txtAccountType.Text[..3]; (double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons); double waterCharges = tiers.first + tiers.second + tiers.last; double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges); double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges); double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges); double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges; double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges); double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges); double amtDue = totalCharges + localTaxes + stateTaxes; txtTotalHCF.Text = consumption.ToString(); txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString(); txtFirstTierConsumption.Text = tiers.first.ToString("F"); txtSecondTierConsumption.Text = tiers.second.ToString("F"); txtLastTierConsumption.Text = tiers.last.ToString("F"); txtWaterCharges.Text = waterCharges.ToString("F"); txtSewerCharges.Text = sewerCharges.ToString("F"); txtEnvironmentCharges.Text = envCharges.ToString("F"); txtServiceCharges.Text = srvCharges.ToString("F"); txtTotalCharges.Text = totalCharges.ToString("F"); txtLocalTaxes.Text = localTaxes.ToString("F"); txtStateTaxes.Text = stateTaxes.ToString("F"); dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value); txtAmountDue.Text = amtDue.ToString("F"); dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value); txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F"); } private void btnSaveWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtBillNumber.Text)) { MessageBox.Show("You must type a (unique) bill number for the " + "water bill you are processing.", "Stellar Water Point", MessageBoxButtons.OK); return; } if (string.IsNullOrEmpty(mtbAccountNumber.Text)) { MessageBox.Show("You must specify the account number of the customer " + "whose water bill you are preparing.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterBills = string.Empty; List<WaterBill> bills = new List<WaterBill>(); string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; FileInfo fiWaterBills = new FileInfo(fileWaterBills); if (fiWaterBills.Exists == true) { strWaterBills = File.ReadAllText(fiWaterBills.FullName); bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; } WaterBill bill = new() { BillNumber = int.Parse(txtBillNumber.Text), AccountNumber = mtbAccountNumber.Text, MeterReadingStartDate = dtpMeterReadingStartDate.Value, MeterReadingEndDate = dtpMeterReadingEndDate.Value, BillingDays = int.Parse(txtBillingDays.Text), CounterReadingStart = int.Parse(txtCounterReadingStart.Text), CounterReadingEnd = int.Parse(txtCounterReadingEnd.Text), TotalHCF = int.Parse(txtTotalHCF.Text), TotalGallons = int.Parse(txtTotalGallons.Text), FirstTierConsumption = double.Parse(txtFirstTierConsumption.Text), SecondTierConsumption = double.Parse(txtSecondTierConsumption.Text), LastTierConsumption = double.Parse(txtLastTierConsumption.Text), WaterCharges = double.Parse(txtWaterCharges.Text), SewerCharges = double.Parse(txtSewerCharges.Text), EnvironmentCharges = double.Parse(txtEnvironmentCharges.Text), ServiceCharges = double.Parse(txtServiceCharges.Text), TotalCharges = double.Parse(txtTotalCharges.Text), LocalTaxes = double.Parse(txtLocalTaxes.Text), StateTaxes = double.Parse(txtStateTaxes.Text), PaymentDueDate = dtpPaymentDueDate.Value, AmountDue = double.Parse(txtAmountDue.Text), LatePaymentDueDate = dtpLatePaymentDueDate.Value, LateAmountDue = double.Parse(txtLateAmountDue.Text) }; bills.Add(bill); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsWaterBills = JsonSerializer.Serialize(bills, typeof(List<WaterBill>), options); File.WriteAllText(fiWaterBills.FullName, jsWaterBills); Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwWaterBills | No Change |
Button | ![]() |
btnProcessWaterBill | &Process Water Bill... |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterBills();
}
}
}
Water Bill # | Account # | Reading Start Date | Reading End Date | Counter Reading Start | Counter Reading End |
451474 | 2068-258-9486 | 01/11/2010 | 04/12/2010 | 103943 | 103956 |
923633 | 5293-957-3395 | 01/17/2010 | 08/18/2010 | 256945 | 256972 |
917829 | 9279-570-8394 | 02/15/2010 | 05/14/2010 | 5205 | 5222 |
202666 | 6986-829-3741 | 03/08/2010 | 06/06/2010 | 5679 | 5690 |
Water Bill Details
.
Practical Learning: Showing a Water Bill
Control | Text | Name | Other Properties | ||
Label | ![]() |
&Water Bill #: | |||
TextBox | ![]() |
txtBillNumber | |||
Button | ![]() |
&Find Water Bill | btnFindWaterBill | ||
GroupBox | ![]() |
Customer Information | |||
Label | ![]() |
&Account #: | |||
TextBox | ![]() |
txtAccountNumber | Enabled: False | ||
Label | ![]() |
Account Name: | |||
TextBox | ![]() |
txtAccountName | Enabled: False | ||
Label | ![]() |
Account Type: | |||
TextBox | ![]() |
txtAccountType | |||
Label | ![]() |
Address: | Enabled: False | ||
TextBox | ![]() |
txtAddress | Enabled: False | ||
TextBox | ![]() |
txtCity | |||
TextBox | ![]() |
txtCounty | Enabled: False | ||
TextBox | ![]() |
txtState | Enabled: False | ||
TextBox | ![]() |
txtZIPCode | Enabled: False | ||
Label | ![]() |
_________________________________________________ | |||
Label | ![]() |
Meter Details: | |||
TextBox | ![]() |
txtMeterDetails | Enabled: False | ||
GroupBox | ![]() |
Meter Reading | |||
Label | ![]() |
Meter &Reading Start Date: | |||
Text Box | ![]() |
txtMeterReadingStartDate | Enabled: False | ||
Label | ![]() |
Meter Reading &End Date: | |||
Text Box | ![]() |
txtMeterReadingEndDate | Enabled: False | ||
Label | ![]() |
Coun&ter Reading Start: | |||
TextBox | ![]() |
txtCounterReadingStart | Enabled: False | ||
Label | ![]() |
Counter Readi&ng End: | |||
TextBox | ![]() |
txtCounterReadingEnd | Enabled: False | ||
GroupBox | ![]() |
Meter Result | |||
Label | ![]() |
Billing Days: | |||
TextBox | ![]() |
txtBillingDays | Enabled: False | ||
Label | ![]() |
Total HCF: | |||
TextBox | ![]() |
txtTotalHCF | Enabled: False | ||
Label | ![]() |
Total Gallons: | |||
TextBox | ![]() |
txtTotalGallons | Enabled: False | ||
Label | ![]() |
First Tier Consumption: | |||
TextBox | ![]() |
txtFirstTierConsumption | Enabled: False | ||
Label | ![]() |
Second Tier: | |||
TextBox | ![]() |
txtSecondTierConsumption | Enabled: False | ||
Label | ![]() |
Last Tier: | |||
TextBox | ![]() |
txtLastTierConsumption | Enabled: False | ||
GroupBox | ![]() |
Consumption Charges | |||
Label | ![]() |
Water Charges: | |||
TextBox | ![]() |
txtWaterCharges | Enabled: False | ||
Label | ![]() |
Sewer Charges: | |||
TextBox | ![]() |
txtSewerCharges | Enabled: False | ||
Label | ![]() |
Environment Charges: | |||
TextBox | ![]() |
txtEnvironmentCharges | Enabled: False | ||
Label | ![]() |
Service Charges: | |||
TextBox | ![]() |
txtServiceCharges | Enabled: False | ||
Label | ![]() |
Total Charges: | |||
TextBox | ![]() |
txtTotalCharges | Enabled: False | ||
GroupBox | ![]() |
Taxes | |||
Label | ![]() |
Local Taxes: | |||
TextBox | ![]() |
txtLocalTaxes | Enabled: False | ||
Label | ![]() |
State Taxes: | |||
TextBox | ![]() |
txtStateTaxes | Enabled: False | ||
GroupBox | ![]() |
Water Bill Payment | |||
Label | ![]() |
Payment Due Date: | |||
Date Time Picker | ![]() |
dtpPaymentDueDate | Enabled: False | ||
Label | ![]() |
Amount Due: | |||
TextBox | ![]() |
txtAmountDue | Enabled: False | ||
Label | ![]() |
Late Payment Due Date: | |||
Text Box | ![]() |
txtLatePaymentDueDate | Enabled: False | ||
Label | ![]() |
&Late Amount Due: | |||
TextBox | ![]() |
txtLateAmountDue | |||
Button | ![]() |
&Close | btnClose |
Form Properties
Form Property | Value |
FormBorderStyle | FixedDialog |
Text | Stellar Water Point - Water Bill Processing |
StartPosition | CenterScreen |
MaximizeBox | False |
MinimizeBox | False |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterBills { public partial class Details : Form { public Details() { InitializeComponent(); } private void btnFindWaterBill_Click(object sender, EventArgs e) { string strWaterBills = string.Empty; // Prepare a bills list to hold the records of water bills List<WaterBill> bills = new List<WaterBill>(); // Specify the file that holds the records of water bills string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; // Create a FileInfo object for the records of water bills FileInfo fiWaterBills = new FileInfo(fileWaterBills); // Find out whether a file that holds the records of water bills exists already if (fiWaterBills.Exists == true) { // If that file exists, create a TextReader object to read the file using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName)) { /* Read the contents of the file that holds the water bills. * Store the read text in the strWaterBills variable that was declared. */ strWaterBills = File.ReadAllText(fiWaterBills.FullName); // Get the water bills and store them in the bills list variable bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text)); foreach (var bill in selected) { txtAccountNumber.Text = bill.AccountNumber; txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString(); txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString(); txtBillingDays.Text = bill.BillingDays.ToString(); txtCounterReadingStart.Text = bill.CounterReadingStart.ToString(); txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString(); txtTotalHCF.Text = bill.TotalHCF.ToString(); txtTotalGallons.Text = bill.TotalGallons.ToString(); txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString(); txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString(); txtLastTierConsumption.Text = bill.LastTierConsumption.ToString(); txtWaterCharges.Text = bill.WaterCharges.ToString(); txtSewerCharges.Text = bill.SewerCharges.ToString(); txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString(); txtServiceCharges.Text = bill.ServiceCharges.ToString(); txtTotalCharges.Text = bill.TotalCharges.ToString(); txtLocalTaxes.Text = bill.LocalTaxes.ToString(); txtStateTaxes.Text = bill.StateTaxes.ToString(); txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString(); txtAmountDue.Text = bill.AmountDue.ToString(); txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString(); txtLateAmountDue.Text = bill.LateAmountDue.ToString(); } } } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); string? strMeterNumber = string.Empty; if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = from consumer in clients where consumer.AccountNumber == txtAccountNumber.Text select consumer; foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; strMeterNumber = client.MeterNumber; txtAccountType.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; txtZIPCode.Text = client.ZIPCode; } } } if (strMeterNumber!.Length > 0) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; IEnumerable<WaterMeter> meters = from measure in waterMeters where measure.MeterNumber == strMeterNumber select measure; foreach (WaterMeter meter in waterMeters) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Other Properties | |
ListView | ![]() |
lvwWaterBills | No Change |
Button | ![]() |
No Change | No Change |
Button | ![]() |
btnWaterBillDetails | Water Bill &Details... Anchor: Bottom, Right |
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.WaterBills
{
public partial class Central : Form
{
public Central()
{
InitializeComponent();
}
private void ShowWaterBills()
{
. . .
}
private void Central_Load(object sender, EventArgs e)
{
ShowWaterBills();
}
private void btnProcessWaterBill_Click(object sender, EventArgs e)
{
Create create = new();
create.ShowDialog();
ShowWaterBills();
}
private void btnViewWaterBill_Click(object sender, EventArgs e)
{
Details details = new();
details.Show();
}
}
}
Water Bill Edition
.
Practical Learning: Creating a Water Bill Editor
Control | (Name) | Text | Additional Properties | |
Button | ![]() |
btnFindWaterBill | &Find | |
Button | ![]() |
btnUpdateWaterBill | &Update Water Bill |
using StellarWaterPoint4.Models;
using System.Text.Json;
namespace StellarWaterPoint4.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
string strWaterBills = string.Empty;
List<WaterBill> bills = new List<WaterBill>();
string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
FileInfo fiWaterBills = new FileInfo(fileWaterBills);
if (fiWaterBills.Exists == true)
{
using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
{
strWaterBills = File.ReadAllText(fiWaterBills.FullName);
bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
foreach (var bill in selected)
{
mtbAccountNumber.Text = bill.AccountNumber;
dtpMeterReadingStartDate.Value = bill.MeterReadingStartDate;
dtpMeterReadingEndDate.Value = bill.MeterReadingEndDate;
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
dtpPaymentDueDate.Value = bill.PaymentDueDate;
txtAmountDue.Text = bill.AmountDue.ToString();
dtpLatePaymentDueDate.Value = bill.LatePaymentDueDate;
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
}
}
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
string? strMeterNumber = string.Empty;
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = from consumer
in clients
where consumer.AccountNumber == mtbAccountNumber.Text
select consumer;
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
}
}
}
if (strMeterNumber!.Length > 0)
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
IEnumerable<WaterMeter> meters = from measure
in waterMeters
where measure.MeterNumber == strMeterNumber
select measure;
foreach (WaterMeter meter in waterMeters)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using StellarWaterPoint4.Models;
using System.Text.Json;
namespace StellarWaterPoint4.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
. . .
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mtbAccountNumber.Text))
{
MessageBox.Show("You must type a valid account number of a customer, " +
"and then click the Find Customer Account button.",
"Stellar Water Point", MessageBoxButtons.OK);
return;
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
string? strMeterNumber = string.Empty;
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = clients.Where(cust => cust.AccountNumber == mtbAccountNumber.Text);
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
}
}
}
if (strMeterNumber!.Length > 0)
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
IEnumerable<WaterMeter> meters = waterMeters.Where(wm => wm.MeterNumber == strMeterNumber);
foreach (WaterMeter meter in meters)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using System.Data;
using Microsoft.Data.SqlClient;
namespace StellarWaterPoint31.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
. . .
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
}
}
using StellarWaterPoint4.Models;
using System.Text.Json;
namespace StellarWaterPoint4.WaterBills
{
public partial class Editor : Form
{
public Editor()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
. . .
}
private void btnFindCustomerAccount_Click(object sender, EventArgs e)
{
. . .
}
private void dtpMeterReadingEndDate_ValueChanged(object sender, EventArgs e)
{
TimeSpan tsDays = dtpMeterReadingEndDate.Value - dtpMeterReadingStartDate.Value;
txtBillingDays.Text = (tsDays.Days + 1).ToString();
}
private void btnEvaluateWaterBill_Click(object sender, EventArgs e)
{
double counterStart = 0, counterEnd = 0;
try
{
counterStart = double.Parse(txtCounterReadingStart.Text);
}
catch (FormatException feCRStart)
{
MessageBox.Show("You must enter a valid value in the Counter Reading Start text box. " +
"The error produced is: " + feCRStart.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
counterEnd = double.Parse(txtCounterReadingEnd.Text);
}
catch (FormatException feCREnd)
{
MessageBox.Show("You must enter a valid value in the Counter Reading End text box. " +
"The error produced is: " + feCREnd.Message,
"Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
double consumption = counterEnd - counterStart;
double gallons = consumption * 748.05;
string strAccountType = txtAccountType.Text[..3];
(double first, double second, double last) tiers = WaterBillManager.CalculateTiers(strAccountType, gallons);
double waterCharges = tiers.first + tiers.second + tiers.last;
double sewerCharges = WaterBillManager.CalculateSewerCharges(strAccountType, waterCharges);
double envCharges = WaterBillManager.CalculateEnvironmentCharges(strAccountType, waterCharges);
double srvCharges = WaterBillManager.CalculateServiceCharges(strAccountType, waterCharges);
double totalCharges = waterCharges + sewerCharges + envCharges + srvCharges;
double localTaxes = WaterBillManager.CalculateLocalTaxes(strAccountType, waterCharges);
double stateTaxes = WaterBillManager.CalculateStateTaxes(strAccountType, waterCharges);
double amtDue = totalCharges + localTaxes + stateTaxes;
txtTotalHCF.Text = consumption.ToString();
txtTotalGallons.Text = ((int)(Math.Ceiling(gallons))).ToString();
txtFirstTierConsumption.Text = tiers.first.ToString("F");
txtSecondTierConsumption.Text = tiers.second.ToString("F");
txtLastTierConsumption.Text = tiers.last.ToString("F");
txtWaterCharges.Text = waterCharges.ToString("F");
txtSewerCharges.Text = sewerCharges.ToString("F");
txtEnvironmentCharges.Text = envCharges.ToString("F");
txtServiceCharges.Text = srvCharges.ToString("F");
txtTotalCharges.Text = totalCharges.ToString("F");
txtLocalTaxes.Text = localTaxes.ToString("F");
txtStateTaxes.Text = stateTaxes.ToString("F");
dtpPaymentDueDate.Value = WaterBillManager.SetPaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtAmountDue.Text = amtDue.ToString("F");
dtpLatePaymentDueDate.Value = WaterBillManager.SetLatePaymentDueDate(strAccountType, dtpMeterReadingEndDate.Value);
txtLateAmountDue.Text = WaterBillManager.CalculateLateAmountDue(strAccountType, amtDue).ToString("F");
}
}
}
Close();
Control | (Name) | Text | Other Properties | |
ListView | ![]() |
lvwWaterBills | No Change | |
Button | ![]() |
btnNewWaterBill | No Change | |
Button | ![]() |
btnViewWaterBill | No Change | |
Button | ![]() |
btnEditWaterBill | &Edit Water Bill... | Anchor: Bottom, Right |
using System.Data; using Microsoft.Data.SqlClient; namespace StellarWaterPoint31.WaterBills { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowWaterBills() { . . . } private void Central_Load(object sender, EventArgs e) { ShowWaterBills(); } private void btnProcessWaterBill_Click(object sender, EventArgs e) { Create create = new(); create.ShowDialog(); ShowWaterBills(); } private void btnViewWaterBill_Click(object sender, EventArgs e) { Details details = new(); details.Show(); } private void btnEditWaterBill_Click(object sender, EventArgs e) { } } }
Account #: 9249-379-6848 and click Find Customer Account Meter Reading Start Date: 1/19/2010 Meter Reading End Date: 4/17/2010 Counter Reading Start: 256953 Counter Reading End: 256966
.
Practical Learning: Editing/Updating a Record
Control | (Name) | Text | |
Button | ![]() |
btnDeleteWaterBill | &Delete Water Bill |
using System.Text.Json;
using StellarWaterPoint4.Models;
namespace StellarWaterPoint4.WaterBills
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void btnFindWaterBill_Click(object sender, EventArgs e)
{
string strWaterBills = string.Empty;
List<WaterBill> bills = new List<WaterBill>();
string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json";
FileInfo fiWaterBills = new FileInfo(fileWaterBills);
if (fiWaterBills.Exists == true)
{
using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName))
{
strWaterBills = File.ReadAllText(fiWaterBills.FullName);
bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!;
IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text));
foreach (var bill in selected)
{
txtAccountNumber.Text = bill.AccountNumber;
txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString();
txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString();
txtBillingDays.Text = bill.BillingDays.ToString();
txtCounterReadingStart.Text = bill.CounterReadingStart.ToString();
txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString();
txtTotalHCF.Text = bill.TotalHCF.ToString();
txtTotalGallons.Text = bill.TotalGallons.ToString();
txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString();
txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString();
txtLastTierConsumption.Text = bill.LastTierConsumption.ToString();
txtWaterCharges.Text = bill.WaterCharges.ToString();
txtSewerCharges.Text = bill.SewerCharges.ToString();
txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString();
txtServiceCharges.Text = bill.ServiceCharges.ToString();
txtTotalCharges.Text = bill.TotalCharges.ToString();
txtLocalTaxes.Text = bill.LocalTaxes.ToString();
txtStateTaxes.Text = bill.StateTaxes.ToString();
txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString();
txtAmountDue.Text = bill.AmountDue.ToString();
txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString();
txtLateAmountDue.Text = bill.LateAmountDue.ToString();
}
}
}
string strCustomers = string.Empty;
List<Customer> clients = new List<Customer>();
string fileCustomers = @"E:\Stellar Water Point2\Customers.json";
FileInfo fiCustomers = new FileInfo(fileCustomers);
string? strMeterNumber = string.Empty;
if (fiCustomers.Exists == true)
{
using (TextReader trCustomers = new StreamReader(fiCustomers.FullName))
{
strCustomers = File.ReadAllText(fiCustomers.FullName);
clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!;
IEnumerable<Customer> customers = from consumer
in clients
where consumer.AccountNumber == txtAccountNumber.Text
select consumer;
foreach (Customer client in customers)
{
txtAccountName.Text = client.AccountName;
strMeterNumber = client.MeterNumber;
txtAccountType.Text = client.AccountType;
txtAddress.Text = client.Address;
txtCity.Text = client.City;
txtCounty.Text = client.County;
txtState.Text = client.State;
txtZIPCode.Text = client.ZIPCode;
}
}
}
if (strMeterNumber!.Length > 0)
{
string strWaterMeters = string.Empty;
List<WaterMeter> waterMeters = new List<WaterMeter>();
string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json";
FileInfo fiWaterMeters = new FileInfo(fileWaterMeters);
if (fiWaterMeters.Exists == true)
{
strWaterMeters = File.ReadAllText(fiWaterMeters.FullName);
waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!;
IEnumerable<WaterMeter> meters = from measure
in waterMeters
where measure.MeterNumber == strMeterNumber
select measure;
foreach (WaterMeter meter in waterMeters)
{
txtMeterDetails.Text = meter.Make + " " +
meter.Model +
" (Meter Size: " + meter.MeterSize + ")";
}
}
}
}
}
}
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterBills { public partial class Delete : Form { public Delete() { InitializeComponent(); } private void btnFindWaterBill_Click(object sender, EventArgs e) { string strWaterBills = string.Empty; // Prepare a bills list to hold the records of water bills List<WaterBill> bills = new List<WaterBill>(); // Specify the file that holds the records of water bills string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; // Create a FileInfo object for the records of water bills FileInfo fiWaterBills = new FileInfo(fileWaterBills); // Find out whether a file that holds the records of water bills exists already if (fiWaterBills.Exists == true) { // If that file exists, create a TextReader object to read the file using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName)) { /* Read the contents of the file that holds the water bills. * Store the read text in the strWaterBills variable that was declared. */ strWaterBills = File.ReadAllText(fiWaterBills.FullName); // Get the water bills and store them in the bills list variable bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; IEnumerable<WaterBill> selected = bills.Where(wb => wb.BillNumber == int.Parse(txtBillNumber.Text)); foreach (var bill in selected) { txtAccountNumber.Text = bill.AccountNumber; txtMeterReadingStartDate.Text = bill.MeterReadingStartDate.ToLongDateString(); txtMeterReadingEndDate.Text = bill.MeterReadingEndDate.ToLongDateString(); txtBillingDays.Text = bill.BillingDays.ToString(); txtCounterReadingStart.Text = bill.CounterReadingStart.ToString(); txtCounterReadingEnd.Text = bill.CounterReadingEnd.ToString(); txtTotalHCF.Text = bill.TotalHCF.ToString(); txtTotalGallons.Text = bill.TotalGallons.ToString(); txtFirstTierConsumption.Text = bill.FirstTierConsumption.ToString(); txtSecondTierConsumption.Text = bill.SecondTierConsumption.ToString(); txtLastTierConsumption.Text = bill.LastTierConsumption.ToString(); txtWaterCharges.Text = bill.WaterCharges.ToString(); txtSewerCharges.Text = bill.SewerCharges.ToString(); txtEnvironmentCharges.Text = bill.EnvironmentCharges.ToString(); txtServiceCharges.Text = bill.ServiceCharges.ToString(); txtTotalCharges.Text = bill.TotalCharges.ToString(); txtLocalTaxes.Text = bill.LocalTaxes.ToString(); txtStateTaxes.Text = bill.StateTaxes.ToString(); txtPaymentDueDate.Text = bill.PaymentDueDate.ToLongDateString(); txtAmountDue.Text = bill.AmountDue.ToString(); txtLatePaymentDueDate.Text = bill.LatePaymentDueDate.ToLongDateString(); txtLateAmountDue.Text = bill.LateAmountDue.ToString(); } } } string strCustomers = string.Empty; List<Customer> clients = new List<Customer>(); string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; FileInfo fiCustomers = new FileInfo(fileCustomers); string? strMeterNumber = string.Empty; if (fiCustomers.Exists == true) { using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { strCustomers = File.ReadAllText(fiCustomers.FullName); clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; IEnumerable<Customer> customers = from consumer in clients where consumer.AccountNumber == txtAccountNumber.Text select consumer; foreach (Customer client in customers) { txtAccountName.Text = client.AccountName; strMeterNumber = client.MeterNumber; txtAccountType.Text = client.AccountType; txtAddress.Text = client.Address; txtCity.Text = client.City; txtCounty.Text = client.County; txtState.Text = client.State; txtZIPCode.Text = client.ZIPCode; } } } if (strMeterNumber!.Length > 0) { string strWaterMeters = string.Empty; List<WaterMeter> waterMeters = new List<WaterMeter>(); string fileWaterMeters = @"E:\Stellar Water Point2\WaterMeters.json"; FileInfo fiWaterMeters = new FileInfo(fileWaterMeters); if (fiWaterMeters.Exists == true) { strWaterMeters = File.ReadAllText(fiWaterMeters.FullName); waterMeters = JsonSerializer.Deserialize<List<WaterMeter>>(strWaterMeters)!; IEnumerable<WaterMeter> meters = from measure in waterMeters where measure.MeterNumber == strMeterNumber select measure; foreach (WaterMeter meter in waterMeters) { txtMeterDetails.Text = meter.Make + " " + meter.Model + " (Meter Size: " + meter.MeterSize + ")"; } } } } private void btnDeleteWaterBill_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtBillNumber.Text)) { MessageBox.Show("Please provide the bill number of the water bill you want to delete, " + "and then click the Find Water Bill button.", "Stellar Water Point", MessageBoxButtons.OK); return; } string strWaterBills = string.Empty; List<WaterBill> waterBills = new List<WaterBill>(); string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; FileInfo fiWaterBills = new FileInfo(fileWaterBills); if (fiWaterBills.Exists == true) { strWaterBills = File.ReadAllText(fiWaterBills.FullName); waterBills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; WaterBill bill = waterBills.Find(bl => bl.BillNumber == int.Parse(txtBillNumber.Text))!; if (bill is not null) { if (MessageBox.Show("Are you sure you want to delete (or remove) or cancel " + "this water bill from the system (you cannot undo the action)?", "Stellar Water Point", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { waterBills.Remove(bill); JsonSerializerOptions options = new JsonSerializerOptions(); options.WriteIndented = true; string jsWaterBills = JsonSerializer.Serialize(waterBills, typeof(List<WaterBill>), options); File.WriteAllText(fiWaterBills.FullName, jsWaterBills); MessageBox.Show("The water bill has been deleted (or removed, or cancelled) from our system.", "Stellar Water Point", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } Close(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
Control | (Name) | Text | Other Properties | |
ListView | ![]() |
lvwWaterBills | Anchor: Bottom, Top, Bottom, Left, Right | |
Button | ![]() |
btnProcessWaterBill | &Process Water Bill... | Anchor: Bottom, Right |
Button | ![]() |
btnViewWaterBill | &View Water Bill... | Anchor: Bottom, Right |
Button | ![]() |
btnEditWaterBill | &Edit Water Bill... | Anchor: Bottom, Right |
Button | ![]() |
btnDeleteWaterBill | &Delete Water Bill... | Anchor: Bottom, Right |
Button | ![]() |
btnClose | &Close | Anchor: Bottom, Right |
using System.Text.Json; using StellarWaterPoint4.Models; namespace StellarWaterPoint4.WaterBills { public partial class Central : Form { public Central() { InitializeComponent(); } private void ShowWaterBills() { // We will need the list of customers to display an account summary in the list view // Declare a strCustomers variable that will hold the records from the JSON file string strCustomers = string.Empty; // Prepare a list to hold the records of customers List<Customer> clients = new List<Customer>(); // Specify the file that holds the records of customers string fileCustomers = @"E:\Stellar Water Point2\Customers.json"; // Create a FileInfo object for the records of customers FileInfo fiCustomers = new FileInfo(fileCustomers); // Check if a file that holds the records of customers was created already if (fiCustomers.Exists == true) { // If that file exists, create a TextReader object to get those records using (TextReader trCustomers = new StreamReader(fiCustomers.FullName)) { // Read the records and store them in the strCustomers variable strCustomers = File.ReadAllText(fiCustomers.FullName); // Use JSON deserialization to get the records of customers clients = JsonSerializer.Deserialize<List<Customer>>(strCustomers)!; } } // Declare a strWaterBills variable that will hold the records from the JSON file string strWaterBills = string.Empty; // Prepare a bills list to hold the records of water bills List<WaterBill> bills = new List<WaterBill>(); // Specify the file that holds the records of water bills string fileWaterBills = @"E:\Stellar Water Point2\WaterBills.json"; // Create a FileInfo object for the records of water bills FileInfo fiWaterBills = new FileInfo(fileWaterBills); // Find out whether a file that holds the records of water bills exists already if (fiWaterBills.Exists == true) { // If that file exists, create a TextReader object to read the file using (TextReader trWaterBills = new StreamReader(fiWaterBills.FullName)) { /* Read the contents of the file that holds the water bills. * Store the read text in the strWaterBills variable that was declared. */ strWaterBills = File.ReadAllText(fiWaterBills.FullName); // Get the water bills and store them in the bills list variable bills = JsonSerializer.Deserialize<List<WaterBill>>(strWaterBills)!; // This variable will be used to keep count of the water bills records int counter = 1; /* We are about to display the water bills records in the list view. * Before proceeding, first remove any record in the list view. */ lvwWaterBills.Items.Clear(); // Visit each record of the water bills foreach (WaterBill client in bills) { /* Prepare a ListViewItem object for each record. * Display the record counter in the first column of this object. */ ListViewItem lviWaterBill = new ListViewItem(counter++.ToString()); // Display the water bill number of the current record lviWaterBill.SubItems.Add(client.BillNumber.ToString()); /* Refer to the list of records of the customers (that list was prepared earlier). * Use LINQ to get the customer whose account number is * the same as the account number of the current record. */ IEnumerable<Customer> customer = clients.Where(cust => cust.AccountNumber == client.AccountNumber); // Now that we have located the customer record, display some details about it foreach (Customer cust in customer) { lviWaterBill.SubItems.Add(client.AccountNumber + " - " + cust.AccountName + ", Type: " + cust.AccountType![..3] + ", (Mtr #: " + cust.MeterNumber + ")"); } /* Continue displaying some other parts of the water bill. * Before there is not enough space for the whole water bill, we display only some values. */ lviWaterBill.SubItems.Add(client.MeterReadingStartDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.MeterReadingEndDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.BillingDays.ToString()); lviWaterBill.SubItems.Add(client.CounterReadingStart.ToString()); lviWaterBill.SubItems.Add(client.CounterReadingEnd.ToString()); lviWaterBill.SubItems.Add(client.TotalHCF.ToString()); lviWaterBill.SubItems.Add(client.TotalGallons.ToString()); lviWaterBill.SubItems.Add(client.PaymentDueDate.ToShortDateString()); lviWaterBill.SubItems.Add(client.AmountDue.ToString()); lvwWaterBills.Items.Add(lviWaterBill); } } } } private void Central_Load(object sender, EventArgs e) { ShowWaterBills(); } private void btnProcessWaterBill_Click(object sender, EventArgs e) { Create create = new(); create.ShowDialog(); ShowWaterBills(); } private void btnViewWaterBill_Click(object sender, EventArgs e) { Details details = new(); details.Show(); } private void btnEditWaterBill_Click(object sender, EventArgs e) { Editor editor = new(); editor.ShowDialog(); ShowWaterBills(); } private void btnDeleteWaterBill_Click(object sender, EventArgs e) { Delete delete = new(); delete.ShowDialog(); ShowWaterBills(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } } }
|
|||
Home | Copyright © 2010-2024, FunctionX, Inc. | Sunday 11 June 2023 | Home |
|