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