Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Runtime.Serialization;
- using Newtonsoft.Json;
- namespace lab1
- {
- public class FlatDAO
- {
- private string path;
- private string json;
- private List<Flat> listofflats;
- public string Json
- {
- get
- {
- return json;
- }
- set
- {
- if (path != null)
- json = value;
- }
- }
- public List<Flat> Listofflats
- {
- get
- {
- return listofflats;
- }
- set
- {
- listofflats = value;
- }
- }
- private string Path
- {
- get
- {
- return path;
- }
- set
- {
- if (!File.Exists(value))
- path = value;
- }
- }
- public FlatDAO(string path)
- {
- this.path = path;
- this.Json = File.ReadAllText(path);
- try
- {
- listofflats = JsonConvert.DeserializeObject<List<Flat>>(json);
- }
- catch (Newtonsoft.Json.JsonReaderException)
- {
- listofflats = new List<Flat>();
- }
- catch (Newtonsoft.Json.JsonSerializationException)
- {
- listofflats = new List<Flat>();
- }
- }
- public bool updateFLAT(string json)
- {
- return WriteToFile(json);
- }
- public bool createFLAT(string json)
- {
- this.Listofflats = JsonConvert.DeserializeObject<List<Flat>>(json);
- return WriteToFile(json);
- }
- public bool deleteFLAT(string json)
- {
- return WriteToFile(json);
- }
- public bool WriteToFile(string jsontext)
- {
- if (File.Exists(path))
- {
- File.WriteAllText(path, jsontext);
- return true;
- }
- else
- return false;
- }
- }
- public class FlatBUS
- {
- private FlatDAO _flatDAO;
- public FlatDAO flatDAO
- {
- get
- {
- return _flatDAO;
- }
- }
- public FlatBUS(string path)
- {
- _flatDAO = new FlatDAO(path);
- }
- public bool createFLAT(Flat flat)
- {
- _flatDAO.Listofflats.Add(flat);
- _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
- return _flatDAO.createFLAT(_flatDAO.Json);
- }
- public Flat readFLATbyId(int id)
- {
- return _flatDAO.Listofflats.Find(x => (x.Idflat == id));
- }
- public bool updateFLATbyId(int id, Flat flat)
- {
- int index = _flatDAO.Listofflats.FindIndex(x => (x.Idflat == id));
- if (index != -1)
- {
- _flatDAO.Listofflats[index] = flat;
- _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
- return _flatDAO.updateFLAT(_flatDAO.Json);
- }
- else
- return false;
- }
- public bool deleteFLATbyId(int id)
- {
- Flat flat = readFLATbyId(id);
- _flatDAO.Listofflats.Remove(flat);
- _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
- return _flatDAO.deleteFLAT(_flatDAO.Json);
- }
- public List<Flat> searchFLATSbyNormative(int standard)
- {
- List<Flat> listofflats = _flatDAO.Listofflats;
- List<Flat> normativeflats = new List<Flat>();
- DateTime currentdate = DateTime.Today;
- DateTime lastdate = DateTime.Today.AddMonths(-3);
- foreach (var flat in listofflats)
- {
- int water = 0;
- List<MeterReading> listofreading = flat.Mr.FindAll(x => (((x.Month == lastdate.Month) || (x.Month == currentdate.Month)) && ((x.Year == lastdate.Year) || (x.Year == currentdate.Year))));
- foreach (MeterReading mr in listofreading)
- water = water + (mr.Hot + mr.Cold);
- if (water > standard)
- normativeflats.Add(flat);
- }
- return normativeflats;
- }
- }
- public class Flat
- {
- private int idflat;
- private int numberofregistered;
- private List<MeterReading> mr;
- public List<MeterReading> Mr
- {
- get
- {
- return mr;
- }
- set
- {
- mr = value;
- }
- }
- public int Idflat
- {
- get
- {
- return idflat;
- }
- set
- {
- if (value > 0)
- idflat = value;
- }
- }
- public int Numberofregistered
- {
- get
- {
- return numberofregistered;
- }
- set
- {
- if (value > 0)
- numberofregistered = value;
- }
- }
- public Flat()
- {
- List<MeterReading> mr = new List<MeterReading>();
- }
- }
- public class MeterReading
- {
- private int year;
- private int month;
- private int hot;
- private int cold;
- public int Month
- {
- get
- {
- return month;
- }
- set
- {
- if ((value > 0) && (value < 13))
- {
- month = value;
- }
- }
- }
- public int Year
- {
- get
- {
- return year;
- }
- set
- {
- DateTime dt = DateTime.Now;
- if ((value > 0) && (value <= dt.Year))
- {
- year = value;
- }
- }
- }
- public int Hot
- {
- get
- {
- return hot;
- }
- set
- {
- hot = value;
- }
- }
- public int Cold
- {
- get
- {
- return cold;
- }
- set
- {
- cold = value;
- }
- }
- }
- public class View
- {
- private FlatBUS _flatBUS;
- public View(string path)
- {
- _flatBUS = new FlatBUS(path);
- }
- public MeterReading readMeterReading()
- {
- int year, month, hot, cold;
- MeterReading mr = new MeterReading();
- Console.WriteLine("Показания за введённый месяц и год: ");
- Console.WriteLine("Vvedite god");
- if (!Int32.TryParse(Console.ReadLine(), out year))
- {
- Console.WriteLine("Ошибка при чтении года!");
- return null;
- }
- else mr.Year = year;
- Console.WriteLine("Vvedite mesyac");
- if (!Int32.TryParse(Console.ReadLine(), out month))
- {
- Console.WriteLine("Ошибка при чтении месяца!");
- return null;
- }
- else
- mr.Month = month;
- Console.WriteLine("Vvedite количество горячей воды: ");
- if (!Int32.TryParse(Console.ReadLine(), out hot))
- {
- Console.WriteLine("Ошибка при чтении количества горячей воды!");
- return null;
- }
- else
- {
- if (hot < 0)
- {
- Console.WriteLine("Вы ввели отрицательно значение!");
- return null;
- }
- else
- mr.Hot = hot;
- }
- Console.WriteLine("Vvedite количество холодной воды: ");
- if (!Int32.TryParse(Console.ReadLine(), out cold))
- {
- Console.WriteLine("Ошибка при чтении количества холодной воды!");
- return null;
- }
- else
- {
- if (cold < 0)
- {
- Console.WriteLine("Вы ввели отрицательно значение!");
- return null;
- }
- else
- mr.Cold = cold;
- }
- return mr;
- }
- public List<MeterReading> readListOfMeterReading()
- {
- Console.WriteLine("Заполните показания счётчика квартиры: ");
- List<MeterReading> listofmeterreading = new List<MeterReading>();
- MeterReading mr = new MeterReading();
- char x;
- do
- {
- mr = readMeterReading();
- if (mr != null)
- listofmeterreading.Add(mr);
- else
- return null;
- Console.WriteLine("Ещё? y/n");
- x = Console.ReadKey().KeyChar;
- } while (x == 'y');
- return listofmeterreading;
- }
- public Flat readFLATfromConsole()
- {
- Flat flat = new Flat();
- Console.WriteLine("Введите номер квартиры:");
- int idflat, numberofregistered;
- if (!Int32.TryParse(Console.ReadLine(), out idflat))
- {
- Console.WriteLine("Ошибка при чтении номера квартиры!");
- return null;
- }
- else
- flat.Idflat = idflat;
- Console.WriteLine("Введите количество проживающих в квартире: ");
- if (!Int32.TryParse(Console.ReadLine(), out numberofregistered))
- {
- Console.WriteLine("Ошибка при чтении номера квартиры!");
- return null;
- }
- else
- flat.Numberofregistered = numberofregistered;
- List<MeterReading> listofmeterreading = readListOfMeterReading();
- if (listofmeterreading == null)
- {
- Console.WriteLine("Ошибка при чтении показаний счётчика!");
- return null;
- }
- else
- flat.Mr = listofmeterreading;
- return flat;
- }
- public void createFLAT()
- {
- Flat flat = readFLATfromConsole();
- if (flat != null)
- {
- if (!_flatBUS.createFLAT(flat))
- {
- Console.WriteLine("Ошибка чтения файла для создания квартиры!");
- return;
- }
- }
- else
- {
- Console.WriteLine("Ошибка чтения квартиры из консоли!");
- return;
- }
- }
- public void readFLATbyId()
- {
- int idflat;
- Console.WriteLine("Введите номер квартиры: ");
- if (!Int32.TryParse(Console.ReadLine(), out idflat))
- {
- Console.WriteLine("Ошибка при чтении номера квартиры!");
- return;
- }
- Flat flat = _flatBUS.readFLATbyId(idflat);
- if (flat != null)
- Console.WriteLine(flat.ToString());
- else
- {
- Console.WriteLine("Квартира с таким номером не найдена!");
- return;
- }
- }
- public void updateFLAT()
- {
- int idflat;
- Console.WriteLine("Введите номер квартиры, которую необходимо обновить: ");
- if (!Int32.TryParse(Console.ReadLine(), out idflat))
- {
- Console.WriteLine("Ошибка при чтении номера квартиры!");
- return;
- }
- Flat flat = readFLATfromConsole();
- if (flat != null)
- {
- if (!_flatBUS.updateFLATbyId(idflat, flat))
- {
- Console.WriteLine("Ошибка чтения файла при обновлении квартиры или ошибка записи в файл!");
- return;
- }
- }
- else
- {
- Console.WriteLine("Ошибка чтения квартиры из консоли!");
- return;
- }
- }
- public void deleteFLAT()
- {
- int idflat;
- Console.WriteLine("Введите номер квартиры, которую необходимо удалить: ");
- if (!Int32.TryParse(Console.ReadLine(), out idflat))
- {
- Console.WriteLine("Ошибка при чтении номера квартиры!");
- return;
- }
- Flat flat = _flatBUS.readFLATbyId(idflat);
- if (flat != null)
- {
- if (!_flatBUS.deleteFLATbyId(idflat))
- {
- Console.WriteLine("Ошибка чтения файла при удалении квартиры!");
- return;
- }
- }
- else
- {
- Console.WriteLine("Квартира с таким номером не найдена!");
- return;
- }
- }
- public void searchFlATSbyNormative()
- {
- int standard;
- Console.WriteLine("Введите норматив за 3 месяца: ");
- if (!Int32.TryParse(Console.ReadLine(), out standard))
- {
- Console.WriteLine("Ошибка при чтении норматива!");
- return;
- }
- List<Flat> normativeflats = _flatBUS.searchFLATSbyNormative(standard);
- foreach (var flat in normativeflats)
- Console.WriteLine(flat.ToString());
- }
- public void ShowAllFlats()
- {
- foreach (var flat in _flatBUS.flatDAO.Listofflats)
- Console.WriteLine(flat.ToString());
- }
- public void main_menu()
- {
- Console.WriteLine("Main Menu:");
- Console.WriteLine("1.Create a flat");
- Console.WriteLine("2.Read a flat by id");
- Console.WriteLine("3.Search a flat by id and update it");
- Console.WriteLine("4.Search a flat by id and delete it");
- Console.WriteLine("5.Vivesti vse kvartiri, u kotorih potreblenie vodi previshaet normativnoe za poslednie 3 mesyac");
- Console.WriteLine("6.Show all flats");
- Console.WriteLine("0.Vihod iz programmi\n");
- Console.WriteLine("Vvedite nomer deistviya: ");
- }
- public void MeterReadingMenu()
- {
- Console.WriteLine("Meter Reading Menu: ");
- Console.WriteLine("Vvedite god");
- }
- public short Menu()
- {
- short choice;
- if (!Int16.TryParse(Console.ReadLine(), out choice))
- {
- return 0;
- }
- return choice;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- View view = new View(@"C:\Users\Daniil\Desktop\flats.json");
- view.main_menu();
- short choice = view.Menu();
- while (choice != 0)
- {
- switch (choice)
- {
- case 1:
- view.createFLAT();
- break;
- case 2:
- view.readFLATbyId();
- break;
- case 3:
- view.updateFLAT();
- break;
- case 4:
- view.deleteFLAT();
- break;
- case 5:
- view.searchFlATSbyNormative();
- break;
- case 6:
- view.ShowAllFlats();
- break;
- default:
- Console.WriteLine("Takoe deistvie ne predusmotreno!");
- break;
- }
- Console.WriteLine("Viberite sleduyushee deistive: ");
- view.main_menu();
- choice = view.Menu();
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment