C#错误检查唯一且上市价格低于及以上

时间:2012-09-24 09:07:16

标签: c#

我需要在数据输入过程中检查ISBN是否唯一,并列出该书的价格低于或高于此值。我试图显示价格,但我只能显示输入的确切价格。

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());
        }
    }


}

}

4 个答案:

答案 0 :(得分: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());
    }           
}

根据您的书类编辑代码。

答案 1 :(得分:0)

你在哪里实现了对象“Book?”, 你应该在那里覆盖方法“ToString()”,你可以在那里决定写“Book.Tostring();”时要显示的值。

答案 2 :(得分:0)

要获得价格与您找到的书籍价格相近的书籍:

  1. 根据价格对书籍进行排序
  2. 找到您找到的图书
  3. 获取之前和之后的书籍(但如果它们具有相同的价格会怎么样?)
  4. 编码留作家庭作业: - )

答案 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 { }

但更好的是,你可以通过使用以下代码来改进它(它做同样的事情,但是更短,更容易阅读):

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变量都是静态的,甚至与其他方法在同一个类中,因此无需将这些变量传递给所有方法,因为它们都可以访问它们反正。

至于添加唯一的isbn,您可以尝试类似:

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