hjälp med c# kod! [hur jag ska få in är underklasser till min klass?]
Jag har fastnat på en uppgift i min kurs i c# och blir galen. Jag vet inte hur jag ska koppla samman min kod.
Jag har byggt upp som ett bibliotek där folk kan välja att antingen lägga till en bok, kolla vilka böcker som lagts till, söka efter en bok eller gå ur programmet. Det jag inte förstår hur jag ska få in är underklasser till min klass som är ett krav.
När man går in i menyn och väljer att man vill lägga till en bok så får man nu upp "ange författare" "ange titel" och sista ska vara ett val istället mellan 3 olika typer av böcker tex: 1. roman 2. tidning 3. novell. Allt detta ska sedan sparas och kunna listas upp.
Huvudklassen är bok.
Jag ska skapa 3 underklasser, en för varje typ av bok.
Har suttit och prövat lite olika saker tex array osv, men jag känner bara att jag förstår inte vad jag håller på med.
Min kod ser ut såhär just nu:
using System;
using System.Collections.Generic;
class MainClass
{
static void Main()
{
BookManager manager = new BookManager();
manager.Menu();
}
}
public class Book
{
public static int NumberOfBooks;
private string title;
private string author;
private string typ;
public Book(string title, string author, string typ)
{
this.title = title;
this.author = author;
this.typ = typ;
NumberOfBooks++;
}
public string GetTitle()
{
return title;
}
public string GetAuthor()
{
return author;
}
public string GetTyp()
{
return typ;
}
public void SetTitle(string title)
{
this.title = title;
}
public override string ToString()
{
return title + " " + author + " " + typ;
}
}
public class Tidskrift : Book
{
public string Tidskrift()
{
typ = "Tidskrift";
}
}
public class Roman : Book
{
public Roman()
{
typ = "Roman";
}
}
public class Novellsamling : Book
{
public Novellsamling()
{
typ = "Novellsamling";
}
}
public class BookManager
{
private List<Book> library;
public BookManager()
{
library = new List<Book>();
}
public void Menu()
{
bool loop = true;
while (loop)
{
int choice = GetMenuChoice();
switch (choice)
{
case 1:
AddBook();
break;
case 2:
ListBooks();
break;
case 3:
SearchByTitle();
break;
case 4:
loop = false;
break;
default:
Console.WriteLine("Fel inmatning, pröva igen!");
break;
}
}
}
private int GetMenuChoice()
{
Console.Write("Välkommen till Biblioteket!\n");
;
Console.WriteLine("1. Lägg till en bok");
Console.WriteLine("2. Lista över tillagda böcker");
Console.WriteLine("3. Sök efter titel");
Console.WriteLine("4. Avsluta");
Console.Write("\nvad vill du göra? : ");
string choice = Console.ReadLine();
int numChoice = Convert.ToInt32(choice);
return numChoice;
}
private void AddBook()
{
Console.Clear();
Console.WriteLine("Titel: ");
string title = Console.ReadLine();
Console.WriteLine("Skribent: ");
string author = Console.ReadLine();
Console.WriteLine(här ska valet mellan typ av bok vara);
string typ = Console.ReadLine();
Console.Clear();
Book b = new Book(title, author, typ);
library.Add(b);
}
private void ListBooks()
{
Console.Clear();
foreach (Book b in library)
{
Console.WriteLine(b.ToString() + "\n");
}
}
private void SearchByTitle()
{
Console.Write("Titel: ");
string titel = Console.ReadLine();
foreach (Book b in library)
{
if (titel == b.GetTitle())
{
Console.WriteLine(b.ToString() + "\n");
}
}
}
}