Advertisement
AuriR

Beginning Programming - Body Class

Feb 28th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. // Example of Object Oriented Programming using the Human Body as the base.
  2. // Written by Auri A. Rahimzadeh, February 28, 2015, @auri on Twitter, Take classes at: http://www.elevenfifty.com
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. class Body {
  8.     // Body status.
  9.     bool _isAlive = true;
  10.     bool _hasToPoop = false;
  11.     bool _hasToPee = false;
  12.     bool _isHungry = true;
  13.     bool _isThirsty = true;
  14.    
  15.     int _bladderLevel = 0;
  16.     int _stomachLevel = 0;
  17.    
  18.     // Body details.
  19.     string _firstName = null;
  20.     string _lastName = null;
  21.     GenderType _gender = GenderType.Undefined;
  22.  
  23.     // Custom Types
  24.     public enum GenderType {
  25.         Male,
  26.         Female,
  27.         Undefined
  28.     }
  29.  
  30.     // Properties
  31.    
  32.     public bool IsThirsty {
  33.         get {
  34.             return _isThirsty;
  35.         }
  36.     }
  37.    
  38.     public bool IsHungry { get { return _isHungry; } }
  39.    
  40.     public bool HasToPoop { get { return _hasToPoop; } }
  41.    
  42.     public bool HasToPee { get { return _hasToPee; } }
  43.    
  44.     public GenderType Gender { get { return _gender; } }
  45.  
  46.     public string FirstName {
  47.         get {
  48.             return _firstName;
  49.         }
  50.         set {
  51.             if (!String.IsNullOrWhiteSpace(value)) _firstName = value.Trim();
  52.             else throw new ArgumentException("First name cannot be blank.");
  53.         }
  54.     }
  55.    
  56.     public string LastName {
  57.         get {
  58.             return _lastName;
  59.         }
  60.         set {
  61.             if (!String.IsNullOrWhiteSpace(value)) _lastName = value.Trim();
  62.             else throw new ArgumentException("Last name cannot be blank.");
  63.         }
  64.     }
  65.    
  66.     // Constructors
  67.    
  68.     public Body(string firstName, string lastName) {
  69.         this.FirstName = firstName;
  70.         this.LastName = lastName;
  71.     }
  72.  
  73.     public Body(string firstName, string lastName, GenderType gender) {
  74.         this.FirstName = firstName;
  75.         this.LastName = lastName;
  76.         this._gender = gender;
  77.     }
  78.  
  79.     // Bodily Functions
  80.    
  81.     public bool Eat() {
  82.         if (_stomachLevel <= 3) {
  83.             _stomachLevel += 1;
  84.             Console.WriteLine("Ok, I've eaten. Thanks for that.");
  85.             return true;
  86.         }
  87.         else {
  88.             _hasToPoop = true;
  89.             Console.WriteLine("Sorry, I'm full. Maybe I should do something about that.");         
  90.             return false;
  91.         }
  92.     }
  93.    
  94.     public bool Drink() {
  95.         if (_bladderLevel <= 5) {
  96.             _bladderLevel += 1;
  97.             Console.WriteLine("Ok, I'drank. Thanks for that.");
  98.             return true;
  99.         }
  100.         else {
  101.             _hasToPee = true;
  102.             Console.WriteLine("Sorry, I'm full. Maybe I should do something about that.");         
  103.             return false;
  104.         }
  105.     }
  106.  
  107.     public bool Pee() {
  108.         if (HasToPee) {
  109.             _bladderLevel = 0;
  110.             _hasToPee = false;
  111.             Console.WriteLine("Phew, I'm relieved!");
  112.             return true;
  113.         }
  114.         else {
  115.             Console.WriteLine("I don't have to pee.");
  116.             return false;
  117.         }
  118.     }
  119.  
  120.     public bool Poop() {
  121.         if (HasToPoop) {
  122.             _stomachLevel = 0;
  123.             _hasToPoop = false;
  124.             Console.WriteLine("Phew, I'm a few pounds lighter!");
  125.             return true;
  126.         }
  127.         else {
  128.             Console.WriteLine("I don't have to poop.");
  129.             return false;
  130.         }
  131.     }
  132.  
  133.     public void Fart() {
  134.         throw new NotImplementedException("Cannot call this on the base class.");
  135.     }
  136. }
  137.  
  138. class Child : Body
  139. {
  140.     public bool IsInSchool { get; private set; }
  141.  
  142.     public new void Fart() {
  143.         Console.WriteLine("::giggles::");
  144.     }
  145.    
  146.     public Child(string firstName, string lastName) : base (firstName, lastName) {
  147.         // nothing else to do here     
  148.     }
  149.  
  150. }
  151.  
  152. class Adult : Body
  153. {  
  154.     public bool IsEmployed { get; private set; }
  155.    
  156.     public new void Fart() {
  157.         Console.WriteLine("Fbbbbbbbbbbbbbbbbbbbbtttttttttttttttttt! Excuse me! ((pew!))");
  158.     }
  159.    
  160.     public Adult(string firstName, string lastName) : base (firstName, lastName) {
  161.         // nothing else to do here 
  162.     }
  163.  
  164. }
  165.  
  166. var Mom = new Adult("Karen", "Cohn");
  167. var Daishaun = new Child("Daishaun", "Rahimzadeh");
  168. var Duke = new Adult("Duke","Poopington");
  169.  
  170. Daishaun.Fart();
  171. Mom.Fart();
  172.  
  173. Daishaun.Drink();
  174. Mom.Poop();
  175.  
  176. bool IsHealthOk(Body person) {
  177.     Console.WriteLine("Went to the doctor. Everything is fine.");
  178.     if (person is Child) (person as Child).Fart();
  179.     else if (person is Adult) (person as Adult).Fart();
  180.     return true;
  181. }
  182.  
  183. IsHealthOk(Daishaun);
  184. IsHealthOk(Mom);
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. // *******************************************************************************************
  192. // *                                         LISTS!                                          *
  193. // *******************************************************************************************
  194.  
  195. var listOfPeople = new List<Body>();
  196. Console.WriteLine("Total number of people: " + listOfPeople.Count);
  197. listOfPeople.Add(new Child("Joe", "Schmoe"));
  198. listOfPeople.Add(new Adult("Auri", "Rahimzadeh"));
  199. listOfPeople.Add(new Adult("Karen", "Cohn"));
  200. listOfPeople.Add(new Child("Scott", "Jones"));
  201. listOfPeople.Add(new Child("Daishaun", "Rahimzadeh"));
  202. listOfPeople.Add(new Adult("Duke", "Poopington"));
  203. listOfPeople.Add(new Adult("Steve", "Wozniak"));
  204. Console.WriteLine("Total number of people: " + listOfPeople.Count);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement