forked from AustinCodingAcademy/csharp-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
finished checkpoint3 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mtrich
wants to merge
1
commit into
master
Choose a base branch
from
Checkpoint3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp2.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace Checkpoint3 | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| //initialises controller class and calls Run method | ||
| Controller controller = new Controller(); | ||
| controller.Run(); | ||
| } | ||
| } | ||
| public class Controller | ||
| { | ||
| //instantiates Dao | ||
| static DAO theDao = new DAO(); | ||
| //sets itemList to Dao list | ||
| static List<Item> 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of using the static 'itemList' here, i would have gotten a "fresh" list from the dao ( |
||
| { | ||
| 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<Item> list(){ | ||
| List<Item> theResult = new List<Item>(); | ||
| 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<Item> myItems {get;set;} | ||
|
|
||
| override | ||
| protected void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ | ||
| optionsBuilder.UseSqlite("Filename=./Items.db"); | ||
| } | ||
| } | ||
| } | ||
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not make the list a static variable here. the point of the dao is to get you the ilst when needed. since it might change in the database outside of this program.