using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Microsoft.VisualBasic;
namespace PortfolioU10787
{
public partial class FishingQuota : Form
{
private List<Boat> boats = new List<Boat>();
private void UpdateBoatsDataGridView()
{
// Clear the DataGridView to remove any existing rows
boatsDataGridView.Rows.Clear();
// Add the boats to the DataGridView
foreach (Boat boat in boats)
{
boatsDataGridView.Rows.Add(boat.Name, boat.LicenseNumber, boat.MaxLoadSize, string.Join(", ", boat.FishSpecies));
}
}
private List<Boat> LoadBoatsFromList()
{
List<Boat> loadedBoats = new List<Boat>();
// Add sample data for testing purposes
loadedBoats.Add(new Boat
{
Name = "Boat 1",
LicenseNumber = "L 123",
MaxLoadSize = 5000,
FishSpecies = new string[] { "Cod", "Haddock", "Plaice" }
});
loadedBoats.Add(new Boat
{
Name = "Boat 2",
LicenseNumber = "LL 456",
MaxLoadSize = 7000,
FishSpecies = new string[] { "Haddock", "Whiting" }
});
return loadedBoats;
}
private void LoadBoatsFromDataStorage()
{
// Clear the boats list to remove any existing boats
boats.Clear();
// TODO: Load boats from your data storage mechanism
List<Boat> loadedBoats = LoadBoatsFromList();
// Add the loaded boats to the boats list
foreach (Boat boat in loadedBoats)
{
boats.Add(boat);
}
// Update the DataGridView with the loaded boats
UpdateBoatsDataGridView();
}
private void MainForm_Load(object sender, EventArgs e)
{
// Load boats from data storage
LoadBoatsFromDataStorage();
LoadDailyCatchesFromDataStorage();
PopulateBoatComboBox();
PopulateFishComboBox();
// Populate the fish species ComboBox with the available fish species from quota data
List<string> fishSpecies = new List<string> { "Cod", "Haddock", "Plaice", "Whiting" };
fishSpeciesComboBox.DataSource = fishSpecies;
// Load the boat names from your data storage
List<string> boatNames = new List<string>();
foreach (Boat boat in boats)
{
boatNames.Add(boat.Name);
}
}
private void LoadBoatNames()
{
// Clear the boat ComboBox control
boatComboBox.Items.Clear();
// Load the boat names from your data storage
List<string> boatNames = new List<string>();
foreach (Boat boat in boats)
{
boatNames.Add(boat.Name);
}
// Add the boat names to the boat ComboBox control
boatComboBox.Items.AddRange(boatNames.ToArray());
}
private void PopulateFishComboBox()
{
// Clear the combo box to remove any existing items
fishSpeciesComboBox.Items.Clear();
// Create a hash set to avoid adding duplicate fish species
HashSet<string> fishSpecies = new HashSet<string>();
// Add each fish species to the combo box
foreach (DailyCatch dailyCatch in dailyCatches)
{
fishSpecies.Add(dailyCatch.FishSpecies);
}
foreach (string fish in fishSpecies)
{
fishSpeciesComboBox.Items.Add(fish);
}
}
private void PopulateBoatComboBox()
{
// Clear existing items in the boatComboBox control
boatComboBox.Items.Clear();
// Load the boat names into the boat ComboBox control
List<string> boatNames = new List<string>();
foreach (Boat boat in boats)
{
boatNames.Add(boat.Name);
}
boatComboBox.DataSource = boatNames;
}
private void LoadDailyCatchesFromDataStorage()
{
// Clear the daily catches list to remove any existing records
dailyCatches.Clear();
try
{
// Open the data storage file for reading
using (StreamReader reader = new StreamReader("catches.txt"))
{
// Read each line of the file and create a new DailyCatch object from the CSV data
string line;
while ((line = reader.ReadLine()) != null)
{
string[] values = line.Split(',');
DailyCatch dailyCatch = new DailyCatch
{
BoatName = values[0],
FishSpecies = values[1],
CatchWeight = float.Parse(values[2])
};
// Add the new daily catch to the list
dailyCatches.Add(dailyCatch);
}
}
// Update the daily catches DataGridView with the loaded data
UpdateDailyCatchesDataGridView();
// Populate the fish combo box
PopulateFishComboBox();
}
catch (Exception ex)
{
MessageBox.Show($"Error loading daily catches: {ex.Message}");
}
// if dailyCatches is still empty, add some sample data
if (dailyCatches.Count == 0)
{
dailyCatches.Add(new DailyCatch
{
BoatName = "Boat 1",
FishSpecies = "Cod",
CatchWeight = 500
});
}
}
private void UpdateDailyCatchesDataGridView()
{
// Clear the DataGridView to remove any existing rows
dailyCatchesDataGridView.Rows.Clear();
// Add the daily catches to the DataGridView, filtered by the selected boat and fish species
string selectedBoatName = boatComboBox.SelectedItem?.ToString();
string selectedFishSpecies = fishSpeciesComboBox.SelectedItem?.ToString();
foreach (DailyCatch dailyCatch in dailyCatches)
{
if ((selectedBoatName == null || dailyCatch.BoatName == selectedBoatName) &&
(selectedFishSpecies == null || dailyCatch.FishSpecies == selectedFishSpecies))
{
dailyCatchesDataGridView.Rows.Add(dailyCatch.BoatName, dailyCatch.FishSpecies, dailyCatch.CatchWeight);
}
}
}
private void UpdateQuotaReportDataGridView()
{
// Clear the DataGridView to remove any existing rows
quotaReportDataGridView.Rows.Clear();
// Calculate the total weight caught for each fish species
Dictionary<string, float> fishTotals = new Dictionary<string, float>();
foreach (DailyCatch dailyCatch in dailyCatches)
{
if (fishTotals.ContainsKey(dailyCatch.FishSpecies))
{
fishTotals[dailyCatch.FishSpecies] += dailyCatch.CatchWeight;
}
else
{
fishTotals[dailyCatch.FishSpecies] = dailyCatch.CatchWeight;
}
}
// Load the quota data from data storage
Dictionary<string, float> quotaData = new Dictionary<string, float>
{
{"Cod", 10000},
{"Haddock", 15000},
{"Plaice", 5000},
{"Whiting", 2000}
};
// Add each fish species and its corresponding data to the DataGridView
foreach (KeyValuePair<string, float> fishTotal in fishTotals)
{
string fishSpecies = fishTotal.Key;
float totalWeight = fishTotal.Value;
float quota = quotaData.ContainsKey(fishSpecies) ? quotaData[fishSpecies] : 0;
float remainingQuota = quota - totalWeight;
quotaReportDataGridView.Rows.Add(fishSpecies, totalWeight, quota, remainingQuota);
}
}
public FishingQuota()
{
InitializeComponent();
}
private void AddBoatToDataGridView(string boatName, string licenseNumber, float maxLoadSize, List<string> fishSpecies)
{
// Add the new boat to the DataGridView
boatsDataGridView.Rows.Add(boatName, licenseNumber, maxLoadSize, string.Join(", ", fishSpecies));
// Add the new boat to the boats list
boats.Add(new Boat
{
Name = boatName,
LicenseNumber = licenseNumber,
MaxLoadSize = maxLoadSize,
FishSpecies = fishSpecies.ToArray()
});
}
private void addBoatButton_Click(object sender, EventArgs e)
{
// Input dialog for boat name
string boatName = Interaction.InputBox("Enter boat name:", "Add Boat");
if (string.IsNullOrWhiteSpace(boatName))
{
MessageBox.Show("Boat name cannot be empty.");
return;
}
// Input dialog for license number
string licenseNumber = Interaction.InputBox("Enter license number (format: L XXX, LL XXX, L XX, LL XX):", "Add Boat");
Regex licenseRegex = new Regex(@"^[A-Za-z]{1,2} \d{2,3}$");
if (!licenseRegex.IsMatch(licenseNumber))
{
MessageBox.Show("Invalid license number format.");
return;
}
// Input dialog for maximum load size
string maxLoadSizeStr = Interaction.InputBox("Enter maximum load size (in kg):", "Add Boat");
if (!float.TryParse(maxLoadSizeStr, out float maxLoadSize) || maxLoadSize <= 0)
{
MessageBox.Show("Invalid maximum load size.");
return;
}
// Input dialog for fish species
string fishSpeciesStr = Interaction.InputBox("Enter the fish species separated by commas (min 2, max 4):", "Add Boat");
List<string> fishSpecies = fishSpeciesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList();
string[] fishSpeciesArray = fishSpecies.ToArray();
if (fishSpecies.Count < 2 || fishSpecies.Count > 4)
{
MessageBox.Show("Invalid number of fish species.");
return;
}
// Add the new boat to the boats DataGridView and list
AddBoatToDataGridView(boatName, licenseNumber, maxLoadSize, fishSpecies);
boats.Add(new Boat
{
Name = boatName,
LicenseNumber = licenseNumber,
MaxLoadSize = maxLoadSize,
FishSpecies = fishSpecies.ToArray()
});
SaveBoatToDataStorage(boatName, licenseNumber, maxLoadSize, fishSpeciesArray);
// Display success message
MessageBox.Show("Boat added successfully.");
}
private void SaveBoatToDataStorage(string boatName, string licenseNumber, float maxLoadSize, string[] fishSpecies)
{
Boat newBoat = new Boat
{
Name = boatName,
LicenseNumber = licenseNumber,
MaxLoadSize = maxLoadSize,
FishSpecies = fishSpecies
};
boats.Add(newBoat);
}
private void editBoatButton_Click(object sender, EventArgs e)
{
// Check if a row is selected in the DataGridView
if (boatsDataGridView.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a row to edit.");
return;
}
// Retrieve the boat information from the selected row
DataGridViewRow selectedRow = boatsDataGridView.SelectedRows[0];
string boatName = selectedRow.Cells[0].Value.ToString();
string licenseNumber = selectedRow.Cells[1].Value.ToString();
float maxLoadSize = float.Parse(selectedRow.Cells[2].Value.ToString());
string fishSpeciesStr = selectedRow.Cells[3].Value.ToString();
string[] fishSpecies = fishSpeciesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
// Show input dialogs to edit the boat information
string newBoatName = Interaction.InputBox("Enter new boat name:", "Edit Boat", boatName);
if (string.IsNullOrWhiteSpace(newBoatName))
{
MessageBox.Show("Boat name cannot be empty.");
return;
}
string newLicenseNumber = Interaction.InputBox("Enter new license number (format: L XXX, LL XXX, L XX, LL XX):", "Edit Boat", licenseNumber);
Regex licenseRegex = new Regex(@"^[A-Za-z]{1,2} \d{2,3}$");
if (!licenseRegex.IsMatch(newLicenseNumber))
{
MessageBox.Show("Invalid license number format.");
return;
}
string newMaxLoadSizeStr = Interaction.InputBox("Enter new maximum load size (in kg):", "Edit Boat", maxLoadSize.ToString());
if (!float.TryParse(newMaxLoadSizeStr, out float newMaxLoadSize) || newMaxLoadSize <= 0)
{
MessageBox.Show("Invalid maximum load size.");
return;
}
string newFishSpeciesStr = Interaction.InputBox("Enter the new fish species separated by commas (min 2, max 4):", "Edit Boat", string.Join(", ", fishSpecies));
string[] newFishSpecies = newFishSpeciesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
if (newFishSpecies.Length < 2 || newFishSpecies.Length > 4)
{
MessageBox.Show("Invalid number of fish species.");
return;
}
// Update the DataGridView and save the changes to data storage
selectedRow.Cells[0].Value = newBoatName;
selectedRow.Cells[1].Value = newLicenseNumber;
selectedRow.Cells[2].Value = newMaxLoadSize;
selectedRow.Cells[3].Value = string.Join(", ", newFishSpecies);
SaveBoatsToDataStorage();
}
private void SaveBoatsToDataStorage()
{
try
{
// Open the data storage file for writing
using (StreamWriter writer = new StreamWriter("boats.txt"))
{
// Write each boat to the file in CSV format
foreach (Boat boat in boats)
{
writer.WriteLine($"{boat.Name},{boat.LicenseNumber},{boat.MaxLoadSize},{string.Join(",", boat.FishSpecies)}");
}
}
MessageBox.Show("Boats data saved successfully.");
}
catch (Exception ex)
{
MessageBox.Show($"Error saving boats data: {ex.Message}");
}
}
private void tabPage2_Click(object sender, EventArgs e)
{
LoadBoatNames();
}
private List<DailyCatch> dailyCatches = new List<DailyCatch>();
private TabPage quotaReportTabPage;
private TabPage dailyCatchTabPage;
private void SaveDailyCatchToDataStorage()
{
try
{
// Open the data storage file for writing
using (StreamWriter writer = new StreamWriter("daily_catch.txt"))
{
// Write each daily catch record to the file in CSV format
foreach (DailyCatch dailyCatch in dailyCatches)
{
writer.WriteLine($"{dailyCatch.BoatName},{dailyCatch.FishSpecies},{dailyCatch.CatchWeight}");
}
}
MessageBox.Show("Daily catch data saved successfully.");
}
catch (Exception ex)
{
MessageBox.Show($"Error saving daily catch data: {ex.Message}");
}
}
private void saveCatchButton_Click(object sender, EventArgs e)
{
// Get the selected boat and fish species from the form's controls
string selectedBoatName = boatComboBox.SelectedItem?.ToString();
string selectedFishSpecies = fishSpeciesComboBox.SelectedItem?.ToString();
// Check if either the boat or fish species has not been selected
if (string.IsNullOrEmpty(selectedBoatName) || string.IsNullOrEmpty(selectedFishSpecies))
{
MessageBox.Show("Please select a boat and fish species.");
return;
}
// Get the catch weight from the form's control
float catchWeight = (float)catchWeightNumericUpDown.Value;
// Create a new DailyCatch object with the selected values
DailyCatch newDailyCatch = new DailyCatch
{
BoatName = selectedBoatName,
FishSpecies = selectedFishSpecies,
CatchWeight = catchWeight
};
// Add the new daily catch to the List<DailyCatch> field
dailyCatches.Add(newDailyCatch);
// Call the method to save the daily catch records to data storage
SaveDailyCatchToDataStorage();
// Display a success message
MessageBox.Show("Daily catch recorded successfully.");
// Clear the form's controls
boatComboBox.SelectedIndex = -1;
fishSpeciesComboBox.SelectedIndex = -1;
catchWeightNumericUpDown.Value = 0;
// Update the daily catches DataGridView
UpdateDailyCatchesDataGridView();
}
private void UpdateQuotaReportDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void boatsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == quotaReportTabPage)
{
UpdateQuotaReportDataGridView();
}
if (tabControl1.SelectedTab == dailyCatchTabPage)
{
// Load the boat names into the boat ComboBox control
LoadBoatNames();
}
}
private void fishSpeciesComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void dailyCatchesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void RecordDailyCatchForm_Load(object sender, EventArgs e)
{
// Load the boat names into the boat ComboBox control
LoadBoatNames();
}
}
}