Advertisement
Guest User

asddddas

a guest
Mar 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace ContactManager
  12. {
  13.     public partial class ContactListForm : Form
  14.     {
  15.  
  16.  
  17.  
  18.  
  19.         //private List<Contact> contacts = new List<Contact>();
  20.  
  21.         private BindingList<Contact> contacts = new BindingList<Contact>();
  22.        
  23.  
  24.         public ContactListForm()
  25.         {
  26.             InitializeComponent();
  27.  
  28.             this.contactList.DataSource = contacts;
  29.             this.contactList.DisplayMember = "Name";
  30.         }
  31.  
  32.  
  33.  
  34.         private void addBtn_Click(object sender, EventArgs e)
  35.         {
  36.             EditorDialog dialog = new EditorDialog(null);
  37.             dialog.OnSave += (Contact customer) => {
  38.  
  39.                 contacts.Add(customer);
  40.                 dialog.Close();
  41.             };
  42.             dialog.Show();
  43.         }
  44.  
  45.  
  46.  
  47.        
  48.  
  49.         private void deleteBtn_Click(object sender, EventArgs e)
  50.         {
  51.             if(this.contactList.SelectedIndex == -1)
  52.             {
  53.                 MessageBox.Show("Kontakt nebyl vybrán...");
  54.                 return;
  55.             }
  56.  
  57.             Contact customer = this.contactList.SelectedItem as Contact;
  58.             this.contacts.Remove(customer);
  59.         }
  60.  
  61.  
  62.         private void editBtn_Click(object sender, EventArgs e)
  63.         {
  64.             if (this.contactList.SelectedIndex == -1)
  65.             {
  66.                 MessageBox.Show("Kontakt nebyl vybrán...");
  67.                 return;
  68.             }
  69.  
  70.             Contact customer = this.contactList.SelectedItem as Contact;
  71.            
  72.             EditorDialog dialog = new EditorDialog(customer);
  73.             dialog.OnSave += (Contact newCustomer) => {
  74.                 this.contacts.Remove(customer);
  75.                 this.contacts.Add(newCustomer);
  76.             };
  77.             dialog.Show();
  78.         }
  79.  
  80.  
  81.  
  82.     }
  83. }
  84. using System;
  85. using System.Collections.Generic;
  86. using System.ComponentModel;
  87. using System.Data;
  88. using System.Drawing;
  89. using System.Linq;
  90. using System.Text;
  91. using System.Threading.Tasks;
  92. using System.Windows.Forms;
  93.  
  94. namespace ContactManager
  95. {
  96.     public partial class EditorDialog : Form
  97.     {
  98.  
  99.         public delegate void CustomerHandle(Contact customer);
  100.        
  101.         public event CustomerHandle OnSave;
  102.         public EditorDialog(Contact customer)
  103.         {
  104.             InitializeComponent();
  105.  
  106.             if (customer != null)
  107.             {
  108.                 this.nameInput.Text = customer?.Name;
  109.                 this.ageInput.Text = customer?.Age?.ToString();
  110.             }
  111.         }
  112.        
  113.         private void saveBtn_Click(object sender, EventArgs e)
  114.         {
  115.            
  116.             if (string.IsNullOrWhiteSpace(this.nameInput.Text))
  117.             {
  118.                 MessageBox.Show("Musíte zadat jméno.");
  119.                 return;
  120.             }
  121.  
  122.             int? age;
  123.             if (string.IsNullOrWhiteSpace(this.ageInput.Text))
  124.             {
  125.                 age = null;
  126.             }
  127.             else if (int.TryParse(this.ageInput.Text, out int tmp))
  128.             {
  129.                 age = tmp;
  130.             }
  131.             else
  132.             {
  133.                 MessageBox.Show("Věk musí být číslo.");
  134.                 return;
  135.             }
  136.            
  137.  
  138.             Contact contact = new Contact
  139.             {
  140.                 Name = this.nameInput.Text,
  141.                 Age = age
  142.             };
  143.            
  144.             if (this.OnSave != null)
  145.             {
  146.                 this.OnSave(contact);
  147.             }
  148.  
  149.            
  150.             this.Close();
  151.         }
  152.     }
  153. }
  154. using System;
  155. using System.Collections.Generic;
  156. using System.Linq;
  157. using System.Text;
  158. using System.Threading.Tasks;
  159.  
  160. namespace MyDynamicArray
  161. {
  162.     public class DynamicArray<TData> where TData : struct, IComparable
  163.     {
  164.  
  165.         private TData?[] data;
  166.  
  167.         public DynamicArray(int length) {
  168.             this.data = new TData?[length];
  169.         }
  170.  
  171.        
  172.  
  173.  
  174.         public int Size {
  175.             get {
  176.                 return this.data.Length;
  177.             }
  178.             set {
  179.                 if (this.data.Length < value)
  180.                 {
  181.                     throw new MyException();
  182.                 }
  183.                 TData?[] tmp = new TData?[value];
  184.                 Array.Copy(this.data, tmp, this.data.Length);
  185.                 this.data = tmp;
  186.             }
  187.         }
  188.  
  189.         public TData? this[int index] {
  190.             get {
  191.                 return this.data[index];
  192.             }
  193.             set {
  194.                 if(index >= this.data.Length)
  195.                 {
  196.                     this.Size = index + 1;
  197.                 }
  198.  
  199.                 this.data[index] = value;
  200.             }
  201.         }
  202.  
  203.  
  204.         public override string ToString()
  205.         {
  206.             StringBuilder sb = new StringBuilder();
  207.             foreach(TData? val in data)
  208.             {
  209.                 sb.Append(val == null ? "-" : val.Value.ToString());
  210.                 sb.Append(", ");
  211.             }
  212.             return sb.ToString();
  213.         }
  214.  
  215.  
  216.  
  217.         public TData? Max()
  218.         {
  219.             TData? val = null;
  220.             foreach (var item in data)
  221.             {
  222.                 if (item == null) { continue; }
  223.  
  224.                 if (val == null)
  225.                 {
  226.                     val = item;
  227.                 }
  228.                 else if (item.Value.CompareTo(val.Value) > 0)
  229.                 {
  230.                     val = item;
  231.                 }
  232.             }
  233.             return val;
  234.         }
  235.  
  236.  
  237.         public bool TryGet(int index, ref TData? value)
  238.         {
  239.             if (index < 0 || index >= data.Length || data[index] == null)
  240.             {
  241.                 return false;
  242.             }
  243.             value = data[index];
  244.             return true;
  245.         }
  246.  
  247.  
  248.         public void Stats(TData value, out int lowerThen, out int largerThen, out int equalsCount)
  249.         {
  250.             int tmpLowerThen = 0;
  251.             int tmpLargerThen = 0;
  252.             int tmpEqualsCount = 0;
  253.  
  254.             foreach (TData? item in data)
  255.             {
  256.                 if (item == null) { continue; }
  257.  
  258.                 int comparison = item.Value.CompareTo(value);
  259.                 if (comparison == 0)
  260.                 {
  261.                     tmpEqualsCount++;
  262.                 }
  263.                 else if (comparison > 0)
  264.                 {
  265.                     tmpLargerThen++;
  266.                 }
  267.                 else
  268.                 {
  269.                     tmpLowerThen++;
  270.                 }
  271.             }
  272.  
  273.             lowerThen = tmpLowerThen;
  274.             largerThen = tmpLargerThen;
  275.             equalsCount = tmpEqualsCount;
  276.         }
  277.  
  278.  
  279.  
  280.     }
  281.  
  282.     public class MyException : Exception
  283.     {
  284.     }
  285.  
  286.  
  287. }
  288.  
  289. using System;
  290. using System.Collections.Generic;
  291. using System.Linq;
  292. using System.Text;
  293. using System.Threading.Tasks;
  294.  
  295. namespace PJ2Test
  296. {
  297.     class Transition
  298.     {
  299.         public DateTime date;
  300.         public Game game;
  301.         public static void PrintStatistic(List<Transition> lst)
  302.         {
  303.             decimal total = 0;
  304.             decimal disc = 0;
  305.             foreach(Transition x in lst)
  306.             {
  307.                 total += x.game.price;
  308.                 disc += x.game.GetPriceIn(x.date);
  309.             }
  310.             Console.WriteLine("Total value: " + total);
  311.             Console.WriteLine("After discount: " + disc);
  312.         }
  313.  
  314.         public Transition(DateTime date, Game game)
  315.         {
  316.             this.date = date;
  317.             this.game = game;
  318.         }
  319.     }
  320. }
  321.  
  322. using System;
  323. using System.Collections.Generic;
  324. using System.Linq;
  325. using System.Text;
  326. using System.Threading.Tasks;
  327.  
  328. namespace PJ2Test
  329. {
  330.     class Sale
  331.     {
  332.         public DateTime start;
  333.         public DateTime end;
  334.         public int percent;
  335.  
  336.         public Sale(DateTime start, DateTime end, int percent)
  337.         {
  338.             this.start = start;
  339.             this.end = end;
  340.             this.percent = percent;
  341.         }
  342.     }
  343. }
  344.  
  345.  
  346. using System;
  347. using System.Collections.Generic;
  348. using System.Linq;
  349. using System.Text;
  350. using System.Threading.Tasks;
  351.  
  352. namespace PJ2Test
  353. {
  354.     class Program
  355.     {
  356.         static void Main(string[] args)
  357.         {
  358.             Game ai = new Game("Alien Isolation", 19.95m);
  359.             Game so = new Game("SOMA", 9.99m);
  360.             ai.AddSale(new Sale(new DateTime(2017, 1, 1), new DateTime(2017, 1, 15), 75));
  361.             so.AddSale(new Sale(new DateTime(2017, 1, 15), new DateTime(2017, 1, 20), 50));
  362.  
  363.             List<Transition> transitions = new List<Transition>();
  364.             transitions.Add(new Transition(new DateTime(2017, 1, 17), ai));
  365.             transitions.Add(new Transition(new DateTime(2017, 1, 17), so));
  366.             Console.WriteLine(
  367.             so.GetPriceIn(new DateTime(2017, 1, 17)));
  368.             Transition.PrintStatistic(transitions);
  369.  
  370.             Transition.PrintStatistic(transitions);
  371.         }
  372.     }
  373. }
  374.  
  375. using System;
  376. using System.Collections.Generic;
  377. using System.Linq;
  378. using System.Text;
  379. using System.Threading.Tasks;
  380.  
  381. namespace PJ2Test
  382. {
  383.     class Game
  384.     {
  385.         public string name;
  386.         public decimal price;
  387.         public List<Sale> sales = new List<Sale>();
  388.        
  389.         public Game(string name, decimal price)
  390.         {
  391.             this.name = name;
  392.             this.price = price;
  393.         }
  394.  
  395.         public void AddSale(Sale sale)
  396.         {
  397.             sales.Add(sale);
  398.         }
  399.         public decimal GetPriceIn(DateTime date)
  400.         {
  401.             decimal temp = price;
  402.             foreach(Sale x in sales)
  403.             {
  404.                 if (date >= x.start && date < x.end)
  405.                 {
  406.                     temp = temp * ((100m - x.percent)/100);
  407.                 }
  408.             }
  409.             return temp;
  410.         }
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement