Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs/
[Bb]in/
[Oo]bj/
*.user
30 changes: 16 additions & 14 deletions 01-Introduction/Alarms.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;


public class Alarms
{
public int countAlarms(int[] volume, int S)
{
return default(int);
}
public int countAlarms(int[] volume, int S)
{
int count = 0;
int n = volume.Length;

while (S > 0)
{
int currentVolume = volume[count % n];
S -= currentVolume;
count++;
}

return count;
}

#region Testing code
[STAThread]
#region Testing code
[STAThread]
private static Boolean KawigiEdit_RunTest(int testNum, int[] p0, int p1, Boolean hasAnswer, int p2) {
Console.Write("Test " + testNum + ": [" + "{");
for (int i = 0; p0.Length > i; ++i) {
Expand Down
22 changes: 12 additions & 10 deletions 01-Introduction/Ameba.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;


public class Ameba
{
public int simulate(int[] X, int A)
{
return default(int);
}
int currentSize = A;

foreach (int gelSize in X)
{
if (gelSize == currentSize)
{
currentSize *= 2;
}
}

return currentSize;
}

#region Testing code
[STAThread]
Expand Down
50 changes: 40 additions & 10 deletions 01-Introduction/RobotOnMoon.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;


public class RobotOnMoon
{
public string isSafeCommand(string[] board, string S)
{
return default(string);
int rows = board.Length;
int cols = board[0].Length;
int r = 0, c = 0;

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (board[i][j] == 'S')
{
r = i;
c = j;
break;
}
}
}

foreach (char cmd in S)
{
int nr = r;
int nc = c;

if (cmd == 'U') nr--;
else if (cmd == 'D') nr++;
else if (cmd == 'L') nc--;
else if (cmd == 'R') nc++;

if (nr < 0 || nr >= rows || nc < 0 || nc >= cols)
{
return "Dead";
}

if (board[nr][nc] != '#')
{
r = nr;
c = nc;
}
}

return "Alive";
}

#region Testing code
Expand Down
12 changes: 12 additions & 0 deletions 03-LINQ/GoldSavings.App/Data/top3_prices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<GoldPrices>
<PriceEntry Date="2026-03-03">
<Value>624.92</Value>
</PriceEntry>
<PriceEntry Date="2026-03-04">
<Value>624.32</Value>
</PriceEntry>
<PriceEntry Date="2026-01-30">
<Value>623.73</Value>
</PriceEntry>
</GoldPrices>
45 changes: 45 additions & 0 deletions 03-LINQ/GoldSavings.App/DataServices/GoldAnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,50 @@ public double GetAveragePrice()
{
return _goldPrices.Average(p => p.Price);
}

public IEnumerable<GoldPrice> GetTop3Prices()
{
return _goldPrices.OrderByDescending(p => p.Price).Take(3);
}

public IEnumerable<GoldPrice> GetBottom3Prices()
{
return _goldPrices.OrderBy(p => p.Price).Take(3);
}

public bool CanProfit(DateTime buyMonth)
{
var pricesInMonth = _goldPrices
.Where(p => p.Date.Year == buyMonth.Year && p.Date.Month == buyMonth.Month)
.ToList();

if (!pricesInMonth.Any()) return false;

var minPrice = pricesInMonth.Min(p => p.Price);
var targetPrice = minPrice * 1.05;

// Ważne: Sprawdzamy daty PÓŹNIEJSZE niż data zakupu najtańszego złota
return _goldPrices.Any(p => p.Date > buyMonth && p.Price > targetPrice);
}

public IEnumerable<GoldPrice> GetSecondTenRanking()
{
return _goldPrices
.Where(p => p.Date.Year >= 2019 && p.Date.Year <= 2022)
.OrderByDescending(p => p.Price).Skip(10).Take(3);
}

public void GetYearlyAverages()
{
var yearsToAnalyze = new[] { 2020, 2023, 2024 };

var averages = _goldPrices.Where(p => yearsToAnalyze.Contains(p.Date.Year))
.GroupBy(p => p.Date.Year).Select(g => new { Year = g.Key, Avg = g.Average(x => x.Price) });

foreach (var item in averages)
{
Console.WriteLine($"Year: {item.Year}, Average Price: {Math.Round(item.Avg, 2)}");
}
}
}
}
20 changes: 20 additions & 0 deletions 03-LINQ/GoldSavings.App/DataServices/GoldDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,25 @@ public async Task<List<GoldPrice>> GetGoldPrices(DateTime startDate, DateTime en
var prices = await _goldClient.GetGoldPrices(startDate, endDate);
return prices ?? new List<GoldPrice>(); // Prevent null values
}
public async Task<List<GoldPrice>> GetAllRequiredData(DateTime startDate)
{
var allPrices = new List<GoldPrice>();
int startYear = startDate.Year;
int endYear = DateTime.Now.Year;

for (int year = startYear; year <= endYear; year++)
{
DateTime fetchStart = new DateTime(year, 1, 1);
DateTime fetchEnd = new DateTime(year, 12, 31);

if (fetchEnd > DateTime.Now) fetchEnd = DateTime.Now;

var yearlyPrices = await GetGoldPrices(fetchStart, fetchEnd);

allPrices.AddRange(yearlyPrices);
}

return allPrices.OrderBy(p => p.Date).ToList();
}
}
}
1 change: 0 additions & 1 deletion 03-LINQ/GoldSavings.App/DataServices/GoldResultPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public static void PrintPrices(List<GoldPrice> prices, string title)
Console.WriteLine($"{price.Date:yyyy-MM-dd} - {price.Price} PLN");
}
}

public static void PrintSingleValue<T>(T value, string title)
{
Console.WriteLine($"\n{title}: {value}");
Expand Down
36 changes: 36 additions & 0 deletions 03-LINQ/GoldSavings.App/DataServices/XmlService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GoldSavings.App.Model;
using System.Xml.Serialization;
using System.Xml.Linq;

namespace GoldSavings.App.Services
{
public class XmService
{
public void SaveToXml(List<GoldPrice> prices, string filePath)
{
var xml = new XDocument(
new XElement("GoldPrices",
prices.Select(p => new XElement("PriceEntry",
new XAttribute("Date", p.Date.ToString("yyyy-MM-dd")),
new XElement("Value", p.Price)
))
)
);
xml.Save(filePath);
}

public List<GoldPrice> LoadFromXml(string filePath)
{
return XDocument.Load(filePath)
.Descendants("PriceEntry")
.Select(x => new GoldPrice {
Date = DateTime.Parse(x.Attribute("Date").Value),
Price = double.Parse(x.Element("Value").Value)
})
.ToList();
}
}
}
2 changes: 1 addition & 1 deletion 03-LINQ/GoldSavings.App/GoldSavings.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>GoldSavings.App</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
30 changes: 26 additions & 4 deletions 03-LINQ/GoldSavings.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GoldSavings.App.Model;
using GoldSavings.App.Client;
using GoldSavings.App.Services;
using System.Runtime.Serialization;
namespace GoldSavings.App;

class Program
Expand All @@ -11,9 +12,8 @@ static void Main(string[] args)

// Step 1: Get gold prices
GoldDataService dataService = new GoldDataService();
DateTime startDate = new DateTime(2024,09,18);
DateTime endDate = DateTime.Now;
List<GoldPrice> goldPrices = dataService.GetGoldPrices(startDate, endDate).GetAwaiter().GetResult();
DateTime startDate = new DateTime(2019,01,01);
List<GoldPrice> goldPrices = dataService.GetAllRequiredData(startDate).GetAwaiter().GetResult();

if (goldPrices.Count == 0)
{
Expand All @@ -25,12 +25,34 @@ static void Main(string[] args)

// Step 2: Perform analysis
GoldAnalysisService analysisService = new GoldAnalysisService(goldPrices);

var avgPrice = analysisService.GetAveragePrice();
var top3 = analysisService.GetTop3Prices();
var bottom3 = analysisService.GetBottom3Prices();

DateTime jan2020 = new DateTime(2020, 01, 01);
bool profit = analysisService.CanProfit(jan2020);
var secondTen = analysisService.GetSecondTenRanking();

// Step 3: Print results

//Step 3: Print results
GoldResultPrinter.PrintSingleValue(Math.Round(avgPrice, 2), "Average Gold Price Last Half Year");
GoldResultPrinter.PrintPrices(top3.ToList(), "Top 3 Prices");
GoldResultPrinter.PrintPrices(bottom3.ToList(), "Bottom 3 Prices");
Console.WriteLine($"\nZakupienie w {jan2020.Year} {jan2020.Month} { (profit ? "zwróci zysk" : "nie zwróci zysku")}");
Console.WriteLine("\nYearly Averages");
analysisService.GetYearlyAverages();

Console.WriteLine("\nSecond Ten (2019-2022)");
GoldResultPrinter.PrintPrices(secondTen.ToList(), "Ranking positions 11-13");

Console.WriteLine("\nGold Analyis Queries with LINQ Completed.");

//Step 4: Save results
XmService xmlService = new XmService();
string filePath = "Data/top3_prices.xml";
xmlService.SaveToXml(top3.ToList(), filePath);
var readData = xmlService.LoadFromXml(filePath);
GoldResultPrinter.PrintPrices(readData, "Read from file");
}
}