EmilySamantha80

CMyShoe (C# version of my first C++ program)

Jul 29th, 2016 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. // Title:  CMyShoe (C# version of my first C++ program)
  2. // Author: Emily Johnson
  3. // Date:   2024-12-20
  4. // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
  5. // You are free to share and adapt this code for any purposes, commerical and non-commercial,
  6. // as long as appropriate credit is given, you indicate if changes were made, and you share your
  7. // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
  8.  
  9. using System;
  10. using System.ComponentModel;
  11.  
  12. namespace CMyShoe
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             // Declare the left shoe
  19.             var leftShoe = new Shoe(Shoe.Foot.Left);
  20.             // Declare the right shoe with inline property setters
  21.             var rightShoe = new Shoe
  22.             {
  23.                 // Set any initial properties you want to set
  24.                 ShoeFoot = Shoe.Foot.Right,
  25.                 TypeOfShoe = Shoe.ShoeType.Slipper
  26.             };
  27.  
  28.             // Print the name of our program
  29.             Console.WriteLine("CMyShoe (C# Edition)");
  30.  
  31.             // Show the shoe properties before setting them
  32.             Console.WriteLine();
  33.             Console.WriteLine("Before Setting Properties...");
  34.             Console.WriteLine("----------------------------");
  35.             // Describe the left shoe
  36.             leftShoe.Describe();
  37.             Console.WriteLine();
  38.             // Describe the right shoe
  39.             rightShoe.Describe();
  40.             Console.WriteLine();
  41.  
  42.             // Set the properties of the left shoe
  43.             leftShoe.ShoeFoot = Shoe.Foot.Left;
  44.             leftShoe.TypeOfShoe = Shoe.ShoeType.HikingBoot;
  45.             leftShoe.Color = "Blue";
  46.             leftShoe.Size = 10.5;
  47.  
  48.             // Set the properties of the right shoe
  49.             rightShoe.ShoeFoot = Shoe.Foot.Right;
  50.             rightShoe.TypeOfShoe = Shoe.ShoeType.Slipper;
  51.             // Demonstrate getting string input from user
  52.             Console.Write("Enter the color of the right shoe: ");
  53.             rightShoe.Color = Console.ReadLine() ?? "Null";
  54.             Console.WriteLine(rightShoe.Color);
  55.  
  56.             // Demonstrates getting a numeric value from user input
  57.             Console.Write("Enter the size of the right shoe: ");
  58.             string inputSize = Console.ReadLine() ?? "Null";
  59.             Console.WriteLine(inputSize);
  60.             if (double.TryParse(inputSize, out double size))
  61.             {
  62.                 rightShoe.Size = size;
  63.             }
  64.             else
  65.             {
  66.                 Console.WriteLine($"Invalid shoe size! Keeping the default value.");
  67.             }
  68.             Console.WriteLine();
  69.  
  70.  
  71.             Console.WriteLine("After Setting Properties...");
  72.             Console.WriteLine("---------------------------");
  73.  
  74.             // Describe the left shoe
  75.             leftShoe.Describe();
  76.             Console.WriteLine();
  77.             // Describe the right shoe
  78.             rightShoe.Describe();
  79.             Console.WriteLine();
  80.         }
  81.     }
  82.  
  83.     public class Shoe
  84.     {
  85.         // Available shoe types
  86.         public enum ShoeType
  87.         {
  88.             [Description("Hiking Boot")]
  89.             HikingBoot,
  90.             [Description("Walking Shoe")]
  91.             WalkingShoe,
  92.             [Description("Running Shoe")]
  93.             RunningShoe,
  94.             [Description("Slipper")]
  95.             Slipper
  96.         }
  97.  
  98.         // Available feet
  99.         public enum Foot
  100.         {
  101.             [Description("Left")]
  102.             Left,
  103.             [Description("Right")]
  104.             Right
  105.         }
  106.  
  107.         // Which foot is the shoe for
  108.         public Foot ShoeFoot { get; set; } = Foot.Left;
  109.  
  110.         // Type of the shoe
  111.         public ShoeType TypeOfShoe { get; set; } = ShoeType.WalkingShoe;
  112.  
  113.         // Color of the shoe. Demonstrates a property with the full declaration
  114.         private string _Color = "Black";
  115.         public string Color
  116.         {
  117.             get
  118.             {
  119.                 return _Color;
  120.             }
  121.             set
  122.             {
  123.                 _Color = value;
  124.             }
  125.         }
  126.  
  127.         // Size of the shoe. Demonstrates a property with a shortened declaration
  128.         public double Size { get; set; } = 10;
  129.  
  130.         // Constructor to set the default values
  131.         public Shoe()
  132.         {
  133.         }
  134.  
  135.         // Shows alternate constructor that calls another constructor
  136.         public Shoe(Foot shoeFoot)
  137.         {
  138.             ShoeFoot = shoeFoot;
  139.         }
  140.  
  141.         // Print out the shoe properties to the console.
  142.         public void Describe()
  143.         {
  144.             Console.WriteLine($"Foot: {ShoeFoot} ({ShoeFoot.GetDescription()})");
  145.             Console.WriteLine($"Shoe Type: {TypeOfShoe} ({TypeOfShoe.GetDescription()})");
  146.             Console.WriteLine($"Color: {Color}");
  147.             Console.WriteLine($"Size: {Size}");
  148.         }
  149.     }
  150.  
  151.     // This demonstrates extension methods.
  152.     // It extends the Enum type to add GetDescription()
  153.     public static class EnumExtensions
  154.     {
  155.  
  156.         // This extension method is broken out so you can use a similar pattern with
  157.         // other MetaData elements in the future. This is your base method for each.
  158.         public static T? GetAttribute<T>(this Enum value) where T : Attribute
  159.         {
  160.             var type = value.GetType();
  161.             var memberInfo = type.GetMember(value.ToString());
  162.             var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
  163.             return attributes.Length > 0
  164.             ? (T)attributes[0]
  165.             : null;
  166.         }
  167.  
  168.         // This method creates a specific call to the above method, requesting the
  169.         // Description MetaData attribute.
  170.         public static string GetDescription(this Enum value)
  171.         {
  172.             var attribute = value.GetAttribute<DescriptionAttribute>();
  173.             return attribute == null ? value.ToString() : attribute.Description;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment