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.Windows.Forms;
- namespace testApp
- {
- public partial class CustomerOverviewForm : Form
- {
- List<Customer> customers = new List<Customer>();
- public CustomerOverviewForm()
- {
- InitializeComponent();
- CreateCustomersAndAccounts();
- }
- private void CreateCustomersAndAccounts()
- {
- customers.Add(new Customer
- {
- Name = "Marika Klasson",
- Id = "910227",
- Accounts = new List<Account> {
- new Account {
- typeOfAccount = CheckingAccount.typeString,
- balance = 89 },
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 14},
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 55 } }
- });
- customers.Add(new Customer
- {
- Name = "Hilda Andersson",
- Id = "880803",
- Accounts = new List<Account> {
- new Account {
- typeOfAccount = CheckingAccount.typeString,
- balance = 13 },
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 0 },
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 0 } }
- });
- customers.Add(new Customer
- {
- Name = "Maja Nilsson",
- Id = "621203",
- Accounts = new List<Account> {
- new Account {
- typeOfAccount = CheckingAccount.typeString,
- balance = 55 },
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 0 },
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 0 } }
- });
- customers.Add(new Customer
- {
- Name = "Johanna Jansson",
- Id = "950820",
- Accounts = new List<Account> {
- new Account {
- balance = 798,
- typeOfAccount = CheckingAccount.typeString },
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 95 },
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 0} }
- });
- customers.Add(new Customer
- {
- Name = "Majbritt Hansson",
- Id = "430525",
- Accounts = new List<Account>{
- new Account {
- typeOfAccount = CheckingAccount.typeString,
- balance = 11},
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 0},
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 133} }
- });
- listCustomerOverview.DataSource = customers;
- comboBox2.DataSource = customers;
- }
- private void listCustomerOverview_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listCustomerOverview.SelectedItem != null)
- {
- dataGridView1.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
- }
- }
- private void btnDeposit_Click(object sender, EventArgs e)
- {
- try
- {
- var value = decimal.Parse(txtAmount.Text);
- var account = (listCustomerOverview.SelectedItem as Customer).Accounts[viewCustomersGrid.CurrentCell.RowIndex];
- account.Deposit(value);
- viewCustomersGrid.DataSource = null;
- viewCustomersGrid.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
- lblTransactionStatus.Text = "Transaction was successfull. You have inserted\n" + txtAmount.Text + " to choosen account";
- }
- catch (Exception)
- {
- lblTransactionStatus.Text = txtAmount.Text + ", is an invalid input. ";
- }
- }
- private void btnWithdraw_Click(object sender, EventArgs e)
- {
- try
- {
- var value = decimal.Parse(txtAmount.Text);
- var account = (listCustomerOverview.SelectedItem as Customer).Accounts[viewCustomersGrid.CurrentCell.RowIndex];
- account.WithDraw(value);
- viewCustomersGrid.DataSource = null;
- viewCustomersGrid.DataSource = (listCustomerOverview.SelectedItem as Customer).Accounts;
- lblTransactionStatus.Text = "Transaction was successfull. You have withdrawed: \n" + txtAmount.Text + " from the choosen account";
- }
- catch
- {
- lblTransactionStatus.Text = txtAmount.Text + " is either an invalid input or\nyou don't have enough money.";
- }
- }
- private void btnAddCustomer_Click(object sender, EventArgs e)
- {
- while (txtAddName.Text == String.Empty || txtAddId.Text.Length < 5)
- {
- lblNotAdded.Text = "Fill out both textboxes.\nId must be atleast 5 characters";
- return;
- }
- if (customers.Any(customer => customer.Id == txtAddId.Text))
- {
- lblNotAdded.Text = "This id: " + txtAddId.Text + " does already exist.";
- }
- else
- {
- customers.Add(new Customer
- {
- Name = txtAddName.Text,
- Id = txtAddId.Text,
- Accounts = new List<Account>{
- new Account {
- typeOfAccount = CheckingAccount.typeString,
- balance = 0},
- new Account {
- typeOfAccount = PayAccount.typeString,
- balance = 0},
- new Account {
- typeOfAccount = SavingsAccount.typeString,
- balance = 0}
- }
- });
- listCustomerOverview.DataSource = null;
- listCustomerOverview.DataSource = customers;
- comboBox2.DataSource = null;
- comboBox2.DataSource = customers;
- txtAddId.Clear();
- txtAddName.Clear();
- btnRemove.Enabled = true;
- lblNotAdded.Text = "Success";
- listCustomerOverview.SelectedIndex = customers.Count - 1;
- }
- }
- private void btnRemove_Click(object sender, EventArgs e)
- {
- customers.RemoveAt(listCustomerOverview.SelectedIndex);
- listCustomerOverview.SelectedIndex = 0;
- listCustomerOverview.DataSource = null;
- listCustomerOverview.DataSource = customers;
- comboBox2.DataSource = null;
- comboBox2.DataSource = customers;
- lblNotAdded.Text = "";
- if (listCustomerOverview.Items.Count == 0)
- {
- btnRemove.Enabled = false;
- viewCustomersGrid.DataSource = null;
- }
- else
- btnRemove.Enabled = true;
- }
- private void searchButton_Click(object sender, EventArgs e)
- {
- if (searchTextbox.Text != String.Empty)
- {
- listboxSearchResult.Items.Clear();
- var searchCustomer = customers
- .Where(customer => customer.Id == searchTextbox.Text || customer.Name.ToLower().Contains(searchTextbox.Text.ToLower()))
- .Select(customer => customer.Name).ToList();
- searchError.Text = "";
- if (searchCustomer.Count == 0)
- {
- searchError.Text = "No results, try again";
- }
- else
- {
- foreach (var customerName in searchCustomer)
- {
- listboxSearchResult.Items.Add(customerName);
- }
- }
- }
- else
- {
- searchError.Text = "inputfield cannot be empty.";
- }
- searchTextbox.Focus();
- }
- private void listboxSearchResult_SelectedIndexChanged(object sender, EventArgs e)
- {
- var selectedCustomer = customers
- .Where(customer => customer.Name == (string)listboxSearchResult.SelectedItem)
- .Select(customer => customer.Accounts);
- viewCustomersGrid.DataSource = selectedCustomer;
- }
- private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
- {
- viewCustomersGrid.DataSource = (comboBox2.SelectedItem as Customer).Accounts;
- }
- }
- }
Add Comment
Please, Sign In to add comment