Advertisement
Guest User

Untitled

a guest
Nov 24th, 2018
25,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.43 KB | None | 0 0
  1. /*Program.cs*/
  2.  
  3. //
  4. // <<Your Name>>
  5. // U. of Illinois, Chicago
  6. // CS 341, Fall 2018
  7. // Project #06: Netflix database application
  8. //
  9.  
  10. using System;
  11. using System.Data;
  12. using System.Data.SqlClient;
  13.  
  14. namespace program
  15. {
  16.  
  17. class Program
  18. {
  19. //
  20. // Connection info for ChicagoCrimes database in Azure SQL:
  21. //
  22. static string connectionInfo = String.Format(@"
  23. Server=tcp:jhummel2.database.windows.net,1433;Initial Catalog=Netflix;
  24. Persist Security Info=False;User ID=student;Password=cs341!uic;
  25. MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;
  26. Connection Timeout=30;
  27. ");
  28.  
  29.  
  30. static void OutputNumMovies()
  31. {
  32. SqlConnection db = null;
  33.  
  34. try
  35. {
  36. db = new SqlConnection(connectionInfo);
  37. db.Open();
  38.  
  39. string sql = string.Format(@"
  40. SELECT Count(*) As NumMovies
  41. FROM Movies;
  42. ");
  43.  
  44. System.Console.WriteLine(sql); // debugging:
  45.  
  46. SqlCommand cmd = new SqlCommand();
  47. cmd.Connection = db;
  48. cmd.CommandText = sql;
  49.  
  50. object result = cmd.ExecuteScalar();
  51.  
  52. db.Close();
  53.  
  54. int numMovies = System.Convert.ToInt32(result);
  55.  
  56. System.Console.WriteLine("Number of movies: {0}", numMovies);
  57. }
  58. catch (Exception ex)
  59. {
  60. System.Console.WriteLine();
  61. System.Console.WriteLine("**Error: {0}", ex.Message);
  62. System.Console.WriteLine();
  63. }
  64. finally
  65. {
  66. // make sure we close connection no matter what happens:
  67. if (db != null && db.State != ConnectionState.Closed)
  68. db.Close();
  69. }
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. static void userInfo(string queryIN){
  77. SqlConnection db = null;
  78.  
  79.  
  80. try
  81. {
  82. db = new SqlConnection(connectionInfo);
  83.  
  84. db.Open();
  85. string sql = queryIN ;
  86. SqlCommand cmd = new SqlCommand();
  87. cmd.Connection = db;
  88. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  89. DataSet ds = new DataSet();
  90. cmd.CommandText = sql;
  91. adapter.Fill(ds);
  92. var rows = ds.Tables["TABLE"].Rows;
  93. //System.Console.WriteLine(rows.Count); //debugging
  94. if (rows.Count == 0){
  95. Console.WriteLine("** User not found...");
  96. }
  97.  
  98. foreach (DataRow row in rows) {
  99.  
  100. int id = System.Convert.ToInt32(row["UserID"]);
  101. string userName = System.Convert.ToString( row["UserName"] );
  102. string occu = System.Convert.ToString(row["Occupation"]);
  103. int numReviews = System.Convert.ToInt32(row["NumReviews"]);
  104. int oneStar = System.Convert.ToInt32(row["OneStar"]);
  105. int twoStar = System.Convert.ToInt32(row["TwoStar"]);
  106. int threeStar = System.Convert.ToInt32(row["ThreeStar"]);
  107. int fourStar = System.Convert.ToInt32(row["FourStar"]);
  108. int fiveStar = System.Convert.ToInt32(row["FiveStar"]);
  109.  
  110. if(row["AvgRating"] != System.DBNull.Value){
  111. float avgRating = System.Convert.ToSingle(row["AvgRating"]);
  112. Console.WriteLine("{0}\nUser id: {1}\nOccupation: {2}\nAvg rating: {3:0.00000}\nNum Reviews: {4}\n 1 star: {5}\n 2 stars: {6}\n 3 stars: {7}\n 4 stars: {8}\n 5 stars: {9}", userName, id, occu, avgRating, numReviews, oneStar, twoStar, threeStar, fourStar, fiveStar);
  113. }
  114. else {
  115. string avgRating = "N/A";
  116. Console.WriteLine("{0}\nUser id: {1}\nOccupation: {2}\nAvg rating: {3}\nNum Reviews: {4}\n 1 star: {5}\n 2 stars: {6}\n 3 stars: {7}\n 4 stars: {8}\n 5 stars: {9}", userName, id, occu, avgRating, numReviews, oneStar, twoStar, threeStar, fourStar, fiveStar);
  117. }
  118. }
  119. }
  120.  
  121. catch (Exception ex) {
  122. System.Console.WriteLine("Error: {0}", ex.Message);
  123. }
  124. finally { // success or failure, make sure connection is closed:
  125. if (db != null && db.State != ConnectionState.Closed) db.Close();
  126.  
  127. }
  128.  
  129. }
  130.  
  131.  
  132.  
  133. static void userInfoHelper(){
  134. System.Console.Write("Enter user id or name>>");
  135. string input = System.Console.ReadLine();
  136. System.Console.WriteLine();
  137. string query = "" ;
  138. int id;
  139.  
  140.  
  141. if(System.Int32.TryParse(input, out id)){
  142. query = string.Format(@"
  143. SELECT UserName, Users.UserID, Occupation, count(Rating) as NumReviews, AVG(Convert(float, Rating)) as AvgRating,
  144. coalesce(count(case when Rating = 1 then 1 end), 0) as OneStar,
  145. coalesce(count(case when Rating = 2 then 1 end), 0) as TwoStar,
  146. coalesce(count(case when Rating = 3 then 1 end), 0) as ThreeStar,
  147. coalesce(count(case when Rating = 4 then 1 end), 0) as FourStar,
  148. coalesce(count(case when Rating = 5 then 1 end), 0) as FiveStar
  149. From Users
  150. LEFT JOIN Reviews ON Users.UserID = Reviews.UserID
  151. Where Users.UserID = {0}
  152. Group By UserName, Users.UserID, Occupation
  153. ", id);
  154. }
  155.  
  156. else {
  157. input = input.Replace("'", "''");
  158. query = string.Format(@"
  159. SELECT UserName, Users.UserID, Occupation, count(Rating) as NumReviews, AVG(Convert(float, Rating)) as AvgRating,
  160. coalesce(count(case when Rating = 1 then 1 end), 0) as OneStar,
  161. coalesce(count(case when Rating = 2 then 1 end), 0) as TwoStar,
  162. coalesce(count(case when Rating = 3 then 1 end), 0) as ThreeStar,
  163. coalesce(count(case when Rating = 4 then 1 end), 0) as FourStar,
  164. coalesce(count(case when Rating = 5 then 1 end), 0) as FiveStar
  165. From Users
  166. LEFT JOIN Reviews ON Users.UserID = Reviews.UserID
  167. Where Users.UserName = '{0}'
  168. Group By UserName, Users.UserID, Occupation
  169. ", input);
  170.  
  171. }
  172. userInfo(query);
  173.  
  174. }
  175.  
  176.  
  177.  
  178. static void movieInfo(string queryIN){
  179. SqlConnection db = null;
  180.  
  181.  
  182. try
  183. {
  184. db = new SqlConnection(connectionInfo);
  185.  
  186. db.Open();
  187. string sql = queryIN ;
  188.  
  189.  
  190.  
  191. SqlCommand cmd = new SqlCommand();
  192. cmd.Connection = db;
  193. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  194. DataSet ds = new DataSet();
  195. cmd.CommandText = sql;
  196. adapter.Fill(ds);
  197. var rows = ds.Tables["TABLE"].Rows;
  198. //System.Console.WriteLine(rows.Count); //debugging
  199. if (rows.Count == 0){
  200. Console.WriteLine("** Movie not found...");
  201. }
  202.  
  203. foreach (DataRow row in rows) {
  204.  
  205.  
  206.  
  207.  
  208. int id = System.Convert.ToInt32(row["MovieID"]);
  209. string movieName = System.Convert.ToString( row["MovieName"] );
  210. int year = System.Convert.ToInt32(row["MovieYear"]);
  211. int numReviews = System.Convert.ToInt32(row["NumReviews"]);
  212. if(row["AvgRating"] != System.DBNull.Value){
  213. float avgRating = System.Convert.ToSingle(row["AvgRating"]);
  214. Console.WriteLine("{0}\n'{1}'\nYear: {2}\nNum Reviews: {3}\nAvg rating: {4:0.00000}\n\n", id, movieName, year, numReviews, avgRating);
  215. }
  216. else{
  217. string avgRating = "N/A";
  218. Console.WriteLine("{0}\n'{1}'\nYear: {2}\nNum Reviews: {3}\nAvg rating: {4}\n\n", id, movieName, year, numReviews, avgRating);
  219. }
  220. }
  221. }
  222.  
  223. catch (Exception ex) {
  224. System.Console.WriteLine("Error: {0}", ex.Message);
  225. }
  226. finally { // success or failure, make sure connection is closed:
  227. if (db != null && db.State != ConnectionState.Closed) db.Close();
  228.  
  229. }
  230.  
  231. }
  232.  
  233. static void movieInfoHelper(){
  234.  
  235. System.Console.Write("Enter movie id or part of movie name>>");
  236. string input = System.Console.ReadLine();
  237. System.Console.WriteLine();
  238. string query = "" ;
  239. int id;
  240.  
  241.  
  242. if(System.Int32.TryParse(input, out id)){
  243. query = string.Format(@"
  244. SELECT Movies.MovieID,MovieName, MovieYear, count(Rating) as NumReviews, AVG(Convert(float,Rating)) as AvgRating
  245. From Movies
  246. LEFT JOIN Reviews ON Movies.MovieID = Reviews.MovieID
  247. Where Movies.MovieID = {0}
  248. Group By MovieName, MovieYear, Movies.MovieID
  249. Order BY MovieName asc
  250. ", id);
  251. }
  252. else {
  253. input = input.Replace("'", "''");
  254. query = string.Format(@"
  255. SELECT Movies.MovieID,MovieName, MovieYear, count(Rating) as NumReviews, AVG(Convert(float,Rating)) as AvgRating
  256. From Movies
  257. LEFT JOIN Reviews ON Movies.MovieID = Reviews.MovieID
  258. Where Movies.MovieName LIKE'%{0}%'
  259. Group By MovieName, MovieYear, Movies.MovieID
  260. Order BY MovieName asc
  261. ", input);
  262.  
  263. }
  264. movieInfo(query);
  265.  
  266. }
  267.  
  268.  
  269.  
  270.  
  271. static void top10Movies()
  272. {
  273.  
  274. SqlConnection db = null;
  275.  
  276. try
  277. {
  278.  
  279.  
  280. db = new SqlConnection(connectionInfo);
  281. db.Open();
  282. string sql = string.Format(@"
  283. SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY (SELECT 10)) Rank, Movies.MovieID, count(Rating) as NumReviews, AVG(Convert(float,Rating)) as AvgRating, MovieName
  284. From Movies
  285. Inner JOIN Reviews ON Movies.MovieID = Reviews.MovieID
  286. Group By Movies.MovieName , Movies.MovieID
  287. Order by AvgRating desc, Movies.Moviename asc;
  288. ");
  289.  
  290.  
  291. SqlCommand cmd = new SqlCommand();
  292. cmd.Connection = db;
  293. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  294. DataSet ds = new DataSet();
  295. cmd.CommandText = sql;
  296. adapter.Fill(ds);
  297. var rows = ds.Tables["TABLE"].Rows;
  298. //System.Console.WriteLine(rows.Count); //rebugging
  299.  
  300.  
  301. Console.WriteLine("Rank\tMovieID\tNumReviews\tAvgRating\tMovieName");
  302.  
  303. foreach (DataRow row in rows) {
  304.  
  305. int rank = System.Convert.ToInt32(row["Rank"]);
  306. int id = System.Convert.ToInt32(row["MovieID"]);
  307. int numReviews = System.Convert.ToInt32(row["NumReviews"]);
  308. float avgRating = System.Convert.ToSingle(row["AvgRating"]);
  309. string movieName = System.Convert.ToString( row["MovieName"] );
  310.  
  311.  
  312. Console.WriteLine("{0}\t{1}\t{2}\t\t{3:0.00000}\t\t'{4}'", rank, id,numReviews,avgRating,movieName);
  313.  
  314. }
  315. }
  316.  
  317. catch (Exception ex) {
  318. System.Console.WriteLine("Error: {0}", ex.Message);
  319. }
  320. finally { // success or failure, make sure connection is closed:
  321. if (db != null && db.State != ConnectionState.Closed) db.Close();
  322. }
  323. }
  324.  
  325.  
  326. static string GetUserCommand()
  327. {
  328. System.Console.WriteLine();
  329. System.Console.WriteLine("What would you like?");
  330. System.Console.WriteLine("m. movie info");
  331. System.Console.WriteLine("t. top-10 info");
  332. System.Console.WriteLine("u. user info");
  333. System.Console.WriteLine("x. exit");
  334. System.Console.Write(">> ");
  335.  
  336. string cmd = System.Console.ReadLine();
  337.  
  338. return cmd.ToLower();
  339. }
  340.  
  341.  
  342. //
  343. // Main:
  344. //
  345. static void Main(string[] args)
  346. {
  347. System.Console.WriteLine("** Netflix Database App **");
  348.  
  349. string cmd = GetUserCommand();
  350.  
  351. while (cmd != "x")
  352. {
  353. //if(cmd == "m"){
  354. //System.Console.WriteLine();
  355. // OutputNumMovies();
  356. //}
  357. if(cmd== "t"){
  358. System.Console.WriteLine();
  359. top10Movies();
  360. }
  361. if(cmd == "m"){
  362. System.Console.WriteLine();
  363. movieInfoHelper();
  364.  
  365. }
  366. if(cmd == "u"){
  367. System.Console.WriteLine();
  368. userInfoHelper();
  369. }
  370. cmd = GetUserCommand();
  371. }
  372.  
  373. System.Console.WriteLine();
  374. System.Console.WriteLine("** Done **");
  375. System.Console.WriteLine();
  376. }
  377.  
  378. }//class
  379. }//namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement