Advertisement
Siwy_Krzysiek

UML praca domowa

Nov 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace Schemat_GUI
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Hello World!");
  12.         }
  13.     }
  14.  
  15.     struct MousePosition
  16.     {
  17.         public int X { get; set; }
  18.         public int Y { get; set; }
  19.  
  20.         MousePosition(int x, int y)
  21.         {
  22.             X = x;
  23.             Y = y;
  24.         }
  25.     }
  26.  
  27.     interface IGUIElement
  28.     {
  29.         void Draw();
  30.         void Click(MousePosition mousePosition);
  31.     }
  32.  
  33.     class Logic
  34.     {
  35.         public void Run()
  36.         {
  37.             Button calculateMeanButton = new Button(MeanButtonClick);
  38.             List<IGUIElement> elements = new List<IGUIElement> { calculateMeanButton };
  39.  
  40.             Menu mainMenu = new Menu(elements);
  41.  
  42.             while (mainMenu.GetIsOpen())
  43.             {
  44.                 mainMenu.Draw();
  45.                 //if (mouse.IsClicked())
  46.                 //{
  47.                 //  mainMenu.Clikc(getMousePosition());
  48.                 //}
  49.             }
  50.         }
  51.  
  52.         private void MeanButtonClick()
  53.         {
  54.             double mean = Calculator.Mean(Calculator.Generate(10));
  55.             File.WriteAllText("wynik.txt", mean.ToString());
  56.  
  57.             DisplayWindow display = new DisplayWindow(mean.ToString());
  58.             display.Draw();
  59.         }
  60.     }
  61.  
  62.     class Menu : IGUIElement
  63.     {
  64.         private List<IGUIElement> elements;
  65.  
  66.         private bool isOpen = true;
  67.         public bool GetIsOpen() { return isOpen; }
  68.  
  69.         public void Close() { isOpen = false; }
  70.         public void Draw() { }
  71.         public void Click(MousePosition mousePosition)
  72.         {
  73.             foreach (var element in elements)
  74.             {
  75.                 element.Click(mousePosition);
  76.             }
  77.         }
  78.  
  79.         public Menu(List<IGUIElement> elements)
  80.         {
  81.             this.elements = elements;
  82.         }
  83.     }
  84.  
  85.     class Button : IGUIElement
  86.     {
  87.         Action onClick;
  88.  
  89.         public void Draw() { }
  90.         public void Click(MousePosition mousePosition)
  91.         {
  92.             //if mousPositon is in buttonArea
  93.             onClick();
  94.         }
  95.  
  96.         public Button (Action onClick)
  97.         {
  98.             this.onClick = onClick;
  99.         }
  100.     }
  101.  
  102.     class DisplayWindow : IGUIElement
  103.     {
  104.         private string content;
  105.  
  106.         public void Click(MousePosition mousePosition)
  107.         {
  108.             // if close icon clicked
  109.             Close();
  110.         }
  111.  
  112.         public void Draw()
  113.         {
  114.             //Display content
  115.         }
  116.  
  117.         public void Close() { }
  118.  
  119.         public DisplayWindow(string content)
  120.         {
  121.             this.content = content;
  122.         }
  123.     }
  124.  
  125.     class Calculator
  126.     {
  127.         public static List<int> Generate(int howMany)
  128.         {
  129.             List<int> randomNumbers = new List<int>(howMany);
  130.             Random random = new Random();
  131.  
  132.             for (int i = 0; i < howMany; i++)
  133.             {
  134.                 randomNumbers.Add(random.Next());
  135.             }
  136.  
  137.             return randomNumbers;
  138.         }
  139.  
  140.         public static int Sum(List<int> numbers)
  141.         {
  142.             int sum = 0;
  143.             foreach (var number in numbers)
  144.             {
  145.                 sum += number;
  146.             }
  147.  
  148.             return sum;
  149.         }
  150.  
  151.         public static double Mean(List<int> numbers)
  152.         {
  153.             return (double)Sum(numbers) / (double) numbers.Count;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement