MeGaDeTH_91

1. Age Restriction

Nov 23rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. namespace BookShop
  2. {
  3. using BookShop.Data;
  4. using BookShop.Initializer;
  5. using BookShop.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Text;
  11.  
  12. public class StartUp
  13. {
  14. public static void Main()
  15. {
  16. using (var context = new BookShopContext())
  17. {
  18. //DbInitializer.ResetDatabase(db);
  19.  
  20. //1.Age Restriction
  21. string command = Console.ReadLine();
  22. string ageRestricted = GetBooksByAgeRestriction(context, command);
  23. Console.WriteLine(ageRestricted);
  24. }
  25. }
  26.  
  27. public static string GetBooksByAgeRestriction(BookShopContext context, string command)
  28. {
  29. var books = context.Books.OrderBy(x => x.Title).ToList();
  30.  
  31. string result = string.Empty;
  32.  
  33. int compare = -1;
  34.  
  35. switch (command.ToLower())
  36. {
  37. case "minor":
  38. compare = 0;
  39. var minors = context.Books
  40. .Where(x => (int)x.AgeRestriction == compare)
  41. .OrderBy(x => x.Title)
  42. .Select(x => x.Title)
  43. .ToList();
  44.  
  45. result = string.Join(Environment.NewLine, minors);
  46.  
  47. break;
  48. case "teen":
  49. compare = 1;
  50. var teens = context.Books
  51. .Where(x => (int)x.AgeRestriction == compare)
  52. .OrderBy(x => x.Title)
  53. .Select(x => x.Title)
  54. .ToList();
  55.  
  56. result = string.Join(Environment.NewLine, teens);
  57.  
  58. break;
  59. case "adult":
  60. compare = 2;
  61. var adults = context.Books
  62. .Where(x => (int)x.AgeRestriction == compare)
  63. .OrderBy(x => x.Title)
  64. .Select(x => x.Title)
  65. .ToList();
  66.  
  67. result = string.Join(Environment.NewLine, adults);
  68. break;
  69. default:
  70. break;
  71. }
  72. return result;
  73. }
  74.  
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment