Search code examples
c#classoopinheritanceoverriding

Trying to create a C# program that uses auto-implemented properties, overrides ToString(), and uses a child class


I'm going to try to be as clear as possible, but bear with me. I need to make a program that:

  1. Create a class named LetterDemo that instantiates objects of two classes (named Letter and Certified Letter)
  2. The Letter class includes auto-implemented properties for name of the letter recipient and the date is was mailed.
  3. In the Letter class, also create a ToString() method that overrides the Object class's ToString() method and retuns a string that contains the name of the class (using GetType() and the Letter's data field values.
  4. Create a child class named CertifiedLetter that contains an auto-implemented property that holds a tracking number for the letter. (I am assuming this would be given if the user indicates that the letter is certified.)

I have most of it figured out but I am stuck on the CertifiedLetter and tracking number part. Any help with clarification of how to use a child class and integrate a method within it would be a life saver. Thank you!

//using System.ComponentModel.Design;
using System.Data;
using System.Globalization;
using System.Runtime.CompilerServices;
using static System.Console;

class LetterDemo
{
    static void Main()
    {
        Letter newLetter = new Letter();

        WriteLine("Please enter name of recipient >> ");
        string inputName = ReadLine();
        newLetter.Name = inputName;

        WriteLine("Please enter the date letter was mailed >> ");
        string inputDate = ReadLine();
        newLetter.DateMailed = inputDate;

        WriteLine("Is letter certified? Y or N >> ");
        string isCertified = ReadLine();

        if (isCertified.Length > 1)
        {
            WriteLine("Please enter only 'Y' or 'N' >> ");
        }
        else
        {
           // Send to CertifiedLetter
                }
        WriteLine(newLetter.ToString());

    }
    class Letter
    {
        private string name;
        private string dateMailed;
        public string Name { get; set; }
        public string DateMailed { get; set; }

        public override string ToString()
        {
              return GetType() + " is being sent to " + Name + " one the day of " + DateMailed + ".";
            
        }
    }
    class CertfiedLetter : Letter
    {
        public static int TrackingNumber()
        {
            Random rnd = new Random();
            int newNum = rnd.Next(1000, 9999);
            int trackingNumber = newNum;
            return trackingNumber;

        }
    }
}

Solution

    • Auto-Implemented Properties: You've correctly implemented auto-implemented properties for Name and DateMailed in the Letter class. You'll want to do something similar for the TrackingNumber in the CertifiedLetter class.

    • CertifiedLetter Class: The CertifiedLetter should inherit from Letter and add an additional property for the tracking number. The TrackingNumber should not be a method but a property. You can generate a tracking number in the constructor of the CertifiedLetter.

    • Handling Certified Letters: In the Main method, you need to create an instance of CertifiedLetter when the user indicates that the letter is certified. This instance should also have a name and a date, in addition to a tracking number.

    • ToString Method in CertifiedLetter: Override the ToString method in CertifiedLetter to include information about the tracking number.

    For example like this:

    class LetterDemo
    {
        static void Main()
        {
            WriteLine("Please enter name of recipient >> ");
            string inputName = ReadLine();
    
            WriteLine("Please enter the date letter was mailed >> ");
            string inputDate = ReadLine();
    
            WriteLine("Is letter certified? Y or N >> ");
            string isCertified = ReadLine();
    
            Letter letter;
    
            if (isCertified.Equals("Y", StringComparison.OrdinalIgnoreCase))
            {
                letter = new CertifiedLetter
                {
                    Name = inputName,
                    DateMailed = inputDate
                };
            }
            else
            {
                letter = new Letter
                {
                    Name = inputName,
                    DateMailed = inputDate
                };
            }
    
            WriteLine(letter.ToString());
        }
    }
    
    class Letter
    {
        public string Name { get; set; }
        public string DateMailed { get; set; }
    
        public override string ToString()
        {
            return GetType().Name + " is being sent to " + Name + " on the day of " + DateMailed + ".";
        }
    }
    
    class CertifiedLetter : Letter
    {
        public int TrackingNumber { get; }
    
        public CertifiedLetter()
        {
            Random rnd = new Random();
            TrackingNumber = rnd.Next(1000, 9999);
        }
    
        public override string ToString()
        {
            return base.ToString() + " The tracking number is " + TrackingNumber + ".";
        }
    }
    

    Edit based on your comment:

    You could print the letter properties more directly:

    static void Main()
    {
        WriteLine("Please enter name of recipient >> ");
        string inputName = ReadLine();
    
        WriteLine("Please enter the date letter was mailed >> ");
        string inputDate = ReadLine();
    
        WriteLine("Is letter certified? Y or N >> ");
        string isCertified = ReadLine();
    
        if (isCertified.Equals("Y", StringComparison.OrdinalIgnoreCase))
        {
            // Create and display a CertifiedLetter
            CertifiedLetter certifiedLetter = new CertifiedLetter
            {
                Name = inputName,
                DateMailed = inputDate
            };
            WriteLine(certifiedLetter.ToString());
        }
        else
        {
            // Create and display a regular Letter
            Letter letter = new Letter
            {
                Name = inputName,
                DateMailed = inputDate
            };
            WriteLine(letter.ToString());
        }
    }