Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.32 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.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Menus
  10.     {
  11.  
  12.  
  13.  
  14.         public enum UserChoice { AdminMenu, UserMenu, PreviousMenu, Route, AddCity, RemoveCity, Exit, List, Undefined };
  15.         public enum MenuState { Main, Administrator, User };
  16.  
  17.         public struct MenuOption
  18.         {
  19.             // structura pentru construirea dinamica a unui meniu
  20.             // reprezinta o optiune intr-un meniu
  21.  
  22.             public readonly string Number;
  23.             public readonly string Text;
  24.             public readonly UserChoice Choice;
  25.  
  26.             public MenuOption(string number, string text, UserChoice choice)
  27.             {
  28.                 Number = number;
  29.                 Text = text;
  30.                 Choice = choice;
  31.             }
  32.         }
  33.  
  34.         public static void MainMenu(out List<MenuOption> options, out string action)
  35.         {
  36.             action = "Selectati rolul";
  37.             options = new List<MenuOption>(3);
  38.             options.Add(new MenuOption("1", "Utilizator", UserChoice.UserMenu));
  39.             options.Add(new MenuOption("2", "Administrator", UserChoice.AdminMenu));
  40.             options.Add(new MenuOption("3", "Iesire", UserChoice.Exit));
  41.         }
  42.  
  43.         public static void AdminMenu(out List<MenuOption> options, out string action)
  44.         {
  45.             action = "Selectati actiunea dorita";
  46.             options = new List<MenuOption>(5);
  47.             options.Add(new MenuOption("1", "Afisarea tuturor oraselor", UserChoice.List));
  48.             options.Add(new MenuOption("2", "Introducerea unui nou oras", UserChoice.AddCity));
  49.             options.Add(new MenuOption("3", "Stergerea unui oras", UserChoice.RemoveCity));
  50.             options.Add(new MenuOption("4", "Intoarcerea la meniul principal", UserChoice.PreviousMenu));
  51.             options.Add(new MenuOption("5", "Iesire", UserChoice.Exit));
  52.          
  53.         }
  54.  
  55.         public static void UserMenu(out List<MenuOption> options, out string action)
  56.         {
  57.             action = "Selectati actiunea dorita";
  58.             options = new List<MenuOption>(4);
  59.             options.Add(new MenuOption("1", "Informatii despre o ruta", UserChoice.Route));
  60.             options.Add(new MenuOption("2", "Afisarea tuturor oraselor", UserChoice.List));
  61.             options.Add(new MenuOption("3", "Intoarcerea la meniul principal", UserChoice.PreviousMenu));
  62.             options.Add(new MenuOption("4", "Iesire", UserChoice.Exit));
  63.         }
  64.        
  65.  
  66.  
  67.     }
  68.  
  69. }
  70. //
  71. using System;
  72. using System.Collections.Generic;
  73. using System.Linq;
  74. using System.Text;
  75. using System.Threading.Tasks;
  76.  
  77. namespace ConsoleApplication1
  78. {
  79.    
  80.     public struct City
  81.     {         // readonly pentru ca structura să fie immutable         // alternativa este abordarea cu câmpuri private şi proprietăţi publice  
  82.         public readonly double Latitude, Longitude;
  83.         public readonly string Name;  
  84.  
  85.         public City(string name, double latitude, double longitude)
  86.         {
  87.           Name = name; Latitude = latitude;
  88.           Longitude = longitude;
  89.         }
  90.     }
  91. }
  92.  
  93. //
  94.  
  95. using ConsoleApplication1;
  96. using System;
  97. using System.Collections.Generic;
  98. using System.Linq;
  99. using System.Text;
  100. using System.Threading.Tasks;
  101.  
  102. namespace lab6_ip
  103. {
  104.     class Program
  105.     {
  106.         static void Main(string[] args)
  107.         {
  108.             Model model = new Model();
  109.             ConsoleView view = new ConsoleView(model);
  110.             Presenter presenter = new Presenter(view, model);
  111.             view.SetPresenter(presenter);
  112.             view.Start();
  113.             //view.SetPresenter(presenter); view.Start();
  114.         }
  115.    
  116.     }
  117. }
  118.  
  119. //
  120.  
  121. using System;
  122. using System.Collections.Generic;
  123. using System.Linq;
  124. using System.Text;
  125. using System.Threading;
  126. using System.Threading.Tasks;
  127.  
  128. namespace ConsoleApplication1
  129. {
  130.     class Presenter :IPresenter
  131.     {
  132.         IModel _model;
  133.         IView _view;
  134.  
  135.         public Presenter(IView view, IModel model)
  136.         {
  137.             view = _view;
  138.             model = _model;
  139.  
  140.         }
  141.  
  142.         public void AddCity(City c)
  143.         {
  144.             throw new NotImplementedException();
  145.         }
  146.  
  147.         public bool CityExists(string name)
  148.         {
  149.             throw new NotImplementedException();
  150.         }
  151.  
  152.         public void ComputeRoute(string city1, string city2)
  153.         {
  154.             throw new NotImplementedException();
  155.         }
  156.  
  157.         public void Exit()
  158.         {
  159.             if (_model.SaveData())
  160.                 _view.Display("Fisierul a fost salvat." + Environment.NewLine, "magenta");
  161.             _view.Display("La revedere.", "default");
  162.             // Application.DoEvents(); // numai pentru Windows Forms
  163.             Thread.Sleep(1000);
  164.             Environment.Exit(0);
  165.         }
  166.  
  167.         public City GetCity(string name)
  168.         {
  169.             throw new NotImplementedException();
  170.         }
  171.  
  172.         public void init()
  173.         {
  174.             if (!_model.DataExists())
  175.             {
  176.                 _view.Display("Fisierul cu orase nu exista." + Environment.NewLine, "red");
  177.             }
  178.             else
  179.             {
  180.                 _model.InitializeData();
  181.                 _view.Display("Fisier incarcat: " + _model.CityCount + " orase." + Environment.NewLine, "magenta");
  182.             }
  183.         }
  184.  
  185.         public void Init()
  186.         {
  187.             if (!_model.DataExists())
  188.             {
  189.                 _view.Display("Fisierul cu orase nu exista." + Environment.NewLine, "red");
  190.             }
  191.             else
  192.             {
  193.                 _model.InitializeData();
  194.                 _view.Display("Fisier incarcat: " + _model.CityCount + " orase." + Environment.NewLine, "magenta");
  195.             }
  196.         }
  197.  
  198.         public void RemoveCity(string name)
  199.         {
  200.             throw new NotImplementedException();
  201.         }
  202.     }
  203.  
  204.  
  205. }
  206.  
  207. //
  208. using System;
  209. using System.Collections.Generic;
  210. using System.Linq;
  211. using System.Text;
  212. using System.Threading.Tasks;
  213.  
  214. namespace ConsoleApplication1
  215. {
  216.     public class BusinessCalculator
  217.     {
  218.         public static double Distance(City c1, City c2)
  219.         {
  220.             // calculeaza distanta in kilometri intre doua puncte de pe suprafata Pamantului
  221.             // identificate prin latitudine si longitudine, folosind formula lui Haversine
  222.  
  223.             double lat1 = c1.Latitude * Math.PI / 180.0;
  224.             double long1 = c1.Longitude * Math.PI / 180.0;
  225.             double lat2 = c2.Latitude * Math.PI / 180.0;
  226.             double long2 = c2.Longitude * Math.PI / 180.0;
  227.  
  228.             double dLat = lat2 - lat1;
  229.             double dLong = long2 - long1;
  230.  
  231.             double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dLong / 2) * Math.Sin(dLong / 2);
  232.             double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  233.  
  234.             double radius = 6371; // raza Pamantului in km
  235.             return radius * c; // distanta in km
  236.         }
  237.  
  238.         /// <summary>
  239.         /// Returns the cost for a given distance
  240.         /// </summary>
  241.         public static double Cost(double distance)
  242.         {
  243.             // aici se poate introduce orice functie de calcul al costului
  244.             double euro = 5.0 + distance / 30.0;
  245.             return euro * 4;
  246.         }
  247.  
  248.     }
  249. }
  250. //
  251. using System;
  252. using System.Collections.Generic;
  253. using System.Linq;
  254. using System.Text;
  255. using System.Threading.Tasks;
  256.  
  257. namespace ConsoleApplication1
  258. {
  259.     class ConsoleView:IView
  260.     {
  261.         IPresenter _IPresenter;
  262.         IModel _model;
  263.         List<Menus.MenuOption> _menuOptions;
  264.         public ConsoleView(IModel model)
  265.         {
  266.             model = _model;
  267.         }
  268.  
  269.         public void Display(string text, string color)
  270.         {
  271.             ConsoleColor c = ConsoleColor.DarkGray;
  272.  
  273.             switch (color)
  274.             {
  275.                 case "default": c = ConsoleColor.White; break;
  276.                 case "red": c = ConsoleColor.Red; break;
  277.                 case "yellow": c = ConsoleColor.Yellow; break;
  278.                 case "blue": c = ConsoleColor.Blue; break;
  279.                 case "magenta": c = ConsoleColor.Magenta; break;
  280.                 case "green": c = ConsoleColor.Green; break;
  281.             }
  282.  
  283.             Console.ForegroundColor = c;
  284.             Console.WriteLine(text);
  285.         }
  286.  
  287.         public void SetPresenter(IPresenter presenter)
  288.         {
  289.             _IPresenter = presenter;
  290.         }
  291.        public  void Start()
  292.         {
  293.             Menus.UserChoice choice = Menus.UserChoice.Undefined;
  294.             Menus.MenuState menuState = Menus.MenuState.Main;
  295.  
  296.             while (choice != Menus.UserChoice.Exit)
  297.             {
  298.                 // preia comanda in functie de starea curenta a meniului
  299.  
  300.                 choice = GetMenuOption(menuState);
  301.  
  302.                 switch (choice)
  303.                 {
  304.                     // comenzi
  305.                     case Menus.UserChoice.Route:
  306.                         string cn1, cn2;
  307.                         GetTwoCities(out cn1, out cn2);
  308.                         _IPresenter.ComputeRoute(cn1, cn2);
  309.                         break;
  310.                     case Menus.UserChoice.RemoveCity:
  311.                         string cityName = GetCity();
  312.                         _IPresenter.RemoveCity(cityName);
  313.                         break;
  314.                     case Menus.UserChoice.AddCity:
  315.                         City c = InputCity();
  316.                         _IPresenter.AddCity(c);
  317.                         break;
  318.                     case Menus.UserChoice.List:
  319.                         ListAll();
  320.                         break;
  321.                     case Menus.UserChoice.Exit:
  322.                         _IPresenter.Exit();
  323.                         // de completat
  324.                         break;
  325.  
  326.                     // navigare meniuri
  327.                     case Menus.UserChoice.AdminMenu:
  328.                         menuState = Menus.MenuState.Administrator;
  329.                         break;
  330.                     case Menus.UserChoice.UserMenu:
  331.                         menuState = Menus.MenuState.User;
  332.                         break;
  333.                     case Menus.UserChoice.PreviousMenu:
  334.                         menuState = Menus.MenuState.Main;
  335.                         break;
  336.                 }
  337.             }
  338.  
  339.         }
  340.  
  341.         private City InputCity()
  342.         {
  343.             throw new NotImplementedException();
  344.         }
  345.  
  346.         private Menus.UserChoice GetMenuOption(Menus.MenuState menuType)
  347.         {
  348.             string action = "";
  349.  
  350.             switch (menuType)
  351.             {
  352.                 case Menus.MenuState.Main:
  353.                     Menus.MainMenu(out _menuOptions, out action);
  354.                     break;
  355.                 case Menus.MenuState.Administrator:
  356.                     Menus.AdminMenu(out _menuOptions, out action);
  357.                     break;
  358.                 case Menus.MenuState.User:
  359.                     Menus.UserMenu(out _menuOptions, out action);
  360.                     break;
  361.             }
  362.  
  363.             Menus.UserChoice choice = Menus.UserChoice.Undefined;
  364.  
  365.             while (choice == Menus.UserChoice.Undefined)
  366.             {
  367.                 Display(action + Environment.NewLine, "yellow");
  368.  
  369.                 for (int i = 0; i < _menuOptions.Count; i++)
  370.                     Display(_menuOptions[i].Number + ". " + _menuOptions[i].Text, "default");
  371.  
  372.                 Console.Write(Environment.NewLine + "Optiunea dumneavoastra: ");
  373.                 string userChoiceNumber = Console.ReadLine();
  374.                 Console.WriteLine();
  375.  
  376.                 for (int i = 0; i < _menuOptions.Count; i++)
  377.                 {
  378.                     if (userChoiceNumber == _menuOptions[i].Number)
  379.                     {
  380.                         choice = _menuOptions[i].Choice;
  381.                         break;
  382.                     }
  383.                 }
  384.             }
  385.  
  386.             return choice;
  387.         }
  388.         private void GetTwoCities(out string cityNAme1, out string cityName2)
  389.         {
  390.             cityNAme1 = "a";
  391.             cityName2 = "b";
  392.         }
  393.         string GetCity()
  394.         {
  395.             return null;
  396.  
  397.         }
  398.  
  399.         City inputCity()
  400.         {
  401.             City city= new City();
  402.             return city;
  403.         }
  404.         void ListAll()
  405.         {
  406.  
  407.         }
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement