Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp123
- {
- class human
- {
- string name;
- int personnr;
- public human(string name, int personnr)
- {
- this.name = name;
- this.personnr = personnr;
- }
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public int Personnr
- {
- get { return personnr; }
- set { personnr = value; }
- }
- }
- class Program
- {
- static int search(List<human> list, int key)
- {
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].Personnr == key)
- return i;
- }
- return -1;
- }
- static int search(List<human> list, int key)
- {
- int found = -1;
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].Personnr == key)
- found = i;
- }
- return found;
- }
- static void Main(string[] args)
- {
- while (true)
- {
- List<human> person = new List<human>();
- person.Add(new human("Thomas", 900331));
- person.Add(new human("Lena", 470727));
- person.Add(new human("Lars", 430721));
- Console.WriteLine("[L]ägg till person och personnummer");
- Console.WriteLine("[S]ök efter personnummer");
- string menu = Console.ReadLine();
- if (menu == "L" || menu == "l")
- {
- Console.WriteLine("Skriv in namn");
- string input = Console.ReadLine();
- Console.WriteLine("Skriv in personnummer 6 siffror");
- int year = Convert.ToInt32(Console.ReadLine());
- person.Add(new human(input, year));
- }
- else if (menu == "S" || menu == "s")
- {
- Console.WriteLine("Skriv in personnr");
- string personnr = Console.ReadLine();
- int key = Convert.ToInt32(personnr);
- int index = search(person, key);
- if (index == -1)
- Console.WriteLine("personen finns inte");
- else
- Console.WriteLine("Personen med personnr " + key + " finns på index " + (index + 1) + " och heter " +
- person[index].Name);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement