2012-09-24 40 views
0

我需要檢查ISBN是否在數據輸入過程中是唯一的,並列出價格低於或高於該圖書的價格。我試圖顯示價格,但我只能顯示確切的價格輸入。C#錯誤檢查獨特價格和低於和高於

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

namespace Assignment_3 
{ 
class Program 
{ 
    public static List<Book> book = new List<Book>(); 
    public static List<BookCategory> bc = new List<BookCategory>(); 

    static void Main(string[] args) 
    { 
     int option; 
     bc.Add(new BookCategory("Comedy")); 
     bc.Add(new BookCategory("Horror")); 
     bc.Add(new BookCategory("Adventure")); 
     do 
     { 
      Console.WriteLine("\t\t----------------------------------------------"); 
      Console.WriteLine("\t\t|\t1. Add new book information   |"); 
      Console.WriteLine("\t\t|\t2. Search book information   |"); 
      Console.WriteLine("\t\t|\t3. Display book information   |"); 
      Console.WriteLine("\t\t|\t4. Display book category    |"); 
      Console.WriteLine("\t\t|\t5. Exit        |"); 
      Console.WriteLine("\t\t----------------------------------------------"); 
      Console.Write("Please choose an option : "); 
      option = int.Parse(Console.ReadLine()); 
      Console.WriteLine(); 

      switch (option) 
      { 
       case 1: Add(book,bc); 
        break; 
       case 2: Search(book,bc); 
        break; 
       case 3: Display(book, bc); 
        break; 
       case 4: Compare(bc); 
        break; 
       case 5: Environment.Exit(0); 
        break; 
       default: Console.WriteLine("You have entered an invalid option ! "); 
        break; 
      } 
     } while (option != 5); 
     Console.Read(); 
    } 

    static void Add(List<Book> b, List<BookCategory> bc) 
    { 
     string title, isbn, author, bookCategory; 
     double price; 
     Console.Write("Enter the book title : "); 
     title = Console.ReadLine(); 
     Console.Write("Enter the ISBN of the book : "); 
     isbn = Console.ReadLine(); 
     Console.Write("Enter the author of the book : "); 
     author = Console.ReadLine(); 
     Console.Write("Enter the book category <Adventure | Horror | Comedy> : "); 
     bookCategory = Console.ReadLine(); 
     Console.Write("Enter the price of the book : "); 
     price = double.Parse(Console.ReadLine()); 
     b.Add(new Book(title, isbn, author, bookCategory, price)); 

     if (bookCategory == "Comedy") 
     { 
      BookCategory tempBookObj = bc.Find(delegate(BookCategory bcg) 
      { 
       return bcg.CategoryName.Equals("Comedy"); 
      }); 
      tempBookObj.BookNo++; 
     } 

     if (bookCategory == "Horror") 
     { 
      BookCategory tempBookObj = bc.Find(delegate(BookCategory bcg) 
      { 
       return bcg.CategoryName.Equals("Horror"); 
      }); 
      tempBookObj.BookNo++; 
     } 

     if (bookCategory == "Adventure") 
     { 
      BookCategory tempBookObj = bc.Find(delegate(BookCategory bcg) 
      { 
       return bcg.CategoryName.Equals("Adventure"); 
      }); 
      tempBookObj.BookNo++; 
     } 

     else 
     { 
      Console.WriteLine("Please enter the book category according to the list ! "); 
     } 
    } 

