Guest User

Untitled

a guest
May 9th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Runtime.Serialization;
  8. using Newtonsoft.Json;
  9.  
  10. namespace lab1
  11. {
  12.     public class FlatDAO
  13.     {
  14.         private string path;
  15.         private string json;
  16.         private List<Flat> listofflats;
  17.         public string Json
  18.         {
  19.             get
  20.             {
  21.                 return json;
  22.             }
  23.             set
  24.             {
  25.                 if (path != null)
  26.                     json = value;
  27.             }
  28.         }
  29.         public List<Flat> Listofflats
  30.         {
  31.             get
  32.             {
  33.                 return listofflats;
  34.             }
  35.             set
  36.             {
  37.                 listofflats = value;
  38.             }
  39.         }
  40.         private string Path
  41.         {
  42.             get
  43.             {
  44.                 return path;
  45.             }
  46.             set
  47.             {
  48.                 if (!File.Exists(value))
  49.                     path = value;
  50.             }
  51.         }
  52.         public FlatDAO(string path)
  53.         {
  54.             this.path = path;
  55.             this.Json = File.ReadAllText(path);
  56.             try
  57.             {
  58.                 listofflats = JsonConvert.DeserializeObject<List<Flat>>(json);
  59.             }
  60.             catch (Newtonsoft.Json.JsonReaderException)
  61.             {
  62.                 listofflats = new List<Flat>();
  63.             }
  64.             catch (Newtonsoft.Json.JsonSerializationException)
  65.             {
  66.                 listofflats = new List<Flat>();
  67.             }
  68.         }
  69.  
  70.         public bool updateFLAT(string json)
  71.         {
  72.             return WriteToFile(json);
  73.         }
  74.         public bool createFLAT(string json)
  75.         {
  76.                 this.Listofflats = JsonConvert.DeserializeObject<List<Flat>>(json);
  77.             return WriteToFile(json);
  78.         }
  79.         public bool deleteFLAT(string json)
  80.         {
  81.             return WriteToFile(json);
  82.         }
  83.         public bool WriteToFile(string jsontext)
  84.         {
  85.             if (File.Exists(path))
  86.             {
  87.                 File.WriteAllText(path, jsontext);
  88.                 return true;
  89.             }
  90.             else
  91.                 return false;
  92.         }
  93.     }
  94.  
  95.     public class FlatBUS
  96.     {
  97.         private FlatDAO _flatDAO;
  98.         public FlatDAO flatDAO
  99.         {
  100.             get
  101.             {
  102.                 return _flatDAO;
  103.             }
  104.         }
  105.         public FlatBUS(string path)
  106.         {
  107.             _flatDAO = new FlatDAO(path);
  108.         }
  109.         public bool createFLAT(Flat flat)
  110.         {
  111.             _flatDAO.Listofflats.Add(flat);
  112.             _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
  113.             return _flatDAO.createFLAT(_flatDAO.Json);
  114.         }
  115.         public Flat readFLATbyId(int id)
  116.         {
  117.             return _flatDAO.Listofflats.Find(x => (x.Idflat == id));
  118.         }
  119.         public bool updateFLATbyId(int id, Flat flat)
  120.         {
  121.             int index = _flatDAO.Listofflats.FindIndex(x => (x.Idflat == id));
  122.             if (index != -1)
  123.             {
  124.                 _flatDAO.Listofflats[index] = flat;
  125.                 _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
  126.                 return _flatDAO.updateFLAT(_flatDAO.Json);
  127.             }
  128.             else
  129.                 return false;
  130.         }
  131.         public bool deleteFLATbyId(int id)
  132.         {
  133.             Flat flat = readFLATbyId(id);
  134.             _flatDAO.Listofflats.Remove(flat);
  135.             _flatDAO.Json = JsonConvert.SerializeObject(_flatDAO.Listofflats);
  136.             return _flatDAO.deleteFLAT(_flatDAO.Json);
  137.         }
  138.         public List<Flat> searchFLATSbyNormative(int standard)
  139.         {
  140.             List<Flat> listofflats = _flatDAO.Listofflats;
  141.             List<Flat> normativeflats = new List<Flat>();
  142.             DateTime currentdate = DateTime.Today;
  143.             DateTime lastdate = DateTime.Today.AddMonths(-3);
  144.             foreach (var flat in listofflats)
  145.             {
  146.                 int water = 0;
  147.                 List<MeterReading> listofreading = flat.Mr.FindAll(x => (((x.Month == lastdate.Month) || (x.Month == currentdate.Month)) && ((x.Year == lastdate.Year) || (x.Year == currentdate.Year))));
  148.                 foreach (MeterReading mr in listofreading)
  149.                     water = water + (mr.Hot + mr.Cold);
  150.                 if (water > standard)
  151.                     normativeflats.Add(flat);
  152.             }
  153.             return normativeflats;
  154.         }
  155.     }
  156.     public class Flat
  157.     {
  158.         private int idflat;
  159.         private int numberofregistered;
  160.         private List<MeterReading> mr;
  161.         public List<MeterReading> Mr
  162.         {
  163.             get
  164.             {
  165.                 return mr;
  166.             }
  167.             set
  168.             {
  169.                 mr = value;
  170.             }
  171.         }
  172.         public int Idflat
  173.         {
  174.             get
  175.             {
  176.                 return idflat;
  177.             }
  178.             set
  179.             {
  180.                 if (value > 0)
  181.                     idflat = value;
  182.             }
  183.         }
  184.         public int Numberofregistered
  185.         {
  186.             get
  187.             {
  188.                 return numberofregistered;
  189.             }
  190.             set
  191.             {
  192.                 if (value > 0)
  193.                     numberofregistered = value;
  194.             }
  195.         }
  196.         public Flat()
  197.         {
  198.             List<MeterReading> mr = new List<MeterReading>();
  199.         }
  200.     }
  201.     public class MeterReading
  202.     {
  203.         private int year;
  204.         private int month;
  205.         private int hot;
  206.         private int cold;
  207.         public int Month
  208.         {
  209.             get
  210.             {
  211.                 return month;
  212.             }
  213.             set
  214.             {
  215.                 if ((value > 0) && (value < 13))
  216.                 {
  217.                     month = value;
  218.                 }
  219.             }
  220.         }
  221.         public int Year
  222.         {
  223.             get
  224.             {
  225.                 return year;
  226.             }
  227.             set
  228.             {
  229.                 DateTime dt = DateTime.Now;
  230.                 if ((value > 0) && (value <= dt.Year))
  231.                 {
  232.                     year = value;
  233.                 }
  234.             }
  235.         }
  236.         public int Hot
  237.         {
  238.             get
  239.             {
  240.                 return hot;
  241.             }
  242.             set
  243.             {
  244.                 hot = value;
  245.             }
  246.         }
  247.         public int Cold
  248.         {
  249.             get
  250.             {
  251.                 return cold;
  252.             }
  253.             set
  254.             {
  255.                 cold = value;
  256.             }
  257.         }
  258.     }
  259.  
  260.     public class View
  261.     {
  262.         private FlatBUS _flatBUS;
  263.  
  264.         public View(string path)
  265.         {
  266.             _flatBUS = new FlatBUS(path);
  267.         }
  268.  
  269.         public MeterReading readMeterReading()
  270.         {
  271.             int year, month, hot, cold;
  272.             MeterReading mr = new MeterReading();
  273.  
  274.             Console.WriteLine("Показания за введённый месяц и год: ");
  275.             Console.WriteLine("Vvedite god");
  276.  
  277.             if (!Int32.TryParse(Console.ReadLine(), out year))
  278.             {
  279.                 Console.WriteLine("Ошибка при чтении года!");
  280.                 return null;
  281.             }
  282.             else mr.Year = year;
  283.  
  284.             Console.WriteLine("Vvedite mesyac");
  285.             if (!Int32.TryParse(Console.ReadLine(), out month))
  286.             {
  287.                 Console.WriteLine("Ошибка при чтении месяца!");
  288.                 return null;
  289.             }
  290.             else
  291.                 mr.Month = month;
  292.  
  293.             Console.WriteLine("Vvedite количество горячей воды: ");
  294.             if (!Int32.TryParse(Console.ReadLine(), out hot))
  295.             {
  296.                 Console.WriteLine("Ошибка при чтении количества горячей воды!");
  297.                 return null;
  298.             }
  299.             else
  300.             {
  301.                 if (hot < 0)
  302.                 {
  303.                     Console.WriteLine("Вы ввели отрицательно значение!");
  304.                     return null;
  305.                 }
  306.                 else
  307.                     mr.Hot = hot;
  308.             }
  309.  
  310.             Console.WriteLine("Vvedite количество холодной воды: ");
  311.             if (!Int32.TryParse(Console.ReadLine(), out cold))
  312.             {
  313.                 Console.WriteLine("Ошибка при чтении количества холодной воды!");
  314.                 return null;
  315.             }
  316.             else
  317.             {
  318.                 if (cold < 0)
  319.                 {
  320.                     Console.WriteLine("Вы ввели отрицательно значение!");
  321.                     return null;
  322.                 }
  323.                 else
  324.                     mr.Cold = cold;
  325.             }
  326.             return mr;
  327.         }
  328.  
  329.         public List<MeterReading> readListOfMeterReading()
  330.         {
  331.             Console.WriteLine("Заполните показания счётчика квартиры: ");
  332.             List<MeterReading> listofmeterreading = new List<MeterReading>();
  333.             MeterReading mr = new MeterReading();
  334.             char x;
  335.             do
  336.             {
  337.                 mr = readMeterReading();
  338.                 if (mr != null)
  339.                     listofmeterreading.Add(mr);
  340.                 else
  341.                     return null;
  342.                 Console.WriteLine("Ещё? y/n");
  343.                 x = Console.ReadKey().KeyChar;
  344.             } while (x == 'y');
  345.             return listofmeterreading;  
  346.         }
  347.  
  348.         public Flat readFLATfromConsole()
  349.         {
  350.             Flat flat = new Flat();
  351.             Console.WriteLine("Введите номер квартиры:");
  352.             int idflat, numberofregistered;
  353.             if (!Int32.TryParse(Console.ReadLine(), out idflat))
  354.             {
  355.                 Console.WriteLine("Ошибка при чтении номера квартиры!");
  356.                 return null;
  357.             }
  358.             else
  359.                 flat.Idflat = idflat;
  360.  
  361.             Console.WriteLine("Введите количество проживающих в квартире: ");
  362.             if (!Int32.TryParse(Console.ReadLine(), out numberofregistered))
  363.             {
  364.                 Console.WriteLine("Ошибка при чтении номера квартиры!");
  365.                 return null;
  366.             }
  367.             else
  368.                 flat.Numberofregistered = numberofregistered;
  369.             List<MeterReading> listofmeterreading = readListOfMeterReading();
  370.             if (listofmeterreading == null)
  371.             {
  372.                 Console.WriteLine("Ошибка при чтении показаний счётчика!");
  373.                 return null;
  374.             }
  375.             else
  376.                 flat.Mr = listofmeterreading;
  377.             return flat;
  378.         }
  379.  
  380.         public void createFLAT()
  381.         {
  382.             Flat flat = readFLATfromConsole();
  383.             if (flat != null)
  384.             {
  385.                 if (!_flatBUS.createFLAT(flat))
  386.                 {
  387.                     Console.WriteLine("Ошибка чтения файла для создания квартиры!");
  388.                     return;
  389.                 }
  390.             }
  391.             else
  392.             {
  393.                 Console.WriteLine("Ошибка чтения квартиры из консоли!");
  394.                 return;
  395.             }
  396.         }
  397.         public void readFLATbyId()
  398.         {
  399.             int idflat;
  400.             Console.WriteLine("Введите номер квартиры: ");
  401.             if (!Int32.TryParse(Console.ReadLine(), out idflat))
  402.             {
  403.                 Console.WriteLine("Ошибка при чтении номера квартиры!");
  404.                 return;
  405.             }
  406.             Flat flat = _flatBUS.readFLATbyId(idflat);
  407.             if (flat != null)
  408.                 Console.WriteLine(flat.ToString());
  409.             else
  410.             {
  411.                 Console.WriteLine("Квартира с таким номером не найдена!");
  412.                 return;
  413.             }
  414.         }
  415.  
  416.         public void updateFLAT()
  417.         {
  418.             int idflat;
  419.             Console.WriteLine("Введите номер квартиры, которую необходимо обновить: ");
  420.             if (!Int32.TryParse(Console.ReadLine(), out idflat))
  421.             {
  422.                 Console.WriteLine("Ошибка при чтении номера квартиры!");
  423.                 return;
  424.             }
  425.             Flat flat = readFLATfromConsole();
  426.             if (flat != null)
  427.             {
  428.                 if (!_flatBUS.updateFLATbyId(idflat, flat))
  429.                 {
  430.                     Console.WriteLine("Ошибка чтения файла при обновлении квартиры или ошибка записи в файл!");
  431.                     return;
  432.                 }
  433.             }
  434.             else
  435.             {
  436.                 Console.WriteLine("Ошибка чтения квартиры из консоли!");
  437.                 return;
  438.             }
  439.         }
  440.         public void deleteFLAT()
  441.         {
  442.             int idflat;
  443.             Console.WriteLine("Введите номер квартиры, которую необходимо удалить: ");
  444.             if (!Int32.TryParse(Console.ReadLine(), out idflat))
  445.             {
  446.                 Console.WriteLine("Ошибка при чтении номера квартиры!");
  447.                 return;
  448.             }
  449.             Flat flat = _flatBUS.readFLATbyId(idflat);
  450.             if (flat != null)
  451.             {
  452.                 if (!_flatBUS.deleteFLATbyId(idflat))
  453.                 {
  454.                     Console.WriteLine("Ошибка чтения файла при удалении квартиры!");
  455.                     return;
  456.                 }
  457.             }
  458.             else
  459.             {
  460.                 Console.WriteLine("Квартира с таким номером не найдена!");
  461.                 return;
  462.             }
  463.         }
  464.  
  465.         public void searchFlATSbyNormative()
  466.         {
  467.             int standard;
  468.             Console.WriteLine("Введите норматив за 3 месяца: ");
  469.             if (!Int32.TryParse(Console.ReadLine(), out standard))
  470.             {
  471.                 Console.WriteLine("Ошибка при чтении норматива!");
  472.                 return;
  473.             }
  474.             List<Flat> normativeflats = _flatBUS.searchFLATSbyNormative(standard);
  475.             foreach (var flat in normativeflats)
  476.                 Console.WriteLine(flat.ToString());
  477.         }
  478.  
  479.         public void ShowAllFlats()
  480.         {
  481.             foreach (var flat in _flatBUS.flatDAO.Listofflats)
  482.                 Console.WriteLine(flat.ToString());
  483.         }
  484.         public void main_menu()
  485.         {
  486.             Console.WriteLine("Main Menu:");
  487.             Console.WriteLine("1.Create a flat");
  488.             Console.WriteLine("2.Read a flat by id");
  489.             Console.WriteLine("3.Search a flat by id and update it");
  490.             Console.WriteLine("4.Search a flat by id and delete it");
  491.             Console.WriteLine("5.Vivesti vse kvartiri, u kotorih potreblenie vodi previshaet normativnoe za poslednie 3 mesyac");
  492.             Console.WriteLine("6.Show all flats");
  493.             Console.WriteLine("0.Vihod iz programmi\n");
  494.             Console.WriteLine("Vvedite nomer deistviya: ");
  495.         }
  496.         public void MeterReadingMenu()
  497.         {
  498.             Console.WriteLine("Meter Reading Menu: ");
  499.             Console.WriteLine("Vvedite god");
  500.  
  501.         }
  502.         public short Menu()
  503.         {
  504.             short choice;
  505.             if (!Int16.TryParse(Console.ReadLine(), out choice))
  506.             {
  507.                 return 0;
  508.             }
  509.             return choice;
  510.         }
  511.     }
  512.     class Program
  513.     {
  514.         static void Main(string[] args)
  515.         {
  516.             View view = new View(@"C:\Users\Daniil\Desktop\flats.json");
  517.             view.main_menu();
  518.             short choice = view.Menu();
  519.             while (choice != 0)
  520.             {
  521.                 switch (choice)
  522.                 {
  523.                     case 1:
  524.                         view.createFLAT();
  525.                         break;
  526.                     case 2:
  527.                         view.readFLATbyId();
  528.                         break;
  529.                     case 3:
  530.                         view.updateFLAT();
  531.                         break;
  532.                     case 4:
  533.                         view.deleteFLAT();
  534.                         break;
  535.                     case 5:
  536.                         view.searchFlATSbyNormative();
  537.                         break;
  538.                     case 6:
  539.                         view.ShowAllFlats();
  540.                         break;
  541.                     default:
  542.                         Console.WriteLine("Takoe deistvie ne predusmotreno!");
  543.                         break;
  544.                 }
  545.                 Console.WriteLine("Viberite sleduyushee deistive: ");
  546.                 view.main_menu();
  547.                 choice = view.Menu();
  548.             }
  549.             Console.ReadKey();
  550.         }
  551.     }
  552. }
Advertisement
Add Comment
Please, Sign In to add comment