1
0

fix: merge submodule 'Lab2'

This commit is contained in:
Rokas Puzonas 2021-12-08 21:01:55 +02:00
commit 920156fae5
33 changed files with 1793 additions and 0 deletions

14
Lab2/.editorconfig Normal file
View File

@ -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

388
Lab2/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.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();
}
}
}

View File

@ -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
1 123 Reksas Buldogas 2014-01-01 Male
2 124 Margis Dalmantinas 2014-02-28 Male
3 125 Bitė Senbernaras 2008-07-17 Female
4 320 Rikis Taksas 2012-01-07 Male
5 123 Reksas Buldogas 2014-01-01 Male
6 415 Pifas Taksas 2014-07-07 Male
7 420 Markas Dalmantinas 2013-02-28 Female
8 123 Reksas Buldogas 2014-01-01 Male

View File

@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.Exercises.Register
{
class DogsRegister
{
private List<Dog> AllDogs;
public DogsRegister()
{
AllDogs = new List<Dog>();
}
public DogsRegister(List<Dog> Dogs)
{
AllDogs = new List<Dog>();
foreach (Dog dog in Dogs)
{
this.AllDogs.Add(dog);
}
}
public void Add(Dog dog)
{
AllDogs.Add(dog);
}
public int DogsCount()
{
return AllDogs.Count;
}
public Dog GetByIndex(int index)
{
return AllDogs[index];
}
public int CountByGender(Gender gender)
{
int count = 0;
foreach (Dog dog in AllDogs)
{
if (dog.Gender.Equals(gender))
{
count++;
}
}
return count;
}
public Dog FindOldestDog()
{
return this.FindOldestDog(this.AllDogs);
}
public List<Dog> FilterByBreed(string breed)
{
List<Dog> Filtered = new List<Dog>();
foreach (Dog dog in AllDogs)
{
if (dog.Breed.Equals(breed)) // uses string method Equals()
{
Filtered.Add(dog);
}
}
return Filtered;
}
public Dog FindOldestDog(string breed)
{
List<Dog> Filtered = this.FilterByBreed(breed);
return this.FindOldestDog(Filtered);
}
private Dog FindOldestDog(List<Dog> Dogs)
{
Dog oldest = Dogs[0];
for (int i = 1; i < Dogs.Count; i++) //starts on index value 1
{
if (DateTime.Compare(oldest.BirthDate, Dogs[i].BirthDate) > 0)
{
oldest = Dogs[i];
}
}
return oldest;
}
private Dog FindDogByID(int ID)
{
foreach (Dog dog in AllDogs)
{
if (dog.ID == ID)
{
return dog;
}
}
return null;
}
public void UpdateVaccinationsInfo(List<Vaccination> Vaccinations)
{
foreach (Vaccination vaccination in Vaccinations)
{
Dog dog = FindDogByID(vaccination.DogID);
if (dog != null && vaccination > dog.LastVaccinationDate)
{
dog.LastVaccinationDate = vaccination.Date;
}
}
}
public List<Dog> FilterByVaccinationExpired()
{
List<Dog> Filtered = new List<Dog>();
foreach (Dog dog in AllDogs)
{
if (dog.RequiresVaccination)
{
Filtered.Add(dog);
}
}
return Filtered;
}
public bool Contains(Dog dog)
{
return AllDogs.Contains(dog);
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.Exercises.Register
{
enum Gender
{
Male = 1,
Female = 2,
}
}

View File

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Lab2.Exercises.Register
{
class InOutUtils
{
public static List<Vaccination> ReadVaccinations(string fileName)
{
List<Vaccination> Vaccinations = new List<Vaccination>();
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 DogsRegister ReadDogs(string fileName)
{
DogsRegister Dogs = new DogsRegister();
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)
{
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));
for (int i = 0; i < register.DogsCount(); i++)
{
Dog dog = register.GetByIndex(i);
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);
}
Console.WriteLine(new string('-', 74));
}
public static void PrintDogs(List<Dog> dogs)
{
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));
foreach (Dog dog in dogs)
{
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);
}
Console.WriteLine(new string('-', 74));
}
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<string> breeds)
{
foreach (string breed in breeds)
{
Console.WriteLine(breed);
}
}
public static void PrintDogsToCSVFile(string fileName, List<Dog> 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);
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="Dogs.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Vaccinations.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
namespace Lab2.Exercises.Register
{
class Program
{
static void Main(string[] args)
{
DogsRegister register = InOutUtils.ReadDogs("Dogs.csv");
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();
List<Vaccination> VaccinationsData = InOutUtils.ReadVaccinations(@"Vaccinations.csv");
register.UpdateVaccinationsInfo(VaccinationsData);
Console.WriteLine("Šunys kuriems reikia vakcinuotis:");
InOutUtils.PrintDogs(register.FilterByVaccinationExpired());
/*
Dog oldestDog = TaskUtils.FindOldestDog(register);
Console.WriteLine("Seniausias šuo:");
InOutUtils.PrintDog(oldestDog);
Console.WriteLine();
List<string> breeds = TaskUtils.FindBreeds(register);
Console.WriteLine("Šunų veislės:");
InOutUtils.PrintBreeds(breeds);
Console.WriteLine();
List<string> 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<Dog> 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);
*/
}
}
}

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.Exercises.Register
{
class TaskUtils
{
public static int CountByGender(List<Dog> dogs, Gender gender)
{
int count = 0;
foreach (Dog dog in dogs)
{
if (dog.Gender.Equals(gender))
{
count++;
}
}
return count;
}
public static Dog FindOldestDog(List<Dog> dogs)
{
Dog oldest = dogs[0]; // means least value
for (int i = 1; i < dogs.Count; i++)
{
if (DateTime.Compare(dogs[i].BirthDate, oldest.BirthDate) < 0)
{
oldest = dogs[i];
}
}
return oldest;
}
public static List<string> FindBreeds(List<Dog> dogs)
{
List<string> Breeds = new List<string>();
foreach (Dog dog in dogs)
{
string breed = dog.Breed;
if (!Breeds.Contains(breed)) // uses List method Contains()
{
Breeds.Add(breed);
}
}
return Breeds;
}
public static List<Dog> FilterByBreed(List<Dog> dogs, string breed)
{
List<Dog> Filtered = new List<Dog>();
foreach (Dog dog in dogs)
{
if (dog.Breed.Equals(breed)) // uses string method Equals()
{
Filtered.Add(dog);
}
}
return Filtered;
}
public static int CountByBreed(List<Dog> dogs, string breed)
{
int count = 0;
foreach (Dog dog in dogs)
{
if (dog.Breed.Equals(breed)) // uses string method Equals()
{
count++;
}
}
return count;
}
public static List<string> FindMostPopularBreeds(List<Dog> dogs)
{
List<string> breeds = FindBreeds(dogs);
int mostPopularCount = CountByBreed(dogs, breeds[0]);
foreach (string breed in breeds)
{
int count = CountByBreed(dogs, breed);
if (count > mostPopularCount)
{
mostPopularCount = count;
}
}
List<string> popularBreeds = new List<string>();
foreach (string breed in breeds)
{
if (CountByBreed(dogs, breed) == mostPopularCount)
{
popularBreeds.Add(breed);
}
}
return popularBreeds;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.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;
}
}
}

View File

@ -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
1 123 2020-07-24
2 124 2021-05-17
3 421 2021-07-01
4 125 2021-01-01
5 320 2021-07-01
6 320 2010-07-01

View File

@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace Lab2.TouristInformationCenter
{
/// <summary>
/// Class that stores functions that are related to reading and writing data
/// </summary>
class InOutUtils
{
/// <summary>
/// Append the museums from the second register to the first one
/// </summary>
/// <param name="register1">First register</param>
/// <param name="register2">Second register</param>
private static void AppendRegister(MuseumsRegister register1, MuseumsRegister register2)
{
for (int i = 0; i < register2.Count(); i++)
{
register1.Add(register2.GetByIndex(i));
}
}
/// <summary>
/// Decode a list of museums from a given list of lines.
/// </summary>
/// <param name="lines"></param>
/// <returns>Register of museums</returns>
public static MuseumsRegister DecodeMuseums(List<string> lines)
{
List<Museum> museums = new List<Museum>();
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<Weekday> workdays = new List<Weekday>();
if (int.Parse(values[2]) == 1)
{
workdays.Add(Weekday.Monday);
}
if (int.Parse(values[3]) == 1)
{
workdays.Add(Weekday.Tuesday);
}
if (int.Parse(values[4]) == 1)
{
workdays.Add(Weekday.Wednesday);
}
if (int.Parse(values[5]) == 1)
{
workdays.Add(Weekday.Thursday);
}
if (int.Parse(values[6]) == 1)
{
workdays.Add(Weekday.Friday);
}
if (int.Parse(values[7]) == 1)
{
workdays.Add(Weekday.Saturday);
}
if (int.Parse(values[8]) == 1)
{
workdays.Add(Weekday.Sunday);
}
double price = double.Parse(values[9]);
bool hasGuide = int.Parse(values[10]) == 1;
Museum museum = new Museum(name, city, manager, type, workdays, price, hasGuide);
museums.Add(museum);
}
return new MuseumsRegister(museums);
}
/// <summary>
/// Read and decode a list of museums from a file.
/// </summary>
/// <param name="filename">Target file</param>
/// <returns>Register of museums</returns>
public static MuseumsRegister ReadMuseumsFromCSV(string filename)
{
List<string> lines = new List<string>();
foreach (string line in File.ReadAllLines(filename, Encoding.UTF8))
{
lines.Add(line);
}
return DecodeMuseums(lines);
}
/// <summary>
/// Read all the entries from a zip file and decode the museums inside the csv entries.
/// </summary>
/// <param name="filename">Target filename</param>
/// <returns>Register of museums</returns>
public static MuseumsRegister ReadMuseumsFromZIP(string filename)
{
MuseumsRegister mainRegister = new MuseumsRegister();
using (ZipArchive zipFile = ZipFile.Open(filename, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in zipFile.Entries)
{
if (!entry.Name.EndsWith(".csv")) continue;
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(entry.Open(), Encoding.UTF8))
{
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine());
}
}
MuseumsRegister register = DecodeMuseums(lines);
AppendRegister(mainRegister, register);
}
}
return mainRegister;
}
/// <summary>
/// Read and decode lists of museums from multiple files and put into a single register.
/// </summary>
/// <param name="filenames">Target files</param>
/// <returns>Register containing museums from all files</returns>
public static MuseumsRegister ReadMuseums(params string[] filenames)
{
MuseumsRegister mainRegister = new MuseumsRegister();
foreach (string filename in filenames)
{
if (filename.EndsWith(".csv"))
{
AppendRegister(mainRegister, ReadMuseumsFromCSV(filename));
}
else if(filename.EndsWith(".zip"))
{
AppendRegister(mainRegister, ReadMuseumsFromZIP(filename));
}
}
return mainRegister;
}
/// <summary>
/// Write and encode a list of museums to a file. The file will be in a csv format using ";" as seperators.
/// </summary>
/// <param name="filename">Target file</param>
/// <param name="museums">List of museums</param>
public static void WriteMuseums(string filename, List<Museum> museums)
{
string[] lines = new string[museums.Count];
for (int i = 0; i < museums.Count; i++)
{
Museum m = museums[i];
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";
lines[i] = String.Join(";", m.City, m.Name, workDays, m.Price);
}
File.WriteAllLines(filename, lines, Encoding.UTF8);
}
/// <summary>
/// Write and encode a list of museums to a file from a register.
/// </summary>
/// <param name="filename">Target location</param>
/// <param name="register">Register containing museums</param>
public static void WriteMuseums(string filename, MuseumsRegister register)
{
List<Museum> museums = new List<Museum>();
for (int i = 0; i < register.Count(); i++)
{
museums.Add(register.GetByIndex(i));
}
WriteMuseums(filename, museums);
}
/// <summary>
/// Write out a list of museums in a table to the console.
/// </summary>
/// <param name="museums">List of museums</param>
public static void PrintMuseums(List<Museum> museums)
{
if (museums.Count == 0)
{
Console.WriteLine("Nėra");
return;
}
Console.WriteLine(new string('-', 120));
Console.WriteLine("| {0,-20} | {1,-10} | {2,-20} | {3,-10} | {4,18} | {5,10} | {6,-4} |", "Vardas", "Miestas", "Atsakingas", "Tipas", "Darbo dienų kiekis", "Kaina", "Turi gidą?");
Console.WriteLine(new string('-', 120));
foreach (Museum m in museums)
{
Console.WriteLine("| {0,-20} | {1,-10} | {2,-20} | {3,-10} | {4,18} | {5,10:f2} | {6,-10} |", m.Name, m.City, m.Manager, m.Type, m.Workdays.Count, m.Price, m.HasGuide ? "Taip" : "Ne");
}
Console.WriteLine(new string('-', 120));
}
/// <summary>
/// Write out a list of museums in a table to the console from a register.
/// </summary>
/// <param name="register">Register containing museums</param>
public static void PrintMuseums(MuseumsRegister register)
{
List<Museum> museums = new List<Museum>();
for (int i = 0; i < register.Count(); i++)
{
museums.Add(register.GetByIndex(i));
}
PrintMuseums(museums);
}
}
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="Museums.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MuseumsKaunas.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="MuseumsVilnius.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,28 @@
using System.Collections.Generic;
namespace Lab2.TouristInformationCenter
{
/// <summary>
/// Class used for storing data related a single museum.
/// </summary>
class Museum
{
public string Name { get; set; }
public string City { get; set; }
public string Manager { get; set; }
public string Type { get; set; }
public List<Weekday> Workdays { get; set; }
public double Price { get; set; }
public bool HasGuide { get; set; }
public Museum(string name, string city, string manager, string type, List<Weekday> workdays, double price, bool hasGuide)
{
Name = name;
Manager = manager;
City = city;
Type = type;
Workdays = workdays;
Price = price;
HasGuide = hasGuide;
}
}
}

View File

@ -0,0 +1,8 @@
Kaunas
Jonas Jonaitis
KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
KaunoMuziejus2;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
KaunoMuziejus6;Space;1;1;1;1;1;1;1;0,99;1
1 Kaunas
2 Jonas Jonaitis
3 KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
4 KaunoMuziejus2;Computer;1;1;0;1;1;1;1;14,39;1
5 KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0
6 KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1
7 KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;1
8 KaunoMuziejus6;Space;1;1;1;1;1;1;1;0,99;1

View File

@ -0,0 +1,235 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab2.TouristInformationCenter
{
/// <summary>
/// Class used to store multiple museums in one place
/// </summary>
class MuseumsRegister
{
private List<Museum> AllMuseums;
public int size = 100;
public MuseumsRegister()
{
AllMuseums = new List<Museum>();
}
public MuseumsRegister(List<Museum> museums)
{
AllMuseums = new List<Museum>();
foreach (Museum museum in museums)
{
Add(museum);
}
}
/// <summary>
/// Add one museum to the register.
/// </summary>
/// <param name="museum">Target museum</param>
public void Add(Museum museum)
{
this.AllMuseums.Add(museum);
}
/// <summary>
/// The amount of stored museums in the register.
/// </summary>
/// <returns>Museum count</returns>
public int Count()
{
return AllMuseums.Count;
}
/// <summary>
/// Access museum from register by index
/// </summary>
/// <param name="index">Target index</param>
/// <returns>Museum</returns>
public Museum GetByIndex(int index)
{
return AllMuseums[index];
}
/// <summary>
/// Return a list of active museums from the register.
/// A museum is considered active if it is working at least some amount a week.
/// </summary>
/// <param name="threshold">Threshold which determines what is active</param>
/// <returns>A list of active museums</returns>
public List<Museum> FilterByActiveMuseums(int threshold)
{
List<Museum> filtered = new List<Museum>();
foreach (Museum museum in AllMuseums)
{
if (museum.Workdays.Count >= threshold)
{
filtered.Add(museum);
}
}
return filtered;
}
/// <summary>
/// Find museum that work the most days in a week
/// </summary>
/// <returns>Most active museum</returns>
private Museum FindMostActiveMuseum()
{
if (AllMuseums.Count == 0)
{
return null;
}
Museum mostActive = AllMuseums[0];
foreach (Museum museum in AllMuseums)
{
if (museum.Workdays.Count > mostActive.Workdays.Count)
{
mostActive = museum;
}
}
return mostActive;
}
/// <summary>
/// Find all museums that work the most days in a week
/// </summary>
/// <returns></returns>
public List<Museum> FindMostActiveMuseums()
{
Museum mostActive = FindMostActiveMuseum();
List<Museum> activeMuseums = new List<Museum>();
foreach (Museum museum in AllMuseums)
{
if (museum.Workdays.Count == mostActive.Workdays.Count)
{
activeMuseums.Add(museum);
}
}
return activeMuseums;
}
/// <summary>
/// Get all of the different types of cities.
/// </summary>
/// <returns>A list of city names</returns>
public List<string> GetAllCities()
{
List<string> cities = new List<string>();
foreach (Museum museum in AllMuseums)
{
if (!cities.Contains(museum.City))
{
cities.Add(museum.City);
}
}
return cities;
}
/// <summary>
/// Get the number of museums that a certain city has.
/// </summary>
/// <param name="city">Target city</param>
/// <returns>Museum count by target city</returns>
public int GetCountByCity(string city)
{
int count = 0;
foreach (Museum museum in AllMuseums)
{
if (museum.City == city)
{
count++;
}
}
return count;
}
/// <summary>
/// Filter the museums by city name from register
/// </summary>
/// <param name="city">Target city</param>
/// <returns>A list of museums</returns>
public List<Museum> FilterByCity(string city)
{
List<Museum> filtered = new List<Museum>();
foreach (Museum museum in AllMuseums)
{
if (museum.City == city)
{
filtered.Add(museum);
}
}
return filtered;
}
/// <summary>
/// Count the number of museums that have a guide from given list
/// </summary>
/// <param name="museums">A list of museums</param>
/// <returns>Museum count that have guides</returns>
public static int CountByGuide(List<Museum> museums)
{
int count = 0;
foreach (Museum museum in museums)
{
if (museum.HasGuide)
{
count++;
}
}
return count;
}
/// <summary>
/// Find city which has the most guides from the register
/// </summary>
/// <returns>City with the most guides</returns>
public string FindCityWithMostGuides()
{
List<string> cities = GetAllCities();
if (cities.Count == 0)
{
return null;
}
string mostPopular = cities[0];
int mostPopularCount = CountByGuide(FilterByCity(cities[0]));
for (int i = 1; i < cities.Count; i++)
{
string city = cities[i];
int count = CountByGuide(FilterByCity(city));
if (count > mostPopularCount)
{
mostPopular = city;
mostPopularCount = count;
}
}
return mostPopular;
}
/// <summary>
/// Filter given museums by property "Type".
/// </summary>
/// <param name="museums">List of museums</param>
/// <param name="type">Target type</param>
/// <returns>A filtered list of museums</returns>
public static List<Museum> FilterByType(List<Museum> museums, string type)
{
List<Museum> filtered = new List<Museum>();
foreach (Museum museum in museums)
{
if (museum.Type == type)
{
filtered.Add(museum);
}
}
return filtered;
}
}
}

View File

@ -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;4,69;1
VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;0
VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;0
VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0
1 Vilnius
2 Petras Petraitis
3 VilnausMuziejus1;Dailė;1;0;0;1;1;0;0;5,49;0
4 VilnausMuziejus2;Computer;1;1;0;1;1;1;1;4,69;1
5 VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;0
6 VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;0
7 VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
namespace Lab2.TouristInformationCenter
{
class Program
{
static void Main(string[] args)
{
// Read all museums from initial data files
MuseumsRegister register = InOutUtils.ReadMuseums("MuseumsKaunas.csv", "MuseumsVilnius.csv");
Console.WriteLine("Visi muziejai:");
InOutUtils.PrintMuseums(register);
Console.WriteLine();
// Find all museums that are the most active
List<Museum> mostActiveMuseums = register.FindMostActiveMuseums();
Console.WriteLine("Aktyviausi muziejai:");
InOutUtils.PrintMuseums(mostActiveMuseums);
Console.WriteLine();
// Find city which has the most museums with guides
string mostPopularCityByGuides = register.FindCityWithMostGuides();
Console.WriteLine("Miestas su daugiausia gidų: {0}", mostPopularCityByGuides);
Console.WriteLine();
// Find all art museums that are active
List<Museum> activeMuseums = register.FilterByActiveMuseums(4);
List<Museum> activeArtMuseums = MuseumsRegister.FilterByType(activeMuseums, "Dailė");
InOutUtils.WriteMuseums("Dailė.csv", activeArtMuseums);
}
}
}

View File

@ -0,0 +1,17 @@

namespace Lab2.TouristInformationCenter
{
/// <summary>
/// Used for storing weekdays as numbers rather than strings
/// </summary>
enum Weekday
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
}

View File

@ -0,0 +1,7 @@
Kaunas
Jonas Jonaitis
KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
KaunoMuziejus2;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;0
1 Kaunas
2 Jonas Jonaitis
3 KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
4 KaunoMuziejus2;Computer;1;1;0;1;1;1;1;14,39;1
5 KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0
6 KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1
7 KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;0

View File

@ -0,0 +1,8 @@
Vilnius
Petras Petraitis
VilnausMuziejus1;History;1;1;0;1;1;0;0;5,49;0
VilnausMuziejus2;Computer;1;1;0;1;1;1;1;4,69;1
VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;1
VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;1
VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0
VilnausMuziejus6;Space;1;1;0;1;0;0;0;1,00;0
1 Vilnius
2 Petras Petraitis
3 VilnausMuziejus1;History;1;1;0;1;1;0;0;5,49;0
4 VilnausMuziejus2;Computer;1;1;0;1;1;1;1;4,69;1
5 VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;1
6 VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;1
7 VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0
8 VilnausMuziejus6;Space;1;1;0;1;0;0;0;1,00;0

View File

@ -0,0 +1,8 @@
Kaunas
Jonas Jonaitis
KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
KaunoMuziejus2;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
KaunoMuziejus6;Space;1;1;1;1;1;1;1;0,99;1
1 Kaunas
2 Jonas Jonaitis
3 KaunoMuziejus1;Dailė;1;0;0;0;1;0;1;3,40;0
4 KaunoMuziejus2;Computer;1;1;0;1;1;1;1;14,39;1
5 KaunoMuziejus3;History;0;1;0;0;1;0;0;3,26;0
6 KaunoMuziejus4;Dailė;0;1;0;1;1;0;1;9,47;1
7 KaunoMuziejus5;Space;1;1;0;0;0;0;1;1,99;1
8 KaunoMuziejus6;Space;1;1;1;1;1;1;1;0,99;1

View File

@ -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;4,69;1
VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;0
VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;0
VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0
1 Vilnius
2 Petras Petraitis
3 VilnausMuziejus1;Dailė;1;0;0;1;1;0;0;5,49;0
4 VilnausMuziejus2;Computer;1;1;0;1;1;1;1;4,69;1
5 VilnausMuziejus3;Dailė;0;1;1;1;1;0;0;1,23;0
6 VilnausMuziejus4;Food;1;1;1;1;1;1;0;6,90;0
7 VilnausMuziejus5;History;0;1;0;1;1;0;1;10,49;0

37
Lab2/Lab2.sln Normal file
View File

@ -0,0 +1,37 @@

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}") = "Lab2.Exercises.Register", "Lab2.Exercises.Register\Lab2.Exercises.Register.csproj", "{64F8D782-79CA-49D2-BA07-A53EE7F7F058}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Savarankiskas", "Savarankiskas\Savarankiskas.csproj", "{40CF34FD-C166-41D9-A728-CA4C9FFCBB4B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab2.TouristInformationCenter", "Lab2.TouristInformationCenter\Lab2.TouristInformationCenter.csproj", "{CB6002C8-B1AA-46A4-A9B1-ABDE53344524}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{64F8D782-79CA-49D2-BA07-A53EE7F7F058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64F8D782-79CA-49D2-BA07-A53EE7F7F058}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64F8D782-79CA-49D2-BA07-A53EE7F7F058}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64F8D782-79CA-49D2-BA07-A53EE7F7F058}.Release|Any CPU.Build.0 = Release|Any CPU
{40CF34FD-C166-41D9-A728-CA4C9FFCBB4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40CF34FD-C166-41D9-A728-CA4C9FFCBB4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40CF34FD-C166-41D9-A728-CA4C9FFCBB4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40CF34FD-C166-41D9-A728-CA4C9FFCBB4B}.Release|Any CPU.Build.0 = Release|Any CPU
{CB6002C8-B1AA-46A4-A9B1-ABDE53344524}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB6002C8-B1AA-46A4-A9B1-ABDE53344524}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB6002C8-B1AA-46A4-A9B1-ABDE53344524}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB6002C8-B1AA-46A4-A9B1-ABDE53344524}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E9FC39F0-DE0D-41FC-94F6-DB6F5F607B72}
EndGlobalSection
EndGlobal

6
Lab2/README.md Normal file
View File

@ -0,0 +1,6 @@
# Lab2 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.

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Savarankiskas
{
/// <summary>
/// Class used to store revelant information about an apartment
/// </summary>
class Apartment
{
public int Id { get; set; }
public double Area { get; set; }
public int RoomCount { get; set; }
public double Price { get; set; }
public string Phone { get; set; }
public int House { get { return (Id - 1) / 27 + 1; } }
public int Floor { get { return (Id - 1) % 9 + 1; } }
public Apartment(int id, double area, int roomCount, double price, string phone)
{
Id = id;
Area = area;
RoomCount = roomCount;
Price = price;
Phone = phone;
}
}
}

View File

