From dd42ef6ebcd8bc7002c6a0064401cb62fe0815f8 Mon Sep 17 00:00:00 2001 From: nikunjud <60669119+nikunjud@users.noreply.github.com> Date: Tue, 4 Feb 2020 19:32:20 +0000 Subject: [PATCH] ProQuest Technical Test Attempt --- .../PhoneBookTestApp/DatabaseUtil.cs | 45 ++++---- .../PhoneBookTestApp/IPhoneBook.cs | 24 +++- PhoneBookTestApp/PhoneBookTestApp/Person.cs | 11 ++ .../PhoneBookTestApp/PhoneBook.cs | 108 +++++++++++++++++- .../PhoneBookTestApp/PhoneBookTestApp.csproj | 4 +- PhoneBookTestApp/PhoneBookTestApp/Program.cs | 65 ++++++++++- 6 files changed, 219 insertions(+), 38 deletions(-) diff --git a/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs b/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs index 05b494f..f3ea6ba 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs @@ -5,10 +5,12 @@ namespace PhoneBookTestApp { public class DatabaseUtil { + /// + /// Create Phone book Table In Database + /// public static void initializeDatabase() { - var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); - dbConnection.Open(); + var dbConnection = GetConnection(); try { @@ -17,19 +19,6 @@ public static void initializeDatabase() "create table PHONEBOOK (NAME varchar(255), PHONENUMBER varchar(255), ADDRESS varchar(255))", dbConnection); command.ExecuteNonQuery(); - - command = - new SQLiteCommand( - "INSERT INTO PHONEBOOK (NAME, PHONENUMBER, ADDRESS) VALUES('Chris Johnson','(321) 231-7876', '452 Freeman Drive, Algonac, MI')", - dbConnection); - command.ExecuteNonQuery(); - - command = - new SQLiteCommand( - "INSERT INTO PHONEBOOK (NAME, PHONENUMBER, ADDRESS) VALUES('Dave Williams','(231) 502-1236', '285 Huron St, Port Austin, MI')", - dbConnection); - command.ExecuteNonQuery(); - } catch (Exception) { @@ -41,18 +30,12 @@ public static void initializeDatabase() } } - public static SQLiteConnection GetConnection() - { - var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); - dbConnection.Open(); - - return dbConnection; - } - + /// + /// Delete Table Phone Book On Application Close + /// public static void CleanUp() { - var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); - dbConnection.Open(); + var dbConnection = GetConnection(); try { @@ -71,5 +54,17 @@ public static void CleanUp() dbConnection.Close(); } } + + /// + /// Get Database Connection + /// + /// Database Connection Object + public static SQLiteConnection GetConnection() + { + var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;"); + dbConnection.Open(); + + return dbConnection; + } } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs b/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs index 1c3aee3..27ecfbe 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs @@ -1,8 +1,26 @@ -namespace PhoneBookTestApp +using System.Collections.Generic; +namespace PhoneBookTestApp { public interface IPhoneBook { - Person findPerson(string firstName, string lastName); - void addPerson(Person newPerson); + /// + /// Find Person With Given Name In Phone Boook + /// + /// Person First Name + /// Person Last Name + /// Found Person Information + Person FindPerson(string firstName, string lastName); + + /// + /// Add Person In Phone Book + /// + /// Person To Be Added + void AddPerson(Person newPerson); + + /// + /// Get All Person Present In The Phone Boook + /// + /// List Of Persons + IList GetAllPersons(); } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/Person.cs b/PhoneBookTestApp/PhoneBookTestApp/Person.cs index 7cb450b..8d69a8c 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/Person.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/Person.cs @@ -2,8 +2,19 @@ { public class Person { + /// + /// Person Name + /// public string name; + + /// + /// Person Phone Number + /// public string phoneNumber; + + /// + /// Person Address + /// public string address; } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs b/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs index 26118d8..48fa1de 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs @@ -1,15 +1,115 @@ -namespace PhoneBookTestApp +using System; +using System.Collections.Generic; +using System.Data.SQLite; +namespace PhoneBookTestApp { public class PhoneBook : IPhoneBook { + /// + /// Add Person In Phone Book + /// + /// Person To Be Added public void AddPerson(Person person) { - throw new System.NotImplementedException(); + var dbConnection = DatabaseUtil.GetConnection(); + try + { + SQLiteCommand command = + new SQLiteCommand( + "INSERT INTO PHONEBOOK (NAME, PHONENUMBER, ADDRESS) VALUES(@personName,@personContact, @personAddress)", + dbConnection); + command.Parameters.AddWithValue("@personName", person.name); + command.Parameters.AddWithValue("@personContact", person.phoneNumber); + command.Parameters.AddWithValue("@personAddress", person.address); + command.ExecuteNonQuery(); + } + catch (Exception) + { + throw; + } + finally + { + dbConnection.Close(); + } } - public Person findPerson() + /// + /// Find Person With Given Name In Phone Boook + /// + /// Person First Name + /// Person Last Name + /// Found Person Information + public Person FindPerson(string firstName, string lastName) { - throw new System.NotImplementedException(); + Person person = new Person(); + if (!(string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(lastName))) + { + var dbConnection = DatabaseUtil.GetConnection(); + try + { + string personName = (firstName + " " + lastName).Trim(); + SQLiteCommand command = + new SQLiteCommand( + "SELECT NAME, PHONENUMBER, ADDRESS FROM PHONEBOOK WHERE NAME LIKE @personName ORDER BY NAME ASC LIMIT 1", dbConnection); + command.Parameters.AddWithValue("@personName", "%" + personName + "%"); + using (var oReader = command.ExecuteReader()) + { + while (oReader.Read()) + { + person.name = oReader.GetString(0); + person.phoneNumber = oReader.GetString(1); + person.address = oReader.GetString(2); + } + } + } + catch (Exception) + { + throw; + } + finally + { + dbConnection.Close(); + } + } + + return person; + } + + /// + /// Get All Person Present In The Phone Boook + /// + /// List Of Persons + public IList GetAllPersons() + { + var dbConnection = DatabaseUtil.GetConnection(); + IList personsList = new List(); + try + { + SQLiteCommand command = + new SQLiteCommand( + "SELECT NAME, PHONENUMBER, ADDRESS FROM PHONEBOOK ", dbConnection); + using (var oReader = command.ExecuteReader()) + { + while (oReader.Read()) + { + Person pp = new Person(); + pp.name = oReader.GetString(0); + pp.phoneNumber = oReader.GetString(1); + pp.address = oReader.GetString(2); + personsList.Add(pp); + } + } + } + catch (Exception) + { + throw; + } + finally + { + dbConnection.Close(); + } + + return personsList; } } } \ No newline at end of file diff --git a/PhoneBookTestApp/PhoneBookTestApp/PhoneBookTestApp.csproj b/PhoneBookTestApp/PhoneBookTestApp/PhoneBookTestApp.csproj index c7cd2cd..84e35f1 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/PhoneBookTestApp.csproj +++ b/PhoneBookTestApp/PhoneBookTestApp/PhoneBookTestApp.csproj @@ -88,7 +88,9 @@ - + + Designer + diff --git a/PhoneBookTestApp/PhoneBookTestApp/Program.cs b/PhoneBookTestApp/PhoneBookTestApp/Program.cs index 937b678..599c5de 100644 --- a/PhoneBookTestApp/PhoneBookTestApp/Program.cs +++ b/PhoneBookTestApp/PhoneBookTestApp/Program.cs @@ -15,20 +15,75 @@ static void Main(string[] args) try { 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 - */ + + Program pg = new Program(); + + // TODO: insert the new person objects into the database + pg.AddNewPersons(); // TODO: print the phone book out to System.out + pg.GetAllPhonebookRecords(); + // TODO: find Cynthia Smith and print out just her entry - // TODO: insert the new person objects into the database + pg.FindRecordWithGivenName("", "mi"); + Console.WriteLine("\nPress any key to exit."); + Console.ReadKey(); } finally { DatabaseUtil.CleanUp(); } } + + /// + /// Add new persons to database + /// + private void AddNewPersons() + { + /* 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 + */ + phonebook.AddPerson(new Person { name = "Chris Johnson", phoneNumber = "(321) 231-7876", address = "452 Freeman Drive, Algonac, MI" }); + phonebook.AddPerson(new Person { name = "Dave Williams", phoneNumber = "(231) 502-1236", address = "285 Huron St, Port Austin, MI" }); + 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" }); + } + + /// + /// Find Record With Given Name + /// + /// First Name Of Person + /// Last Name Of Person + private void FindRecordWithGivenName(string firstName, string lastName) + { + Person person= phonebook.FindPerson(firstName.Trim(), lastName.Trim()); + Console.WriteLine(Environment.NewLine); + if (person != null && person.address != null) { + Console.WriteLine("Found below Record"); + Console.WriteLine("Name = {0}, PhoneNumber = {1}, Address = {2}", person.name, + person.phoneNumber, person.address); + } + else + { + Console.WriteLine("No Matching Record Found"); + } + + } + + /// + /// Show All Data Present In phonebok + /// + private void GetAllPhonebookRecords() + { + var personsList = phonebook.GetAllPersons(); + for (int index = 0; index < personsList.Count; index++) + { + Console.WriteLine("{0} ,Name = {1}, PhoneNumber = {2}, Address = {3}", index, personsList[index].name, + personsList[index].phoneNumber, personsList[index].address); + } + } + } }