Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. namespace BookShop
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using BookShop.Data;
  8. using BookShop.Initializer;
  9. using BookShop.Models;
  10.  
  11. public class StartUp
  12. {
  13. public static void Main()
  14. {
  15. using (var db = new BookShopContext())
  16. {
  17. //DbInitializer.ResetDatabase(db);
  18.  
  19. //var command = Console.ReadLine();
  20. //Console.WriteLine(GetBooksByAgeRestriction(db, command));
  21.  
  22. //Console.WriteLine(GetGoldenBooks(db));
  23.  
  24. string result = GetBooksByPrice(db);
  25.  
  26. Console.WriteLine(result);
  27. }
  28. }
  29.  
  30. public static string GetBooksByAgeRestriction(BookShopContext context, string command)
  31. {
  32. int enumValue = -1;
  33. if (command.ToLower() == "minor")
  34. {
  35. enumValue = 0;
  36. }
  37. else if (command.ToLower() == "teen")
  38. {
  39. enumValue = 1;
  40. }
  41. else if (command.ToLower() == "adult")
  42. {
  43. enumValue = 2;
  44. }
  45.  
  46. var titles = context.Books.Where(b => b.AgeRestriction == (AgeRestriction)enumValue)
  47. .Select(t => t.Title).OrderBy(x=>x).ToArray();
  48.  
  49. return string.Join(Environment.NewLine, titles);
  50. }
  51.  
  52. public static string GetGoldenBooks(BookShopContext context)
  53. {
  54. var goldenBooksTitles = context.Books
  55. .Where(b => b.EditionType == (EditionType)2 && b.Copies<5000)
  56. .OrderBy(b=>b.BookId)
  57. .Select(t => t.Title)
  58. .ToArray();
  59.  
  60. return string.Join(Environment.NewLine, goldenBooksTitles);
  61. }
  62.  
  63. public static string GetBooksByPrice(BookShopContext context)
  64. {
  65. using (context)
  66. {
  67. var books = context.Books
  68. .Where(b => b.Price > 40)
  69. .OrderByDescending(b => b.Price)
  70. .Select(b => new
  71. {
  72. Title = b.Title,
  73. Price = b.Price
  74. })
  75. .ToList();
  76.  
  77. var sb = new StringBuilder();
  78. foreach (var b in books)
  79. {
  80. sb.AppendLine($"{b.Title} - ${b.Price}");
  81. }
  82. return sb.ToString().TrimEnd();
  83. }
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement