Advertisement
amarek

OOP LV6 - Zadatak2

Nov 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 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.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace LV6___Zadatak_2
  13. {
  14.     public partial class form_imenik : Form
  15.     {
  16.         public form_imenik()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         class Kontakt
  22.         {
  23.             #region data_members
  24.             private string ime;
  25.             public string prezime;
  26.             private string broj_telefona;
  27.             #endregion
  28.  
  29.             #region public_methods
  30.             public Kontakt()
  31.             {
  32.                 ime = ""; prezime = ""; broj_telefona = "";
  33.             }
  34.             public Kontakt(string i, string p, string b)
  35.             {
  36.                 ime = i; prezime = p; broj_telefona = b;
  37.             }
  38.             public override string ToString()
  39.             {
  40.                 return ime + "\t" + prezime + "\t" + broj_telefona;
  41.             }
  42.             #endregion
  43.         }
  44.  
  45.         List<Kontakt> listKontakt = new List<Kontakt>();
  46.         string path = "C:\\Users\\Student\\Documents\\Visual Studio 2015\\Projects\\LV6 - Zadatak 2\\LV6 - Zadatak 2\\imenik.txt";
  47.  
  48.         private void form_imenik_Load(object sender, EventArgs e)
  49.         {
  50.             using (System.IO.StreamReader reader = new System.IO.StreamReader(@path))
  51.             {
  52.                 string line;
  53.                 while ((line = reader.ReadLine()) != null)
  54.                 {
  55.                     string[] parts = line.Split('\t');
  56.                     Kontakt K = new Kontakt(parts[0], parts[1], parts[2]);
  57.                     listKontakt.Add(K);
  58.                 }
  59.                 listBox_popis.DataSource = null;
  60.                 listBox_popis.DataSource = listKontakt;
  61.             }
  62.  
  63.         }
  64.  
  65.         private void button_dodaj_Click(object sender, EventArgs e)
  66.         {
  67.             bool error = false;
  68.  
  69.             if (textBox_ime.Text == "" || textBox_prezime.Text == "" || textBox_broj.Text == "")
  70.             {
  71.                 error = true;
  72.                 MessageBox.Show("Neko od polja ostalo je prazno.", "Pogreška!");
  73.             }
  74.             else if (!Regex.IsMatch(textBox_broj.Text, @"^[0-9]+$"))
  75.             {
  76.                 error = true;
  77.                 MessageBox.Show("Broj telefona je neispravan!", "Pogreška!");
  78.             }
  79.             else
  80.             {
  81.                 error = false;
  82.             }
  83.  
  84.             if (!error)
  85.             {
  86.                 Kontakt K = new Kontakt(textBox_ime.Text, textBox_prezime.Text, textBox_broj.Text);
  87.                 listKontakt.Add(K);
  88.                 listBox_popis.DataSource = null;
  89.                 listBox_popis.DataSource = listKontakt;
  90.                 textBox_ime.Text = String.Empty;
  91.                 textBox_prezime.Text = String.Empty;
  92.                 textBox_broj.Text = String.Empty;
  93.             }
  94.         }
  95.  
  96.         private void button_izlaz_Click(object sender, EventArgs e)
  97.         {
  98.             using (System.IO.StreamWriter write = new System.IO.StreamWriter(@path))
  99.             {
  100.                 foreach (Kontakt K in listKontakt)
  101.                 {
  102.                     {
  103.                         write.WriteLine(K.ToString());
  104.                     }
  105.                 }
  106.             }
  107.             Application.Exit();
  108.         }
  109.  
  110.         private void button_pretraga_Click(object sender, EventArgs e)
  111.         {
  112.             bool state = false;
  113.  
  114.             if (textBox_pretraga.Text == "")
  115.             {
  116.                 MessageBox.Show("Niste unjeli prezime!", "Pogreška!");
  117.             }
  118.             else
  119.             {
  120.                 foreach (Kontakt K in listKontakt)
  121.                 {
  122.                     if (K.prezime.Contains(textBox_pretraga.Text))
  123.                     {
  124.                         state = true;
  125.                         break;
  126.                     }
  127.                     else
  128.                     {
  129.                         state = false;
  130.                     }
  131.                 }
  132.  
  133.                 if (state)
  134.                 {
  135.                     MessageBox.Show("Kontakt postoji!", "Pretraga!");
  136.                 }
  137.                 else
  138.                 {
  139.                     MessageBox.Show("Kontakt ne postoji!", "Pretraga!");
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement