HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Console Address Book application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bookconsoleaddressapplication

Problem

I'm in my second week/second section of learning C# through Treehouse/on my own. I recently switched from learning Javascript and am looking for a review of my code. I believe I've tested everything and have refactored to get it as concise as I can. The project is fully functional and does the following:

  • Adds a new contact



  • Updates existing contacts



  • Checks for duplicate contacts



  • if contact is a duplicate it asks the user if they want to update



  • returns(prints to the console) new/updated contact to view



  • Removes existing contacts



  • Formats contacts (upper/lower case) ("oH, bILly") //=> Oh, Billy



  • Allows a user to quit a process at anytime



  • Allows user to view the entire list of contacts



Some things I considered:

  • Using List



  • I wanted the practice with c# arrays since they differ from the javascript array literal



  • Adding a method that would allow a user to enter partial names and get a list of possible matches



  • Changing the switch(name) in Main() to an if/else to make it a bit more readable.



ADDITIONAL NOTE: I think it is fairly self-documenting, but I wrote it so, if you have any questions please ask. And, ContainsEntry() was written by someone else. I think that covers it..here's my code:

Contact class

namespace AddressBook {
    class Contact {
        public string Name { get; set; }
        public string Address { get; set; }

        public Contact(string name, string address) {
            Name = name;
            Address = address;
        }
    } 
}


AddressBook class

```
using System;

namespace AddressBook {
class AddressBook {

public readonly Contact[] contacts;

public AddressBook() {
contacts = new Contact[2]; ;
}

public bool AddEntry(string name, string address) {
if (!ContainsEntry(name)) {
name = FormatContact(name);
address = FormatContact(address);
Contact AddContact = new Contact(name, address);

Solution

A few things that caught my eye:

Keep things private as much as possible, especially fields/properties. This restricts how someone else can use your class. They should only be able to access or manipulate any data, through methods you've created. This applies as well to the class Contact, keep it inside the AddressBook class and private. The user doesn't need to know what a Contact object is, only the information required to create/access one.

Since the menu(es) are specific to the AddressBook, have another class handle the collection and use AddressBook to handle the interface. Possible something like this:

class AddressBook
{
    class ContactCollection

    {
        class Contact
        {

        }
    }    
}


Since you want incremental search capabilities, I would suggest a Dictionary> and institute an indexed collection of possible search terms

Here's a little more about how the structure can look:

public class AddressBook
{
    class Contact
    {
        public string Name { get; private set; }
        public string Address { get; private set; }

        public Contact(string name, string address)
        {
            Name = name;
            Address = address;
        }
        public void EditInfo(string address = "")
        {

        }
        public override string ToString()
        {
            return $"{Name}\n{Address}\n";
        }
    }
    class ContactCollection
    {
        Dictionary contacts = new Dictionary();
        Dictionary> index = new Dictionary>();
        public bool AddEntry(string name, string address)
        {

        }
        public bool EditEntry(string name)
        {

        }
        public void ShowEntry(string name)
        {

        }
        public void ShowAll()
        {

        }
    }
    ContactCollection cc = new ContactCollection();

    void ShowMenu()
    {

    }
}


You'll notice how I've used a combination public and private in the Contact class. And in the ContactCollection the collections are private but the methods are public.

Code Snippets

class AddressBook
{
    class ContactCollection

    {
        class Contact
        {

        }
    }    
}
public class AddressBook
{
    class Contact
    {
        public string Name { get; private set; }
        public string Address { get; private set; }

        public Contact(string name, string address)
        {
            Name = name;
            Address = address;
        }
        public void EditInfo(string address = "")
        {

        }
        public override string ToString()
        {
            return $"{Name}\n{Address}\n";
        }
    }
    class ContactCollection
    {
        Dictionary<string,Contact> contacts = new Dictionary<string, Contact>();
        Dictionary<string, List<Contact>> index = new Dictionary<string, List<Contact>>();
        public bool AddEntry(string name, string address)
        {

        }
        public bool EditEntry(string name)
        {

        }
        public void ShowEntry(string name)
        {

        }
        public void ShowAll()
        {

        }
    }
    ContactCollection cc = new ContactCollection();

    void ShowMenu()
    {

    }
}

Context

StackExchange Code Review Q#162433, answer score: 2

Revisions (0)

No revisions yet.