1 svar
913 visningar
bitbuzz 1 – Fd. Medlem
Postad: 16 dec 2020 20:59 Redigerad: 16 dec 2020 22:57

Add Biblioterkarien till Bokhyllan

Hej alla,

Jag har skrivit ett bokhylla c# app som jag ser ut nedan, Jag behöver utveckla den från en bokhylla till ett bibliotek.

så jag ska nu lägga till en bibliotekarie som hanterar alla böcker åt oss och bibliotekarie ska sköta alla inmatningar, sökning och hämtning av böcker.

Anledningen att publicera min kod är bara jag ville veta om jag kan göra något annat med min kod. Bibliotekarie måste vara en klass och innehålla åtminstone tre metoder så som 

class Bibliotekarien
{
private List<Bok> bookLista;
public void HämtaBoklista()
public void SkapaBok()
public void SökBok()
}

alla förslag kommer att uppskattas.

Bookhyllans kod:

 

using System;
using System.Collections.Generic;

namespace bookShelf
{
class Program
{
static void Main(string[] args)
{
List<Book> bookShelf = new List<Book>();
int menu = 0;
while (menu != 3)
{
Console.WriteLine("\t Bibiloteket");
Console.WriteLine("\t[1] Lägg till en ny bok");
Console.WriteLine("\t[2] Visa alla böcker");
Console.WriteLine("\t[3] Avsluta");
Console.Write("\n\tVälj: ");
if (Int32.TryParse(Console.ReadLine(), out menu))
{
Console.WriteLine();
switch (menu)
{
case 1:
bookShelf.Add(MakeNewBook());
break;
case 2:
Show(bookShelf);
break;
case 3:
break;
default:
Console.WriteLine("\n\tSkriv en siffra");
break;
}
}
else
{
Console.WriteLine("\n\tDu måste skriva en siffra (1-3).\n");
}
}
}
public static int BookTypeSubMenu()
{
int choice;
while (true)
{
Console.WriteLine("\n\t Välj nummer, [1] Roman, [2] Tidsskrift, [3] Novellsamling");
Console.Write("\n\t");
if (Int32.TryParse(Console.ReadLine(), out choice))
{
if (choice > 0 && choice < 4)
{
Console.WriteLine("\n\tLagrat\n");
return choice;
}
else
{
Console.WriteLine("\n\tDu måste skriva en siffra (1-3)");
}
}
}
}
public static Book MakeNewBook()
{
Console.Write("\tTitel: ");
string titel = Console.ReadLine();

Console.Write("\n\tFörfattare: ");
string author = Console.ReadLine();

int type = BookTypeSubMenu();
Book newBook;
switch (type)
{
case 1:
newBook = new Novel(titel, author);
return newBook;
case 2:
newBook = new Magazine(titel, author);
return newBook;
case 3:
newBook = new Collection(titel, author);
return newBook;
default:
newBook = new Book(titel, author);
return newBook;
}
}
public static void Show(List<Book> bookShelf)
{
if (bookShelf.Count == 0)
{
Console.WriteLine("\tBibloteket är tomt\n");
}
else
{
foreach (Book book in bookShelf)
{
Console.WriteLine("\t" + book.ToString());
}
}
Console.WriteLine();
}
}
class Book
{
protected string title;
protected string author;
protected string type;
public Book(string title, string author)
{
this.title = title;
this.author = author;
}
public override string ToString()
{
return "\"" + title + "\" skriven av " + author + ". (" + type + ")";
}
}
class Novel : Book
{
public Novel (string title, string author) : base(title, author)
{
this.type = "Roman";
}
}
class Magazine : Book
{
public Magazine(string title, string author) : base(title, author)
{
this.type = "Tidskrift";
}
}
class Collection : Book
{
public Collection(string title, string author) : base(title, author)
{
this.type = "Novellsamling";
}
}

}

Lindehaven 820 – Lärare
Postad: 17 dec 2020 09:06

Det finns arv i din kod, ok. Det finns fler konstruktioner för objektorientering, t ex aggregering och komposition, som kan behövas i detta fall. En klass som "har" andra klasser kallas aggregerad. En klass som "består" av andra klasser kallas komponerad.

Klassen Bibliotekarien är just nu en komposition av klassen Bok. Är det riktigt? Består verkligen en bibliotekarie av böcker?

Fundera på vad ett bibliotek "består" av och "har". Skapa och aggregera/komponera klasserna utifrån det.

Svara Avbryt
Close