garnettkg

Zad6 Delegati,interfejsi

Jan 26th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Napisati klasu Predmet koja sadrži polja:
  2. Id – automatski generisan celi broj
  3. Naziv
  4. Šifra
  5. Ocena
  6.  
  7. Napraviti formu koja omogućava unos pomenutih podataka (osim ID-a) i smešta objekte tipa Predmet u ListBox kontrolu. Dodati dugme “prosek” koje omogućava prikaz prosečne ocene za sve unete predmete.
  8. Na postojeću formu dodati tekstualno polje za unos broja indeksa studenta u formatu SSSS-BB/GG (smer-broj-godina).
  9. Dodati dugme “snimi” koje će omogućiti snimanje unetih predmeta u datoteku koja sadži broj indeksa studenta i ekstenziju “pol” (položeno).
  10. Dodati dugme “pročitaj” koje će omogućiti pronalaženje datoteke sa zadatim brojem indeksa i prikaz položenih predmeta (pročitanih iz datoteke) u ListBox kontroli.
  11.  
  12. Voditi računa o mogućim izuzecima u radu sa datotekama, prikazivati korisniku odgovarajuće poruke.
  13.  
  14.  
  15.  
  16.  
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Data;
  21. using System.Drawing;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Windows.Forms;
  25. using System.IO;
  26.  
  27. namespace V6G1
  28. {
  29. public partial class Form1 : Form
  30. {
  31. public Form1()
  32. {
  33. InitializeComponent();
  34. }
  35.  
  36. private void btnDodaj_Click(object sender, EventArgs e)
  37. {
  38. Predmet p = new Predmet(txtNaziv.Text, txtSifra.Text,
  39. int.Parse(txtOcena.Text));
  40. lbPredmeti.Items.Add(p);
  41. }
  42.  
  43. private void btnProsek_Click(object sender, EventArgs e)
  44. {
  45. int s=0;
  46. for (int i = 0; i < lbPredmeti.Items.Count; i++)
  47. {
  48. Predmet p = lbPredmeti.Items[i] as Predmet;
  49. s += p.Ocena;
  50. }
  51. lblProsek.Text = (s*1.0 / lbPredmeti.Items.Count).ToString();
  52. }
  53.  
  54. private void btnSnimi_Click(object sender, EventArgs e)
  55. {
  56. //NRT-10/10 -> NRT1010.pol
  57. string putanja=txtIndeks.Text.Replace("-","").Replace("/", "");
  58. putanja+=".txt";
  59. FileStream fs =
  60. new FileStream(putanja, FileMode.Create, FileAccess.Write);
  61.  
  62. StreamWriter writer = new StreamWriter(fs);
  63. for (int i = 0; i < lbPredmeti.Items.Count; i++)
  64. {
  65. Predmet p = lbPredmeti.Items[i] as Predmet;
  66. string linija = p.Naziv + "|" + p.Sifra + "|" + p.Ocena;
  67. writer.WriteLine(linija);
  68. }
  69. writer.Close();
  70. }
  71.  
  72. private void btnProcitaj_Click(object sender, EventArgs e)
  73. {
  74. string putanja = txtIndeks.Text.Replace("-", "").Replace("/", "");
  75. putanja += ".txt";
  76. FileStream fs =
  77. new FileStream(putanja, FileMode.Open, FileAccess.Read);
  78. StreamReader reader = new StreamReader(fs);
  79. lbPredmeti.Items.Clear();
  80. while (!reader.EndOfStream)
  81. {
  82. string linija = reader.ReadLine();
  83. string[] podaci = linija.Split('|');
  84. Predmet p = new Predmet(podaci[0], podaci[1],
  85. int.Parse(podaci[2]));
  86. lbPredmeti.Items.Add(p);
  87. }
  88.  
  89. }
  90. }
  91. }
  92.  
  93.  
  94.  
  95. using System;
  96. using System.Collections.Generic;
  97. using System.Linq;
  98. using System.Text;
  99.  
  100. namespace V6G1
  101. {
  102. class Predmet
  103. {
  104. static int idGen = 1;
  105. int id = idGen++;
  106. string naziv;
  107. string sifra;
  108. int ocena;
  109. public Predmet(string naziv, string sifra, int ocena)
  110. {
  111. this.naziv = naziv;
  112. this.sifra = sifra;
  113. this.ocena = ocena;
  114. }
  115. public override string ToString()
  116. {
  117. return naziv + " " + ocena;
  118. }
  119.  
  120. public string Naziv { get { return naziv; } }
  121. public string Sifra { get { return sifra; } }
  122. public int Ocena { get { return ocena; } }
  123. }
  124. }
  125.  
  126.  
  127. using System;
  128. using System.Collections.Generic;
  129. using System.Linq;
  130. using System.Windows.Forms;
  131.  
  132. namespace V6G1
  133. {
  134. static class Program
  135. {
  136. /// <summary>
  137. /// The main entry point for the application.
  138. /// </summary>
  139. [STAThread]
  140. static void Main()
  141. {
  142. Application.EnableVisualStyles();
  143. Application.SetCompatibleTextRenderingDefault(false);
  144. Application.Run(new Form1());
  145. }
  146. }
  147. }
Add Comment
Please, Sign In to add comment