Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Merge pull request #7 from 2nd-Semester-Project/main
Browse files Browse the repository at this point in the history
  • Loading branch information
svenons authored Mar 19, 2024
2 parents ccec02d + 41f5833 commit 3dd8132
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 198 deletions.
65 changes: 63 additions & 2 deletions HeatOptimiser/Classes/SourceDataManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
using OfficeOpenXml; // dotnet add package EPPlus
using System.Globalization;

namespace HeatOptimiser
{
public class SourceDataPoint
{
public DateTime? TimeFrom;
public DateTime? TimeTo;
public double? HeatDemand;
public double? ElectricityPrice;
}
public class SourceData {
public List<SourceDataPoint> SummerData;
public List<SourceDataPoint> WinterData;
public SourceData(){
SourceDataManager sourceManager = new SourceDataManager();
SummerData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2);
WinterData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 7);
}
}
public class SourceDataManager : ISourceDataManager
{
public SourceDataManager()
// Example usage: List<SourceDataPoint> SourceList = SourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2); Columns and Rows start with 1!!!!
public List<SourceDataPoint> LoadXLSXFile(string file, int rowStart, int columnStart, int workSheetNumber = 0)
{
throw new System.NotImplementedException();
var sourceList = new List<SourceDataPoint>();

ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // EPPlus license

using (var package = new ExcelPackage(new FileInfo(file)))
{
ExcelWorksheet? worksheet = null;
try {
worksheet = package.Workbook.Worksheets[workSheetNumber];
}
catch (Exception e) {
Console.WriteLine($"Worksheet not found: {e}");
return sourceList;
}

if (worksheet.Dimension == null)
{
Console.WriteLine("The worksheet is empty.");
return sourceList;
}
for (int row = rowStart; row <= worksheet.Dimension.End.Row; row++)
{
try {
DateTime temp;
string[] formats = { "dd/MM/yyyy HH.mm.ss", "dd/MM/yyyy HH:mm:ss", "HH.mm.ss", "HH:mm:ss" };
SourceDataPoint sourceData = new SourceDataPoint
{
TimeFrom = DateTime.TryParseExact(worksheet.Cells[row, columnStart].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null,
TimeTo = DateTime.TryParseExact(worksheet.Cells[row, columnStart + 1].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null,
HeatDemand = worksheet.Cells[row, columnStart + 2]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 2].Value.ToString()!) : null,
ElectricityPrice = worksheet.Cells[row, columnStart + 3]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 3].Value.ToString()!) : null
};
sourceList.Add(sourceData);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
}
}
}

return sourceList;
}
}
}
4 changes: 4 additions & 0 deletions HeatOptimiser/HeatOptimiser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EPPlus" Version="7.0.10" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion HeatOptimiser/Interfaces/ISourceDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace HeatOptimiser
{
public interface ISourceDataManager
{

public List<SourceDataPoint> LoadXLSXFile(string filePath, int rowStartd, int columnStart, int workSheetNumber);
}
}
3 changes: 1 addition & 2 deletions HeatOptimiser/Interfaces/IUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace HeatOptimiser
{
public interface IUserInterface {
public void AddUnit(){}

public void example() {}
}
}
1 change: 0 additions & 1 deletion HeatOptimiser/ProductionAssets.json

This file was deleted.

4 changes: 2 additions & 2 deletions HeatOptimiser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Program {
public static void Main()
{
new TextBasedUI().Interface();
}
new TextBasedUI().example();
}
}
}
199 changes: 9 additions & 190 deletions HeatOptimiser/UserInterfaces/Textbased.cs
Original file line number Diff line number Diff line change
@@ -1,196 +1,15 @@
using System;
using System.Collections.Generic;
namespace HeatOptimiser
{
public class TextBasedUI : AssetManager
{
private readonly SourceDataManager sourceDataManager;
private readonly Optimiser optimiser;
private readonly SourceData sourceData;

public TextBasedUI()
{
sourceDataManager = new SourceDataManager();
sourceData = new SourceData();
optimiser = new Optimiser(sourceDataManager, this);
}
public void Interface()
public class TextBasedUI: IUserInterface {
public void example()
{
AssetManager assetManager = new AssetManager();

while (true)
{
Console.WriteLine("\nHello, User");
Console.WriteLine("Type Number To Select:");
Console.WriteLine("1. Add Unit Option");
Console.WriteLine("2. Edit Unit");
Console.WriteLine("3. Delete Unit");
Console.WriteLine("4. Save Units");
Console.WriteLine("5. Optimise Schedule");
Console.WriteLine("6. Exit");

string? userInput = Console.ReadLine();

switch (userInput)
{

case "1":
Console.WriteLine("Selected: Add Unit");

Console.WriteLine("Enter unit name:");
string? name = Console.ReadLine();

Console.WriteLine("Enter unit image path:");
string? image = Console.ReadLine();

if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(image))
{
Console.WriteLine("Enter unit heat:");
double heat;
while (!double.TryParse(Console.ReadLine(), out heat))
{
Console.WriteLine("Invalid input. Please enter a valid number for heat:");
}
Console.WriteLine("Enter unit electricity:");
double electricity;
while (!double.TryParse(Console.ReadLine(), out electricity))
{
Console.WriteLine("Invalid input. Please enter a valid number for electricity:");
}
Console.WriteLine("Enter unit energy:");
double energy;
while (!double.TryParse(Console.ReadLine(), out energy))
{
Console.WriteLine("Invalid input. Please enter a valid number for energy:");
}
Console.WriteLine("Enter unit cost:");
double cost;
while (!double.TryParse(Console.ReadLine(), out cost))
{
Console.WriteLine("Invalid input. Please enter a valid number for cost:");
}
Console.WriteLine("Enter unit carbon dioxide:");
double carbonDioxide;
while (!double.TryParse(Console.ReadLine(), out carbonDioxide))
{
Console.WriteLine("Invalid input. Please enter a valid number for carbon dioxide:");
}
assetManager.AddUnit(name, image, heat, electricity, energy, cost, carbonDioxide);
}
else
{
Console.WriteLine("Name and Image cannot be empty or null. Unit not added.");
}
break;

case "2":
Console.WriteLine("Selected: Edit Unit");

Console.WriteLine("Enter Unit ID:");
Guid id;
while (!Guid.TryParse(Console.ReadLine(), out id))
{
Console.WriteLine("Invalid GUID. Enter a valid Unit ID");
}
Console.WriteLine("Enter property index to edit (0: Name, 1: Image, 2: Heat, 3: Electricity, 4: Energy, 5: Cost, 6: CarbonDioxide):");
int index;
while(!int.TryParse(Console.ReadLine(), out index) || index < 0 || index > 6)
{
Console.WriteLine("Invalid input. Please enter a number from 0 - 6");
}

Console.WriteLine("Enter new value:");
string value = Console.ReadLine();
try
{
if(index >= 0 && index <= 1)
{
assetManager.EditUnit(id , index , value);
}
else if (index >=2 && index <= 6)
{
double doubleValue;
while(!double.TryParse(value, out doubleValue))
{
Console.WriteLine("Invalid input. Please enter a valid number:");
value = Console.ReadLine();
}
assetManager.EditUnit(id, index, doubleValue);
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
break;
case "3":
Console.WriteLine("Selected: Delete Unit");
Console.WriteLine("Enter Unit ID");
Guid deleteId;
while(!Guid.TryParse(Console.ReadLine(), out deleteId))
{
Console.WriteLine("Invalid GUID. Enter a valid Unit ID:");
}
try
{
assetManager.DeleteUnit(deleteId);
Console.WriteLine("Unit deleted successfully.");
}
catch(Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
break;

case "4":
Console.WriteLine("Selected: Save Units");
Console.WriteLine("Enter file name to save units:");
string fileName = Console.ReadLine();
try
{
assetManager.SaveUnits(assetManager.GetAllUnits(), fileName);
Console.WriteLine("Units saved successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
break;
case "5":
Console.WriteLine("Selected: Optimise Schedule");
Console.WriteLine("Enter start date (dd/MM/yyyy):");
DateTime startDate;
while (!DateTime.TryParseExact(Console.ReadLine(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out startDate))
{
Console.WriteLine("Invalid input. Please enter a valid date (dd/MM/yyyy):");
}
Console.WriteLine("Enter end date (dd/MM/yyyy):");
DateTime endDate;
while (!DateTime.TryParseExact(Console.ReadLine(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out endDate))
{
Console.WriteLine("Invalid input. Please enter a valid date (dd/MM/yyyy):");
}
Schedule schedule = optimiser.Optimise(startDate, endDate);
DisplaySchedule(schedule);
break;
Console.WriteLine("Hello, GitHub!");

case "6":
Console.WriteLine("Exiting...");
return;
default:
Console.WriteLine("Invalid input. Please select a valid option.");
break;
}
}
SourceData sourceData = new SourceData();
Console.WriteLine();
Console.WriteLine("Here comes the stats from SourceDataManager data:");
Console.WriteLine($"Summer data contains {sourceData.SummerData.Count} entries.");
Console.WriteLine($"Winter data contains {sourceData.WinterData.Count} entries.");
}

private void DisplaySchedule(Schedule schedule)
{
Console.WriteLine("Optimised Schedule:");
foreach (var hour in schedule.schedule)
{
Console.WriteLine($"Hour: {hour.Hour}, Assets: {string.Join(",", hour.Assets)}, Demands: {string.Join(",", hour.Demands)}");
}
}
}
}
}
Binary file added HeatOptimiser/data/sourcedata.xlsx
Binary file not shown.

0 comments on commit 3dd8132

Please sign in to comment.