Obawiam się, że aby uczyć się poprzez czytanie kodu trzeba znać podstawy składni, ale niech będzie:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace TurboStudentManager
{
// nasza klasa definiująca studenta
public class Student
{
// to są właściwości właściwości
public int NrIndeksu { get; set; }
public string Imie { get; set; }
public string Nazwisko { get; set; }
public List<int> oceny { get; set; }
// konstruktor bezparametrowy, potrzebny do serializacji
public Student()
{
this.oceny = new List<int>();
}
// tego konstruktora będziemy używać podczas tworzenia obiektów, moglibyśmy tamtego, ale ten jest fajniejszy
public Student(int nrIndeksu, string imie, string nazwisko)
{
this.NrIndeksu = nrIndeksu;
this.Imie = imie;
this.Nazwisko = nazwisko;
this.oceny = new List<int>();
}
// studentowi można dodać ocenę, będzie robiła to ta funkcja
public void DodajOcene(int ocena)
{
this.oceny.Add(ocena);
}
// przeciążamy metodę ToString, więc będzie nam łatwo wyświetlać dane studenta
public override string ToString()
{
StringBuilder sb = new StringBuilder(); // to taka śmieszna klasa do wydajnego budowania napisów
sb.AppendFormat("Student: {0} {1}, nr indeksu: {2}\n", this.Imie, this.Nazwisko, this.NrIndeksu);
sb.Append("Oceny:");
foreach (int o in this.oceny)
{
sb.AppendFormat(" {0}", o);
}
return sb.ToString();
}
}
// główna klasa programu
class Program
{
// lista studentów, na której będziemy pracować w aplikacji
private static List<Student> studenci = new List<Student>();
// główna funkcja aplikacji
static void Main(string[] args)
{
bool koniec = false;
do // dzięki tej wspaniałej pętli będziemy mieli menu w aplikacji
{
Console.WriteLine("\nWybierz opcję:\n1 - Dodaj studenta\n2 - Wyświetl listę studentów \n3 - Dodaj ocene\n4 - Zapisz do pliku\n5 - Wyjście");
int option = -1;
while (!int.TryParse(Console.ReadLine(), out option)) // sprawdzamy, czy użytkownik wpisał liczbę
{
Console.WriteLine("Wybierz poprawną opcję menu");
}
// w zależności od wyboru użytkownika uruchamiamy odpowiednią funkcję, a nie wsadzamy cały kod do jednej
switch (option)
{
case 1:
DodajStudenta();
break;
case 2:
WyswietlListe();
break;
case 3:
DodajOcene();
break;
case 4:
ZapiszDoPliku();
break;
case 5:
koniec = true;
break;
default:
Console.WriteLine("Nieznana opcja menu");
break;
}
} while (!koniec);
Console.WriteLine("KONIEC!");
}
private static void DodajStudenta()
{
Console.Write("Podaj nazwisko: ");
string nazwisko = Console.ReadLine();
Console.Write("Podaj imię: ");
string imie = Console.ReadLine();
Console.Write("Podaj nr indeksu: ");
int nrIndeksu = int.Parse(Console.ReadLine());
Student nowy = new Student(nrIndeksu, imie, nazwisko);
studenci.Add(nowy);
}
private static void WyswietlListe()
{
Console.WriteLine("------------------STUDENCI---------------------");
foreach (var s in studenci)
{
Console.WriteLine(s);
Console.WriteLine();
}
Console.WriteLine("-----------------------------------------------");
}
private static void DodajOcene()
{
Console.Write("Podaj nr indeksu studenta: ");
int nrIndeksu = int.Parse(Console.ReadLine());
Student s = ZnajdzStudenta(nrIndeksu);
if (s == null)
{
Console.WriteLine("Nie ma takiego studenta!");
}
else
{
Console.Write("Podaj ocenę: ");
int ocena = int.Parse(Console.ReadLine());
s.DodajOcene(ocena);
}
}
private static Student ZnajdzStudenta(int nrIndeksu)
{
foreach (var s in studenci)
{
if (s.NrIndeksu == nrIndeksu)
{
return s;
}
}
return null;
}
private static void ZapiszDoPliku()
{
// serializacja to najprostszy sposób zapisu struktur danych do pliku, ale można oczywiście zrobić to po swojemu
using (FileStream fs = new FileStream(@"D:\Temp\studenci.txt", FileMode.OpenOrCreate))
{
new XmlSerializer(typeof(List<Student>)).Serialize(fs, studenci);
}
}
}
}