diff --git a/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs b/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs index 05b494f..102afef 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs @@ -1,14 +1,15 @@ using System; +using System.Collections.Generic; +using System.Data; using System.Data.SQLite; namespace PhoneBookTestApp { public class DatabaseUtil { - public static void initializeDatabase() + public static void InitializeDatabase() { - var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); - dbConnection.Open(); + var dbConnection = OpenConnection(); try { @@ -31,17 +32,82 @@ public static void initializeDatabase() command.ExecuteNonQuery(); } - catch (Exception) - { - throw; - } finally { dbConnection.Close(); } } - public static SQLiteConnection GetConnection() + public static List List() + { + var result = new List(); + + using (SQLiteConnection connect = OpenConnection()) + { + using (SQLiteCommand cmd = connect.CreateCommand()) + { + cmd.CommandText = @"SELECT * FROM PHONEBOOK"; + cmd.CommandType = CommandType.Text; + SQLiteDataReader r = cmd.ExecuteReader(); + while (r.Read()) + { + result.Add( + new Person + { + name = r["NAME"].ToString(), + phoneNumber = r["PHONENUMBER"].ToString(), + address = r["ADDRESS"].ToString(), + }); + } + } + } + return result; + } + + public static Person Find(string firstName, string lastName) + { + Console.WriteLine($"Query for {firstName} {lastName}"); + var result = new Person(); + using (SQLiteConnection conn = OpenConnection()) + { + var cmd = new SQLiteCommand( + "SELECT * FROM PHONEBOOK WHERE NAME LIKE @name", + conn + ); + cmd.Parameters.AddWithValue("@name", firstName + " " + lastName); + SQLiteDataReader r = cmd.ExecuteReader(); + + while (r.Read()) + { + result = new Person + { + name = r["NAME"].ToString(), + phoneNumber = r["PHONENUMBER"].ToString(), + address = r["ADDRESS"].ToString(), + }; + } + } + return result; + } + + public static void Add(Person newPerson) + { + var result = new List(); + + using (SQLiteConnection conn = OpenConnection()) + { + var cmd = new SQLiteCommand( + "INSERT INTO PHONEBOOK (NAME, PHONENUMBER, ADDRESS) VALUES(@name, @phonenumber, @address)", + conn); + cmd.Parameters.AddWithValue("@name", newPerson.name); + cmd.Parameters.AddWithValue("@phonenumber", newPerson.phoneNumber); + cmd.Parameters.AddWithValue("@address", newPerson.address); + cmd.ExecuteNonQuery(); + Console.WriteLine("row inserted"); + } + } + + public static SQLiteConnection OpenConnection() { var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); dbConnection.Open(); diff --git a/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs b/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs index 1c3aee3..01296b5 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs @@ -2,7 +2,7 @@ { public interface IPhoneBook { - Person findPerson(string firstName, string lastName); - void addPerson(Person newPerson); + Person FindPerson(string firstName, string lastName); + void AddPerson(Person newPerson); } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/Person.cs b/PhoneBookTestApp/PhoneBookTestApp/Person.cs index 7cb450b..edd12f5 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/Person.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/Person.cs @@ -5,5 +5,10 @@ public class Person public string name; public string phoneNumber; public string address; + + public override string ToString() + { + return $"{{\n\tname: {name},\n\tphoneNumber: {phoneNumber},\n\taddress: {address}\n}}"; + } } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs b/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs index 26118d8..a8a0e09 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs @@ -1,15 +1,22 @@ -namespace PhoneBookTestApp +using System.Collections.Generic; + +namespace PhoneBookTestApp { public class PhoneBook : IPhoneBook { - public void AddPerson(Person person) + public void AddPerson(Person newPerson) + { + DatabaseUtil.Add(newPerson); + } + public Person FindPerson(string firstName, string lastName) { - throw new System.NotImplementedException(); + return DatabaseUtil.Find(firstName, lastName); } - public Person findPerson() + public override string ToString() { - throw new System.NotImplementedException(); + var personlist = DatabaseUtil.List(); + return $"{string.Join(",\n", personlist)}"; } } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/Program.cs b/PhoneBookTestApp/PhoneBookTestApp/Program.cs index 937b678..38ff913 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/Program.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/Program.cs @@ -1,34 +1,51 @@ using System; using System.Collections.Generic; -using System.Data.SQLite; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PhoneBookTestApp { class Program { - private PhoneBook phonebook = new PhoneBook(); + static PhoneBook phonebook = new PhoneBook(); static void Main(string[] args) { try { - DatabaseUtil.initializeDatabase(); + DatabaseUtil.InitializeDatabase(); /* TODO: create person objects and put them in the PhoneBook and database * John Smith, (248) 123-4567, 1234 Sand Hill Dr, Royal Oak, MI * Cynthia Smith, (824) 128-8758, 875 Main St, Ann Arbor, MI */ - // TODO: print the phone book out to System.out - // TODO: find Cynthia Smith and print out just her entry - // TODO: insert the new person objects into the database + phonebook.AddPerson( + new Person + { + name = "John Smith", + phoneNumber = "(248) 123-4567", + address = "1234 Sand Hill Dr, Royal Oak, MI" + }); + phonebook.AddPerson( + new Person { + name = "Cynthia Smith", + phoneNumber = "(824) 128-8758", + address = "875 Main St, Ann Arbor, MI" } + ); + Console.WriteLine("print the phone book out to System.out"); + Console.WriteLine(phonebook); + + Console.WriteLine("find Cynthia Smith and print out just her entry "); + Console.WriteLine(phonebook.FindPerson(firstName: "Cynthia", lastName: "Smith")); + + } + catch(Exception ex) + { + Console.WriteLine(ex.ToString()); } finally { DatabaseUtil.CleanUp(); } + Console.ReadLine(); } } } diff --git a/PhoneBookTestApp/PhoneBookTestApp/Tests/PhoneBookTest.cs b/PhoneBookTestApp/PhoneBookTestApp/Tests/PhoneBookTest.cs index d2c870d..5a05e2e 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/Tests/PhoneBookTest.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/Tests/PhoneBookTest.cs @@ -8,13 +8,13 @@ namespace PhoneBookTestAppTests public class PhoneBookTest { [Test] - public void addPerson() + public void AddPerson() { Assert.Fail(); } [Test] - public void findPerson() + public void FindPerson() { Assert.Fail(); }