Skip to content

Commit

Permalink
library interface working
Browse files Browse the repository at this point in the history
  • Loading branch information
ehilst515 committed Sep 3, 2020
1 parent 3ae4d3c commit 4ea35f8
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 5 deletions.
16 changes: 15 additions & 1 deletion Library.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library", "Library\Library.csproj", "{B99D3954-CA4A-40E4-97A2-6BE43B8CE00F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibraryProgram", "Library\LibraryProgram.csproj", "{B99D3954-CA4A-40E4-97A2-6BE43B8CE00F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibraryTests", "LibraryTests\LibraryTests.csproj", "{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -30,5 +32,17 @@ Global
{B99D3954-CA4A-40E4-97A2-6BE43B8CE00F}.Release|x64.Build.0 = Release|Any CPU
{B99D3954-CA4A-40E4-97A2-6BE43B8CE00F}.Release|x86.ActiveCfg = Release|Any CPU
{B99D3954-CA4A-40E4-97A2-6BE43B8CE00F}.Release|x86.Build.0 = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|x64.Build.0 = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|x86.ActiveCfg = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Debug|x86.Build.0 = Debug|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|Any CPU.Build.0 = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|x64.ActiveCfg = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|x64.Build.0 = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|x86.ActiveCfg = Release|Any CPU
{BD85273C-41E0-41DE-B629-C8C1D0A7C05F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
20 changes: 20 additions & 0 deletions Library/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
namespace LibraryProgram
{
public class Author
{
public string FirstName { get; set; }
public string LastName { get; set; }

public string Name()
{
return $"{FirstName} {LastName}";
}

public Author(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
}
24 changes: 24 additions & 0 deletions Library/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
namespace LibraryProgram
{
class Book
{
public string Title { get; set; }

public Author Author { get; set; }

public int NumberOfPages { get; set; }

public Genre Genre { get; set; }
}

enum Genre
{
Mystery,
Fantasy,
Nonficiton,
ComicBook,
ScienceFiction

}
}
53 changes: 53 additions & 0 deletions Library/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace LibraryProgram
{
public class Library<T> : IEnumerable<T>
{
private int count = 0;
private T[] books = new T[5];
public int Count => count;

public void Add(T book)
{
if (count >= books.Length)
{
Array.Resize(ref books, books.Length * 2);
}
books[count] = book;
count++;
}

public bool Remove(int index)
{
if (index < 0)
return false;

for (int i = index; i < count; i++)
books[i] = books[i + 1];

books[count] = default;
count--;
return true;
}



public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < count; i++)
{
yield return books[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
File renamed without changes.
115 changes: 111 additions & 4 deletions Library/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,119 @@
using System;

namespace Library
using System;


namespace LibraryProgram
{
class Program
{
public static Library<Book> Library { get; set; }
public static Library<Book> BookBag { get; set; }

static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Library = new Library<Book>();

start:

Console.WriteLine("Welcome to the library!");
Console.WriteLine("Enter a number with the corresponding command; 1: View Books, 2: Add a book");
string input = Console.ReadLine();

if (input == "1")
{
LoadBooks();
goto start;
}


if (input == "2")
{
Console.WriteLine("Enter book title:");
string inputTitle = Console.ReadLine();

Console.WriteLine("Enter Author first name:");
string inputAuthorFirst = Console.ReadLine();

Console.WriteLine("Enter Author last name:");
string inputAuthorLast = Console.ReadLine();

Console.WriteLine("Enter number of pages:");
string inputNumberOfPages = Console.ReadLine();
int numberNumberOfPages = Convert.ToInt32(inputNumberOfPages);

Console.WriteLine("Enter number genre:");
string inputGenre = Console.ReadLine();
int numberGenre = Convert.ToInt32(inputGenre);

AddABook(inputTitle, inputAuthorFirst, inputAuthorLast, numberNumberOfPages, Genre.Fantasy);

Console.Clear();

Console.WriteLine("Book added! Updated Library: ");
LoadBooks();

goto start;
}

Console.ReadLine();

}

//static string UI()
//{
// Console.WriteLine("Welcome to the library!");
// Console.WriteLine("Enter a number with the corresponding command; 1: View Books, 2: Add a book");
// string input = Console.ReadLine();

// return input;

//}

static void AddABook(string title, string firstName, string lastName, int numberOfPages, Genre genre)
{
Book book = new Book()
{
Title = title,
Author = new Author(firstName, lastName)
{
FirstName = firstName,
LastName = lastName
},
NumberOfPages = numberOfPages,
Genre = genre
};

Library.Add(book);
}

static void LoadBooks()
{
AddABook("A Song of Ice and Fire", "George RR", "Martin", 694, Genre.Fantasy);
AddABook("Shade's Children", "Garth", "Nix", 310, Genre.ScienceFiction);
AddABook("Skelling", "David", "Almond", 176, Genre.ScienceFiction);

int counter = 1;
foreach (Book book in Library)
Console.WriteLine($"{counter++} : {book.Title}, {book.Author.Name()}, {book.NumberOfPages}, {book.Genre}");
}


//static void ReturnBook()
//{
// Dictionary<int, Book> books = new Dictionary<int, Book>();
// Console.WriteLine("Which book would you like to return");
// int counter = 1;
// foreach (var item in BookBag)
// {
// books.Add(counter, item);
// Console.WriteLine($"{counter++}. {item.Title} - {item.Author.FirstName} {item.Author.LastName}");

// }

// string response = Console.ReadLine();
// int.TryParse(response, out int selection);
// books.TryGetValue(selection, out Book returnedBook);
// BookBag.Remove(returnedBook);
// Library.Add(returnedBook);
//}
}
}
19 changes: 19 additions & 0 deletions LibraryTests/LibraryTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Library\LibraryProgram.csproj" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions LibraryTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using LibraryProgram;
using Xunit;

namespace LibraryTests
{
public class UnitTest1
{
[Fact]
public void Library_starts_empty()
{
Library<string> library = new Library<string>();

Assert.Empty(library);
}

[Fact]
public void Library_can_store()
{
Library<string> storeLibrary = new Library<string>();

storeLibrary.Add("book");
storeLibrary.Add("GoT");

Assert.Equal(new[] { "book", "GoT" }, storeLibrary);
}

[Fact]
public void Library_can_remove()
{
// Arange
Library<string> removeLibrary = new Library<string>();

removeLibrary.Add("book");
removeLibrary.Add("Harry Potter");
removeLibrary.Add("GoT");


// Act
bool result = removeLibrary.Remove(1);

// Assert
Assert.True(result);
Assert.Equal(new[] { "book", "GoT" }, removeLibrary);
}

}
}

0 comments on commit 4ea35f8

Please sign in to comment.