@ -0,0 +1,6 @@
1;80;3;999,99;+37012345678
34;180;2;1300,00;+37012445678
100;280;4;1300,45;+37012445678
69;40;2;600,99;+37012945678
27;69;2;420,69;+37012345678
28;420;1;0,99;+37092345678
1 1 80 3 999,99 +37012345678
2 34 180 2 1300,00 +37012445678
3 100 280 4 1300,45 +37012445678
4 69 40 2 600,99 +37012945678
5 27 69 2 420,69 +37012345678
6 28 420 1 0,99 +37092345678

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Savarankiskas
{
class ApartmentsRegister
{
private List<Apartment> AllApartments;
public ApartmentsRegister()
{
AllApartments = new List<Apartment>();
}
public ApartmentsRegister(List<Apartment> apartments)
{
AllApartments = new List<Apartment>();
foreach (Apartment apartment in apartments)
{
Add(apartment);
}
}
public void Add(Apartment apartment)
{
AllApartments.Add(apartment);
}
public int Count()
{
return AllApartments.Count;
}
public Apartment GetApartment(int index)
{
return AllApartments[index];
}
public static List<Apartment> FilterByRoomCount(List<Apartment> apartments, int roomCount)
{
List<Apartment> Filtered = new List<Apartment>();
foreach (Apartment apartment in apartments)
{
if (apartment.RoomCount == roomCount)
{
Filtered.Add(apartment);
}
}
return Filtered;
}
public List<Apartment> FilterByRoomCount(int roomCount)
{
return FilterByRoomCount(AllApartments, roomCount);
}
public static List<Apartment> FilterByPrice(List<Apartment> apartments, double maxPrice)
{
List<Apartment> Filtered = new List<Apartment>();
foreach (Apartment apartment in apartments)
{
if (apartment.Price <= maxPrice)
{
Filtered.Add(apartment);
}
}
return Filtered;
}
public List<Apartment> FilterByPrice(double maxPrice)
{
return FilterByPrice(AllApartments, maxPrice);
}
public static List<Apartment> FilterByFloor(List<Apartment> apartments, int minFloor, int maxFloor)
{
List<Apartment> Filtered = new List<Apartment>();
foreach (Apartment apartment in apartments)
{
if (apartment.Floor >= minFloor && apartment.Floor <= maxFloor)
{
Filtered.Add(apartment);
}
}
return Filtered;
}
public List<Apartment> FilterByFloor(int minFloor, int maxFloor)
{
return FilterByFloor(AllApartments, minFloor, maxFloor);
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Savarankiskas
{
class InOutUtils
{
public static ApartmentsRegister ReadAparments(string filename)
{
List<Apartment> apartments = new List<Apartment>();
string[] lines = File.ReadAllLines(filename, Encoding.UTF8);
foreach (string line in lines)
{
string[] values = line.Split(';');
int id = int.Parse(values[0]);
double area = double.Parse(values[1]);
int roomCount = int.Parse(values[2]);
double price = double.Parse(values[3]);
string phone = values[4];
Apartment apartment = new Apartment(id, area, roomCount, price, phone);
apartments.Add(apartment);
}
return new ApartmentsRegister(apartments);
}
public static void PrintApartments(ApartmentsRegister register)
{
Console.WriteLine(new string('-', 78));
Console.WriteLine("| {0,4} | {1,7} | {2,15} | {3,10} | {4,10} | {5,-13} |", "Num", "Aukštas", "Kambarių kiekis", "Plotas", "Kaina", "Telefonas");
Console.WriteLine(new string('-', 78));
for (int i = 0; i < register.Count(); i++)
{
Apartment aparment = register.GetApartment(i);
Console.WriteLine("| {0,4} | {1,7} | {2,15} | {3,10:f2} | {4,10:f2} | {5,-13} |", aparment.Id, aparment.Floor, aparment.RoomCount, aparment.Area, aparment.Price, aparment.Phone);
}
Console.WriteLine(new string('-', 78));
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
namespace Savarankiskas
{
class Program
{
static void Main(string[] args)
{
ApartmentsRegister register = InOutUtils.ReadAparments("Apartments.csv");
Console.WriteLine("Visi butai:");
InOutUtils.PrintApartments(register);
Console.WriteLine();
Console.WriteLine("Įveskite norimą kambarių kiekį:");
int roomCount = int.Parse(Console.ReadLine());
Console.WriteLine("Įveskite maksimalią buto kainą:");
double maxPrice = double.Parse(Console.ReadLine());
Console.WriteLine("Įveskite žemiausią norimą aukštą [1-9]:");
int minFloor = int.Parse(Console.ReadLine());
Console.WriteLine("Įveskite aukščiausią norimą aukštą [{0}-9]:", minFloor);
int maxFloor = int.Parse(Console.ReadLine());
Console.WriteLine();
List<Apartment> filtered = register.FilterByRoomCount(roomCount);
filtered = ApartmentsRegister.FilterByPrice(filtered, maxPrice);
filtered = ApartmentsRegister.FilterByFloor(filtered, minFloor, maxFloor);
ApartmentsRegister filteredRegister = new ApartmentsRegister(filtered);
Console.WriteLine("Atrinkti butai:");
InOutUtils.PrintApartments(filteredRegister);
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="Apartments.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

5
Lab2/global.json Normal file
View File

@ -0,0 +1,5 @@
{
"sdk": {
"version": "3.0.103"
}
}