forked from ehilst515/Lab08Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
290 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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; | ||
} | ||
} | ||
} |
This file contains 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,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 | ||
|
||
} | ||
} |
This file contains 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,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.
This file contains 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 |
---|---|---|
@@ -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); | ||
//} | ||
} | ||
} |
This file contains 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,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> |
This file contains 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,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); | ||
} | ||
|
||
} | ||
} |