Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace BookShop
- {
- using System;
- using System.Linq;
- using System.Text;
- using BookShop.Data;
- using BookShop.Initializer;
- using BookShop.Models;
- public class StartUp
- {
- public static void Main()
- {
- using (var db = new BookShopContext())
- {
- //DbInitializer.ResetDatabase(db);
- //var command = Console.ReadLine();
- //Console.WriteLine(GetBooksByAgeRestriction(db, command));
- //Console.WriteLine(GetGoldenBooks(db));
- string result = GetBooksByPrice(db);
- Console.WriteLine(result);
- }
- }
- public static string GetBooksByAgeRestriction(BookShopContext context, string command)
- {
- int enumValue = -1;
- if (command.ToLower() == "minor")
- {
- enumValue = 0;
- }
- else if (command.ToLower() == "teen")
- {
- enumValue = 1;
- }
- else if (command.ToLower() == "adult")
- {
- enumValue = 2;
- }
- var titles = context.Books.Where(b => b.AgeRestriction == (AgeRestriction)enumValue)
- .Select(t => t.Title).OrderBy(x=>x).ToArray();
- return string.Join(Environment.NewLine, titles);
- }
- public static string GetGoldenBooks(BookShopContext context)
- {
- var goldenBooksTitles = context.Books
- .Where(b => b.EditionType == (EditionType)2 && b.Copies<5000)
- .OrderBy(b=>b.BookId)
- .Select(t => t.Title)
- .ToArray();
- return string.Join(Environment.NewLine, goldenBooksTitles);
- }
- public static string GetBooksByPrice(BookShopContext context)
- {
- using (context)
- {
- var books = context.Books
- .Where(b => b.Price > 40)
- .OrderByDescending(b => b.Price)
- .Select(b => new
- {
- Title = b.Title,
- Price = b.Price
- })
- .ToList();
- var sb = new StringBuilder();
- foreach (var b in books)
- {
- sb.AppendLine($"{b.Title} - ${b.Price}");
- }
- return sb.ToString().TrimEnd();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement