Advertisement
jyoung12387

Basic PersonModel Program

Mar 13th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace testProgram
  5. {              
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             PersonModel jason = new PersonModel("Jason", "Young", 33);
  11.             PersonModel joe = new PersonModel("Joe", "Smith", 44);
  12.             PersonModel mark = new PersonModel("Mark", "Jones", 26);
  13.            
  14.             List<PersonModel> people = new List<PersonModel>();
  15.             people.Add(jason);
  16.             people.Add(joe);
  17.             people.Add(mark);
  18.            
  19.             foreach(var person in people)
  20.                 person.DisplayPersonInfo();
  21.         }
  22.     }
  23.    
  24.     class PersonModel
  25.     {
  26.         private int Id {get; set;}
  27.         public string FirstName {get; set;}
  28.         public string LastName {get; set;}
  29.         public int Age {get; set;}
  30.        
  31.         public PersonModel() {}
  32.        
  33.         public PersonModel (string firstName, string lastName, int age)
  34.         {
  35.             this.Id = GetId();
  36.             this.FirstName = firstName;
  37.             this.LastName = lastName;
  38.             this.Age = age;
  39.         }
  40.        
  41.         private static int personId = 1000;
  42.        
  43.         // Set unique Id for each created person
  44.         private int GetId()
  45.         {
  46.             return ++personId;
  47.         }
  48.        
  49.         public void DisplayPersonInfo()
  50.         {
  51.             Console.WriteLine("ID: {0}  First Name: {1}, Last Name: {2}, Age: {3}", this.Id, this.FirstName, this.LastName, this.Age);
  52.         }
  53.        
  54.     }
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement