Advertisement
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;
- namespace ConsoleApplication1
- {
- class Menus
- {
- public enum UserChoice { AdminMenu, UserMenu, PreviousMenu, Route, AddCity, RemoveCity, Exit, List, Undefined };
- public enum MenuState { Main, Administrator, User };
- public struct MenuOption
- {
- // structura pentru construirea dinamica a unui meniu
- // reprezinta o optiune intr-un meniu
- public readonly string Number;
- public readonly string Text;
- public readonly UserChoice Choice;
- public MenuOption(string number, string text, UserChoice choice)
- {
- Number = number;
- Text = text;
- Choice = choice;
- }
- }
- public static void MainMenu(out List<MenuOption> options, out string action)
- {
- action = "Selectati rolul";
- options = new List<MenuOption>(3);
- options.Add(new MenuOption("1", "Utilizator", UserChoice.UserMenu));
- options.Add(new MenuOption("2", "Administrator", UserChoice.AdminMenu));
- options.Add(new MenuOption("3", "Iesire", UserChoice.Exit));
- }
- public static void AdminMenu(out List<MenuOption> options, out string action)
- {
- action = "Selectati actiunea dorita";
- options = new List<MenuOption>(5);
- options.Add(new MenuOption("1", "Afisarea tuturor oraselor", UserChoice.List));
- options.Add(new MenuOption("2", "Introducerea unui nou oras", UserChoice.AddCity));
- options.Add(new MenuOption("3", "Stergerea unui oras", UserChoice.RemoveCity));
- options.Add(new MenuOption("4", "Intoarcerea la meniul principal", UserChoice.PreviousMenu));
- options.Add(new MenuOption("5", "Iesire", UserChoice.Exit));
- }
- public static void UserMenu(out List<MenuOption> options, out string action)
- {
- action = "Selectati actiunea dorita";
- options = new List<MenuOption>(4);
- options.Add(new MenuOption("1", "Informatii despre o ruta", UserChoice.Route));
- options.Add(new MenuOption("2", "Afisarea tuturor oraselor", UserChoice.List));
- options.Add(new MenuOption("3", "Intoarcerea la meniul principal", UserChoice.PreviousMenu));
- options.Add(new MenuOption("4", "Iesire", UserChoice.Exit));
- }
- }
- }
- //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- public struct City
- { // readonly pentru ca structura să fie immutable // alternativa este abordarea cu câmpuri private şi proprietăţi publice
- public readonly double Latitude, Longitude;
- public readonly string Name;
- public City(string name, double latitude, double longitude)
- {
- Name = name; Latitude = latitude;
- Longitude = longitude;
- }
- }
- }
- //
- using ConsoleApplication1;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace lab6_ip
- {
- class Program
- {
- static void Main(string[] args)
- {
- Model model = new Model();
- ConsoleView view = new ConsoleView(model);
- Presenter presenter = new Presenter(view, model);
- view.SetPresenter(presenter);
- view.Start();
- //view.SetPresenter(presenter); view.Start();
- }
- }
- }
- //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Presenter :IPresenter
- {
- IModel _model;
- IView _view;
- public Presenter(IView view, IModel model)
- {
- view = _view;
- model = _model;
- }
- public void AddCity(City c)
- {
- throw new NotImplementedException();
- }
- public bool CityExists(string name)
- {
- throw new NotImplementedException();
- }
- public void ComputeRoute(string city1, string city2)
- {
- throw new NotImplementedException();
- }
- public void Exit()
- {
- if (_model.SaveData())
- _view.Display("Fisierul a fost salvat." + Environment.NewLine, "magenta");
- _view.Display("La revedere.", "default");
- // Application.DoEvents(); // numai pentru Windows Forms
- Thread.Sleep(1000);
- Environment.Exit(0);
- }
- public City GetCity(string name)
- {
- throw new NotImplementedException();
- }
- public void init()
- {
- if (!_model.DataExists())
- {
- _view.Display("Fisierul cu orase nu exista." + Environment.NewLine, "red");
- }
- else
- {
- _model.InitializeData();
- _view.Display("Fisier incarcat: " + _model.CityCount + " orase." + Environment.NewLine, "magenta");
- }
- }
- public void Init()
- {
- if (!_model.DataExists())
- {
- _view.Display("Fisierul cu orase nu exista." + Environment.NewLine, "red");
- }
- else
- {
- _model.InitializeData();
- _view.Display("Fisier incarcat: " + _model.CityCount + " orase." + Environment.NewLine, "magenta");
- }
- }
- public void RemoveCity(string name)
- {
- throw new NotImplementedException();
- }
- }
- }
- //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- public class BusinessCalculator
- {
- public static double Distance(City c1, City c2)
- {
- // calculeaza distanta in kilometri intre doua puncte de pe suprafata Pamantului
- // identificate prin latitudine si longitudine, folosind formula lui Haversine
- double lat1 = c1.Latitude * Math.PI / 180.0;
- double long1 = c1.Longitude * Math.PI / 180.0;
- double lat2 = c2.Latitude * Math.PI / 180.0;
- double long2 = c2.Longitude * Math.PI / 180.0;
- double dLat = lat2 - lat1;
- double dLong = long2 - long1;
- double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dLong / 2) * Math.Sin(dLong / 2);
- double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
- double radius = 6371; // raza Pamantului in km
- return radius * c; // distanta in km
- }
- /// <summary>
- /// Returns the cost for a given distance
- /// </summary>
- public static double Cost(double distance)
- {
- // aici se poate introduce orice functie de calcul al costului
- double euro = 5.0 + distance / 30.0;
- return euro * 4;
- }
- }
- }
- //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class ConsoleView:IView
- {
- IPresenter _IPresenter;
- IModel _model;
- List<Menus.MenuOption> _menuOptions;
- public ConsoleView(IModel model)
- {
- model = _model;
- }
- public void Display(string text, string color)
- {
- ConsoleColor c = ConsoleColor.DarkGray;
- switch (color)
- {
- case "default": c = ConsoleColor.White; break;
- case "red": c = ConsoleColor.Red; break;
- case "yellow": c = ConsoleColor.Yellow; break;
- case "blue": c = ConsoleColor.Blue; break;
- case "magenta": c = ConsoleColor.Magenta; break;
- case "green": c = ConsoleColor.Green; break;
- }
- Console.ForegroundColor = c;
- Console.WriteLine(text);
- }
- public void SetPresenter(IPresenter presenter)
- {
- _IPresenter = presenter;
- }
- public void Start()
- {
- Menus.UserChoice choice = Menus.UserChoice.Undefined;
- Menus.MenuState menuState = Menus.MenuState.Main;
- while (choice != Menus.UserChoice.Exit)
- {
- // preia comanda in functie de starea curenta a meniului
- choice = GetMenuOption(menuState);
- switch (choice)
- {
- // comenzi
- case Menus.UserChoice.Route:
- string cn1, cn2;
- GetTwoCities(out cn1, out cn2);
- _IPresenter.ComputeRoute(cn1, cn2);
- break;
- case Menus.UserChoice.RemoveCity:
- string cityName = GetCity();
- _IPresenter.RemoveCity(cityName);
- break;
- case Menus.UserChoice.AddCity:
- City c = InputCity();
- _IPresenter.AddCity(c);
- break;
- case Menus.UserChoice.List:
- ListAll();
- break;
- case Menus.UserChoice.Exit:
- _IPresenter.Exit();
- // de completat
- break;
- // navigare meniuri
- case Menus.UserChoice.AdminMenu:
- menuState = Menus.MenuState.Administrator;
- break;
- case Menus.UserChoice.UserMenu:
- menuState = Menus.MenuState.User;
- break;
- case Menus.UserChoice.PreviousMenu:
- menuState = Menus.MenuState.Main;
- break;
- }
- }
- }
- private City InputCity()
- {
- throw new NotImplementedException();
- }
- private Menus.UserChoice GetMenuOption(Menus.MenuState menuType)
- {
- string action = "";
- switch (menuType)
- {
- case Menus.MenuState.Main:
- Menus.MainMenu(out _menuOptions, out action);
- break;
- case Menus.MenuState.Administrator:
- Menus.AdminMenu(out _menuOptions, out action);
- break;
- case Menus.MenuState.User:
- Menus.UserMenu(out _menuOptions, out action);
- break;
- }
- Menus.UserChoice choice = Menus.UserChoice.Undefined;
- while (choice == Menus.UserChoice.Undefined)
- {
- Display(action + Environment.NewLine, "yellow");
- for (int i = 0; i < _menuOptions.Count; i++)
- Display(_menuOptions[i].Number + ". " + _menuOptions[i].Text, "default");
- Console.Write(Environment.NewLine + "Optiunea dumneavoastra: ");
- string userChoiceNumber = Console.ReadLine();
- Console.WriteLine();
- for (int i = 0; i < _menuOptions.Count; i++)
- {
- if (userChoiceNumber == _menuOptions[i].Number)
- {
- choice = _menuOptions[i].Choice;
- break;
- }
- }
- }
- return choice;
- }
- private void GetTwoCities(out string cityNAme1, out string cityName2)
- {
- cityNAme1 = "a";
- cityName2 = "b";
- }
- string GetCity()
- {
- return null;
- }
- City inputCity()
- {
- City city= new City();
- return city;
- }
- void ListAll()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement