diff --git a/Lab3/.editorconfig b/Lab3/.editorconfig new file mode 100644 index 0000000..3159b2a --- /dev/null +++ b/Lab3/.editorconfig @@ -0,0 +1,14 @@ +root=true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = tab +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.cs] +indent_style = space +indent_size = 4 + diff --git a/Lab3/.gitignore b/Lab3/.gitignore new file mode 100644 index 0000000..26ab8f4 --- /dev/null +++ b/Lab3/.gitignore @@ -0,0 +1,388 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Nuget personal access tokens and Credentials +# nuget.config + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +.idea/ +*.sln.iml diff --git a/Lab3/Lab3.Exercises.Register/Dog.cs b/Lab3/Lab3.Exercises.Register/Dog.cs new file mode 100644 index 0000000..ab6d581 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Dog.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.Exercises.Register +{ + class Dog + { + public int ID { get; set; } + public string Name { get; set; } + public string Breed { get; set; } + public DateTime BirthDate { get; set; } + public Gender Gender { get; set; } + public DateTime LastVaccinationDate { get; set; } + + private const int VaccinationDuration = 1; + + public int Age + { + get + { + DateTime today = DateTime.Today; + int age = today.Year - this.BirthDate.Year; + if (BirthDate.Date > today.AddYears(-age)) + { + age--; + } + return age; + } + } + + public bool RequiresVaccination + { + get + { + if (LastVaccinationDate.Equals(DateTime.MinValue)) + { + return true; + } + return LastVaccinationDate.AddYears(VaccinationDuration).CompareTo(DateTime.Now) < 0; + } + } + + + public Dog(int id, string name, string breed, DateTime birthDate, Gender gender) + { + ID = id; + Name = name; + Breed = breed; + BirthDate = birthDate; + Gender = gender; + } + + public override bool Equals(object other) + { + return ID == ((Dog)other).ID; + } + public override int GetHashCode() + { + return ID.GetHashCode(); + } + + public int CompareTo(Dog other) + { + int genderComparison = Gender.CompareTo(other.Gender); + if (genderComparison == 0) + { + return Breed.CompareTo(other.Breed); + } + else + { + return genderComparison; + } + } + } +} diff --git a/Lab3/Lab3.Exercises.Register/Dogs.csv b/Lab3/Lab3.Exercises.Register/Dogs.csv new file mode 100644 index 0000000..731dbf3 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Dogs.csv @@ -0,0 +1,8 @@ +123;Reksas;Buldogas;2014-01-01;Male +124;Margis;Dalmantinas;2014-02-28;Male +125;Bitė;Senbernaras;2008-07-17;Female +320;Rikis;Taksas;2012-01-07;Male +123;Reksas;Buldogas;2014-01-01;Male +415;Pifas;Taksas;2014-07-07;Male +420;Markas;Dalmantinas;2013-02-28;Female +123;Reksas;Buldogas;2014-01-01;Male \ No newline at end of file diff --git a/Lab3/Lab3.Exercises.Register/DogsContainer.cs b/Lab3/Lab3.Exercises.Register/DogsContainer.cs new file mode 100644 index 0000000..3fdbbd4 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/DogsContainer.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.Exercises.Register +{ + class DogsContainer + { + private Dog[] dogs; + private int Capacity; + public int Count { get; private set; } + + public DogsContainer(int capacity = 16) + { + Capacity = capacity; + dogs = new Dog[capacity]; + } + + public DogsContainer(DogsContainer container) : this(container.Capacity) //call another constructor + { + for (int i = 0; i < container.Count; i++) + { + Add(container.Get(i)); + } + } + + public void Add(Dog dog) + { + if (Count == Capacity) + { + EnsureCapacity(Capacity * 2); + } + + dogs[Count] = dog; + Count++; + } + public Dog Get(int index) + { + return dogs[index]; + } + public bool Contains(Dog dog) + { + for (int i = 0; i < Count; i++) + { + if (dogs[i].Equals(dog)) + { + return true; + } + } + return false; + } + + public void Put(int index, Dog dog) + { + dogs[index] = dog; + } + + public void Insert(int index, Dog dog) + { + if (Count == Capacity) + { + EnsureCapacity(Capacity * 2); + } + for (int i = Count+1; i > index; i--) + { + dogs[i] = dogs[i-1]; + } + dogs[index] = dog; + Count++; + } + + public void RemoveAt(int index) + { + for (int i = index; i < Count; i++) + { + dogs[i] = dogs[i + 1]; + } + Count--; + } + + public void Remove(Dog dog) + { + for (int i = 0; i < Count; i++) + { + if (dogs[i].ID == dog.ID) + { + RemoveAt(i); + return; + } + } + } + + private void EnsureCapacity(int minimumCapacity) + { + if (minimumCapacity > Capacity) + { + Dog[] dogsClone = new Dog[minimumCapacity]; + for (int i = 0; i < Count; i++) + { + dogsClone[i] = dogs[i]; + } + Capacity = minimumCapacity; + dogs = dogsClone; + } + } + + public void Sort() + { + bool flag = true; + while (flag) + { + flag = false; + for (int i = 0; i < this.Count - 1; i++) + { + Dog a = this.dogs[i]; + Dog b = this.dogs[i + 1]; + if (a.CompareTo(b) > 0) + { + this.dogs[i] = b; + this.dogs[i + 1] = a; + flag = true; + } + } + } + } + + public DogsContainer Intersect(DogsContainer other) + { + DogsContainer result = new DogsContainer(); + for (int i = 0; i < Count; i++) + { + Dog current = dogs[i]; + if (other.Contains(current)) + { + result.Add(current); + } + } + return result; + } + + + + } + +} diff --git a/Lab3/Lab3.Exercises.Register/DogsRegister.cs b/Lab3/Lab3.Exercises.Register/DogsRegister.cs new file mode 100644 index 0000000..c9772a7 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/DogsRegister.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.Exercises.Register +{ + class DogsRegister + { + private DogsContainer AllDogs; + public DogsRegister() + { + AllDogs = new DogsContainer(); + } + public DogsRegister(DogsContainer Dogs) + { + AllDogs = new DogsContainer(); + for (int i = 0; i < Dogs.Count; i++) + { + this.AllDogs.Add(Dogs.Get(i)); + } + } + public void Add(Dog dog) + { + AllDogs.Add(dog); + } + + public int DogsCount() + { + return AllDogs.Count; + } + + public Dog GetByIndex(int index) + { + return AllDogs.Get(index); + } + + public int CountByGender(Gender gender) + { + int count = 0; + for (int i = 0; i < AllDogs.Count; i++) + { + Dog dog = AllDogs.Get(i); + if (dog.Gender.Equals(gender)) + { + count++; + } + } + return count; + } + + public Dog FindOldestDog() + { + return this.FindOldestDog(this.AllDogs); + } + + public DogsContainer FilterByBreed(string breed) + { + DogsContainer Filtered = new DogsContainer(); + for (int i = 0; i < AllDogs.Count; i++) + { + Dog dog = AllDogs.Get(i); + if (dog.Breed.Equals(breed)) // uses string method Equals() + { + Filtered.Add(dog); + } + } + return Filtered; + } + + public Dog FindOldestDog(string breed) + { + DogsContainer Filtered = this.FilterByBreed(breed); + return this.FindOldestDog(Filtered); + } + private Dog FindOldestDog(DogsContainer Dogs) + { + Dog oldest = Dogs.Get(0); + for (int i = 1; i < Dogs.Count; i++) //starts on index value 1 + { + Dog dog = Dogs.Get(i); + if (DateTime.Compare(oldest.BirthDate, dog.BirthDate) > 0) + { + oldest = dog; + } + } + return oldest; + } + private Dog FindDogByID(int ID) + { + for (int i = 0; i < AllDogs.Count; i++) + { + Dog dog = AllDogs.Get(i); + if (dog.ID == ID) + { + return dog; + } + } + return null; + + } + public void UpdateVaccinationsInfo(List Vaccinations) + { + foreach (Vaccination vaccination in Vaccinations) + { + Dog dog = FindDogByID(vaccination.DogID); + if (dog != null && vaccination > dog.LastVaccinationDate) + { + dog.LastVaccinationDate = vaccination.Date; + } + } + } + + public DogsContainer FilterByVaccinationExpired() + { + DogsContainer Filtered = new DogsContainer(); + for (int i = 0; i < AllDogs.Count; i++) + { + Dog dog = AllDogs.Get(i); + if (dog.RequiresVaccination) + { + Filtered.Add(dog); + } + } + return Filtered; + } + public bool Contains(Dog dog) + { + return AllDogs.Contains(dog); + } + } +} diff --git a/Lab3/Lab3.Exercises.Register/Gender.cs b/Lab3/Lab3.Exercises.Register/Gender.cs new file mode 100644 index 0000000..93c36ba --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Gender.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.Exercises.Register +{ + enum Gender + { + Male = 1, + Female = 2, + } +} diff --git a/Lab3/Lab3.Exercises.Register/InOutUtils.cs b/Lab3/Lab3.Exercises.Register/InOutUtils.cs new file mode 100644 index 0000000..0afd7b1 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/InOutUtils.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab3.Exercises.Register +{ + class InOutUtils + { + public static List ReadVaccinations(string fileName) + { + List Vaccinations = new List(); + string[] Lines = File.ReadAllLines(fileName); + foreach (string line in Lines) + { + string[] values = line.Split(';'); + int id = int.Parse(values[0]); + DateTime vaccinationDate = DateTime.Parse(values[1]); + Vaccination v = new Vaccination(id, vaccinationDate); + Vaccinations.Add(v); + } + return Vaccinations; + } + + public static DogsContainer ReadDogs(string fileName) + { + DogsContainer Dogs = new DogsContainer(); + string[] lines = File.ReadAllLines(fileName, Encoding.UTF8); + foreach (string line in lines) + { + string[] values = line.Split(';'); + int id = int.Parse(values[0]); + string name = values[1]; + string breed = values[2]; + DateTime birthDate = DateTime.Parse(values[3]); + Gender gender; + Enum.TryParse(values[4], out gender); //tries to convert value to enum + Dog dog = new Dog(id, name, breed, birthDate, gender); + if (!Dogs.Contains(dog)) + { + Dogs.Add(dog); + } + } + return Dogs; + } + public static void PrintDogs(DogsRegister register) + { + PrintDogsHeader(); + for (int i = 0; i < register.DogsCount(); i++) + { + Dog dog = register.GetByIndex(i); + PrintDogsRow(register.GetByIndex(i)); + } + PrintDogsFooter(); + } + + public static void PrintDogs(List dogs) + { + PrintDogsHeader(); + foreach (Dog dog in dogs) + { + PrintDogsRow(dog); + } + PrintDogsFooter(); + } + + public static void PrintDogs(DogsContainer container) + { + PrintDogsHeader(); + for (int i = 0; i < container.Count; i++) + { + Dog dog = container.Get(i); + PrintDogsRow(dog); + } + PrintDogsFooter(); + } + + public static void PrintDogs(string label, DogsContainer container) + { + PrintDogsHeader(label); + for (int i = 0; i < container.Count; i++) + { + Dog dog = container.Get(i); + PrintDogsRow(dog); + } + PrintDogsFooter(); + } + + private static void PrintDogsHeader() + { + Console.WriteLine(new string('-', 74)); + Console.WriteLine("| {0,8} | {1,-15} | {2,-15} | {3,-12} | {4,-8} |", "Reg.Nr.", "Vardas", "Veislė", "Gimimo data", "Lytis"); + Console.WriteLine(new string('-', 74)); + } + + private static void PrintDogsHeader(string label) + { + Console.WriteLine(new string('-', 74)); + Console.WriteLine("| {0,-70} |", label); + Console.WriteLine(new string('-', 74)); + Console.WriteLine("| {0,8} | {1,-15} | {2,-15} | {3,-12} | {4,-8} |", "Reg.Nr.", "Vardas", "Veislė", "Gimimo data", "Lytis"); + Console.WriteLine(new string('-', 74)); + } + + private static void PrintDogsFooter() + { + Console.WriteLine(new string('-', 74)); + } + + private static void PrintDogsRow(Dog dog) + { + Console.WriteLine("| {0,8} | {1,-15} | {2,-15} | {3,-12:yyyy-MM-dd} | {4,-8} |", dog.ID, dog.Name, dog.Breed, dog.BirthDate, dog.Gender); + } + + + public static void PrintDog(Dog dog) + { + Console.WriteLine("Vardas: {0}, Veislė: {1}, Amžius: {2}", dog.Name, dog.Breed, dog.Age); + } + + public static void PrintBreeds(List breeds) + { + foreach (string breed in breeds) + { + Console.WriteLine(breed); + } + } + + public static void PrintDogsToCSVFile(string fileName, List Dogs) + { + string[] lines = new string[Dogs.Count + 1]; + lines[0] = String.Format("{0};{1};{2};{3};{4}", "Reg.Nr.", "Vardas", "Veislė", "Gimimo data", "Lytis"); + for (int i = 0; i < Dogs.Count; i++) + { + lines[i + 1] = String.Format("{0};{1};{2};{3};{4}", Dogs[i].ID, Dogs[i].Name, Dogs[i].Breed, Dogs[i].BirthDate, Dogs[i].Gender); + } + File.WriteAllLines(fileName, lines, Encoding.UTF8); + } + } +} diff --git a/Lab3/Lab3.Exercises.Register/Lab3.Exercises.Register.csproj b/Lab3/Lab3.Exercises.Register/Lab3.Exercises.Register.csproj new file mode 100644 index 0000000..84ebe8a --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Lab3.Exercises.Register.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/Lab3/Lab3.Exercises.Register/Program.cs b/Lab3/Lab3.Exercises.Register/Program.cs new file mode 100644 index 0000000..1c3e2a4 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Program.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; + +namespace Lab3.Exercises.Register +{ + class Program + { + static void Main(string[] args) + { + Dog dog1 = new Dog(1, "Foo", "FooBreed", DateTime.Now, Gender.Male); + + Dog dog2 = new Dog(2, "Bar", "BarBreed", DateTime.Now, Gender.Male); + + Dog dog3 = new Dog(3, "Baz", "BazBreed", DateTime.Now, Gender.Female); + + DogsContainer container = new DogsContainer(); + container.Add(dog1); + container.Add(dog2); + container.Add(dog3); + InOutUtils.PrintDogs(container); + container.Remove(1); + InOutUtils.PrintDogs(container); + + /* + DogsContainer container = InOutUtils.ReadDogs("Dogs.csv"); + container.Sort(); + InOutUtils.PrintDogs("Container", container); + + DogsContainer otherContainer = new DogsContainer(container); + InOutUtils.PrintDogs("Other container", otherContainer); + */ + + /* + DogsContainer container = InOutUtils.ReadDogs("Dogs.csv"); + DogsRegister register = new DogsRegister(container); + List vaccinationsData = InOutUtils.ReadVaccinations(@"Vaccinations.csv"); + register.UpdateVaccinationsInfo(vaccinationsData); + + Console.WriteLine("Registro informacija:"); + InOutUtils.PrintDogs(register); + Console.WriteLine(); + + Console.WriteLine("Iš viso šunų: {0}", register.DogsCount()); + Console.WriteLine("Patinų: {0}", register.CountByGender(Gender.Male)); + Console.WriteLine("Patelių: {0}", register.CountByGender(Gender.Female)); + Console.WriteLine(); + + Console.WriteLine("Šunys kuriems reikia vakcinuotis:"); + DogsContainer dogsThatNeedVaccination = register.FilterByVaccinationExpired(); + InOutUtils.PrintDogs(dogsThatNeedVaccination); + Console.WriteLine(); + + string vaccinationBreed = "Taksas"; + DogsContainer allDogsByBreed = register.FilterByBreed(vaccinationBreed); + Console.WriteLine("Šunys kuriems reikia vakcinuotis ({0}):", vaccinationBreed); + InOutUtils.PrintDogs(allDogsByBreed.Intersect(dogsThatNeedVaccination)); + */ + + /* + Dog oldestDog = TaskUtils.FindOldestDog(register); + Console.WriteLine("Seniausias šuo:"); + InOutUtils.PrintDog(oldestDog); + Console.WriteLine(); + + List breeds = TaskUtils.FindBreeds(register); + Console.WriteLine("Šunų veislės:"); + InOutUtils.PrintBreeds(breeds); + Console.WriteLine(); + + List popularBreeds = TaskUtils.FindMostPopularBreeds(register); + Console.WriteLine("Populiariausios šunų veislės:"); + InOutUtils.PrintBreeds(popularBreeds); + Console.WriteLine(); + + Console.WriteLine("Kokios veislės šunis atrinkti?"); + string selectedBreed = Console.ReadLine().Trim(); + if (selectedBreed.Equals("")) + { + Console.WriteLine("Šunio veislė neįvesta"); + return; + } + + List filteredByBreed = TaskUtils.FilterByBreed(register, selectedBreed); + if (filteredByBreed.Count == 0) + { + Console.WriteLine("Šunų su veisle '{0}' nerasta", selectedBreed); + return; + } + + Dog oldestFilteredDog = TaskUtils.FindOldestDog(filteredByBreed); + Console.WriteLine("Seniausias šuo pagal '{0}' veislę:", selectedBreed); + InOutUtils.PrintDog(oldestFilteredDog); + InOutUtils.PrintDogs(filteredByBreed); + string fileName = selectedBreed + ".csv"; + InOutUtils.PrintDogsToCSVFile(fileName, filteredByBreed); + */ + + } + } +} diff --git a/Lab3/Lab3.Exercises.Register/Vaccination.cs b/Lab3/Lab3.Exercises.Register/Vaccination.cs new file mode 100644 index 0000000..a9c9f45 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Vaccination.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.Exercises.Register +{ + class Vaccination + { + public int DogID { get; set; } + public DateTime Date { get; set; } + public Vaccination(int dogID, DateTime date) + { + DogID = dogID; + Date = date; + } + public static bool operator <(Vaccination vaccination, DateTime date) + { + return vaccination.Date.CompareTo(date) < 0; + } + public static bool operator >(Vaccination vaccination, DateTime date) + { + return vaccination.Date.CompareTo(date) > 0; + } + + } +} diff --git a/Lab3/Lab3.Exercises.Register/Vaccinations.csv b/Lab3/Lab3.Exercises.Register/Vaccinations.csv new file mode 100644 index 0000000..48d2239 --- /dev/null +++ b/Lab3/Lab3.Exercises.Register/Vaccinations.csv @@ -0,0 +1,6 @@ +123;2020-07-24 +124;2021-05-17 +421;2021-07-01 +125;2021-01-01 +320;2021-07-01 +320;2010-07-01 \ No newline at end of file diff --git a/Lab3/Lab3.TouristInformationCenter/InOutUtils.cs b/Lab3/Lab3.TouristInformationCenter/InOutUtils.cs new file mode 100644 index 0000000..0e2025b --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/InOutUtils.cs @@ -0,0 +1,213 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.IO.Compression; + +namespace Lab3.TouristInformationCenter +{ + /// + /// Class that stores functions that are related to reading and writing data + /// + class InOutUtils + { + /// + /// Append the museums from the second container to the first one + /// + /// First container + /// Second container + private static void AppendContainer(MuseumsContainer container1, MuseumsContainer container2) + { + for (int i = 0; i < container2.Count; i++) + { + container1.Add(container2.Get(i)); + } + } + + /// + /// Decode a list of museums from a given list of lines. + /// + /// + /// Container of museums + public static MuseumsContainer DecodeMuseums(List lines) + { + MuseumsContainer container = new MuseumsContainer(lines.Count-2); + string city = lines[0]; + string manager = lines[1]; + for (int i = 2; i < lines.Count; i++) + { + string line = lines[i]; + string[] values = line.Split(';'); + string name = values[0]; + string type = values[1]; + List workdays = new List(); + for (int j = 1; j <= 7; j++) + { + if (int.Parse(values[j + 1]) == 1) + { + workdays.Add((Weekday)j); + } + } + double price = double.Parse(values[9]); + bool hasGuide = int.Parse(values[10]) == 1; + + Museum museum = new Museum(name, city, manager, type, workdays, price, hasGuide); + container.Add(museum); + } + return container; + } + + /// + /// Read and decode a list of museums from a file. + /// + /// Target file + /// Container of museums + public static MuseumsContainer ReadMuseumsFromCSV(string filename) + { + List lines = new List(); + foreach (string line in File.ReadAllLines(filename, Encoding.UTF8)) + { + lines.Add(line); + } + return DecodeMuseums(lines); + } + + /// + /// Read all the entries from a zip file and decode the museums inside the csv entries. + /// + /// Target filename + /// Register of museums + public static MuseumsContainer ReadMuseumsFromZIP(string filename) + { + MuseumsContainer mainContainer = new MuseumsContainer(); + + using (ZipArchive zipFile = ZipFile.Open(filename, ZipArchiveMode.Read)) + { + foreach (ZipArchiveEntry entry in zipFile.Entries) + { + if (!entry.Name.EndsWith(".csv")) continue; + List lines = new List(); + using (StreamReader reader = new StreamReader(entry.Open(), Encoding.UTF8)) + { + while (!reader.EndOfStream) + { + lines.Add(reader.ReadLine()); + } + } + + MuseumsContainer contaienr = DecodeMuseums(lines); + AppendContainer(mainContainer, contaienr); + } + + } + + return mainContainer; + } + + /// + /// Read and decode lists of museums from multiple files and put into a single register. + /// + /// Target files + /// Register containing museums from all files + public static MuseumsContainer ReadMuseums(params string[] filenames) + { + MuseumsContainer mainContainer = new MuseumsContainer(); + foreach (string filename in filenames) + { + if (filename.EndsWith(".csv")) + { + AppendContainer(mainContainer, ReadMuseumsFromCSV(filename)); + } + else if(filename.EndsWith(".zip")) + { + AppendContainer(mainContainer, ReadMuseumsFromZIP(filename)); + } + } + return mainContainer; + } + + private static string CreateMuseumLine(Museum museum) + { + string workDays = ""; + workDays += museum.Workdays.Contains(Weekday.Monday) ? "1" : "0"; + workDays += museum.Workdays.Contains(Weekday.Tuesday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Wednesday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Thursday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Friday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Saturday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Sunday) ? ";1" : ";0"; + + return String.Join(";", museum.City, museum.Name, workDays, museum.Price); + } + + /// + /// Write and encode a list of museums to a file. The file will be in a csv format using ";" as seperators. + /// + /// Target file + /// Container of museums + public static void WriteMuseums(string filename, MuseumsContainer container) + { + string[] lines = new string[container.Count]; + for (int i = 0; i < container.Count; i++) + { + Museum museum = container.Get(i); + lines[i] = CreateMuseumLine(museum); + } + File.WriteAllLines(filename, lines, Encoding.UTF8); + } + + /// + /// Write and encode a list of museums to a file from a register. + /// + /// Target location + /// Register containing museums + public static void WriteMuseums(string filename, MuseumsRegister register) + { + int n = register.Count(); + string[] lines = new string[n]; + for (int i = 0; i < n; i++) + { + Museum museum = register.GetByIndex(i); + lines[i] = CreateMuseumLine(museum); + } + File.WriteAllLines(filename, lines, Encoding.UTF8); + } + + /// + /// Write out a container of museums in a table to the console. + /// + /// Container of museums + public static void PrintMuseums(MuseumsContainer container) + { + if (container.Count == 0) + { + Console.WriteLine("Nėra"); + return; + } + + Console.WriteLine(new string('-', 123)); + Console.WriteLine("| {0,-20} | {1,-10} | {2,-20} | {3,-10} | {4,18} | {5,13} | {6,-4} |", "Vardas", "Miestas", "Atsakingas", "Tipas", "Darbo dienų kiekis", "Kaina", "Turi gidą?"); + Console.WriteLine(new string('-', 123)); + for (int i = 0; i < container.Count; i++) + { + Museum m = container.Get(i); + Console.WriteLine("| {0,-20} | {1,-10} | {2,-20} | {3,-10} | {4,18} | {5,13:f2} | {6,-10} |", m.Name, m.City, m.Manager, m.Type, m.Workdays.Count, m.Price, m.HasGuide ? "Taip" : "Ne"); + } + Console.WriteLine(new string('-', 123)); + } + + /// + /// Write out a list of museums in a table to the console from a register. + /// + /// Register containing museums + public static void PrintMuseums(MuseumsRegister register) + { + MuseumsContainer museums = new MuseumsContainer(); + for (int i = 0; i < register.Count(); i++) + { + museums.Add(register.GetByIndex(i)); + } + PrintMuseums(museums); + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/Lab3.TouristInformationCenter.csproj b/Lab3/Lab3.TouristInformationCenter/Lab3.TouristInformationCenter.csproj new file mode 100644 index 0000000..0ebd173 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/Lab3.TouristInformationCenter.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/Lab3/Lab3.TouristInformationCenter/Museum.cs b/Lab3/Lab3.TouristInformationCenter/Museum.cs new file mode 100644 index 0000000..0ed07ee --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/Museum.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +namespace Lab3.TouristInformationCenter +{ + /// + /// Class used for storing data related a single museum. + /// + class Museum + { + public string Name { get; set; } + public string City { get; set; } + public string Manager { get; set; } + public string Type { get; set; } + public List Workdays { get; set; } + public double Price { get; set; } + public bool HasGuide { get; set; } + public Museum(string name, string city, string manager, string type, List workdays, double price, bool hasGuide) + { + Name = name; + Manager = manager; + City = city; + Type = type; + Workdays = workdays; + Price = price; + HasGuide = hasGuide; + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/MuseumsContainer.cs b/Lab3/Lab3.TouristInformationCenter/MuseumsContainer.cs new file mode 100644 index 0000000..50f2b2a --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/MuseumsContainer.cs @@ -0,0 +1,246 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.TouristInformationCenter +{ + class MuseumsContainer + { + private Museum[] museums; + private int Capacity; + public int Count { get; private set; } + public MuseumsContainer(int capacity = 16) + { + Capacity = capacity; + museums = new Museum[capacity]; + } + + public MuseumsContainer(MuseumsContainer container) : this(container.Capacity) + { + for (int i = 0; i < container.Count; i++) + { + Add(container.Get(i)); + } + } + + /// + /// Add a museum to the end of the container + /// + /// Target museum + public void Add(Museum museum) + { + if (Count == Capacity) + { + EnsureCapacity(Capacity * 2); + } + + museums[Count] = museum; + Count++; + } + + /// + /// Get a musem by index from container + /// + /// Target index + /// Museum at given index + public Museum Get(int index) + { + return museums[index]; + } + + /// + /// Check if given museum exists in container + /// + /// Target museum + /// True if contains + public bool Contains(Museum museum) + { + for (int i = 0; i < Count; i++) + { + if (museums[i].Equals(museum)) + { + return true; + } + } + return false; + } + + /// + /// Put a museum at a specific index + /// + /// Target index + /// Target museum + public void Put(int index, Museum museum) + { + museums[index] = museum; + } + + /// + /// Filter by property "HasGuide" + /// + /// Target value + /// A filtered container of museums + public MuseumsContainer FilterByGuide(bool hasGuide) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.HasGuide == hasGuide) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Type". + /// + /// Target type + /// A filtered container of museums + public MuseumsContainer FilterByType(string type) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Type == type) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Price". + /// + /// Maximum allowed price to be included + /// A container with museums that dont't have a larger price then "maxPrice" + public MuseumsContainer FilterByPrice(double maxPrice) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Price <= maxPrice) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Type" + /// + /// Target city + /// A container of museums + public MuseumsContainer FilterByCity(string city) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.City == city) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Return a list of "active" museums from the contaner. + /// A museum is considered active if it is working at least some amount a week. + /// + /// Threshold which determines what is active + /// A container of "active" museums + public MuseumsContainer FilterByActivity(int threshold) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Workdays.Count >= threshold) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Name" + /// + /// Target name + /// A filtered container + public MuseumsContainer FilterByName(string name) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Name == name) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Get all of the different types of cities. + /// + /// A list of city names + public List GetAllCities() + { + List cities = new List(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (!cities.Contains(museum.City)) + { + cities.Add(museum.City); + } + } + return cities; + } + + /// + /// Sort container by "City" and "Name" in alphabetical order. + /// + public void Sort() + { + for (int i = 0; i < Count - 1; i++) + { + for (int j = i + 1; j < Count; j++) + { + Museum a = museums[i]; + Museum b = museums[j]; + int nameCompare = a.Name.CompareTo(b.Name); + int cityCompare = a.City.CompareTo(b.City); + if (cityCompare > 0 || (cityCompare == 0 && nameCompare > 0)) + { + museums[i] = b; + museums[j] = a; + } + } + } + } + + private void EnsureCapacity(int minimumCapacity) + { + if (minimumCapacity <= Capacity) return; + + Museum[] museumsClone = new Museum[minimumCapacity]; + for (int i = 0; i < Count; i++) + { + museumsClone[i] = museums[i]; + } + Capacity = minimumCapacity; + museums = museumsClone; + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/MuseumsKaunas.csv b/Lab3/Lab3.TouristInformationCenter/MuseumsKaunas.csv new file mode 100644 index 0000000..f3076a3 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/MuseumsKaunas.csv @@ -0,0 +1,8 @@ +Kaunas +Jonas Jonaitis +KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0 +KaunoMuziejusB;Computer;1;1;0;1;1;1;1;14,39;1 +KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0 +KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1 +KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;1 +KaunoMuziejusB;Space;1;1;1;1;0;1;1;0,99;1 \ No newline at end of file diff --git a/Lab3/Lab3.TouristInformationCenter/MuseumsRegister.cs b/Lab3/Lab3.TouristInformationCenter/MuseumsRegister.cs new file mode 100644 index 0000000..7d4d560 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/MuseumsRegister.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.TouristInformationCenter +{ + /// + /// Class used to store multiple museums in one place + /// + class MuseumsRegister + { + private MuseumsContainer AllMuseums; + + public MuseumsRegister() + { + AllMuseums = new MuseumsContainer(); + } + + public MuseumsRegister(MuseumsContainer museums) + { + AllMuseums = new MuseumsContainer(museums); + } + + /// + /// Add one museum to the register. + /// + /// Target museum + public void Add(Museum museum) + { + AllMuseums.Add(museum); + } + + /// + /// The amount of stored museums in the register. + /// + /// Museum count + public int Count() + { + return AllMuseums.Count; + } + + /// + /// Access museum from register by index + /// + /// Target index + /// Museum + public Museum GetByIndex(int index) + { + return AllMuseums.Get(index); + } + + /// + /// Return a list of active museums from the register. + /// A museum is considered active if it is working at least some amount a week. + /// + /// Threshold which determines what is active + /// A list of active museums + public MuseumsContainer FilterByActiveMuseums(int threshold) + { + return AllMuseums.FilterByActivity(threshold); + } + + /// + /// Find museum that work the most days in a week + /// + /// Most active museum + public Museum FindMostActiveMuseum() + { + if (AllMuseums.Count == 0) + { + return null; + } + + Museum mostActive = AllMuseums.Get(0); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (museum.Workdays.Count > mostActive.Workdays.Count) + { + mostActive = museum; + } + } + return mostActive; + } + + /// + /// Find all museums that work the most days in a week + /// + /// + public MuseumsContainer FindMostActiveMuseums() + { + Museum mostActive = FindMostActiveMuseum(); + MuseumsContainer activeMuseums = new MuseumsContainer(); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (museum.Workdays.Count == mostActive.Workdays.Count) + { + activeMuseums.Add(museum); + } + } + return activeMuseums; + } + + /// + /// Get all of the different types of cities. + /// + /// A list of city names + public List GetAllCities() + { + return AllMuseums.GetAllCities(); + } + + /// + /// Filter the museums by city name from register + /// + /// Target city + /// A container of museums + public MuseumsContainer FilterByCity(string city) + { + return AllMuseums.FilterByCity(city); + } + + /// + /// Filter by property "Type". + /// + /// Target type + /// A filtered container of museums + public MuseumsContainer FilterByType(string type) + { + return AllMuseums.FilterByType(type); + } + + private List GetMuseumNames() + { + List result = new List(); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (!result.Contains(museum.Name)) + { + result.Add(museum.Name); + } + } + return result; + } + + /// + /// Find all museums that have matching names + /// + /// A container with museums that have matching names + public MuseumsContainer FindMuseumsWithDuplicateNames() + { + MuseumsContainer result = new MuseumsContainer(); + List names = GetMuseumNames(); + foreach (string name in names) + { + MuseumsContainer museumsByName = AllMuseums.FilterByName(name); + if (museumsByName.Count <= 1) continue; + + for (int i = 0; i < museumsByName.Count; i++) + { + result.Add(museumsByName.Get(i)); + } + } + return result; + } + + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/MuseumsVilnius.csv b/Lab3/Lab3.TouristInformationCenter/MuseumsVilnius.csv new file mode 100644 index 0000000..7cb90da --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/MuseumsVilnius.csv @@ -0,0 +1,7 @@ +Vilnius +Petras Petraitis +VilnausMuziejus1;Dailė;1;0;0;1;1;0;0;5,49;0 +VilnausMuziejus2;Computer;1;1;0;1;1;1;1;0,00;1 +VilnausMuziejusA;Dailė;0;1;1;1;1;0;0;1,23;0 +VilnausMuziejusA;Food;1;1;1;1;1;1;0;6,90;0 +VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0 \ No newline at end of file diff --git a/Lab3/Lab3.TouristInformationCenter/Program.cs b/Lab3/Lab3.TouristInformationCenter/Program.cs new file mode 100644 index 0000000..4f7e921 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; + +namespace Lab3.TouristInformationCenter +{ + class Program + { + static void Main(string[] args) + { + // Read all museums from initial data files + MuseumsContainer container = InOutUtils.ReadMuseums("MuseumsKaunas.csv", "MuseumsVilnius.csv"); + MuseumsRegister register = new MuseumsRegister(container); + Console.WriteLine("Visi muziejai:"); + InOutUtils.PrintMuseums(register); + Console.WriteLine(); + + // Write out a list of cities and if they have at least one museum that is free and with a guide + List cities = register.GetAllCities(); + foreach (string city in cities) + { + MuseumsContainer freeMuseumsWithGuide = register + .FilterByCity(city) + .FilterByPrice(0) + .FilterByGuide(true); + bool passesCriteria = freeMuseumsWithGuide.Count > 0; + Console.WriteLine("{0}: {1}", city, passesCriteria ? "Taip" : "Ne"); + } + Console.WriteLine(); + + // Find all museums that are the most active + MuseumsContainer mostActiveMuseums = register.FindMostActiveMuseums(); + Console.WriteLine("Aktyviausi muziejai:"); + InOutUtils.PrintMuseums(mostActiveMuseums); + Console.WriteLine(); + + // Find museums that have duplicate names + MuseumsContainer museumsWithDuplicateNames = register.FindMuseumsWithDuplicateNames(); + InOutUtils.WriteMuseums("Sutampta.csv", museumsWithDuplicateNames); + + // Find all art museums and sort them + MuseumsContainer artMuseums = register.FilterByType("Dailė"); + artMuseums.Sort(); + InOutUtils.WriteMuseums("Dailė.csv", artMuseums); + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/Weekday.cs b/Lab3/Lab3.TouristInformationCenter/Weekday.cs new file mode 100644 index 0000000..a13903f --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/Weekday.cs @@ -0,0 +1,17 @@ + +namespace Lab3.TouristInformationCenter +{ + /// + /// Used for storing weekdays as numbers rather than strings + /// + enum Weekday + { + Monday = 1, + Tuesday = 2, + Wednesday = 3, + Thursday = 4, + Friday = 5, + Saturday = 6, + Sunday = 7 + } +} diff --git a/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsKaunas.csv b/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsKaunas.csv new file mode 100644 index 0000000..f82f969 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsKaunas.csv @@ -0,0 +1,8 @@ +Kaunas +Jonas Jonaitis +KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0 +KaunoMuziejusB;Computer;1;1;0;1;1;1;1;14,39;1 +KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0 +KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1 +KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;1 +KaunoMuziejusB;Space;1;1;1;1;0;1;1;0,99;1 diff --git a/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsVilnius.csv b/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsVilnius.csv new file mode 100644 index 0000000..a0f2827 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/tests/1/MuseumsVilnius.csv @@ -0,0 +1,7 @@ +Vilnius +Petras Petraitis +VilnausMuziejus1;Dailė;1;0;0;1;1;0;0;5,49;0 +VilnausMuziejus2;Computer;1;1;0;1;1;1;1;0,00;1 +VilnausMuziejusA;Dailė;0;1;1;1;1;0;0;1,23;0 +VilnausMuziejusA;Food;1;1;1;1;1;1;0;6,90;0 +VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0 diff --git a/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsKaunas.csv b/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsKaunas.csv new file mode 100644 index 0000000..f9a0d58 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsKaunas.csv @@ -0,0 +1,5 @@ +Kaunas +Jonas Joniausias +KaunoMuziejusB;Computer;1;1;0;1;1;1;1;14,39;1 +KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0 +KaunoMuziejusB;Space;1;1;1;1;0;1;1;0,99;0 diff --git a/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsVilnius.csv b/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsVilnius.csv new file mode 100644 index 0000000..4e4b204 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenter/tests/2/MuseumsVilnius.csv @@ -0,0 +1,5 @@ +Vilnius +Petras Petrausias +VilnausMuziejus2;Computer;1;1;0;1;1;1;1;0,00;0 +VilnausMuziejusA;Dailė;0;1;1;1;1;0;0;1,23;0 +VilnausMuziejusB;Food;1;1;1;1;1;1;0;6,90;0 diff --git a/Lab3/Lab3.TouristInformationCenterExtra/InOutUtils.cs b/Lab3/Lab3.TouristInformationCenterExtra/InOutUtils.cs new file mode 100644 index 0000000..c3d00ea --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/InOutUtils.cs @@ -0,0 +1,224 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.IO.Compression; + +namespace Lab3.TouristInformationCenterExtra +{ + /// + /// Class that stores functions that are related to reading and writing data + /// + class InOutUtils + { + /// + /// Append the museums from the second container to the first one + /// + /// First container + /// Second container + private static void AppendContainer(MuseumsContainer container1, MuseumsContainer container2) + { + for (int i = 0; i < container2.Count; i++) + { + container1.Add(container2.Get(i)); + } + } + + /// + /// Decode a single museum from a string + /// + /// Target city + /// Target manager + /// Encoded string + /// A decode museum + public static Museum DecodeMusem(string city, string manager, string encoded) + { + string[] values = encoded.Split(';'); + string name = values[0]; + string type = values[1]; + List workdays = new List(); + for (int i = 1; i <= 7; i++) + { + if (int.Parse(values[i + 1]) == 1) + { + workdays.Add((Weekday)i); + } + } + double price = double.Parse(values[9]); + bool hasGuide = int.Parse(values[10]) == 1; + + return new Museum(name, city, manager, type, workdays, price, hasGuide); + } + + /// + /// Decode a list of museums from a given list of lines. + /// + /// + /// Container of museums + public static MuseumsContainer DecodeMuseums(List lines) + { + MuseumsContainer container = new MuseumsContainer(lines.Count-2); + string city = lines[0]; + string manager = lines[1]; + for (int i = 2; i < lines.Count; i++) + { + Museum museum = DecodeMusem(city, manager, lines[i]); + container.Add(museum); + } + return container; + } + + /// + /// Read and decode a list of museums from a file. + /// + /// Target file + /// Container of museums + public static MuseumsContainer ReadMuseumsFromCSV(string filename) + { + List lines = new List(); + foreach (string line in File.ReadAllLines(filename, Encoding.UTF8)) + { + lines.Add(line); + } + return DecodeMuseums(lines); + } + + /// + /// Read all the entries from a zip file and decode the museums inside the csv entries. + /// + /// Target filename + /// Register of museums + public static MuseumsContainer ReadMuseumsFromZIP(string filename) + { + MuseumsContainer mainContainer = new MuseumsContainer(); + + using (ZipArchive zipFile = ZipFile.Open(filename, ZipArchiveMode.Read)) + { + foreach (ZipArchiveEntry entry in zipFile.Entries) + { + if (!entry.Name.EndsWith(".csv")) continue; + List lines = new List(); + using (StreamReader reader = new StreamReader(entry.Open(), Encoding.UTF8)) + { + while (!reader.EndOfStream) + { + lines.Add(reader.ReadLine()); + } + } + + MuseumsContainer contaienr = DecodeMuseums(lines); + AppendContainer(mainContainer, contaienr); + } + + } + + return mainContainer; + } + + /// + /// Read and decode lists of museums from multiple files and put into a single register. + /// + /// Target files + /// Register containing museums from all files + public static MuseumsContainer ReadMuseums(params string[] filenames) + { + MuseumsContainer mainContainer = new MuseumsContainer(); + foreach (string filename in filenames) + { + if (filename.EndsWith(".csv")) + { + AppendContainer(mainContainer, ReadMuseumsFromCSV(filename)); + } + else if(filename.EndsWith(".zip")) + { + AppendContainer(mainContainer, ReadMuseumsFromZIP(filename)); + } + } + return mainContainer; + } + + private static string CreateMuseumLine(Museum museum) + { + string workDays = ""; + workDays += museum.Workdays.Contains(Weekday.Monday) ? "1" : "0"; + workDays += museum.Workdays.Contains(Weekday.Tuesday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Wednesday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Thursday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Friday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Saturday) ? ";1" : ";0"; + workDays += museum.Workdays.Contains(Weekday.Sunday) ? ";1" : ";0"; + + return String.Join(";", museum.City, museum.Name, workDays, museum.Price); + } + + /// + /// Write and encode a list of museums to a file. The file will be in a csv format using ";" as seperators. + /// + /// Target file + /// Container of museums + public static void WriteMuseums(string filename, MuseumsContainer container) + { + string[] lines = new string[container.Count]; + for (int i = 0; i < container.Count; i++) + { + Museum museum = container.Get(i); + lines[i] = CreateMuseumLine(museum); + } + File.WriteAllLines(filename, lines, Encoding.UTF8); + } + + /// + /// Write and encode a list of museums to a file from a register. + /// + /// Target location + /// Register containing museums + public static void WriteMuseums(string filename, MuseumsRegister register) + { + int n = register.Count(); + string[] lines = new string[n]; + for (int i = 0; i < n; i++) + { + Museum museum = register.GetByIndex(i); + lines[i] = CreateMuseumLine(museum); + } + File.WriteAllLines(filename, lines, Encoding.UTF8); + } + + /// + /// Write out a container of museums in a table to the console. + /// + /// Container of museums + public static void PrintMuseums(MuseumsContainer container) + { + if (container.Count == 0) + { + Console.WriteLine("Nėra"); + return; + } + + Console.WriteLine(new string('-', 115)); + Console.WriteLine("| {0,20} | {1,-10} | {2,20} | {3,-10} | {4,-18} | {5,-3} | {6,-4} |", "Vardas", "Miestas", "Atsakingas", "Tipas", "Darbo dienų kiekis", "Kaina", "Turi gidą?"); + Console.WriteLine(new string('-', 115)); + for (int i = 0; i < container.Count; i++) + { + Museum m = container.Get(i); + Console.WriteLine("| {0,20} | {1,-10} | {2, 20} | {3,-10} | {4,-18} | {5,-5:f2} | {6,-10} |", m.Name, m.City, m.Manager, m.Type, m.Workdays.Count, m.Price, m.HasGuide ? "Taip" : "Ne"); + } + Console.WriteLine(new string('-', 115)); + } + + /// + /// Write out a list of museums in a table to the console from a register. + /// + /// Register containing museums + public static void PrintMuseums(MuseumsRegister register) + { + MuseumsContainer museums = new MuseumsContainer(); + for (int i = 0; i < register.Count(); i++) + { + museums.Add(register.GetByIndex(i)); + } + PrintMuseums(museums); + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenterExtra/Lab3.TouristInformationCenterExtra.csproj b/Lab3/Lab3.TouristInformationCenterExtra/Lab3.TouristInformationCenterExtra.csproj new file mode 100644 index 0000000..0ebd173 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/Lab3.TouristInformationCenterExtra.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/Lab3/Lab3.TouristInformationCenterExtra/Museum.cs b/Lab3/Lab3.TouristInformationCenterExtra/Museum.cs new file mode 100644 index 0000000..8bff237 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/Museum.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +namespace Lab3.TouristInformationCenterExtra +{ + /// + /// Class used for storing data related a single museum. + /// + class Museum + { + public string Name { get; set; } + public string City { get; set; } + public string Manager { get; set; } + public string Type { get; set; } + public List Workdays { get; set; } + public double Price { get; set; } + public bool HasGuide { get; set; } + public Museum(string name, string city, string manager, string type, List workdays, double price, bool hasGuide) + { + Name = name; + Manager = manager; + City = city; + Type = type; + Workdays = workdays; + Price = price; + HasGuide = hasGuide; + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenterExtra/MuseumsContainer.cs b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsContainer.cs new file mode 100644 index 0000000..6ba73d2 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsContainer.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.IO.MemoryMappedFiles; + +namespace Lab3.TouristInformationCenterExtra +{ + class MuseumsContainer + { + + public int Count { get; private set; } + private static Random random = new Random(); + private string Filename; + private FileStream fs; + + public MuseumsContainer() + { + Filename = ".container"+random.Next(1000).ToString(); + fs = File.Create(Filename); + } + + public MuseumsContainer(MuseumsContainer container) : this() + { + for (int i = 0; i < container.Count; i++) + { + Add(container.Get(i)); + } + } + + ~MuseumsContainer() + { + fs.Close(); + //File.Delete(Filename); + } + + private void SeekToEnd() + { + fs.Seek(0, SeekOrigin.End); + } + + private static Museum DecodeMuseum(string encoded) + { + string[] values = encoded.Split(';'); + string name = values[0]; + string city = values[1]; + string manager = values[2]; + string type = values[3]; + List workdays = new List(); + for (int i = 1; i <= 7; i++) + { + if (int.Parse(values[i+3]) == 1) + { + workdays.Add((Weekday)i); + } + } + double price = double.Parse(values[11]); + bool hasGuide = int.Parse(values[12]) == 1; + + return new Museum(name, city, manager, type, workdays, price, hasGuide); + } + + private static string EncodeMuseum(Museum museum) + { + int[] workDays = new int[museum.Workdays.Count]; + for (int i = 0; i < museum.Workdays.Count; i++) + { + Weekday day = museum.Workdays[i]; + workDays[i] = Convert.ToInt32(day); + } + + int hasGuide = Convert.ToInt32(museum.HasGuide); + string workDaysJoined = String.Join(";", workDays); + + return String.Join(";", museum.Name, museum.City, museum.Manager, museum.Type, workDaysJoined, museum.Price, hasGuide); + } + + /// + /// Add a museum to the end of the container + /// + /// Target museum + public void Add(Museum museum) + { + SeekToEnd(); + string encoded = EncodeMuseum(museum); + byte[] bytes = new UTF8Encoding(true).GetBytes(encoded); + fs.Write(bytes); + Count++; + } + + /// + /// Get a musem by index from container + /// + /// Target index + /// Museum at given index + public Museum Get(int index) + { + return museums[index]; + } + + /// + /// Check if given museum exists in container + /// + /// Target museum + /// True if contains + public bool Contains(Museum museum) + { + for (int i = 0; i < Count; i++) + { + if (museums[i].Equals(museum)) + { + return true; + } + } + return false; + } + + /// + /// Put a museum at a specific index + /// + /// Target index + /// Target museum + public void Put(int index, Museum museum) + { + museums[index] = museum; + } + + /// + /// Filter by property "HasGuide" + /// + /// Target value + /// A filtered container of museums + public MuseumsContainer FilterByGuide(bool hasGuide) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.HasGuide == hasGuide) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Type". + /// + /// Target type + /// A filtered container of museums + public MuseumsContainer FilterByType(string type) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Type == type) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Price". + /// + /// Maximum allowed price to be included + /// A container with museums that dont't have a larger price then "maxPrice" + public MuseumsContainer FilterByPrice(double maxPrice) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Price <= maxPrice) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Type" + /// + /// Target city + /// A container of museums + public MuseumsContainer FilterByCity(string city) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.City == city) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Return a list of "active" museums from the contaner. + /// A museum is considered active if it is working at least some amount a week. + /// + /// Threshold which determines what is active + /// A container of "active" museums + public MuseumsContainer FilterByActivity(int threshold) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Workdays.Count >= threshold) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Filter by property "Name" + /// + /// Target name + /// A filtered container + public MuseumsContainer FilterByName(string name) + { + MuseumsContainer filtered = new MuseumsContainer(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (museum.Name == name) + { + filtered.Add(museum); + } + } + return filtered; + } + + /// + /// Get all of the different types of cities. + /// + /// A list of city names + public List GetAllCities() + { + List cities = new List(); + for (int i = 0; i < Count; i++) + { + Museum museum = museums[i]; + if (!cities.Contains(museum.City)) + { + cities.Add(museum.City); + } + } + return cities; + } + + /// + /// Sort container by "City" and "Name" in alphabetical order. + /// + public void Sort() + { + for (int i = 0; i < Count - 1; i++) + { + for (int j = i + 1; j < Count; j++) + { + Museum a = museums[i]; + Museum b = museums[j]; + int nameCompare = a.Name.CompareTo(b.Name); + int cityCompare = a.City.CompareTo(b.City); + if (cityCompare > 0 || (cityCompare == 0 && nameCompare > 0)) + { + museums[i] = b; + museums[j] = a; + } + } + } + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenterExtra/MuseumsKaunas.csv b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsKaunas.csv new file mode 100644 index 0000000..f3076a3 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsKaunas.csv @@ -0,0 +1,8 @@ +Kaunas +Jonas Jonaitis +KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0 +KaunoMuziejusB;Computer;1;1;0;1;1;1;1;14,39;1 +KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0 +KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1 +KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;1 +KaunoMuziejusB;Space;1;1;1;1;0;1;1;0,99;1 \ No newline at end of file diff --git a/Lab3/Lab3.TouristInformationCenterExtra/MuseumsRegister.cs b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsRegister.cs new file mode 100644 index 0000000..e8d6d44 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsRegister.cs @@ -0,0 +1,166 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab3.TouristInformationCenterExtra +{ + /// + /// Class used to store multiple museums in one place + /// + class MuseumsRegister + { + private MuseumsContainer AllMuseums; + + public MuseumsRegister() + { + AllMuseums = new MuseumsContainer(); + } + + public MuseumsRegister(MuseumsContainer museums) + { + AllMuseums = new MuseumsContainer(museums); + } + + /// + /// Add one museum to the register. + /// + /// Target museum + public void Add(Museum museum) + { + AllMuseums.Add(museum); + } + + /// + /// The amount of stored museums in the register. + /// + /// Museum count + public int Count() + { + return AllMuseums.Count; + } + + /// + /// Access museum from register by index + /// + /// Target index + /// Museum + public Museum GetByIndex(int index) + { + return AllMuseums.Get(index); + } + + /// + /// Return a list of active museums from the register. + /// A museum is considered active if it is working at least some amount a week. + /// + /// Threshold which determines what is active + /// A list of active museums + public MuseumsContainer FilterByActiveMuseums(int threshold) + { + return AllMuseums.FilterByActivity(threshold); + } + + /// + /// Find museum that work the most days in a week + /// + /// Most active museum + public Museum FindMostActiveMuseum() + { + if (AllMuseums.Count == 0) + { + return null; + } + + Museum mostActive = AllMuseums.Get(0); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (museum.Workdays.Count > mostActive.Workdays.Count) + { + mostActive = museum; + } + } + return mostActive; + } + + /// + /// Find all museums that work the most days in a week + /// + /// + public MuseumsContainer FindMostActiveMuseums() + { + Museum mostActive = FindMostActiveMuseum(); + MuseumsContainer activeMuseums = new MuseumsContainer(); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (museum.Workdays.Count == mostActive.Workdays.Count) + { + activeMuseums.Add(museum); + } + } + return activeMuseums; + } + + /// + /// Get all of the different types of cities. + /// + /// A list of city names + public List GetAllCities() + { + return AllMuseums.GetAllCities(); + } + + /// + /// Filter the museums by city name from register + /// + /// Target city + /// A container of museums + public MuseumsContainer FilterByCity(string city) + { + return AllMuseums.FilterByCity(city); + } + + /// + /// Filter by property "Type". + /// + /// Target type + /// A filtered container of museums + public MuseumsContainer FilterByType(string type) + { + return AllMuseums.FilterByType(type); + } + + private List GetMuseumNames() + { + List result = new List(); + for (int i = 0; i < AllMuseums.Count; i++) + { + Museum museum = AllMuseums.Get(i); + if (!result.Contains(museum.Name)) + { + result.Add(museum.Name); + } + } + return result; + } + + public MuseumsContainer FindMuseumsWithDuplicateNames() + { + MuseumsContainer result = new MuseumsContainer(); + List names = GetMuseumNames(); + foreach (string name in names) + { + MuseumsContainer museumsByName = AllMuseums.FilterByName(name); + if (museumsByName.Count <= 1) continue; + + for (int i = 0; i < museumsByName.Count; i++) + { + result.Add(museumsByName.Get(i)); + } + } + return result; + } + + } +} diff --git a/Lab3/Lab3.TouristInformationCenterExtra/MuseumsVilnius.csv b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsVilnius.csv new file mode 100644 index 0000000..7cb90da --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/MuseumsVilnius.csv @@ -0,0 +1,7 @@ +Vilnius +Petras Petraitis +VilnausMuziejus1;Dailė;1;0;0;1;1;0;0;5,49;0 +VilnausMuziejus2;Computer;1;1;0;1;1;1;1;0,00;1 +VilnausMuziejusA;Dailė;0;1;1;1;1;0;0;1,23;0 +VilnausMuziejusA;Food;1;1;1;1;1;1;0;6,90;0 +VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0 \ No newline at end of file diff --git a/Lab3/Lab3.TouristInformationCenterExtra/Program.cs b/Lab3/Lab3.TouristInformationCenterExtra/Program.cs new file mode 100644 index 0000000..317e5f5 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/Program.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; + +namespace Lab3.TouristInformationCenterExtra +{ + class Program + { + static void Main(string[] args) + { + List workDays = new List { Weekday.Monday }; + Museum museum = new Museum("Foo", "Bar", "Baz", "IDK", workDays, 4.20, true); + + MuseumsContainer container = new MuseumsContainer(); + container.Add(museum); + + /* + // Read all museums from initial data files + MuseumsContainer container = InOutUtils.ReadMuseums("MuseumsKaunas.csv", "MuseumsVilnius.csv"); + MuseumsRegister register = new MuseumsRegister(container); + Console.WriteLine("Visi muziejai:"); + InOutUtils.PrintMuseums(register); + Console.WriteLine(); + + // Write out a list of cities and if they have at least one museum that is free and with a guide + List cities = register.GetAllCities(); + foreach (string city in cities) + { + MuseumsContainer freeMuseumsWithGuide = register + .FilterByCity(city) + .FilterByPrice(0) + .FilterByGuide(true); + bool passesCriteria = freeMuseumsWithGuide.Count > 0; + Console.WriteLine("{0}: {1}", city, passesCriteria ? "Taip" : "Ne"); + } + Console.WriteLine(); + + // Find all museums that are the most active + MuseumsContainer mostActiveMuseums = register.FindMostActiveMuseums(); + Console.WriteLine("Aktyviausi muziejai:"); + InOutUtils.PrintMuseums(mostActiveMuseums); + Console.WriteLine(); + + // Find museums that have duplicate names + MuseumsContainer museumsWithDuplicateNames = register.FindMuseumsWithDuplicateNames(); + InOutUtils.WriteMuseums("Sutampta.csv", museumsWithDuplicateNames); + + // Find all art museums and sort them + MuseumsContainer artMuseums = register.FilterByType("Dailė"); + artMuseums.Sort(); + InOutUtils.WriteMuseums("Dailė.csv", artMuseums); + */ + } + } +} diff --git a/Lab3/Lab3.TouristInformationCenterExtra/Weekday.cs b/Lab3/Lab3.TouristInformationCenterExtra/Weekday.cs new file mode 100644 index 0000000..221ca39 --- /dev/null +++ b/Lab3/Lab3.TouristInformationCenterExtra/Weekday.cs @@ -0,0 +1,17 @@ + +namespace Lab3.TouristInformationCenterExtra +{ + /// + /// Used for storing weekdays as numbers rather than strings + /// + enum Weekday + { + Monday = 1, + Tuesday = 2, + Wednesday = 3, + Thursday = 4, + Friday = 5, + Saturday = 6, + Sunday = 7 + } +} diff --git a/Lab3/Lab3.sln b/Lab3/Lab3.sln new file mode 100644 index 0000000..2e397d8 --- /dev/null +++ b/Lab3/Lab3.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1525 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.Exercises.Register", "Lab3.Exercises.Register\Lab3.Exercises.Register.csproj", "{2379B111-C112-4C7F-B4D9-9149B809E980}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Savarankiskas4", "Savarankiskas4\Savarankiskas4.csproj", "{FA2F782C-658B-4ED2-A93F-B2B334FBCBB8}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.TouristInformationCenter", "Lab3.TouristInformationCenter\Lab3.TouristInformationCenter.csproj", "{C89E170A-8C75-483F-B992-78C8B6DB08DB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.TouristInformationCenterExtra", "Lab3.TouristInformationCenterExtra\Lab3.TouristInformationCenterExtra.csproj", "{86E4FDD5-ADBC-434A-966F-2FD59428ECBF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2379B111-C112-4C7F-B4D9-9149B809E980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2379B111-C112-4C7F-B4D9-9149B809E980}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2379B111-C112-4C7F-B4D9-9149B809E980}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2379B111-C112-4C7F-B4D9-9149B809E980}.Release|Any CPU.Build.0 = Release|Any CPU + {FA2F782C-658B-4ED2-A93F-B2B334FBCBB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA2F782C-658B-4ED2-A93F-B2B334FBCBB8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA2F782C-658B-4ED2-A93F-B2B334FBCBB8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA2F782C-658B-4ED2-A93F-B2B334FBCBB8}.Release|Any CPU.Build.0 = Release|Any CPU + {C89E170A-8C75-483F-B992-78C8B6DB08DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C89E170A-8C75-483F-B992-78C8B6DB08DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C89E170A-8C75-483F-B992-78C8B6DB08DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C89E170A-8C75-483F-B992-78C8B6DB08DB}.Release|Any CPU.Build.0 = Release|Any CPU + {86E4FDD5-ADBC-434A-966F-2FD59428ECBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86E4FDD5-ADBC-434A-966F-2FD59428ECBF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86E4FDD5-ADBC-434A-966F-2FD59428ECBF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86E4FDD5-ADBC-434A-966F-2FD59428ECBF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {27634FF3-4EBA-49EC-9207-F08AD686478C} + EndGlobalSection +EndGlobal diff --git a/Lab3/README.md b/Lab3/README.md new file mode 100644 index 0000000..41d88da --- /dev/null +++ b/Lab3/README.md @@ -0,0 +1,6 @@ +# Lab3 example project + +This project is from the "Object oriented programming" course in KTU. + +It is not polished in any way, so don't expect anything from this. + diff --git a/Lab3/Savarankiskas4/Chemijos.csv b/Lab3/Savarankiskas4/Chemijos.csv new file mode 100644 index 0000000..1d26a28 --- /dev/null +++ b/Lab3/Savarankiskas4/Chemijos.csv @@ -0,0 +1,6 @@ +Chemijos fakultetas +Watson;Brauner;CH-1/2;2;8;10 +Kelly;Brian;CH-1/2;3;9;7;6 +Garcia;Blair;CH-1/2;2;6;5 +Kennedy;Benjamin;CI-1/1;2;7;8 +Kouma;Peter;CI-1/1;3;5;5;6 \ No newline at end of file diff --git a/Lab3/Savarankiskas4/Elektros.csv b/Lab3/Savarankiskas4/Elektros.csv new file mode 100644 index 0000000..40652e6 --- /dev/null +++ b/Lab3/Savarankiskas4/Elektros.csv @@ -0,0 +1,7 @@ +Elektros fakultetas +Lee;Amy;EL-1/1;2;10;10 +Cole;Annie ;EL-1/1;3;10;10;10 +Lowe;Rose;EL-1/1;4;10;10;10;10 +Moore;Mary;EL-1/2;3;10;10;10 +White;Alicia;EL-1/2;2;10;10 +Bruce;Lucy;EL-1/2;3;10;10;10 \ No newline at end of file diff --git a/Lab3/Savarankiskas4/InOutUtils.cs b/Lab3/Savarankiskas4/InOutUtils.cs new file mode 100644 index 0000000..8b494c1 --- /dev/null +++ b/Lab3/Savarankiskas4/InOutUtils.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Savarankiskas4 +{ + static class InOutUtils + { + public static StudentsContainer ReadStudents(string filename) + { + StudentsContainer container = new StudentsContainer(); + string[] lines = File.ReadAllLines(filename, Encoding.UTF8); + string faculty = lines[0].TrimEnd(); + for (int i = 1; i < lines.Length; i++) + { + string[] values = lines[i].Split(';'); + string surname = values[0]; + string name = values[1]; + string group = values[2]; + int gradeCount = int.Parse(values[3]); + List grades = new List(); + for (int j = 0; j < gradeCount; j++) + { + int grade = int.Parse(values[4 + j]); + grades.Add(grade); + } + Student student = new Student(surname, name, faculty, group, grades); + container.Add(student); + } + return container; + } + + private static int FindTotalCount(StudentsContainer[] containers) + { + int count = 0; + foreach (StudentsContainer container in containers) + { + count += container.Count; + } + return count; + } + + private static StudentsContainer MergeContainers(StudentsContainer[] containers) + { + int count = FindTotalCount(containers); + StudentsContainer merged = new StudentsContainer(Math.Max(count, 1)); + foreach (StudentsContainer container in containers) + { + for (int i = 0; i < container.Count; i++) + { + merged.Add(container.Get(i)); + } + } + return merged; + } + + public static StudentsContainer ReadStudents(params string[] filenames) + { + StudentsContainer[] containers = new StudentsContainer[filenames.Length]; + for (int i = 0; i < filenames.Length; i++) + { + string filename = filenames[i]; + containers[i] = ReadStudents(filename); + } + + return MergeContainers(containers); + } + + public static void PrintStudents(string label, StudentsContainer container) + { + Console.WriteLine(new string('-', 99)); + Console.WriteLine("| {0,-95} |", label); + Console.WriteLine(new string('-', 99)); + Console.WriteLine("| {0,-15} | {1,-15} | {2,-25} | {3,-12} | {4,16} |", "Pavardė", "Vardas", "Fakultetas", "Grupė", "Pažymių vidurkis"); + Console.WriteLine(new string('-', 99)); + for (int i = 0; i < container.Count; i++) + { + Student student = container.Get(i); + Console.WriteLine("| {0,-15} | {1,-15} | {2,-25} | {3,-12} | {4,16:f2} |", student.Surname, student.Name, student.Faculty, student.Group, student.GradesAverage); + } + Console.WriteLine(new string('-', 99)); + } + } +} diff --git a/Lab3/Savarankiskas4/Informatikos.csv b/Lab3/Savarankiskas4/Informatikos.csv new file mode 100644 index 0000000..132eddb --- /dev/null +++ b/Lab3/Savarankiskas4/Informatikos.csv @@ -0,0 +1,7 @@ +Informatikos fakultetas +Puzonas;Rokas;IF-1/1;2;10;10 +Mercedes;Sarah;IF-1/1;3;7;8;9 +Mary;Elizabeth;IF-1/1;2;9;10 +Ben;Niamh;IF-1/2;3;9;8;7 +Reyes;Hannah;IF-1/2;4;10;9;10;10 +William;Beth;IF-1/2;2;10;10 \ No newline at end of file diff --git a/Lab3/Savarankiskas4/Program.cs b/Lab3/Savarankiskas4/Program.cs new file mode 100644 index 0000000..dc158e2 --- /dev/null +++ b/Lab3/Savarankiskas4/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; + +namespace Savarankiskas4 +{ + class Program + { + static void Main(string[] args) + { + StudentsContainer container = InOutUtils.ReadStudents("Chemijos.csv", "Elektros.csv", "Informatikos.csv"); + InOutUtils.PrintStudents("Studentai", container); + Console.WriteLine(); + + StudentsRegister register = new StudentsRegister(container); + List> averages = register.FindGradesAverages(); + Console.WriteLine("Grupių pažymių vidurkiai:"); + foreach (Tuple tuple in averages) + { + Console.WriteLine("{0}: {1}", tuple.Item1, tuple.Item2); + } + } + } +} + diff --git a/Lab3/Savarankiskas4/Savarankiskas4.csproj b/Lab3/Savarankiskas4/Savarankiskas4.csproj new file mode 100644 index 0000000..b8d8638 --- /dev/null +++ b/Lab3/Savarankiskas4/Savarankiskas4.csproj @@ -0,0 +1,20 @@ + + + + Exe + netcoreapp2.1 + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/Lab3/Savarankiskas4/Student.cs b/Lab3/Savarankiskas4/Student.cs new file mode 100644 index 0000000..5e0f5a2 --- /dev/null +++ b/Lab3/Savarankiskas4/Student.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Savarankiskas4 +{ + class Student + { + public string Surname { get; set; } + public string Name { get; set; } + public string Faculty { get; set; } + public string Group { get; set; } + public List Grades { get; set; } + public float GradesAverage + { + get + { + float sum = 0; + foreach (int grade in Grades) + { + sum += grade; + } + return sum/Grades.Count; + } + } + + public Student(string surname, string name, string faculty, string group, List grades) + { + Surname = surname; + Name = name; + Faculty = faculty; + Group = group; + Grades = grades; + } + + public override bool Equals(object obj) + { + var student = obj as Student; + return student != null && + Surname == student.Surname && + Name == student.Name && + Faculty == student.Faculty && + Group == student.Group; + } + + public override int GetHashCode() + { + return HashCode.Combine(Surname, Name, Faculty, Group); + } + + } +} diff --git a/Lab3/Savarankiskas4/StudentsContainer.cs b/Lab3/Savarankiskas4/StudentsContainer.cs new file mode 100644 index 0000000..db8f368 --- /dev/null +++ b/Lab3/Savarankiskas4/StudentsContainer.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Savarankiskas4 +{ + class StudentsContainer + { + private int Capacity; + public int Count { get; private set; } = 0; + private Student[] students; + public StudentsContainer(int capacity = 16) + { + Capacity = capacity; + students = new Student[capacity]; + } + + public void Add(Student student) + { + if (Count == Capacity) + { + EnsureCapacity(Capacity * 2); + } + + students[Count] = student; + Count++; + } + + public void Put(int index, Student student) + { + students[index] = student; + } + + public Student Get(int index) + { + return students[index]; + } + + public bool Contains(Student student) + { + for (int i = 0; i < Count; i++) + { + if (students[i].Equals(student)) + { + return true; + } + } + return false; + } + + public void Insert(int index, Student student) + { + for (int i = Count + 1; i > index; i--) + { + students[i] = students[i - 1]; + } + students[index] = student; + Count++; + } + + public void RemoveAt(int index) + { + for (int i = index; i < Count; i++) + { + students[i] = students[i + 1]; + } + Count--; + } + + public void Remove(Student student) + { + for (int i = 0; i < Count; i++) + { + if (students[i].Equals(student)) + { + RemoveAt(i); + return; + } + } + } + + private void EnsureCapacity(int minimumCapacity) + { + if (minimumCapacity > Capacity) + { + Student[] dogsClone = new Student[minimumCapacity]; + for (int i = 0; i < Count; i++) + { + dogsClone[i] = students[i]; + } + Capacity = minimumCapacity; + students = dogsClone; + } + } + + } +} diff --git a/Lab3/Savarankiskas4/StudentsRegister.cs b/Lab3/Savarankiskas4/StudentsRegister.cs new file mode 100644 index 0000000..64a121e --- /dev/null +++ b/Lab3/Savarankiskas4/StudentsRegister.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Savarankiskas4 +{ + class StudentsRegister + { + private StudentsContainer students; + public StudentsRegister() + { + students = new StudentsContainer(); + } + public StudentsRegister(StudentsContainer container) + { + students = new StudentsContainer(); + for (int i = 0; i < container.Count; i++) + { + students.Add(container.Get(i)); + } + } + + public void Add(Student student) + { + students.Add(student); + } + + public Student Get(int index) + { + return students.Get(index); + } + + public int StudentsCount() + { + return students.Count; + } + + public List FindAllGroups() + { + List groups = new List(); + for (int i = 0; i < students.Count; i++) + { + Student student = students.Get(i); + if (!groups.Contains(student.Group)) + { + groups.Add(student.Group); + } + } + return groups; + } + + public float FindGradesAverageByGroup(string group) + { + float sum = 0; + int n = 0; + for (int i = 0; i < students.Count; i++) + { + Student student = students.Get(i); + if (student.Group == group) + { + sum += student.GradesAverage; + n++; + } + } + return sum / n; + } + + public List> FindGradesAverages() + { + List groups = FindAllGroups(); + List> averages = new List>(); + foreach (string group in groups) + { + float average = FindGradesAverageByGroup(group); + averages.Add(new Tuple(group, average)); + } + + averages.Sort(delegate (Tuple group1, Tuple group2) { + int gradesComparison = -group1.Item2.CompareTo(group2.Item2); + if (gradesComparison == 0) + { + return group1.Item1.CompareTo(group2.Item1); + } else + { + return gradesComparison; + } + }); + + return averages; + } + + } +} diff --git a/Lab3/global.json b/Lab3/global.json new file mode 100644 index 0000000..4aa91ae --- /dev/null +++ b/Lab3/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "3.0.103" + } +}