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);
for (int i = 0; i < contacts.Length; i++) {
if (contacts[i] == null) {
contacts[i] = AddContact;
Console.WriteLine("Address Book updated. Name: {0} -- Address: {1} has been added!", name, address);
return true;
}
}
Console.WriteLine($"Cannot add ({name}) to Address Book since it is full!");
return false;
}
else {
Console.WriteLine($"({name}) already exists in Address Book!");
UpdateContact(name);
}
return false;
}
public bool UpdateContact(string originalName) {
Console.Write("Are you sure you would you like to update the Contact? -- Type 'Y' or 'N': ");
string userResponse = Console.ReadLine().ToLower();
if (userResponse == "y") {
Console.Write($"Would you like to update {originalName}'s name or address? TYPE - 'Name' for name and 'Address' for address: ");
string contactToUpdate = Console.ReadLine().ToLower();
Console.Write($"Please enter changes to the {contactToUpdate} here: ");
string updatedContact = Console.ReadLine().Trim();
updatedContact = FormatContact(updatedContact);
int index = GetEntryIndex(originalName);
switch(contactToUpdate) {
case "name":
contacts[index].Name = updatedContact;
Console.WriteLine($"Contact {originalName} updated to {updatedContact}");
return true;
case "address":
contacts[index].Address = updatedContact;
Console.WriteLine($"Contact {originalName}'s {contactToUpdate} updated to {updatedContact}");
return true;
}
}
return false;
}
private string FormatContact(string stringToFormat) {
char[] arr = stringToFormat.ToCharArray();
for (int i = 0; i < arr.Length; i++) {
int num;
if (i == 0 || arr[i - 1] == ' ' && !( int.TryParse(arr[i].ToString(), out num) ) ) {
arr[i] = Convert.ToChar( arr[i].ToString().ToUpper() );
}
else {
arr[i] = Convert.ToChar(arr[i].ToString().ToLower());
}
}
return String.Concat(arr);
}
private int GetEntryIndex(string name) {
for (int i = 0; i < contacts.Length; i++) {
if (contacts[i] != null && contacts[i].Name.ToLower() == name.ToLower())
return i;
}
return -1;
}
private bool ContainsEntry(string name) {
return GetEntryIndex(name) != -1;
}
public void RemoveEntry(string name) {
var index = GetEntryIndex(name);
if (index != -1) {
contacts[index] = null;
Console.WriteLine("{0} removed from contacts", name);
}
}
public string ViewContactsList() {
string contactList = "";
foreach (Contact contact in contacts) {
if (contact == null) {
continue;
}
contactList += String.Format("Name: {0} -- Address: {1}" + Environment.NewLine, contact.Name, contact.Address);
}
return (contactList != String.Empty) ? contactList : "Your Address Book is empty.";
}
}
}
Program class
using System;
namespace AddressBook {
class Program {
static void Main(string[] args) {
AddressBook addressBook = new AddressBook();
PromptUser();
void Menu() {
Console.WriteLine("TYPE:");
Console.WriteLine("'Add' to add a contact: ");
Console.WriteLine("'View' to view the list of contacts: ");
Console.WriteLine("'Remove' to select and remove a contact: ");
Console.WriteLine("'Update' to select and update a contact: ");
Console.WriteLine("'Quit' at anytime to exit: ");
}
void UpdateAddressBook(string userInput) {
string name = "";
string address = "";
switch ( userInput.ToLower() ) {
case "add":
Console.Write("Enter a name: ");
name = Console.ReadLine().Trim();
switch(name) {
case "quit":
break;
default:
Console.Write("Enter an address: ");
address = Console.ReadLine().Trim();
switch (address) {
case "quit":
break;
default:
addressBook.AddEntry(name, address);
break;
}
break;
}
break;
case "remove":
Console.Write("Enter a name to remove: ");
name = Console.ReadLine();
switch (name) {
case "quit":
break;
default:
addressBook.RemoveEntry(name);
break;
}
break;
case "view":
Console.WriteLine(addressBook.ViewContactsList());
break;
case "update":
Console.WriteLine("Please enter the name of the Contact you wish to update");
name = Console.ReadLine();
addressBook.UpdateContact(name);
break;
}
}
void PromptUser() {
Menu();
string userInput = "";
while (userInput != "quit") {
Console.WriteLine("What would you like to do?");
userInput = Console.ReadLine().Trim();
UpdateAddressBook(userInput);
}
}
}
}
}