-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeopleFetcher.cs
76 lines (56 loc) · 2.12 KB
/
PeopleFetcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace CatWorx.BadgeMaker{
class PeopleFetcher{
public static List<Employee> GetEmployees(){
List<Employee> employees = new List<Employee>();
while(true)
{
// Move the initial prompt inside the loop, so it repeats for each employee
Console.WriteLine("Enter first name (leave empty to exit): ");
// change input to firstName
string firstName = Console.ReadLine();
if (firstName == "")
{
break;
}
// add a Console.ReadLine() for each value
Console.Write("Enter last name: ");
string lastName = Console.ReadLine();
Console.Write("Enter ID: ");
int id = Int32.Parse(Console.ReadLine());
Console.Write("Enter Photo URL:");
string photoUrl = Console.ReadLine();
Employee currentEmployee = new Employee(firstName, lastName, id, photoUrl);
employees.Add(currentEmployee);
}
return employees;
}
public static List<Employee> GetFromAPI(){
List<Employee> employees = new List<Employee>();
//make the api call
using(WebClient client = new WebClient()){
string response = client.DownloadString("https://randomuser.me/api/?results=10&nat=us&inc=name,id,picture");
//allows us to parse the JSON from our api call
dynamic dynJson = JsonConvert.DeserializeObject(response);
foreach (var item in dynJson.results)
{
string empId = item.id.value.ToString();
empId = empId.Replace("-","");
int empNum = Int32.Parse(empId);
Employee emp = new Employee(
item.name.first.ToString(),
item.name.last.ToString(),
empNum,
item.picture.large.ToString()
);
employees.Add(emp);
}
}
return employees;
}
}
}