From 90d5be453b18227534ee367801c310a0f9dbca93 Mon Sep 17 00:00:00 2001 From: Michael Rich Date: Thu, 20 Dec 2018 01:18:47 -0600 Subject: [PATCH] finished checkpoint3 --- Checkpoint3/Checkpoint3.csproj | 12 ++ Checkpoint3/Items.db | Bin 0 -> 12288 bytes Checkpoint3/Program.cs | 236 +++++++++++++++++++++++++++++++++ Checkpoint3/ToDo.db | 0 4 files changed, 248 insertions(+) create mode 100644 Checkpoint3/Checkpoint3.csproj create mode 100644 Checkpoint3/Items.db create mode 100644 Checkpoint3/Program.cs create mode 100644 Checkpoint3/ToDo.db diff --git a/Checkpoint3/Checkpoint3.csproj b/Checkpoint3/Checkpoint3.csproj new file mode 100644 index 00000000..56f6f2cf --- /dev/null +++ b/Checkpoint3/Checkpoint3.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp2.1 + + + + + + + diff --git a/Checkpoint3/Items.db b/Checkpoint3/Items.db new file mode 100644 index 0000000000000000000000000000000000000000..8bd2dc88c361769fe6353b37f0fca6de3b861caf GIT binary patch literal 12288 zcmeI&&u-H&7yxjm4J;jM4H{@j?cpN`v8n?Jahe2zP*PN_OGldtdYU}PU8Ckt>$sF2 z*)za9aO4qq30?w@ya0~cPGTH5jRX2V$4TtOpQB$+R?>gn36+3hDr2VLF}Y3*gFFWy zgw%8`>bed?pP1{0KIZMeMU6bHeJSg^3YG7KKG8c26hHwKKmim$0Te(16hHwKKmioE zRDr|7)$K;Z7$|lUiEJ9_W$$q&rZbW7i_K!Q=Q_R%zVo8v!i9Tx5*CYHJ+l76F*dfV zRpV@#GhVcnh_lt9@LLXh)oa5}c@uQl?ZAe%=ew`m9(Y|JyrWJBnq9B&_Z;01_Tm2V z`JlsI`@rc9VBZ~pbL4m1p3Z*Ydj9Sz+e?vg8BSD~CN}u)+hxA>)xKhCmi;+jd9z-o zXXFZP5&DgOrBm9XpLBVqOCc|p`a&={3ZMWApa2S>01BW03ZMWApa2T|Ljn4cJl(T+ znhZl31CIogVR8!dRE{%!?M~BDB2$o!h2o itemList = theDao.list(); + + //run method calls interaction method for now (plan to add methods to this class for logic done during different cases) + public void Run() + { + //clears console before showing UI + Console.Clear(); + Interaction(); + } + + //interaction method contains logic to decide what to do based on user interaction (will move most logic to other methods) + public void Interaction() + { + + //string to hold user input for use in loop and switch statements + string input =" "; + + //welcome message + Console.WriteLine("Welcome to the TODO list!"+"\r\n"); + //while user has not entered quit + while(input != "quit") + { + //lists available functions + Console.WriteLine("Available functions:\n\n"+ + "add: add item to list\n"+ + "delete: remove item from list\n"+ + "mark done: mark item as done\n"+ + "list pending: list pending items\n"+ + "list done: list completed items\n"+ + "list all: list all the items\n"+ + "quit: exit program\n"); + + //asks user what they want to do + Console.WriteLine("What would you like to do?"); + //stores input + input = Console.ReadLine().ToLower(); + //switch statement that decides what to do based on user input + switch(input) + { + //case for adding an item to database + case "add": + AddItem(); + break; + //case for deleting an item from database + case "delete": + DeleteItem(); + break; + //case for marking an item done in database + case "mark done": + MarkItem(); + break; + //case to list pending items + case "list pending": + ListItems(false,true); + break; + //case to list done items + case "list done": + ListItems(true,true); + break; + //case to list all items + case "list all": + ListItems(true,false); + break; + //if user enters anything other than an available case, lets user know + default: + Console.WriteLine("incorrect input entered"); + break; + } + //clears console before while loop resets + Console.Clear(); + } + + } + + static void AddItem() + { + Console.WriteLine("Enter a description for your task:"); + string itemDescription = Console.ReadLine(); + theDao.add(itemDescription); + itemList = theDao.list(); + } + static void DeleteItem() + { + foreach(Item eachItem in itemList) + { + Console.WriteLine(eachItem); + } + Console.WriteLine("\nEnter item to delete by Id:"); + int itemToDelete = Convert.ToInt16(Console.ReadLine()); + theDao.remove(itemToDelete); + itemList = theDao.list(); + } + static void MarkItem() + { + foreach(Item eachItem in itemList) + { + Console.WriteLine(eachItem); + } + Console.WriteLine("Enter item Id to mark done:"); + int itemToMark = Convert.ToInt16(Console.ReadLine()); + theDao.markDone(itemToMark); + itemList = theDao.list(); + } + static void ListItems(bool statusChecked, bool checkingSpecific) + { + foreach(Item listedItem in itemList) + { + if(checkingSpecific == true) + { + if(listedItem.Status == statusChecked) + { + Console.WriteLine(listedItem); + } + } + else + { + Console.WriteLine(listedItem); + } + } + //enter key press detector + System.Console.WriteLine("press enter to go back"); + string enterChecker = Console.ReadLine(); + } + } + + //Item class for each item in todo list + public class Item + { + //each item has an Id, a description, and a Status + public int Id{get;set;} + public string Description{get;set;} + public bool Status{get;set;} + //constructor overload used in adding an item with default status of not finished + public Item(string description) + { + this.Description = description; + this.Status = false; + } + + //gives item a string + override + public String ToString(){ + return Id+": "+Description+" is "+(Status? "finished" : "pending"); + } + } + + public class DAO + { + public Context context; + public DAO(){ + //instantiates context + context = new Context(); + //esures data base is created + context.Database.EnsureCreated(); + } + + //list that has items from database added to it + public List list(){ + List theResult = new List(); + foreach(Item anItem in context.myItems) + { + theResult.Add(anItem); + } + return theResult; + } + //method to add items to myItems Data base set + public void add(String description) + { + context.myItems.Add(new Item(description)); + //saves changes + context.SaveChanges(); + } + //method to remove from DBset + public void remove(int id) + { + foreach(Item checkedItem in context.myItems){ + if(checkedItem.Id == id) + { + context.myItems.Remove(checkedItem); + } + } + context.SaveChanges(); + } + //method to find from DBset + public void markDone(int id) + { + foreach(Item checkedItem in context.myItems){ + if(checkedItem.Id == id) + { + checkedItem.Status = true; + } + } + context.SaveChanges(); + } + + public Item GetItem(String lookingFor) + { + foreach(Item anItem in context.myItems){ + if(anItem.Id.ToString() == lookingFor) + { + return anItem; + } + } + return null; + } + } + public class Context : DbContext { + public DbSet myItems {get;set;} + + override + protected void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ + optionsBuilder.UseSqlite("Filename=./Items.db"); + } + } +} diff --git a/Checkpoint3/ToDo.db b/Checkpoint3/ToDo.db new file mode 100644 index 00000000..e69de29b