    public static void Search(List<Book> b, List<BookCategory> bc) 
    { 
     int option; 
     string target, target1; 
     double target2; 
     List<Book> book1 = new List<Book>(); 
     Console.WriteLine("\t\t--------------------------------------"); 
     Console.WriteLine("\t\t|\t1. ISBN      |"); 
     Console.WriteLine("\t\t|\t2. Book Title    |"); 
     Console.WriteLine("\t\t|\t3. Book Price    |"); 
     Console.WriteLine("\t\t|\t4. Back to main menu   |"); 
     Console.WriteLine("\t\t--------------------------------------"); 
     Console.Write("Please choose an option : "); 
     option = int.Parse(Console.ReadLine()); 
     Console.WriteLine(); 

     switch (option) 
     { 
      case 1: Console.Write("Enter the ISBN of book to be searched : "); 
       target = Console.ReadLine(); 
       Book result = b.Find(delegate(Book bk) 
       { 
        return bk.ISBN.Equals(target); 
       }); 

       if (result == null) 
        Console.WriteLine("Product not found !"); 
       else 
        Console.WriteLine(result); 
       break; 

      case 2: Console.Write("Enter the title of book to be searched : "); 
       target1 = Console.ReadLine(); 
       Book result1 = b.Find(delegate(Book bk) 
       { 
        return bk.Title.Equals(target1); 
       }); 
       if (result1 == null) 
        Console.WriteLine("Product not found !"); 
       else 
        Console.WriteLine(result1); 
       break; 

      case 3: Console.Write("Enter the price of book to be searched : "); 
       target2 = double.Parse(Console.ReadLine()); 
       Book result2 = b.Find(delegate(Book bk) 
       { 
        return bk.Price.Equals(target2); 
       }); 

       if (result2 == null) 
        Console.WriteLine("Product not found !"); 
       else 
        Console.WriteLine(result2); 
       break; 

      case 4: 
       break; 
     } 
    } 

    public static void Display(List<Book> b, List<BookCategory> bc) 
    { 
     b.Sort(); 
     foreach (Book bk in b) 
     { 
      Console.WriteLine(bk.ToString()); 
     }   
    } 

    public static void Compare(List<BookCategory> bc) 
    { 
     bc.Sort(); 
     foreach (BookCategory bk in bc) 
     { 
      Console.WriteLine(bk.ToString()); 
     } 
    } 


} 

}

+1

請降低你的問題具體的事情。 – akton

+3

你應該使用比'b','bc','bk'更好的變量名稱。我會建議'書籍','類別'和'書'。另外'BookCategory'看起來應該是一個'enum'而不是一個字符串列表。 – verdesmarald

+1

注意:不要通過'delegate'匿名方法,請查看[Lambda表達式](http://msdn.microsoft.com/zh-cn/library/bb397687.aspx)。 –

回答

0

你需要根據你的書類來顯示你的書的細節有點像

public static void Display(List<Book> b, List<BookCategory> bc) 
{ 
    b.Sort(); 
    foreach (Book bk in b) 
    { 
     Console.WriteLine(bk.title + (bk.price+10).ToString()); 
    }   
} 

編輯的代碼。

0

你在哪裏實施的對象「書? 你應該重寫有方法‘的ToString()’在那裏你可以決定的值要書寫時,顯示‘Book.Tostring();’

0

要解決這個問題,你發現這本書的價格書:

  1. 排序按價格
  2. 書找到您發現
  3. 之前獲取的書和後書(但如果他們有什麼相同的價格?)

編碼留作家庭作業:-)

0

if塊在Add方法是錯誤的。相反的:

if (bookCategory == "Comedy") { } 
if (bookCategory == "Horror") { } 
if (bookCategory == "Adventure") { } 
else { } 

...你應該有:

if (bookCategory == "Comedy") { } 
else if (bookCategory == "Horror") { } 
else if (bookCategory == "Adventure") { } 
else { } 

但更好的是,你可以通過下面的代碼(它做同樣的事情改進,但有點短,更容易IMO閱讀):

switch (bookCategory) 
{ 
    case "Comedy": 
    case "Horror": 
    case "Adventure": 
     BookCategory tempBookObj = bc.Find(delegate(BookCategory bcg) 
     { 
      return bcg.CategoryName.Equals(bookCategory); 
     }); 
     tempBookObj.BookNo++; 
     break; 
    default: 
     Console.WriteLine("Please enter the book category according to the list ! "); 
     break; 
} 

而且,由於雙方你bookbc變量是靜態的,即使在同一類的其他方法,沒有必要通過圍繞這些變種可以分成所有方法,因爲無論如何它們都可以訪問它們。

至於添加獨特的書號,你可以嘗試這樣的:

do { 
    Console.Write("Enter the ISBN of the book : "); 
    isbn = Console.ReadLine(); 
} while (!books.Any(b => b.Isbn.Equals(isbn))) // or any other way of comparing 
相關問題