1
\$\begingroup\$

My code works but I don't believe its the most efficient way of doing it. I'm still learning.

Here is what the program requirements are:

  1. Executable code will fall within a loop that continues to loop until menu item 3 is entered. The menu (see screenshots below) appears first, and what follows depends on which menu item chosen.

  2. A base Contact class needs to be created with the following properties:

    a. First name – require an entry for it

    b. Last name – require an entry for it

    c. Address – entry not required

    • But if user just presses Enter without providing entry, Address = “N/A”

    d. State – entry not required

    • But if user just presses Enter without providing entry, State = “N/A”

    e. Zip Code – entry not required

    • But if user just presses Enter without providing entry, Zip = 0

    f. Country

    • If State has a value, then Country is set to “USA” – don’t prompt for a Country

    • If State does not have a value: - entry for Country not required

    • But if user just presses Enter without providing a Country, Country = “N/A”

    g. Phone number (land-line) – entry not required

    • But if user just presses Enter without providing entry, Phone-land = “N/A”

    h. Phone number (cell) – entry not required

    • But if user just presses Enter without providing entry, Phone-cell = “N/A”
  3. Extend the base Contact class to a child class that holds only family/friend contacts. This child class will contain 3 of its own properties:

    a. Relationship to you – the user can choose one of the options the program will provide (see screenshots below).

    b. Birthday (data type DateTime). The program will ask the user for the month, day, and year, and those three items will create the date.

    c. The date 10 days before birthday (so you know when to start shopping for a present)

    • The user won’t provide this date – the program will calculate it from the Birthday.
  4. Also extend the base Contact class to a child class that holds only Business contacts. This child class will contain 3 of its own properties:

    a. Company – if the user just presses Enter, Company = “ACME Yarn and Supplies”

    b. Position – entry not required

    • But if user just presses Enter without providing entry, Position = “N/A”

    c. Email address

    • If Company = “ACME Yarn and Supplies”, then Email address is set to [email protected] – don’t prompt for the email address

    • If Company = anything else, prompt for the email address.

The program needs to do the following:

  1. Display this menu. Don’t move on until the user types in a 1, 2 or 3. Nothing else accepted.

  2. If the user enters 1, prompt for the values to the base properties.

    a. Make sure that first and last name receive entries

    b. Address and State either get entries, or = N/A

    c. Zip code either gets an entry, or = 0

    d. Country forced to be USA if State has an entry (don’t prompt for it).
    Otherwise, prompt for it, and it either gets an entry, or = N/A

    e. The 2 phone numbers either get entries, or = N/A

  3. Prompt for the properties unique to a friend/family contact.

    a. You’re free to come up with a better way to request the value for the Relationship property – or you can do it like I did. Just make sure the property gets a value

    b. Ask for the birthday, and this property needs to be data type DateTime. There’s a lot of helpful information online pertaining to DateTime data.

    • I chose to ask for the components of the date object and use them to create the data for Birthday. You don’t have to handle this the way I did, but the property does need to be DateTime.

    c. Let your program do the necessary calculations from the provided Birthday date to provide a value for the property holding the date that is 10 days before the Birthday.

  4. Display the properties of the Family/Friend object you just created. This is where we’d write this info to storage if it were a finalized program, but it isn’t, so splash it to the screen.

    a. Make sure to format the dates in a way that only the date is displayed, not the time.

  5. The “Press Enter to continue” is ReadLine() pausing so that you can look at these property values before looping back to display the menu again. Put that pause in your program too.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using static System.Console;

    namespace GroupProject_MidTerm_
    {
    class Program
    {
        static void Main(string[] args)
       {
        int choose;
        string fName, lName, address, state, Country, zip, phoneLand, phoneCell, Relationship, Company, Position, Email, birthday2;
        DateTime Birthday;


        WriteLine("1. Create a Friend / Family entry");
        WriteLine("2. Create a Business Contact entry");
        WriteLine("3. Quit");

        Write("Choose Item 1, 2, or 3: ");
        while (!int.TryParse(ReadLine(), out choose) || choose > 3 || choose < 1)
        {
            WriteLine("Please try again.");
        }

        while (choose != 3)
        {
            if (choose == 1)
            {
                baseContact(out fName, out lName, out address, out state, out zip, out Country, out phoneLand, out phoneCell);
                ffChild(out Relationship, out Birthday, out birthday2);

                //display information
                WriteLine();
                WriteLine("Family - Friend entry: ");
                WriteLine();
                WriteLine("Name: {0} {1}", fName, lName);
                WriteLine("Address: {0}", address);
                WriteLine("State: {0}", state);
                WriteLine("Zip Code: {0}", zip);
                WriteLine("Country: {0}", Country);
                WriteLine("Land Line Number: {0}", phoneLand);
                WriteLine("Cell Number: {0}", phoneCell);
                WriteLine();
                WriteLine("Relationship: {0}", Relationship);
                if (birthday2 != "")
                {
                    WriteLine("Birthday: {0}", Birthday);
                    WriteLine("10 days before birthday: {0}", Birthday.AddDays(-10));
                }
                WriteLine();
                //WriteLine("Relationship: {0}", Relationship);

                Write("Press Enter to continue");
                ReadLine();
                WriteLine();
                WriteLine("1. Create a Friend / Family entry");
                WriteLine("2. Create a Business Contact entry");
                WriteLine("3. Quit");
                WriteLine();
                Write("Choose Item 1, 2, or 3: ");
                choose = Convert.ToInt32(ReadLine());

            }
            else if (choose == 2)
            {
                baseContact(out fName, out lName, out address, out state, out zip, out Country, out phoneLand, out phoneCell);
                businessChild(out Company, out Position, out Email);

                //display information
                WriteLine();
                WriteLine("Business Contact entry: ");
                WriteLine();
                WriteLine("Name: {0} {1}", fName, lName);
                WriteLine("Address: {0}", address);
                WriteLine("State: {0}", state);
                WriteLine("Zip Code: {0}", zip);
                WriteLine("Country: {0}", Country);
                WriteLine("Land Line Number: {0}", phoneLand);
                WriteLine("Cell Number: {0}", phoneCell);
                WriteLine();
                WriteLine("Company: {0}", Company);
                WriteLine("Position: {0}", Position);
                WriteLine("Email: {0}", Email);
                WriteLine();
                Write("Press Enter to continue");
                ReadLine();
                WriteLine();
                WriteLine("1. Create a Friend / Family entry");
                WriteLine("2. Create a Business Contact entry");
                WriteLine("3. Quit");
                WriteLine();
                Write("Choose Item 1, 2, or 3: ");
                choose = Convert.ToInt32(ReadLine());

            }
        }
    }
    private static void baseContact(out string firstName, out string lastName, out string Address, out string State, out string zip, out string country, out string phoneLand, out string phoneCell)
    {
        Write("First Name: ");
        firstName = ReadLine();
        while (firstName == "")
        {
            Write("Please enter a first name: ");
            firstName = ReadLine();
        }

        Write("Last Name: ");
        lastName = ReadLine();
        while (lastName == "")
        {
            Write("please enter a last name: ");
            lastName = ReadLine();
        }

        Write("Address: (If not available, press enter): ");
        Address = ReadLine();
        if (Address == "")
        {
            Address = "N/A";
        }
        Write("State: (If not available, press enter): ");
        State = ReadLine();
        if (State == "")
        {
            State = "N/A";
        }
        Write("Zip: (If no zip, press enter): ");
        zip = ReadLine();
        if (zip.Length < 5)
        {
            zip = "N/A";
        }
        if (State == "N/A")
        {
            Write("Country (press enter is not available): ");
            country = ReadLine();
            if (country == "")
            {
                country = "N/A";
            }
        }
        else
        {
            country = "USA";
        }
        Write("Phone Number(Land-Line): ");
        phoneLand = ReadLine();
        if (phoneLand == "")
        {
            phoneLand = "N/A";
        }
        Write("Phone Number(Cell): ");
        phoneCell = ReadLine();
        if (phoneCell == "")
        {
            phoneCell = "N/A";
        }

    }
    private static void businessChild(out string company, out string position, out string email)
    {
        Write("Company (or press enter if AMCE Yarn and Supply): ");
        company = ReadLine();
        if (company == "")
        {
            company = "ACME Yarn and Supply";
        }

        Write("Position (press enter if N/A): ");
        position = ReadLine();
        if (position == "")
        {
            position = "N/A";
        }

        if (company == "ACME Yarn and Supply")
        {
            email = "[email protected]";
        }
        else
        {
            Write("Email Address: ");
            email = ReadLine();
        }
    }
    private static void ffChild(out string relationship, out DateTime birthday, out string birthday2)
    {
        int year = 0, month = 0, day = 0;
        string amonth;
        birthday = new DateTime(1, 1, 1);

        WriteLine("Relationship Choices:");
        WriteLine("(SP) Spouse");
        WriteLine("(C) Child");
        WriteLine("(P) Parent");
        WriteLine("(S) Sibling");
        WriteLine("(Enter) Other");

        Write("Relationship to you? (key in letter or Enter): ");
        relationship = ReadLine().ToUpper();
        if (relationship == "")
        {
            relationship = "Other";
        }
        else if (relationship == "SP")
        {
            relationship = "Spouse";
        }
        else if (relationship == "C")
        {
            relationship = "Child";
        }
        else if (relationship == "P")
        {
            relationship = "Parent";
        }
        else if (relationship == "S")
        {
            relationship = "Sibling";
        }

        Write("Month of birthday (or enter if not entering birthday): ");
        amonth = ReadLine();
        if (amonth != "")
        {
            month = Convert.ToInt32(amonth);

            if (month != 0)
            {
                Write("Day of birthday: ");
                day = Convert.ToInt32(ReadLine());

                Write("4 digit year of birthday: ");
                year = Convert.ToInt32(ReadLine());
            }
            birthday = new DateTime(year, month, day);
        }
        birthday2 = Convert.ToString(birthday);
        if (birthday2 == "1/1/0001 12:00:00 AM")
        {
            birthday2 = "";
        }
       }
      }
    }
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

Frank, looks like homework from school. No worries. You should follow program requirements and instructions. I post just parts of possible solution.

Create separate class Contact.cs, e.g.

class Contact
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

Create separate class Address.cs, use constructor to setup default values, e.g.

class Address
{
    public string Street { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
    public string Country { get; set; }
    public string PhoneLand { get; set; }
    public string PhoneCell { get; set; }

    public Address()
    {
        Street = "N/A";
        State = "N/A";
        Zip = 0;
        Country = "N/A";
        PhoneLand = "N/A";
        PhoneCell = "N/A";
    }
}

Create child class ContactFamily.cs, e.g.

class ContactFamily : Contact
{
    public enum RelationshipType
    {
        Spouse,
        Child,
        Parent,
        Sibbing,
        Other
    }

    public RelationshipType Relationship { get; set; }
    public DateTime Birthday { get; set; }
    public DateTime BirthdayNotificationDate { get; set; }

}

Create separate class ContactBusiness.cs

class ContactBusiness : Contact
{
  //todo properties and constructor
}

It's just a object concept. Create object methods. Use setters if needed instead of auto-property (case: If Company = “ACME Yarn and Supplies”...). Follow advice from tinstaafl. You have to fight yourself and learn basic concepts of C#. Don't forget to test users inputs to avoid exceptions. Good luck.

\$\endgroup\$
2
  • \$\begingroup\$ The constructor in Address is unnecessary if you provide those default values on the properties themselves. \$\endgroup\$ Commented Oct 23, 2016 at 22:02
  • 1
    \$\begingroup\$ EBrown, you are right , it is possible in C# 6 and later. public string Street { get; set; } = "N/A"; \$\endgroup\$
    – Tomas Paul
    Commented Oct 23, 2016 at 22:11
2
\$\begingroup\$

Right off the start, your code isn't following the assignment. It specifically says to create a Contact class, then it says to create an extended class. Not only is this required, but it also cleans up your code, tremendously. All those out parameters can be removed and your method can return an instance of the right class, instead.

Instead of building the different menus inline, it would be more intuitive and easier to read, if you break the different menus out into methods, that return the choice.

Using switch blocks to process the results of the menu choice will also help organize the code better.

Just a side note, creating the classes for this assignment is probably one of the main things the assignment is meant to teach you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.