diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7279f31c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vs/ +[Bb]in/ +[Oo]bj/ +*.user \ No newline at end of file diff --git a/01-Introduction/Alarms.cs b/01-Introduction/Alarms.cs index 569c57a4..1d3ceb0b 100644 --- a/01-Introduction/Alarms.cs +++ b/01-Introduction/Alarms.cs @@ -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) { diff --git a/01-Introduction/Ameba.cs b/01-Introduction/Ameba.cs index 05d77ead..95c17aeb 100644 --- a/01-Introduction/Ameba.cs +++ b/01-Introduction/Ameba.cs @@ -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] diff --git a/01-Introduction/RobotOnMoon.cs b/01-Introduction/RobotOnMoon.cs index 76d7f7fc..aec6deba 100644 --- a/01-Introduction/RobotOnMoon.cs +++ b/01-Introduction/RobotOnMoon.cs @@ -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 diff --git a/03-LINQ/GoldSavings.App/Data/top3_prices.xml b/03-LINQ/GoldSavings.App/Data/top3_prices.xml new file mode 100644 index 00000000..08d5abef --- /dev/null +++ b/03-LINQ/GoldSavings.App/Data/top3_prices.xml @@ -0,0 +1,12 @@ + + + + 624.92 + + + 624.32 + + + 623.73 + + \ No newline at end of file diff --git a/03-LINQ/GoldSavings.App/DataServices/GoldAnalysisService.cs b/03-LINQ/GoldSavings.App/DataServices/GoldAnalysisService.cs index 951871f5..3a1e7ae7 100644 --- a/03-LINQ/GoldSavings.App/DataServices/GoldAnalysisService.cs +++ b/03-LINQ/GoldSavings.App/DataServices/GoldAnalysisService.cs @@ -17,5 +17,50 @@ public double GetAveragePrice() { return _goldPrices.Average(p => p.Price); } + + public IEnumerable GetTop3Prices() + { + return _goldPrices.OrderByDescending(p => p.Price).Take(3); + } + + public IEnumerable 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 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)}"); + } + } } } diff --git a/03-LINQ/GoldSavings.App/DataServices/GoldDataService.cs b/03-LINQ/GoldSavings.App/DataServices/GoldDataService.cs index fddb85b6..ccca6cbe 100644 --- a/03-LINQ/GoldSavings.App/DataServices/GoldDataService.cs +++ b/03-LINQ/GoldSavings.App/DataServices/GoldDataService.cs @@ -20,5 +20,25 @@ public async Task> GetGoldPrices(DateTime startDate, DateTime en var prices = await _goldClient.GetGoldPrices(startDate, endDate); return prices ?? new List(); // Prevent null values } + public async Task> GetAllRequiredData(DateTime startDate) + { + var allPrices = new List(); + 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(); + } } } diff --git a/03-LINQ/GoldSavings.App/DataServices/GoldResultPrinter.cs b/03-LINQ/GoldSavings.App/DataServices/GoldResultPrinter.cs index 6502ddd9..719f1297 100644 --- a/03-LINQ/GoldSavings.App/DataServices/GoldResultPrinter.cs +++ b/03-LINQ/GoldSavings.App/DataServices/GoldResultPrinter.cs @@ -14,7 +14,6 @@ public static void PrintPrices(List prices, string title) Console.WriteLine($"{price.Date:yyyy-MM-dd} - {price.Price} PLN"); } } - public static void PrintSingleValue(T value, string title) { Console.WriteLine($"\n{title}: {value}"); diff --git a/03-LINQ/GoldSavings.App/DataServices/XmlService.cs b/03-LINQ/GoldSavings.App/DataServices/XmlService.cs new file mode 100644 index 00000000..67901f8c --- /dev/null +++ b/03-LINQ/GoldSavings.App/DataServices/XmlService.cs @@ -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 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 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(); + } + } +} \ No newline at end of file diff --git a/03-LINQ/GoldSavings.App/GoldSavings.App.csproj b/03-LINQ/GoldSavings.App/GoldSavings.App.csproj index 62429232..e069dbd2 100644 --- a/03-LINQ/GoldSavings.App/GoldSavings.App.csproj +++ b/03-LINQ/GoldSavings.App/GoldSavings.App.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 GoldSavings.App enable enable diff --git a/03-LINQ/GoldSavings.App/Program.cs b/03-LINQ/GoldSavings.App/Program.cs index 75357ab9..1d43b984 100644 --- a/03-LINQ/GoldSavings.App/Program.cs +++ b/03-LINQ/GoldSavings.App/Program.cs @@ -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 @@ -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 goldPrices = dataService.GetGoldPrices(startDate, endDate).GetAwaiter().GetResult(); + DateTime startDate = new DateTime(2019,01,01); + List goldPrices = dataService.GetAllRequiredData(startDate).GetAwaiter().GetResult(); if (goldPrices.Count == 0) { @@ -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"); } }