Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example of Object Oriented Programming using the Human Body as the base.
- // Written by Auri A. Rahimzadeh, February 28, 2015, @auri on Twitter, Take classes at: http://www.elevenfifty.com
- using System;
- using System.Collections.Generic;
- class Body {
- // Body status.
- bool _isAlive = true;
- bool _hasToPoop = false;
- bool _hasToPee = false;
- bool _isHungry = true;
- bool _isThirsty = true;
- int _bladderLevel = 0;
- int _stomachLevel = 0;
- // Body details.
- string _firstName = null;
- string _lastName = null;
- GenderType _gender = GenderType.Undefined;
- // Custom Types
- public enum GenderType {
- Male,
- Female,
- Undefined
- }
- // Properties
- public bool IsThirsty {
- get {
- return _isThirsty;
- }
- }
- public bool IsHungry { get { return _isHungry; } }
- public bool HasToPoop { get { return _hasToPoop; } }
- public bool HasToPee { get { return _hasToPee; } }
- public GenderType Gender { get { return _gender; } }
- public string FirstName {
- get {
- return _firstName;
- }
- set {
- if (!String.IsNullOrWhiteSpace(value)) _firstName = value.Trim();
- else throw new ArgumentException("First name cannot be blank.");
- }
- }
- public string LastName {
- get {
- return _lastName;
- }
- set {
- if (!String.IsNullOrWhiteSpace(value)) _lastName = value.Trim();
- else throw new ArgumentException("Last name cannot be blank.");
- }
- }
- // Constructors
- public Body(string firstName, string lastName) {
- this.FirstName = firstName;
- this.LastName = lastName;
- }
- public Body(string firstName, string lastName, GenderType gender) {
- this.FirstName = firstName;
- this.LastName = lastName;
- this._gender = gender;
- }
- // Bodily Functions
- public bool Eat() {
- if (_stomachLevel <= 3) {
- _stomachLevel += 1;
- Console.WriteLine("Ok, I've eaten. Thanks for that.");
- return true;
- }
- else {
- _hasToPoop = true;
- Console.WriteLine("Sorry, I'm full. Maybe I should do something about that.");
- return false;
- }
- }
- public bool Drink() {
- if (_bladderLevel <= 5) {
- _bladderLevel += 1;
- Console.WriteLine("Ok, I'drank. Thanks for that.");
- return true;
- }
- else {
- _hasToPee = true;
- Console.WriteLine("Sorry, I'm full. Maybe I should do something about that.");
- return false;
- }
- }
- public bool Pee() {
- if (HasToPee) {
- _bladderLevel = 0;
- _hasToPee = false;
- Console.WriteLine("Phew, I'm relieved!");
- return true;
- }
- else {
- Console.WriteLine("I don't have to pee.");
- return false;
- }
- }
- public bool Poop() {
- if (HasToPoop) {
- _stomachLevel = 0;
- _hasToPoop = false;
- Console.WriteLine("Phew, I'm a few pounds lighter!");
- return true;
- }
- else {
- Console.WriteLine("I don't have to poop.");
- return false;
- }
- }
- public void Fart() {
- throw new NotImplementedException("Cannot call this on the base class.");
- }
- }
- class Child : Body
- {
- public bool IsInSchool { get; private set; }
- public new void Fart() {
- Console.WriteLine("::giggles::");
- }
- public Child(string firstName, string lastName) : base (firstName, lastName) {
- // nothing else to do here
- }
- }
- class Adult : Body
- {
- public bool IsEmployed { get; private set; }
- public new void Fart() {
- Console.WriteLine("Fbbbbbbbbbbbbbbbbbbbbtttttttttttttttttt! Excuse me! ((pew!))");
- }
- public Adult(string firstName, string lastName) : base (firstName, lastName) {
- // nothing else to do here
- }
- }
- var Mom = new Adult("Karen", "Cohn");
- var Daishaun = new Child("Daishaun", "Rahimzadeh");
- var Duke = new Adult("Duke","Poopington");
- Daishaun.Fart();
- Mom.Fart();
- Daishaun.Drink();
- Mom.Poop();
- bool IsHealthOk(Body person) {
- Console.WriteLine("Went to the doctor. Everything is fine.");
- if (person is Child) (person as Child).Fart();
- else if (person is Adult) (person as Adult).Fart();
- return true;
- }
- IsHealthOk(Daishaun);
- IsHealthOk(Mom);
- // *******************************************************************************************
- // * LISTS! *
- // *******************************************************************************************
- var listOfPeople = new List<Body>();
- Console.WriteLine("Total number of people: " + listOfPeople.Count);
- listOfPeople.Add(new Child("Joe", "Schmoe"));
- listOfPeople.Add(new Adult("Auri", "Rahimzadeh"));
- listOfPeople.Add(new Adult("Karen", "Cohn"));
- listOfPeople.Add(new Child("Scott", "Jones"));
- listOfPeople.Add(new Child("Daishaun", "Rahimzadeh"));
- listOfPeople.Add(new Adult("Duke", "Poopington"));
- listOfPeople.Add(new Adult("Steve", "Wozniak"));
- Console.WriteLine("Total number of people: " + listOfPeople.Count);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement