0

我需要在数据输入期间检查 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

4 回答 4

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
于 2012-09-24T09:32:30.820 回答
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());
    }           
}

根据您的书类编辑代码。

于 2012-09-24T09:15:07.107 回答
0

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

于 2012-09-24T09:19:07.877 回答
0

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

  1. 根据价格对书籍进行排序
  2. 找到你找到的书
  3. 在之前和之后获取书籍(但如果它们的价格相同怎么办?)

编码留作作业:-)

于 2012-09-24T09:19:09.530 回答