Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions PhoneBookTestApp/PhoneBookTestApp/DatabaseUtil.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -31,17 +32,82 @@ public static void initializeDatabase()
command.ExecuteNonQuery();

}
catch (Exception)
{
throw;
}
finally
{
dbConnection.Close();
}
}

public static SQLiteConnection GetConnection()
public static List<Person> List()
{
var result = new List<Person>();

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<Person>();

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();
Expand Down
4 changes: 2 additions & 2 deletions PhoneBookTestApp/PhoneBookTestApp/IPhoneBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions PhoneBookTestApp/PhoneBookTestApp/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}";
}
}
}
17 changes: 12 additions & 5 deletions PhoneBookTestApp/PhoneBookTestApp/PhoneBook.cs
Original file line number Diff line number Diff line change
@@ -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)}";
}
}
}
35 changes: 26 additions & 9 deletions PhoneBookTestApp/PhoneBookTestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
4 changes: 2 additions & 2 deletions PhoneBookTestApp/PhoneBookTestApp/Tests/PhoneBookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down