Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections;
- class Worker
- {
- int id;
- string name;
- float wage;
- public Worker(int id, string name, float wage)
- {
- this.id = id;
- this.name = name;
- this.wage = wage;
- }
- public void WriteToFile(string file)
- {
- string tempString = "id: " + this.id + " name: " + this.name + " wage: " + this.wage + "\n";
- File.AppendAllText(file, tempString);
- }
- }
- class Ohjelma
- {
- static void Main(string[] args)
- {
- ArrayList workers = new ArrayList();
- string path = "C:\\Users\\Aksu\\source\\repos\\testiprojekti\\testiprojekti\\rekisteri.txt";
- if (File.Exists(path))
- {
- File.Delete(path);
- }
- else
- {
- File.Create(path);
- }
- Console.WriteLine("Anna kolmen työntekijän tiedot (id, nimi, palkka): ");
- //creating three workers
- CreateWorker(workers);
- CreateWorker(workers);
- CreateWorker(workers);
- for (int i = 0; i < workers.Count; i++)
- {
- ((Worker)workers[i]).WriteToFile(path);
- }
- //asks for id and prints worker with that id
- Console.WriteLine("Anna työntekijän id:");
- int IdSearch = Int32.Parse(Console.ReadLine());
- string result = string.Empty;
- var lines = File.ReadAllLines(path);
- bool containsString = false;
- foreach (var line in lines)
- {
- if (line.Contains("id: " + IdSearch + " "))
- {
- var text = line.Replace("id :", "");
- result = text.Trim();
- containsString = true;
- }
- }
- if (containsString == true)
- {
- Console.WriteLine(result);
- }
- else if (containsString == false)
- {
- Console.WriteLine("Työntekijää ei löydy id-numerolla " + IdSearch);
- }
- }
- //asks for worker info and creates worker
- static void CreateWorker(ArrayList workers)
- {
- int id;
- string name;
- float wage;
- Console.WriteLine("Anna id:");
- id = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Anna nimi:");
- name = Convert.ToString(Console.ReadLine());
- Console.WriteLine("Anna palkka:");
- wage = (float)Convert.ToDouble(Console.ReadLine());
- workers.Add(new Worker(id, name, wage));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement