Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Title: CMyShoe (C# version of my first C++ program)
- // Author: Emily Johnson
- // Date: 2024-12-20
- // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
- // You are free to share and adapt this code for any purposes, commerical and non-commercial,
- // as long as appropriate credit is given, you indicate if changes were made, and you share your
- // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
- using System;
- using System.ComponentModel;
- namespace CMyShoe
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Declare the left shoe
- var leftShoe = new Shoe(Shoe.Foot.Left);
- // Declare the right shoe with inline property setters
- var rightShoe = new Shoe
- {
- // Set any initial properties you want to set
- ShoeFoot = Shoe.Foot.Right,
- TypeOfShoe = Shoe.ShoeType.Slipper
- };
- // Print the name of our program
- Console.WriteLine("CMyShoe (C# Edition)");
- // Show the shoe properties before setting them
- Console.WriteLine();
- Console.WriteLine("Before Setting Properties...");
- Console.WriteLine("----------------------------");
- // Describe the left shoe
- leftShoe.Describe();
- Console.WriteLine();
- // Describe the right shoe
- rightShoe.Describe();
- Console.WriteLine();
- // Set the properties of the left shoe
- leftShoe.ShoeFoot = Shoe.Foot.Left;
- leftShoe.TypeOfShoe = Shoe.ShoeType.HikingBoot;
- leftShoe.Color = "Blue";
- leftShoe.Size = 10.5;
- // Set the properties of the right shoe
- rightShoe.ShoeFoot = Shoe.Foot.Right;
- rightShoe.TypeOfShoe = Shoe.ShoeType.Slipper;
- // Demonstrate getting string input from user
- Console.Write("Enter the color of the right shoe: ");
- rightShoe.Color = Console.ReadLine() ?? "Null";
- Console.WriteLine(rightShoe.Color);
- // Demonstrates getting a numeric value from user input
- Console.Write("Enter the size of the right shoe: ");
- string inputSize = Console.ReadLine() ?? "Null";
- Console.WriteLine(inputSize);
- if (double.TryParse(inputSize, out double size))
- {
- rightShoe.Size = size;
- }
- else
- {
- Console.WriteLine($"Invalid shoe size! Keeping the default value.");
- }
- Console.WriteLine();
- Console.WriteLine("After Setting Properties...");
- Console.WriteLine("---------------------------");
- // Describe the left shoe
- leftShoe.Describe();
- Console.WriteLine();
- // Describe the right shoe
- rightShoe.Describe();
- Console.WriteLine();
- }
- }
- public class Shoe
- {
- // Available shoe types
- public enum ShoeType
- {
- [Description("Hiking Boot")]
- HikingBoot,
- [Description("Walking Shoe")]
- WalkingShoe,
- [Description("Running Shoe")]
- RunningShoe,
- [Description("Slipper")]
- Slipper
- }
- // Available feet
- public enum Foot
- {
- [Description("Left")]
- Left,
- [Description("Right")]
- Right
- }
- // Which foot is the shoe for
- public Foot ShoeFoot { get; set; } = Foot.Left;
- // Type of the shoe
- public ShoeType TypeOfShoe { get; set; } = ShoeType.WalkingShoe;
- // Color of the shoe. Demonstrates a property with the full declaration
- private string _Color = "Black";
- public string Color
- {
- get
- {
- return _Color;
- }
- set
- {
- _Color = value;
- }
- }
- // Size of the shoe. Demonstrates a property with a shortened declaration
- public double Size { get; set; } = 10;
- // Constructor to set the default values
- public Shoe()
- {
- }
- // Shows alternate constructor that calls another constructor
- public Shoe(Foot shoeFoot)
- {
- ShoeFoot = shoeFoot;
- }
- // Print out the shoe properties to the console.
- public void Describe()
- {
- Console.WriteLine($"Foot: {ShoeFoot} ({ShoeFoot.GetDescription()})");
- Console.WriteLine($"Shoe Type: {TypeOfShoe} ({TypeOfShoe.GetDescription()})");
- Console.WriteLine($"Color: {Color}");
- Console.WriteLine($"Size: {Size}");
- }
- }
- // This demonstrates extension methods.
- // It extends the Enum type to add GetDescription()
- public static class EnumExtensions
- {
- // This extension method is broken out so you can use a similar pattern with
- // other MetaData elements in the future. This is your base method for each.
- public static T? GetAttribute<T>(this Enum value) where T : Attribute
- {
- var type = value.GetType();
- var memberInfo = type.GetMember(value.ToString());
- var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);
- return attributes.Length > 0
- ? (T)attributes[0]
- : null;
- }
- // This method creates a specific call to the above method, requesting the
- // Description MetaData attribute.
- public static string GetDescription(this Enum value)
- {
- var attribute = value.GetAttribute<DescriptionAttribute>();
- return attribute == null ? value.ToString() : attribute.Description;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement