Guest User

Untitled

a guest
Jan 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.37 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.Windows.Forms;
  9.  
  10. namespace testApp
  11. {
  12.     public partial class CustomerOverviewForm : Form
  13.     {
  14.         List<Customer> customers = new List<Customer>();
  15.  
  16.         public CustomerOverviewForm()
  17.         {
  18.             InitializeComponent();
  19.             CreateCustomersAndAccounts();
  20.  
  21.         }
  22.  
  23.         private void CreateCustomersAndAccounts()
  24.         {
  25.  
  26.             customers.Add(new Customer
  27.             {
  28.                 Name = "Marika Klasson",
  29.                 Id = "910227",
  30.                 Accounts = new List<Account> {
  31.                 new Account {  
  32.                 typeOfAccount = CheckingAccount.typeString,
  33.                 balance = 89 },
  34.                 new Account {
  35.                 typeOfAccount = PayAccount.typeString,
  36.                 balance = 14},
  37.                 new Account {
  38.                 typeOfAccount = SavingsAccount.typeString,
  39.                 balance = 55 } }
  40.             });
  41.  
  42.             customers.Add(new Customer
  43.             {
  44.                 Name = "Hilda Andersson",
  45.                 Id = "880803",
  46.                 Accounts = new List<Account> {
  47.                 new Account {
  48.                 typeOfAccount = CheckingAccount.typeString,
  49.                 balance = 13 },
  50.                 new Account {
  51.                 typeOfAccount = PayAccount.typeString,
  52.                 balance = 0 },
  53.                 new Account {
  54.                 typeOfAccount = SavingsAccount.typeString,
  55.                 balance = 0 } }
  56.             });
  57.  
  58.             customers.Add(new Customer
  59.            {
  60.                Name = "Maja Nilsson",
  61.                Id = "621203",
  62.                Accounts = new List<Account> {
  63.                 new Account {  
  64.                 typeOfAccount = CheckingAccount.typeString,
  65.                 balance = 55 },
  66.                 new Account {
  67.                 typeOfAccount = PayAccount.typeString,
  68.                 balance = 0 },
  69.                 new Account {
  70.                 typeOfAccount = SavingsAccount.typeString,
  71.                 balance = 0 } }
  72.            });
  73.             customers.Add(new Customer
  74.             {
  75.                 Name = "Johanna Jansson",
  76.                 Id = "950820",
  77.                 Accounts = new List<Account> {
  78.                 new Account {
  79.                 balance = 798,
  80.                 typeOfAccount = CheckingAccount.typeString },
  81.                 new Account {  
  82.                 typeOfAccount = PayAccount.typeString,
  83.                 balance = 95 },
  84.                 new Account {
  85.                 typeOfAccount = SavingsAccount.typeString,
  86.                 balance = 0} }
  87.  
  88.             });
  89.  
  90.             customers.Add(new Customer
  91.             {
  92.                 Name = "Majbritt Hansson",
  93.                 Id = "430525",
  94.                 Accounts = new List<Account>{
  95.                 new Account {
  96.                 typeOfAccount = CheckingAccount.typeString,
  97.                 balance = 11},
  98.                 new Account {
  99.                 typeOfAccount = PayAccount.typeString,
  100.                 balance = 0},
  101.                 new Account {
  102.                 typeOfAccount = SavingsAccount.typeString,
  103.                 balance = 133} }
  104.             });
  105.  
  106.             listCustomerOverview.DataSource = customers;
  107.  
  108.             comboBox2.DataSource = customers;
  109.         }
  110.  
  111.  
  112.         private void listCustomerOverview_SelectedIndexChanged(object sender, EventArgs e)
  113.         {
  114.             if (listCustomerOverview.SelectedItem != null)
  115.             {
  116.                 dataGridView1.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
  117.             }
  118.  
  119.         }
  120.  
  121.  
  122.         private void btnDeposit_Click(object sender, EventArgs e)
  123.         {
  124.             try
  125.             {
  126.                 var value = decimal.Parse(txtAmount.Text);
  127.                 var account = (listCustomerOverview.SelectedItem as Customer).Accounts[viewCustomersGrid.CurrentCell.RowIndex];
  128.                 account.Deposit(value);
  129.                 viewCustomersGrid.DataSource = null;
  130.                 viewCustomersGrid.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
  131.                 lblTransactionStatus.Text = "Transaction was successfull. You have inserted\n" + txtAmount.Text + " to choosen account";
  132.             }
  133.             catch (Exception)
  134.             {
  135.                 lblTransactionStatus.Text = txtAmount.Text + ", is an invalid input. ";
  136.             }
  137.  
  138.         }
  139.  
  140.         private void btnWithdraw_Click(object sender, EventArgs e)
  141.         {
  142.             try
  143.             {
  144.                 var value = decimal.Parse(txtAmount.Text);
  145.                 var account = (listCustomerOverview.SelectedItem as Customer).Accounts[viewCustomersGrid.CurrentCell.RowIndex];
  146.                 account.WithDraw(value);
  147.                 viewCustomersGrid.DataSource = null;
  148.                 viewCustomersGrid.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
  149.                 lblTransactionStatus.Text = "Transaction was successfull. You have withdrawed: \n" + txtAmount.Text + " from the choosen account";
  150.             }
  151.             catch
  152.             {
  153.                 lblTransactionStatus.Text = txtAmount.Text + " is either an invalid input or\nyou don't have enough money.";
  154.             }
  155.         }
  156.  
  157.  
  158.         private void btnAddCustomer_Click(object sender, EventArgs e)
  159.         {
  160.  
  161.             while (txtAddName.Text == String.Empty || txtAddId.Text.Length < 5)
  162.             {
  163.                 lblNotAdded.Text = "Fill out both textboxes.\nId must be atleast 5 characters";
  164.                 return;
  165.             }
  166.  
  167.             if (customers.Any(customer => customer.Id == txtAddId.Text))
  168.             {
  169.                 lblNotAdded.Text = "This id: " + txtAddId.Text + " does already exist.";
  170.             }
  171.  
  172.             else
  173.             {
  174.                 customers.Add(new Customer
  175.                 {
  176.                     Name = txtAddName.Text,
  177.                     Id = txtAddId.Text,
  178.                     Accounts = new List<Account>{
  179.                             new Account {
  180.                             typeOfAccount = CheckingAccount.typeString,
  181.                             balance = 0},
  182.                             new Account {
  183.                             typeOfAccount = PayAccount.typeString,
  184.                             balance = 0},
  185.                             new Account {
  186.                             typeOfAccount = SavingsAccount.typeString,
  187.                             balance = 0}
  188.                     }
  189.                 });
  190.  
  191.                 listCustomerOverview.DataSource = null;
  192.                 listCustomerOverview.DataSource = customers;
  193.                 comboBox2.DataSource = null;
  194.                 comboBox2.DataSource = customers;
  195.                 txtAddId.Clear();
  196.                 txtAddName.Clear();
  197.                 btnRemove.Enabled = true;
  198.                 lblNotAdded.Text = "Success";
  199.                 listCustomerOverview.SelectedIndex = customers.Count - 1;
  200.             }
  201.         }
  202.  
  203.         private void btnRemove_Click(object sender, EventArgs e)
  204.         {
  205.             customers.RemoveAt(listCustomerOverview.SelectedIndex);
  206.             listCustomerOverview.SelectedIndex = 0;
  207.             listCustomerOverview.DataSource = null;
  208.             listCustomerOverview.DataSource = customers;
  209.             comboBox2.DataSource = null;
  210.             comboBox2.DataSource = customers;
  211.             lblNotAdded.Text = "";
  212.  
  213.             if (listCustomerOverview.Items.Count == 0)
  214.             {
  215.                 btnRemove.Enabled = false;
  216.                 viewCustomersGrid.DataSource = null;
  217.             }
  218.             else
  219.                 btnRemove.Enabled = true;
  220.  
  221.         }
  222.  
  223.         private void searchButton_Click(object sender, EventArgs e)
  224.         {
  225.  
  226.             if (searchTextbox.Text != String.Empty)
  227.             {
  228.                 listboxSearchResult.Items.Clear();
  229.  
  230.                 var searchCustomer = customers
  231.                     .Where(customer => customer.Id == searchTextbox.Text || customer.Name.ToLower().Contains(searchTextbox.Text.ToLower()))
  232.                     .Select(customer => customer.Name).ToList();
  233.  
  234.                 searchError.Text = "";
  235.  
  236.                 if (searchCustomer.Count == 0)
  237.                 {
  238.                     searchError.Text = "No results, try again";
  239.                 }
  240.  
  241.                 else
  242.                 {
  243.                     foreach (var customerName in searchCustomer)
  244.                     {
  245.                         listboxSearchResult.Items.Add(customerName);
  246.                     }
  247.                 }
  248.             }
  249.  
  250.             else
  251.             {
  252.                 searchError.Text = "inputfield cannot be empty.";
  253.             }
  254.  
  255.             searchTextbox.Focus();
  256.  
  257.         }
  258.  
  259.         private void listboxSearchResult_SelectedIndexChanged(object sender, EventArgs e)
  260.         {
  261.             var selectedCustomer = customers
  262.             .Where(customer => customer.Name == (string)listboxSearchResult.SelectedItem)
  263.             .Select(customer => customer.Accounts);
  264.  
  265.             viewCustomersGrid.DataSource = selectedCustomer;
  266.  
  267.         }
  268.  
  269.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  270.         {
  271.             viewCustomersGrid.DataSource = (comboBox2.SelectedItem as Customer).Accounts;
  272.         }
  273.     }
  274. }
Add Comment
Please, Sign In to add comment