Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ContactManager
- {
- public partial class ContactListForm : Form
- {
- //private List<Contact> contacts = new List<Contact>();
- private BindingList<Contact> contacts = new BindingList<Contact>();
- public ContactListForm()
- {
- InitializeComponent();
- this.contactList.DataSource = contacts;
- this.contactList.DisplayMember = "Name";
- }
- private void addBtn_Click(object sender, EventArgs e)
- {
- EditorDialog dialog = new EditorDialog(null);
- dialog.OnSave += (Contact customer) => {
- contacts.Add(customer);
- dialog.Close();
- };
- dialog.Show();
- }
- private void deleteBtn_Click(object sender, EventArgs e)
- {
- if(this.contactList.SelectedIndex == -1)
- {
- MessageBox.Show("Kontakt nebyl vybrán...");
- return;
- }
- Contact customer = this.contactList.SelectedItem as Contact;
- this.contacts.Remove(customer);
- }
- private void editBtn_Click(object sender, EventArgs e)
- {
- if (this.contactList.SelectedIndex == -1)
- {
- MessageBox.Show("Kontakt nebyl vybrán...");
- return;
- }
- Contact customer = this.contactList.SelectedItem as Contact;
- EditorDialog dialog = new EditorDialog(customer);
- dialog.OnSave += (Contact newCustomer) => {
- this.contacts.Remove(customer);
- this.contacts.Add(newCustomer);
- };
- dialog.Show();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ContactManager
- {
- public partial class EditorDialog : Form
- {
- public delegate void CustomerHandle(Contact customer);
- public event CustomerHandle OnSave;
- public EditorDialog(Contact customer)
- {
- InitializeComponent();
- if (customer != null)
- {
- this.nameInput.Text = customer?.Name;
- this.ageInput.Text = customer?.Age?.ToString();
- }
- }
- private void saveBtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrWhiteSpace(this.nameInput.Text))
- {
- MessageBox.Show("Musíte zadat jméno.");
- return;
- }
- int? age;
- if (string.IsNullOrWhiteSpace(this.ageInput.Text))
- {
- age = null;
- }
- else if (int.TryParse(this.ageInput.Text, out int tmp))
- {
- age = tmp;
- }
- else
- {
- MessageBox.Show("Věk musí být číslo.");
- return;
- }
- Contact contact = new Contact
- {
- Name = this.nameInput.Text,
- Age = age
- };
- if (this.OnSave != null)
- {
- this.OnSave(contact);
- }
- this.Close();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyDynamicArray
- {
- public class DynamicArray<TData> where TData : struct, IComparable
- {
- private TData?[] data;
- public DynamicArray(int length) {
- this.data = new TData?[length];
- }
- public int Size {
- get {
- return this.data.Length;
- }
- set {
- if (this.data.Length < value)
- {
- throw new MyException();
- }
- TData?[] tmp = new TData?[value];
- Array.Copy(this.data, tmp, this.data.Length);
- this.data = tmp;
- }
- }
- public TData? this[int index] {
- get {
- return this.data[index];
- }
- set {
- if(index >= this.data.Length)
- {
- this.Size = index + 1;
- }
- this.data[index] = value;
- }
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- foreach(TData? val in data)
- {
- sb.Append(val == null ? "-" : val.Value.ToString());
- sb.Append(", ");
- }
- return sb.ToString();
- }
- public TData? Max()
- {
- TData? val = null;
- foreach (var item in data)
- {
- if (item == null) { continue; }
- if (val == null)
- {
- val = item;
- }
- else if (item.Value.CompareTo(val.Value) > 0)
- {
- val = item;
- }
- }
- return val;
- }
- public bool TryGet(int index, ref TData? value)
- {
- if (index < 0 || index >= data.Length || data[index] == null)
- {
- return false;
- }
- value = data[index];
- return true;
- }
- public void Stats(TData value, out int lowerThen, out int largerThen, out int equalsCount)
- {
- int tmpLowerThen = 0;
- int tmpLargerThen = 0;
- int tmpEqualsCount = 0;
- foreach (TData? item in data)
- {
- if (item == null) { continue; }
- int comparison = item.Value.CompareTo(value);
- if (comparison == 0)
- {
- tmpEqualsCount++;
- }
- else if (comparison > 0)
- {
- tmpLargerThen++;
- }
- else
- {
- tmpLowerThen++;
- }
- }
- lowerThen = tmpLowerThen;
- largerThen = tmpLargerThen;
- equalsCount = tmpEqualsCount;
- }
- }
- public class MyException : Exception
- {
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PJ2Test
- {
- class Transition
- {
- public DateTime date;
- public Game game;
- public static void PrintStatistic(List<Transition> lst)
- {
- decimal total = 0;
- decimal disc = 0;
- foreach(Transition x in lst)
- {
- total += x.game.price;
- disc += x.game.GetPriceIn(x.date);
- }
- Console.WriteLine("Total value: " + total);
- Console.WriteLine("After discount: " + disc);
- }
- public Transition(DateTime date, Game game)
- {
- this.date = date;
- this.game = game;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PJ2Test
- {
- class Sale
- {
- public DateTime start;
- public DateTime end;
- public int percent;
- public Sale(DateTime start, DateTime end, int percent)
- {
- this.start = start;
- this.end = end;
- this.percent = percent;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PJ2Test
- {
- class Program
- {
- static void Main(string[] args)
- {
- Game ai = new Game("Alien Isolation", 19.95m);
- Game so = new Game("SOMA", 9.99m);
- ai.AddSale(new Sale(new DateTime(2017, 1, 1), new DateTime(2017, 1, 15), 75));
- so.AddSale(new Sale(new DateTime(2017, 1, 15), new DateTime(2017, 1, 20), 50));
- List<Transition> transitions = new List<Transition>();
- transitions.Add(new Transition(new DateTime(2017, 1, 17), ai));
- transitions.Add(new Transition(new DateTime(2017, 1, 17), so));
- Console.WriteLine(
- so.GetPriceIn(new DateTime(2017, 1, 17)));
- Transition.PrintStatistic(transitions);
- Transition.PrintStatistic(transitions);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PJ2Test
- {
- class Game
- {
- public string name;
- public decimal price;
- public List<Sale> sales = new List<Sale>();
- public Game(string name, decimal price)
- {
- this.name = name;
- this.price = price;
- }
- public void AddSale(Sale sale)
- {
- sales.Add(sale);
- }
- public decimal GetPriceIn(DateTime date)
- {
- decimal temp = price;
- foreach(Sale x in sales)
- {
- if (date >= x.start && date < x.end)
- {
- temp = temp * ((100m - x.percent)/100);
- }
- }
- return temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement