2017-12-01 8 views
-3

나는 도서관 신청서를 쓰고 있는데, 나는 제목, 저자, 그리고 어떤 유형의 책 (Roman, Tidskrift, novellsamling)을 쓸 수 있어야한다.도서관 신청

이 프로그램은 제목과 저자의 두 가지 질문에 답할 때 1-3 가지 옵션이 제공되며 책 유형은 1입니다. Roman 2. Tidskrift 3. Novellsamling.

1, 2 또는 3으로 대답해야합니다. 그런 다음 VisaBöcker (영어로 된 ShowBooks) 메서드를 호출하면 1,2 또는 3의 옵션이 저장되도록 어떻게 만들 수 있습니까? 사용자가 저장하고자하는 옵션에 저장됩니다. 즉, 1을 로마자로 선택하면 "1"을 로마식 클래스에 저장하려면 어떻게해야합니까? 그러면 "Title" + "Author" + "Type (1,2,3/Roman,Tidskrift,Novellsamling)이 표시됩니다. 예 : Pippi Långstrump by Astrid Lindgren. (Roman) <- the type of book, number 1 chosen.

지금은 책의 유형을 묻는 메시지가 표시 될 때 Roman이라는 단어를 입력 할 수 있지만 옵션으로 1,2,3 개의 문자 만 쓰면됩니다. 네가 원하는 반에 그것을 저장해야한다. 예를 들면 로마인. 대신 예약 유형을 나타내는 클래스를 사용

class Bok 
{ 
    public string Titel = ""; 
    public string Skribent = ""; 
    public string Typ; 

    class Roman : Bok 
    { 
     public Roman() 
     { 
      Typ = "Roman"; 
     } 
    } 

    class Tidskrift : Bok 
    { 
     public Tidskrift() 
     { 
      Typ = "Tidskrift"; 
     } 
    } 

    class Novellsamling : Bok 
    { 
     public Novellsamling() 
     { 
      Typ = "Novellsamling"; 
     } 
    } 
} 

static List<string[]> Böcker = new List<string[]>(); 
static string[] bok; 

public static void RegistreraBok() 
{ 
    bok = new string[3]; 

    Console.Write("\n\tSkriv in titel: "); 
    bok[0] = Console.ReadLine(); 

    Console.Write("\n\tSkriv in författare: "); 
    bok[1] = Console.ReadLine(); 

    Console.Write("\n\tÄr boken en [1] Roman, [2] Tidsskrift eller [3] Novellsamling?: "); 
    bok[2] = Console.ReadLine(); 

    Console.WriteLine("\n\tSparat!"); 
    Console.ReadLine(); 

    Böcker.Add(bok); 
    Console.Clear(); 
} 

public static void VisaBöcker() 
{ 
    for (int i = 0; i < Böcker.Count; i++) 
    { 
     Console.WriteLine("\t" + Böcker[i][0] + " av " + Böcker[i][1] + "." + " (" + Böcker[i][2] + ")"); 
    } 
} 

답변

0

내가 수정하려고 할 수 있습니다 이런 식의 코드 (https://pastebin.com/fz4i1p0P)

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static List<book> _booksList = new List<book>(); 
     static Hashtable _ht = new Hashtable(); 
     static void Main(string[] args) 
     { 
      _ht.Add(BookTypes.Roman, "Roman"); 
      _ht.Add(BookTypes.Journal, "Journal"); 
      _ht.Add(BookTypes.StoryCollection, "Collection of stories"); 

      Console.WriteLine("\t Hello and welcome to the library!"); 
      int userInput; 
      bool isRunning = true; 
      while (isRunning) 
      { 
       Console.WriteLine("\n\t[1] Add a book" + 
        "\n\t[2] Show Books" + 
        "\n\t[3] End program"); 
       Console.Write("\n\tSelect: "); 
       Int32.TryParse(Console.ReadLine(), out userInput); 
       switch (userInput) 
       { 
        case 1: 
         Console.Clear(); 
         RegisterBook(); 
         break; 
        case 2: 
         Console.Clear(); 
         ShowAllBooks(); 
         break; 
        case 3: 
         isRunning = false; 
         break; 
        default: 
         Console.Clear(); 
         Console.WriteLine("\n\tDu kan endast välja 1-3 i menyn."); 
         break; 
       } 
      } 
     } 
     static void RegisterBook() 
     { 
      book bok = new book(); 
      Console.Write("\n\tEnter Title: "); 
      bok.Title = Console.ReadLine(); 
      Console.Write("\n\t Author : "); 
      bok.Author = Console.ReadLine(); 
      Console.Write("\n\tEnter Type of book - Roman-1, Journal-2, Collection of stories-3?: "); 
      bok.BookType = (BookTypes)Convert.ToInt32(Console.ReadLine()); 
      _booksList.Add(bok); 
      Console.WriteLine("\n\t Added Book Successfully !"); 
      Console.ReadLine(); 
      Console.Clear(); 
     } 
     public static void ShowAllBooks() 
     { 
      for (int i = 0; i < _booksList.Count; i++) 
      { 
       Console.WriteLine("\t" +" Title: "+ _booksList[i].Title + " Author: " + _booksList[i].Author+ " Type: " + _ht[_booksList[i].BookType].ToString()); 
      } 
     } 
    } 
    public class book 
    { 
     public string Title { get; set; } 
     public string Author { get; set; } 
     public BookTypes BookType { get; set; } 
    } 
    public enum BookTypes 
    { 
     Roman=1, 
     Journal=2, 
     StoryCollection=3 
    } 
} 

확인해주세요.

+0

와우 매우 감사합니다. 곧 더 자세히 설명해 드리겠습니다. 완벽하게 작동하는 것처럼 보입니다. 스타일과 글쓰기/언어에 대한 '터치'를 추가 할 것입니다. 뭔가있을 경우 알려 드리겠습니다. –

1

, 당신은 다음과 같은 방법으로

public enum BookTypes 
{ 
    Roman =1, 
    Tidskrift=2, 
    Novellsamling=3 

} 

에 그리고 당신의 기능에 열거를 시도 할 수 있습니다, 당신은이

 BookTypes eTypes = BookTypes.Novellsamling; 
     Console.WriteLine("Enter Type of Book: = "); 
     eTypes = (BookTypes) Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Type: = " + eTypes.ToString()); 
+0

저는 이것을 내 코드에 맞출 수 없으며 전에 enum을 사용하지 않았습니다. https://pastebin.com/dZKnGVrT, 내 코드가 모두 비슷해 보이는 것입니다. 코드를 어디에 넣을까요? 대체 할 부분은 무엇입니까? BookTypes가 발견되지 않고 현재 컨텍스트에 존재하지 않는다는 오류 만받습니다. –

+0

https://pastebin.com/Xqc4AsXv 이제 제목, 저자, 그리고 책의 유형으로 모든 책을 쓸 때, 나는 무엇을 입력해야할지 모르겠다. WriteLine은 "VisaBöcker()"를 보여 주며, "book of type?" 책의 종류가 표시되는 곳입니다. 그래서 1을 선택하면 "Roman"이라고 표시됩니다. eTypes.ToString()); 현재 컨텍스트에 존재하지 않기 때문에 거기에서 작동하지 않습니다. RegisteraBok에서 작동하지만 eTypes = (BookTypes) Convert.ToInt32 (Console.ReadLine()); 존재합니다. –