Implement IProduct interface

This commit is contained in:
nek0 2020-04-27 16:14:20 +02:00
parent 3558e3057f
commit c252b91ca6
4 changed files with 37 additions and 30 deletions

View file

@ -25,7 +25,7 @@ namespace Bücherwurm
foreach (var book in BookList)
{
Console.WriteLine("---");
Console.WriteLine("ID: {0}", book.BookId);
Console.WriteLine("ID: {0}", book.ProductId);
Console.WriteLine("Author: {0}", book.Author);
Console.WriteLine("Titel: {0}", book.Title);
}
@ -71,7 +71,7 @@ namespace Bücherwurm
case "j":
{
Correct = true;
Catalogue.Add(Title, Author, Country, ImgLink, Link, Language, int.Parse(Pages), int.Parse(Year));
Catalogue.AddBook(Title, Author, Country, ImgLink, Link, Language, int.Parse(Pages), int.Parse(Year));
Console.WriteLine("Buch wurde hinzugefügt.");
break;
}

23
Book.cs
View file

@ -2,11 +2,8 @@ using System.Text.Json.Serialization;
namespace Bücherwurm
{
class Book
class Book : IProduct
{
[JsonIgnoreAttribute]
public int BookId {get; set;}
[JsonPropertyName("title")]
public string Title {get; set;}
@ -16,9 +13,6 @@ namespace Bücherwurm
[JsonPropertyName("country")]
public string Country {get; set;}
[JsonPropertyName("imageLink")]
public string ImageLink {get; set;}
[JsonPropertyName("link")]
public string Link {get; set;}
@ -30,12 +24,19 @@ namespace Bücherwurm
[JsonPropertyName("year")]
public int Year {get; set;}
[JsonIgnore]
public int ProductId { get; set; }
[JsonIgnore]
public string Category { get; }
[JsonPropertyName("imageLink")]
public string ImageLink { get; set; }
public Book(int Id, string Title, string Author,
string Country, string ILink, string Link, string Language,
int Pages, int Year)
{
BookId = Id;
ProductId = Id;
Category = "book";
this.Title = Title;
this.Author = Author;
this.Country = Country;
@ -51,11 +52,11 @@ namespace Bücherwurm
}
public void OverwriteNullId(int Id)
public void OverwriteNullId(int id)
{
if (BookId == 0)
if (ProductId == 0)
{
BookId = Id;
ProductId = id;
}
}
}

View file

@ -1,53 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
namespace Bücherwurm
{
class Catalogue
{
private List<Book> Books {get; set;}
private List<IProduct> Products {get; set;}
private int NextId {get; set;}
public Catalogue()
{
Books = new List<Book>();
Products = new List<IProduct>();
NextId = 1;
}
public void Import(string JsonString)
{
var intermediateBooks = JsonSerializer.Deserialize<List<Book>>(JsonString);
foreach (var ibook in intermediateBooks)
var IntermediateBooks = JsonSerializer.Deserialize<List<IProduct>>(JsonString);
foreach (var Ibook in IntermediateBooks)
{
ibook.OverwriteNullId(NextId);
NextId = NextId + 1;
Ibook.OverwriteNullId(NextId);
NextId += 1;
}
Books = intermediateBooks;
Products = IntermediateBooks;
}
public void Add(string Title, string Author,
public void AddBook(string Title, string Author,
string Country, string ILink, string Link,
string Language, int Pages, int Year)
{
Books.Add(new Book(NextId, Title, Author, Country, ILink, Link, Language, Pages, Year));
Products.Add(new Book(NextId, Title, Author, Country, ILink, Link, Language, Pages, Year));
NextId = NextId++;
}
public void Remove(int BookID)
public void Add(IProduct product)
{
Books.RemoveAll(book => book.BookId == BookID);
Products.Add(product);
}
public void Remove(int bookID)
{
Products.RemoveAll(book => book.ProductId == bookID);
}
public List<Book> GetBooks()
{
return Books;
return Products.FindAll(prod => prod.Category == "book").Cast<Book>().ToList();
}
public Book LookupBook(int BookId)
public Book LookupBook(int bookId)
{
return Books.Find(book => book.BookId == BookId);
return (Book)Products.Find(book => book.ProductId == bookId && book.Category == "book");
}
}
}

View file

@ -13,9 +13,9 @@ namespace Bücherwurm
NextId = 1;
}
public void Add(Book Book)
public void Add(IProduct Book)
{
InventoryList.Add(new Item(NextId, Book.BookId));
InventoryList.Add(new Item(NextId, Book.ProductId));
NextId = NextId + 1;
}