From c1dff4fabf9d981a23586013b287b56bbbf27c41 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 15 Dec 2021 20:00:20 +0200 Subject: [PATCH] feat: complete "Lab5.TouristInformationCenter" --- Lab5/Lab5.Sport/Lab5.Sport.csproj | 11 +- .../InOutUtils.cs | 110 +++++++++++++----- .../Lab5.TouristInformationCenter.csproj | 14 ++- .../Lab5.TouristInformationCenter/Location.cs | 10 +- .../LocationsAlytus.csv | 9 ++ .../LocationsContainer.cs | 99 ---------------- .../LocationsKaunas.csv | 12 +- .../LocationsVilnius.csv | 10 +- Lab5/Lab5.TouristInformationCenter/Museum.cs | 4 +- Lab5/Lab5.TouristInformationCenter/Program.cs | 38 ++---- .../Lab5.TouristInformationCenter/Register.cs | 110 +----------------- Lab5/Lab5.TouristInformationCenter/Statue.cs | 2 +- 12 files changed, 143 insertions(+), 286 deletions(-) diff --git a/Lab5/Lab5.Sport/Lab5.Sport.csproj b/Lab5/Lab5.Sport/Lab5.Sport.csproj index 958d2f1..6cf5e32 100644 --- a/Lab5/Lab5.Sport/Lab5.Sport.csproj +++ b/Lab5/Lab5.Sport/Lab5.Sport.csproj @@ -2,7 +2,16 @@ Exe - netcoreapp3.0 + netcoreapp2.1 + + + PreserveNewest + + + PreserveNewest + + + diff --git a/Lab5/Lab5.TouristInformationCenter/InOutUtils.cs b/Lab5/Lab5.TouristInformationCenter/InOutUtils.cs index 67dc32c..2663303 100644 --- a/Lab5/Lab5.TouristInformationCenter/InOutUtils.cs +++ b/Lab5/Lab5.TouristInformationCenter/InOutUtils.cs @@ -37,22 +37,39 @@ namespace Lab5.TouristInformationCenter 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; */ + string[] values = line.Split(';', StringSplitOptions.RemoveEmptyEntries); + string name = values[0]; + string address = values[1]; + int year = int.Parse(values[2]); - /* Location location = new Location(name, city, manager, type, workdays, price, hasGuide); */ - /* container.Add(location); */ + Location location; + if (values.Length == 5) + { + string author = values[3]; + string monumentName = values[4]; + location = new Statue(city, manager, name, address, year, author, monumentName); + } + else if (values.Length == 13) + { + string type = values[3]; + List workdays = new List(); + for (int j = 1; j <= 7; j++) + { + if (int.Parse(values[j + 3]) == 1) + { + workdays.Add((Weekday)j); + } + } + double price = double.Parse(values[11]); + bool hasGuide = int.Parse(values[12]) == 1; + location = new Museum(city, manager, name, address, year, type, workdays, price, hasGuide); + } + else + { + throw new Exception($"Attempt to parse unknown line: {lines[i]}"); + } + + container.Add(location); } return container; } @@ -128,17 +145,28 @@ namespace Lab5.TouristInformationCenter private static string CreateLocationLine(Location location) { -/* string workDays = ""; */ -/* workDays += location.Workdays.Contains(Weekday.Monday) ? "1" : "0"; */ -/* workDays += location.Workdays.Contains(Weekday.Tuesday) ? ";1" : ";0"; */ -/* workDays += location.Workdays.Contains(Weekday.Wednesday) ? ";1" : ";0"; */ -/* workDays += location.Workdays.Contains(Weekday.Thursday) ? ";1" : ";0"; */ -/* workDays += location.Workdays.Contains(Weekday.Friday) ? ";1" : ";0"; */ -/* workDays += location.Workdays.Contains(Weekday.Saturday) ? ";1" : ";0"; */ -/* workDays += location.Workdays.Contains(Weekday.Sunday) ? ";1" : ";0"; */ -/* */ -/* return String.Join(";", location.City, location.Name, workDays, location.Price); */ - return ""; + if (location is Museum) + { + Museum m = location as Museum; + string workDays = ""; + workDays += m.Workdays.Contains(Weekday.Monday) ? "1" : "0"; + workDays += m.Workdays.Contains(Weekday.Tuesday) ? ";1" : ";0"; + workDays += m.Workdays.Contains(Weekday.Wednesday) ? ";1" : ";0"; + workDays += m.Workdays.Contains(Weekday.Thursday) ? ";1" : ";0"; + workDays += m.Workdays.Contains(Weekday.Friday) ? ";1" : ";0"; + workDays += m.Workdays.Contains(Weekday.Saturday) ? ";1" : ";0"; + workDays += m.Workdays.Contains(Weekday.Sunday) ? ";1" : ";0"; + return String.Join(";", m.Name, m.Address, m.Year, m.Type, workDays, m.Price, m.HasGuide ? "1" : "0"); + } + else if (location is Statue) + { + Statue s = location as Statue; + return String.Join(";", s.Name, s.Address, s.Year, s.Author, s.MonumentName); + } else + { + throw new Exception("What you doin??"); + } + } /// @@ -157,6 +185,26 @@ namespace Lab5.TouristInformationCenter File.WriteAllLines(filename, lines, Encoding.UTF8); } + /// + /// Write and encode a list of statues from a container to a file + /// + /// Target file + /// Target container + public static void WriteStatues(string filename, LocationsContainer container) + { + using (StreamWriter writer = File.CreateText(filename)) + { + for (int i = 0; i < container.Count; i++) + { + Statue statue = container.Get(i) as Statue; + if (statue != null) + { + writer.WriteLine("{0};{1};{2};{3};{4}", statue.Name, statue.Address, statue.Year, statue.Author, statue.MonumentName); + } + } + } + } + /// /// Write and encode a list of locations to a file from a register. /// @@ -186,15 +234,15 @@ namespace Lab5.TouristInformationCenter 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)); + Console.WriteLine(new string('-', 84)); + Console.WriteLine("| {0,-10} | {1,-20} | {2,-18} | {3,-15} | {4,5} |", "Miestas", "Atsakingas", "Vardas", "Adresas", "Metai"); + Console.WriteLine(new string('-', 84)); for (int i = 0; i < container.Count; i++) { Location l = container.Get(i); - /* Console.WriteLine("| {0,-20} | {1,-10} | {2,-20} | {3,-10} | {4,18} | {5,13:f2} | {6,-10} |", l.Name, l.City, l.Manager, l.Type, l.Workdays.Count, l.Price, l.HasGuide ? "Taip" : "Ne"); */ + Console.WriteLine("| {0,-10} | {1,-20} | {2,-18} | {3,-15} | {4,5} |", l.City, l.Manager, l.Name, l.Address, l.Year); } - Console.WriteLine(new string('-', 123)); + Console.WriteLine(new string('-', 84)); } /// diff --git a/Lab5/Lab5.TouristInformationCenter/Lab5.TouristInformationCenter.csproj b/Lab5/Lab5.TouristInformationCenter/Lab5.TouristInformationCenter.csproj index 958d2f1..2a46536 100644 --- a/Lab5/Lab5.TouristInformationCenter/Lab5.TouristInformationCenter.csproj +++ b/Lab5/Lab5.TouristInformationCenter/Lab5.TouristInformationCenter.csproj @@ -2,7 +2,19 @@ Exe - netcoreapp3.0 + netcoreapp2.1 + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + diff --git a/Lab5/Lab5.TouristInformationCenter/Location.cs b/Lab5/Lab5.TouristInformationCenter/Location.cs index fdebed6..3a02217 100644 --- a/Lab5/Lab5.TouristInformationCenter/Location.cs +++ b/Lab5/Lab5.TouristInformationCenter/Location.cs @@ -3,19 +3,19 @@ namespace Lab5.TouristInformationCenter { abstract class Location { - public string Name { get; set; } public string City { get; set; } + public string Manager { get; set; } + public string Name { get; set; } public string Address { get; set; } public int Year { get; set; } - public string Manager { get; set; } - public Location(string name, string city, string address, int year, string manager) + public Location(string city, string manager, string name, string address, int year) { - Name = name; City = city; + Manager = manager; + Name = name; Address = address; Year = year; - Manager = manager; } public int CompareTo(Location other) diff --git a/Lab5/Lab5.TouristInformationCenter/LocationsAlytus.csv b/Lab5/Lab5.TouristInformationCenter/LocationsAlytus.csv index 973fad3..32454c4 100644 --- a/Lab5/Lab5.TouristInformationCenter/LocationsAlytus.csv +++ b/Lab5/Lab5.TouristInformationCenter/LocationsAlytus.csv @@ -1,2 +1,11 @@ Alytus Ona Onaite +AlytausMuziejusZ;GatveZ;1999;Dailė;1;0;0;0;1;0;1;3,40;0 +AlytausMuziejusX;GatveX;2021;Computer;1;1;0;1;1;1;1;14,39;1 +StatulaH;KlebonasGatve2;1995;Tas Klebonas;Klebobo paminklas +StatulaG;KlebonasGatve1;1995;Tas Klebonas;Klebobo paminklas +StatulaP;KlebonasGatve2;1995;Tas Klebonas;Klebobo paminklas +AlytausMuziejusV;GatveV;2014;Dailė;0;1;0;1;1;0;1;9,47;1 +AlytausMuziejusB;GatveX;1950;Space;1;1;0;0;0;0;1;1,99;1 +StatulaC;KlebonasGatve;1995;Tas Klebonas;Klebobo paminklas +AlytausMuziejusN;GatveN;1920;Space;1;1;1;1;0;1;1;0,99;1 diff --git a/Lab5/Lab5.TouristInformationCenter/LocationsContainer.cs b/Lab5/Lab5.TouristInformationCenter/LocationsContainer.cs index 52383d6..4dbd821 100644 --- a/Lab5/Lab5.TouristInformationCenter/LocationsContainer.cs +++ b/Lab5/Lab5.TouristInformationCenter/LocationsContainer.cs @@ -73,105 +73,6 @@ namespace Lab5.TouristInformationCenter locations[index] = location; } - /* - - /// - /// Filter by property "HasGuide" - /// - /// Target value - /// A filtered container of locations - public LocationsContainer FilterByGuide(bool hasGuide) - { - LocationsContainer filtered = new LocationsContainer(); - for (int i = 0; i < Count; i++) - { - Location location = locations[i]; - if (location.HasGuide == hasGuide) - { - filtered.Add(location); - } - } - return filtered; - } - - /// - /// Filter by property "Type". - /// - /// Target type - /// A filtered container of locations - public LocationsContainer FilterByType(string type) - { - LocationsContainer filtered = new LocationsContainer(); - for (int i = 0; i < Count; i++) - { - Location location = locations[i]; - if (location.Type == type) - { - filtered.Add(location); - } - } - return filtered; - } - - /// - /// Filter by property "Price". - /// - /// Maximum allowed price to be included - /// A container with locations that dont't have a larger price then "maxPrice" - public LocationsContainer FilterByPrice(double maxPrice) - { - LocationsContainer filtered = new LocationsContainer(); - for (int i = 0; i < Count; i++) - { - Location location = locations[i]; - if (location.Price <= maxPrice) - { - filtered.Add(location); - } - } - return filtered; - } - - /// - /// Return a list of "active" locations from the contaner. - /// A location is considered active if it is working at least some amount a week. - /// - /// Threshold which determines what is active - /// A container of "active" locations - public LocationsContainer FilterByActivity(int threshold) - { - LocationsContainer filtered = new LocationsContainer(); - for (int i = 0; i < Count; i++) - { - Location location = locations[i]; - if (location.Workdays.Count >= threshold) - { - filtered.Add(location); - } - } - return filtered; - } - - /// - /// Filter by property "Name" - /// - /// Target name - /// A filtered container - public LocationsContainer FilterByName(string name) - { - LocationsContainer filtered = new LocationsContainer(); - for (int i = 0; i < Count; i++) - { - Location location = locations[i]; - if (location.Name == name) - { - filtered.Add(location); - } - } - return filtered; - } - */ - /// /// Sort container by given comparator /// diff --git a/Lab5/Lab5.TouristInformationCenter/LocationsKaunas.csv b/Lab5/Lab5.TouristInformationCenter/LocationsKaunas.csv index f3076a3..d7412cf 100644 --- a/Lab5/Lab5.TouristInformationCenter/LocationsKaunas.csv +++ b/Lab5/Lab5.TouristInformationCenter/LocationsKaunas.csv @@ -1,8 +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 +KaunoMuziejus1;Gatve1;1998;Dailė;1;0;0;0;1;0;1;3,40;0 +KaunoMuziejusB;GatveB;2000;Computer;1;1;0;1;1;1;1;14,39;1 +KaunoMuziejus3;Gatve3;1969;History;0;1;0;0;1;0;0;3,26;0 +KaunoMuziejus4;Gatve4;1970;Dailė;0;1;0;1;1;0;1;9,47;1 +KaunoMuziejus5;Gatve5;1989;Space;1;1;0;0;0;0;1;1,99;1 +KaunoMuziejusB;GatveC;1991;Space;1;1;1;1;0;1;1;0,99;1 diff --git a/Lab5/Lab5.TouristInformationCenter/LocationsVilnius.csv b/Lab5/Lab5.TouristInformationCenter/LocationsVilnius.csv index 7cb90da..628e329 100644 --- a/Lab5/Lab5.TouristInformationCenter/LocationsVilnius.csv +++ b/Lab5/Lab5.TouristInformationCenter/LocationsVilnius.csv @@ -1,7 +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 +VilnausMuziejus1;Gatve1;2020;Dailė;1;0;0;1;1;0;0;5,49;0 +VilnausMuziejus2;Gatve2;1978;Computer;1;1;0;1;1;1;1;0,00;1 +VilnausMuziejusA;GatveA;1990;Dailė;0;1;1;1;1;0;0;1,23;0 +StatulaO;OnosGatve;1989;Tas Ona;Onos paminklas +VilnausMuziejus5;Gatve5;1999;History;0;1;0;1;1;0;1;10,49;0 diff --git a/Lab5/Lab5.TouristInformationCenter/Museum.cs b/Lab5/Lab5.TouristInformationCenter/Museum.cs index 0e769f6..dca3c6c 100644 --- a/Lab5/Lab5.TouristInformationCenter/Museum.cs +++ b/Lab5/Lab5.TouristInformationCenter/Museum.cs @@ -5,14 +5,14 @@ namespace Lab5.TouristInformationCenter /// /// Class used for storing data related a single museum. /// - class Location : Location + class Museum : Location { public string Type { get; set; } public List Workdays { get; set; } public double Price { get; set; } public bool HasGuide { get; set; } - public Location(string name, string city, string address, int year, string manager, string type, List workdays, double price, bool hasGuide) : base(name, city, address, year, manager) + public Museum(string city, string manager, string name, string address, int year, string type, List workdays, double price, bool hasGuide) : base(city, manager, name, address, year) { Type = type; Workdays = workdays; diff --git a/Lab5/Lab5.TouristInformationCenter/Program.cs b/Lab5/Lab5.TouristInformationCenter/Program.cs index f6f827f..2a48c45 100644 --- a/Lab5/Lab5.TouristInformationCenter/Program.cs +++ b/Lab5/Lab5.TouristInformationCenter/Program.cs @@ -7,7 +7,6 @@ namespace Lab5.TouristInformationCenter { static void Main(string[] args) { - // Read all museums from initial data files LocationsContainer container = InOutUtils.ReadLocations("LocationsKaunas.csv", "LocationsVilnius.csv", "LocationsAlytus.csv"); Register register = new Register(container); Console.WriteLine("Visos vietos:"); @@ -19,42 +18,21 @@ namespace Lab5.TouristInformationCenter Console.WriteLine(); List types = register.FindCommonTypesWithGuidesAtWeekends(); - Console.Write("Lankytinų vietų tipai kuriouse galima apsilankyti visuose miestuose savaitgaliais:"); + Console.WriteLine("Lankytinų vietų tipai kuriouse galima apsilankyti visuose miestuose savaitgaliais:"); foreach (string type in types) { Console.WriteLine("* {0}", type); } 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(); + Console.Write("Įveskite norimas autorius: "); + //string inputAuthor = Console.ReadLine(); + string inputAuthor = "Tas Klebonas"; + LocationsContainer locationsByAuthor = register.FindLocationsByAuthor(inputAuthor); + locationsByAuthor.Sort(new LocationsComparatorByNameAddress()); + InOutUtils.WriteStatues("PaminklaiAutorius.csv", locationsByAuthor); - // 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); - */ + InOutUtils.WriteLocations("Po1990.csv", register.FindLocationsAfterYear(1990)); } } } diff --git a/Lab5/Lab5.TouristInformationCenter/Register.cs b/Lab5/Lab5.TouristInformationCenter/Register.cs index d934b51..a6d6b9d 100644 --- a/Lab5/Lab5.TouristInformationCenter/Register.cs +++ b/Lab5/Lab5.TouristInformationCenter/Register.cs @@ -53,7 +53,7 @@ namespace Lab5.TouristInformationCenter int count = 0; for (int i = 0; i < AllLocations.Count; i++) { - Location museum = AllLocations.Get(i) as Location; + Museum museum = AllLocations.Get(i) as Museum; if (museum != null && museum.HasGuide) { count++; @@ -85,7 +85,7 @@ namespace Lab5.TouristInformationCenter List types = new List(); for (int i = 0; i < AllLocations.Count; i++) { - Location museum = AllLocations.Get(i) as Location; + Museum museum = AllLocations.Get(i) as Museum; if (museum != null && !types.Contains(museum.Type)) { types.Add(museum.Type); @@ -105,10 +105,11 @@ namespace Lab5.TouristInformationCenter { for (int i = 0; i < AllLocations.Count; i++) { - Location museum = AllLocations.Get(i) as Location; + Museum museum = AllLocations.Get(i) as Museum; if (museum != null && museum.Type == type && museum.City == city) { types[type]++; + break; } } } @@ -130,7 +131,7 @@ namespace Lab5.TouristInformationCenter for (int i = 0; i < AllLocations.Count; i++) { Museum museum = AllLocations.Get(i) as Museum; - if (museum != null && museum.Type == type && museum.HasGuide) + if (museum != null && museum.Type == type && museum.HasGuide && (museum.Workdays.Contains(Weekday.Saturday) || museum.Workdays.Contains(Weekday.Sunday))) { return true; } @@ -178,106 +179,5 @@ namespace Lab5.TouristInformationCenter } return locations; } - - /* - /// - /// Return a list of active locations from the register. - /// A location is considered active if it is working at least some amount a week. - /// - /// Threshold which determines what is active - /// A list of active locations - public LocationsContainer FilterByActiveLocations(int threshold) - { - return AllLocations.FilterByActivity(threshold); - } - - /// - /// Find location that work the most days in a week - /// - /// Most active location - public Location FindMostActiveLocation() - { - if (AllLocations.Count == 0) - { - return null; - } - - Location mostActive = AllLocations.Get(0); - for (int i = 0; i < AllLocations.Count; i++) - { - Location location = AllLocations.Get(i); - if (location.Workdays.Count > mostActive.Workdays.Count) - { - mostActive = location; - } - } - return mostActive; - } - - /// - /// Find all locations that work the most days in a week - /// - /// - public LocationsContainer FindMostActiveLocations() - { - Location mostActive = FindMostActiveLocation(); - LocationsContainer activeLocations = new LocationsContainer(); - for (int i = 0; i < AllLocations.Count; i++) - { - Location location = AllLocations.Get(i); - if (location.Workdays.Count == mostActive.Workdays.Count) - { - activeLocations.Add(location); - } - } - return activeLocations; - } - - /// - /// Filter by property "Type". - /// - /// Target type - /// A filtered container of locations - public LocationsContainer FilterByType(string type) - { - return AllLocations.FilterByType(type); - } - - private List GetLocationNames() - { - List result = new List(); - for (int i = 0; i < AllLocations.Count; i++) - { - Location location = AllLocations.Get(i); - if (!result.Contains(location.Name)) - { - result.Add(location.Name); - } - } - return result; - } - - /// - /// Find all locations that have matching names - /// - /// A container with locations that have matching names - public LocationsContainer FindLocationsWithDuplicateNames() - { - LocationsContainer result = new LocationsContainer(); - List names = GetLocationNames(); - foreach (string name in names) - { - LocationsContainer locationsByName = AllLocations.FilterByName(name); - if (locationsByName.Count <= 1) continue; - - for (int i = 0; i < locationsByName.Count; i++) - { - result.Add(locationsByName.Get(i)); - } - } - return result; - } - */ - } } diff --git a/Lab5/Lab5.TouristInformationCenter/Statue.cs b/Lab5/Lab5.TouristInformationCenter/Statue.cs index 1038bd9..706906f 100644 --- a/Lab5/Lab5.TouristInformationCenter/Statue.cs +++ b/Lab5/Lab5.TouristInformationCenter/Statue.cs @@ -5,7 +5,7 @@ namespace Lab5.TouristInformationCenter public string Author { get; set; } public string MonumentName { get; set; } - public Statue(string name, string city, string address, int year, string manager, string author, string monumentName) : base(name, city, address, year, manager) + public Statue(string city, string manager, string name, string address, int year, string author, string monumentName) : base(city, manager, name, address, year) { Author = author; MonumentName = monumentName;