-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path772629.hs
More file actions
478 lines (411 loc) · 18.9 KB
/
772629.hs
File metadata and controls
478 lines (411 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
-- ||
-- || Discrete Mathematics and Functional Programming
-- || Haskell Coursework: Film Review
-- || Student Number: 772629
-- ||
-- ||
-- || Variables
-- ||
import Data.List
import Data.Char
-- Film Type Defines
type Title = String
type Director = String
type Year = Int
type Fanname = String
type Fans = [Fanname]
type Film = (Title, Director, Year, Fans)
-- ||
-- || Core Functionality
-- ||
-- Returns all the films available in the database
getAllFilms :: [Film]
getAllFilms = testDatabase
-- Adds a new film to the database
addNewFilm :: String -> String -> Int -> [Film] -> [Film]
addNewFilm ti di ye db = db++[(ti, di, ye, [])]
-- Returns all films as a formatted string
filmsAsString :: [Film] -> String
filmsAsString [] = ""
filmsAsString ((ti, di, yr, fa):xs) = "\nTitle: " ++ ti ++ "\nDirector: " ++ di ++ "\nYear: " ++ show ( yr ) ++ "\nFan Total: " ++ show( length ( fa ) ) ++ "\n" ++ filmsAsString xs
-- Converts all fans to a formatted string (different data type [Fans])
fansAsString :: [Fans] -> String
fansAsString [] = ""
fansAsString (x:xs) = "\nFan Name: " ++ intercalate "\nFan Name: " x ++ fansAsString xs
-- Converts all fans to a formatted string (different data type [Fanname])
fanNameAsString :: [Fanname] -> String
fanNameAsString [] = ""
fanNameAsString (x:xs) = "\nFan Name: " ++ intercalate "\n" [x] ++ fanNameAsString xs
-- Returns all films released after the year (Not Including)
filmsReleasedAfterYear :: Int -> [Film] -> [Film]
filmsReleasedAfterYear year db = [ (ti, di, yr, fan) | (ti, di, yr, fan) <- db, yr > year ]
-- Returns all films that a user is a fan of
fanFilms :: String -> [Film] -> [Film]
fanFilms user db = [(ti, di, yr, fan) | (ti, di, yr, fan) <- db, elem user fan ]
-- Returns all fans of a particular film
fansOfFilm :: String -> [Film] -> [Fans]
fansOfFilm film db = [ fan | (ti, di, yr, fan) <- db, film == ti ]
-- Returns all film once a user has become a fan
addFan :: String -> String -> [Film] -> [Film]
addFan title name db = [ if ti == title && elem name fa == False then (ti, di, yr, fa++[name]) else (ti, di, yr, fa) | (ti, di, yr, fa) <- db ]
-- Fans of all films directed by a particular directors
fansOfDirector :: String -> [Film] -> [Fans]
fansOfDirector director ((ti, di, yr, fa):xs)
| length xs == 0 = []
| director == di = fa : fansOfDirector director xs
| otherwise = fansOfDirector director xs
-- Returns a list of fans without their duplicates
fansOfDirectorNoRepeats:: String -> [Film] -> [Fanname]
fansOfDirectorNoRepeats director db = nub(concat (fansOfDirector director db))
-- Checks if user is a fan of a specific film
checkIfFan :: String -> [Film] -> Bool
checkIfFan user [(ti, di, yr, fa)] = elem user fa
-- Gets the count of number of films by a director that a specific fan is fan of
getCountFilmsByDirWithFan :: String -> Director -> [Film] -> Int
getCountFilmsByDirWithFan fan director ((ti, di, yr, fa):xs)
| director == di && checkIfFan fan [(ti, di, yr, fa)] = 1 + getCountFilmsByDirWithFan fan di xs
| length xs > 0 = getCountFilmsByDirWithFan fan director xs
| otherwise = 0
-- Returns a list of directors without their duplicates
getAllDirectorsWithoutRepeats :: [Film] -> [Director]
getAllDirectorsWithoutRepeats db = nub(getAllDirectors db)
-- Returns a list of all the directors within a database
getAllDirectors :: [Film] -> [Director]
getAllDirectors ((ti, di, yr, fa):xs)
| length xs > 0 = [di] ++ getAllDirectors xs
| otherwise = []
-- Returns a list of tuples with director & count of times a user is a fan
directorsWithFanCounter :: String -> [Film] -> [(Director, Int)]
directorsWithFanCounter fan db = [ (dir, (getCountFilmsByDirWithFan fan dir db)) | dir <- getAllDirectorsWithoutRepeats(db) ]
-- Returns a formatted list of the tuples of director and count of times a user is a fan
directorsWithFanToString :: [(Director, Int)] -> String
directorsWithFanToString ((dir, cou):xs)
| length xs > 0 = "Director: " ++ dir ++ " - Count: "++ show(cou) ++ "\n" ++ directorsWithFanToString xs
| length xs == 0 = "Director: " ++ dir ++ " - Count: "++ show(cou)
| otherwise = ""
-- Check if film exists
checkFilmExists :: String -> [Film] -> Bool
checkFilmExists film ((ti, di, yr, fa):xs)
| ti == film = True
| xs == [] = False
| otherwise = checkFilmExists film xs
-- Check if director exists
checkDirectorExists :: String -> [Film] -> Bool
checkDirectorExists dir ((ti, di, yr, fa):xs)
| di == dir = True
| xs == [] = False
| otherwise = checkDirectorExists dir xs
-- Removes all symbols / non letters from a string
removeSymbols :: String -> String
removeSymbols = filter isLetter
-- ||
-- || Interface Functionality
-- ||
-- Main Functionality
main :: IO ()
main = do
putStrLn("========================================================")
putStrLn("Film Database")
loadedFile <- readFile "test-data.txt"
let filmsDatabase = read loadedFile
putStrLn("\nSuccesfully loaded "++ show(length filmsDatabase) ++" films!")
putStrLn("========================================================\n")
putStr("Please input your name: ")
name <- getLine
putStrLn ("\n")
inMainMenu name filmsDatabase
-- Returns an error message for incorrect input
inSendErrorInput :: String -> String
inSendErrorInput "int" = "\n[Error]: The value entered was not the expected value (Number / Int)\n[Error]: Option is being reset, please try again.\n"
inSendErrorInput "string" = "\n[Error]: The value entered was not the expected value (Word / String)\n[Error]: Option is being reset, please try again.\n"
inSendErrorInput "input" = "\n[Error]: There was no input given!\n[Error]: Option is being reset, please try again.\n"
inSendErrorInput "year" = "\n[Error]: The year is not valid, enter a value after year 1900 - 2100.!\n[Error]: Option is being reset, please try again.\n"
inSendErrorInput "option" = "\n[Error]: An incorrect action was given, please try again!\n"
inSendErrorInput _ = ""
-- Main Menu Options
inMainMenu :: String -> [Film] -> IO()
inMainMenu user filmDB = do
putStrLn("========================================================")
putStrLn("Welcome to the Film Database, " ++ user ++".\nEnter the number for the required option.")
putStrLn(" 1 | Add a film")
putStrLn(" 2 | Display all films within the database")
putStrLn(" 3 | Display films released after a certain year")
putStrLn(" 4 | Get all films you're a fan of")
putStrLn(" 5 | Get all the fans of a particular film")
putStrLn(" 6 | Assign yourself as a fan to a particular film")
putStrLn(" 7 | Check all fans of a particular director")
putStrLn(" 8 | Get all directors that you're a fan of with count")
putStrLn(" 0 | Save and Exit")
putStrLn("========================================================\n")
putStr("Please insert your option: ")
option <- getLine
putStrLn("\n")
inAction option user filmDB
-- Actions for the system
inAction :: String -> String -> [Film] -> IO ()
-- Save the database and exit the UI
inAction "0" user filmDB = inSaveAndExit user filmDB
-- Add a new film to the database
inAction "1" user filmDB = inAddNewFilm user filmDB
-- Display all the films within the database
inAction "2" user filmDB = inGetAllFilms user filmDB
-- Get all the films that were released after a certain date
inAction "3" user filmDB = inGetAllFilmsAfter user filmDB
-- Get all the films with a particular fan
inAction "4" user filmDB = inGetFanFilms user filmDB
-- Get all the fans of a particular film
inAction "5" user filmDB = inGetFansOfFilm user filmDB
-- Assign a fan to a particular film
inAction "6" user filmDB = inAddFan user filmDB
-- All fans of films directed by a particular director
inAction "7" user filmDB = inFansOfDirector user filmDB
-- All directors & no. of their films that a particular fan is is a fan of
inAction "8" user filmDB = inFilmsByAllDirectors user filmDB
-- Display errors if the input is invalid!
inAction _ user filmDB = do
putStrLn (inSendErrorInput "option" )
inMainMenu user filmDB
-- UI Function to save and exit the UI
inSaveAndExit :: String -> [Film] -> IO()
inSaveAndExit user filmDB =
do
putStrLn("========================================================")
putStrLn("Saving the Database")
-- Avoids the issue with Parse Error, Force writing ALL of the filmDB before continuing.
length filmDB `seq` writeFile "test-data.txt" ( show filmDB )
putStrLn("Saved the Database successfully")
putStrLn("Exiting the system")
putStrLn("========================================================\n")
-- UI function to add a new film to the database
inAddNewFilm :: String -> [Film] -> IO()
inAddNewFilm user filmDB =
do
putStrLn("========================================================")
putStrLn("Add new film to database\n")
putStr("Enter the film title: ")
filmTitl <- getLine
let filmTitle = removeSymbols filmTitl
putStr("Enter the film director: ")
filmDire <- getLine
let filmDirector = removeSymbols filmDire
putStr("Enter the film release date: ")
filmYear <- getLine
if filmTitle == "" && filmDirector == "" && filmYear == ""
then do
putStrLn ("Returning you to the main menu")
inMainMenu user filmDB
else do
if filmTitle == "" || filmDirector == "" || filmYear == ""
then do
putStrLn(inSendErrorInput "input")
inAddNewFilm user filmDB
else do
-- To validate the films; reads the filmYear input, converts to an Integer & String, if the film exists with nothing inside the String, it's a valid input / number! otherwise just catch and display an error.
case reads filmYear :: [(Integer, String)] of
[(n, "")] -> do
let year = (read filmYear :: Int)
if year >= 1990 && year <= 2100
then do
putStrLn("Adding your film to the database")
let filmDBnew = addNewFilm filmTitle filmDirector year filmDB
putStrLn("========================================================\n")
inMainMenu user filmDBnew
else do
putStrLn(inSendErrorInput "year")
inAddNewFilm user filmDB
_ -> do
putStrLn( inSendErrorInput "int" )
inAddNewFilm user filmDB
-- UI Function to return all the films as a string
inGetAllFilms :: String -> [Film] -> IO()
inGetAllFilms user filmDB =
do
putStrLn("========================================================")
putStrLn("View all the films within the database")
putStrLn( filmsAsString filmDB )
putStrLn("========================================================\n")
inMainMenu user filmDB
-- UI Functions to get all the films released after a certain date
inGetAllFilmsAfter :: String -> [Film] -> IO()
inGetAllFilmsAfter user filmDB =
do
putStrLn("========================================================")
putStrLn("View all films released after a certain date.")
putStr("Which year: ")
year <- getLine
putStrLn("\n")
if year == ""
then do
putStrLn(inSendErrorInput "input")
inGetAllFilmsAfter user filmDB
else do
case reads year :: [(Integer, String)] of
[(n, "")] -> do
let filmYear = (read year :: Int)
if filmYear >= 1990 && filmYear <= 2100
then do
putStrLn("All films released after "++ show( filmYear ))
let result = filmsReleasedAfterYear filmYear filmDB
if result /= []
then do
putStrLn(filmsAsString( result ))
else do
putStrLn("There are no films released after "++ show( filmYear ) ++ "\n")
putStrLn("========================================================\n")
inMainMenu user filmDB
else do
putStrLn(inSendErrorInput "year")
inGetAllFilmsAfter user filmDB
_ -> do
putStrLn(inSendErrorInput "int")
inGetAllFilmsAfter user filmDB
-- UI Function to return all the fans of a specific film
inGetFanFilms :: String -> [Film] -> IO()
inGetFanFilms user filmDB =
do
putStrLn("========================================================")
putStrLn("View all the films with a particular fan.")
putStrLn("\nAll films with "++ user ++" as a fan\n")
let result = fanFilms user filmDB
if result /= []
then do
putStrLn( filmsAsString( fanFilms user filmDB ) )
else do
putStrLn("There are no films with "++ user ++" as a fan!\n")
putStrLn("========================================================\n")
inMainMenu user filmDB
-- UI Function for returning all the fans of a specific film
inGetFansOfFilm :: String -> [Film] -> IO()
inGetFansOfFilm user filmDB =
do
putStrLn("========================================================")
putStrLn("View all the fans of a particular film.")
putStr("Film Name: ")
film <- getLine
if film == ""
then do
putStrLn(inSendErrorInput "input")
inGetFansOfFilm user filmDB
else do
if checkFilmExists film filmDB == False
then do
putStrLn("[Error] The film "++ film ++" doesn't exist in the database")
else do
putStrLn("\nAll fans of a particular film "++ film ++"\n")
putStrLn( fansAsString ( fansOfFilm film filmDB ) )
putStrLn("========================================================\n")
inMainMenu user filmDB
-- UI Function for add a user as a fan to a specific film
inAddFan :: String -> [Film] -> IO()
inAddFan user filmDB =
do
putStrLn("========================================================")
putStrLn("Add a particular user as a fan for a film.")
putStr("Film Name: ")
film <- getLine
if film == ""
then do
putStrLn(inSendErrorInput "input")
inAddFan user filmDB
else do
if checkFilmExists film filmDB == False
then do
putStrLn("[Error] The film "++ film ++" doesn't exist, did you mean something else?")
putStrLn("========================================================\n")
inMainMenu user filmDB
else do
let filmDBnew = addFan film user filmDB
putStrLn("\nAdded "++ user ++" to the fan list for "++ film ++"\n")
putStrLn( filmsAsString filmDBnew )
putStrLn("========================================================\n")
inMainMenu user filmDBnew
-- UI Function for returning the fans of a director
inFansOfDirector :: String -> [Film] -> IO()
inFansOfDirector user filmDB =
do
putStrLn("========================================================")
putStrLn("View all the fans of a particular director.")
putStr("Director Name: ")
director <- getLine
if director == ""
then do
putStrLn(inSendErrorInput "input")
inFansOfDirector user filmDB
else do
if checkDirectorExists director filmDB == False
then do
putStrLn("[Error] The director does not exist in our database!")
else do
putStrLn("\nAll fans of director "++ director ++"\n")
putStrLn( fanNameAsString ( fansOfDirectorNoRepeats director filmDB ) )
putStrLn("========================================================\n")
inMainMenu user filmDB
-- UI Function for returning the directors with the total fans that like their films
inFilmsByAllDirectors :: String -> [Film] -> IO()
inFilmsByAllDirectors user filmDB =
do
putStrLn("========================================================")
putStrLn("View all directors with number of a fan is liking their films.")
putStrLn("\nCount of times for "++ user ++"\n")
putStrLn( directorsWithFanToString( directorsWithFanCounter user filmDB ))
putStrLn("========================================================\n")
inMainMenu user filmDB
-- UI functions to return a custom error message before returning to the main menu
inErrorMessage :: String -> [Film] -> String -> IO()
inErrorMessage user filmDB error =
do
putStrLn(error)
inMainMenu user filmDB
-- ||
-- || Demo Functionality
-- ||
demo :: Int -> IO ()
-- All films after adding "Alien: Covenant" by "Ridley Scott" 2017
demo 1 = putStrLn( filmsAsString( addNewFilm "Alien: Covenant" "Ridley Scott" 2017 testDatabase ) )
-- Returning all films in a formatted way
demo 2 = putStrLn( filmsAsString testDatabase )
-- All films that were released after 2008
demo 3 = putStrLn( filmsAsString( filmsReleasedAfterYear 2008 testDatabase ) )
-- All films that "Liz" is a fan of
demo 4 = putStrLn( filmsAsString( fanFilms "Liz" testDatabase ) )
-- All fans of the movie "Jaws"
demo 5 = putStrLn( fansAsString ( fansOfFilm "Jaws" testDatabase ) )
-- All films after "Liz" says she becomes fan of "The Fly"
demo 6 = putStrLn( filmsAsString ( addFan "The Fly" "Liz" testDatabase ) )
-- All films after "Liz" says she becomes fan of "Avatar"
demo 66 = putStrLn( filmsAsString ( addFan "Avatar" "Liz" testDatabase ) )
-- All fans of films directed by "James Cameron" (Without Duplicates)
demo 7 = putStrLn( fanNameAsString ( fansOfDirectorNoRepeats "James Cameron" testDatabase ))
-- All directors & no. of their films that "Liz" is a fan of (Without Duplicates)
demo 8 = putStrLn( directorsWithFanToString( directorsWithFanCounter "Liz" testDatabase ))
-- Catch any incorrect entries for the demo
demo _ = putStrLn "Invalid Demo Requested"
-- ||
-- || Database used to demonstrate the functionality of the system
-- ||
testDatabase :: [Film]
testDatabase = [("Blade Runner", "Ridley Scott", 1982, ["Zoe", "Heidi", "Jo", "Kate", "Emma", "Liz", "Sam", "Olga", "Tim"]),
("The Fly", "David Cronenberg", 1986, ["Garry", "Dave", "Zoe", "Kevin", "Emma"]),
("Body Of Lies", "Ridley Scott", 2008, ["Bill", "Olga", "Tim", "Zoe", "Paula"]),
("Avatar", "James Cameron", 2009, ["Dave", "Amy", "Liz"]),
("Titanic", "James Cameron", 1997, ["Zoe", "Emma", "Paula", "Liz", "Olga", "Dave"]),
("The Departed", "Martin Scorsese", 2006, ["Wally", "Liz", "Kevin", "Tim", "Emma"]),
("Aliens", "Ridley Scott", 1986, ["Dave", "Garry", "Liz", "Sam", "Wally", "Kate", "Zoe"]),
("Kingdom Of Heaven", "Ridley Scott", 2005, ["Jo", "Wally", "Emma"]),
("Prometheus", "Ridley Scott", 2012, ["Kevin", "Tim", "Emma", "Jo", "Liz"]),
("E.T. The Extra-Terrestrial", "Steven Spielberg", 1982, ["Dave", "Amy", "Garry", "Ian", "Neal"]),
("Bridge of Spies", "Steven Spielberg", 2015, ["Wally", "Sam", "Dave", "Neal"]),
("Jaws", "Steven Spielberg", 1975, ["Dave", "Jo", "Zoe", "Wally", "Emma", "Kate"]),
("The Martian", "Ridley Scott", 2015, ["Wally", "Sam", "Dave", "Jo", "Jenny", "Kate", "Emma", "Olga"]),
("The BFG", "Steven Spielberg", 2016, ["Sam", "Wally", "Dave", "Jo", "Kate"]),
("The Shawshank Redemption", "Frank Darabont", 1994, ["Dave", "Amy", "Bill", "Garry", "Ian", "Neal", "Kate", "Jenny", "Zoe"]),
("Gladiator", "Ridley Scott", 2000, ["Olga", "Neal", "Kate", "Heidi", "Bill", "Sam", "Zoe"]),
("The Green Mile", "Frank Darabont", 1999, ["Kevin", "Tim", "Emma", "Heidi"]),
("True Lies", "James Cameron", 1994, ["Sam", "Dave"]),
("Super 8", "J J Abrams", 2011, ["Kevin", "Tim", "Emma", "Olga", "Heidi"]),
("Minority Report", "Steven Spielberg", 2002, ["Kevin", "Kate", "Tim", "Emma", "Olga", "Jenny", "Zoe"]),
("War Horse", "Steven Spielberg", 2011, ["Garry", "Bill", "Olga", "Jo", "Wally", "Emma", "Tim", "Kate", "Zoe"]),
("Silence", "Martin Scorsese", 2016, ["Wally", "Emma", "Tim", "Heidi", "Bill", "Olga", "Jo"]),
("The Terminal", "Steven Spielberg", 2004, ["Kate", "Dave", "Jo", "Wally", "Emma"]),
("Star Wars: The Force Awakens", "J J Abrams", 2015, ["Emma", "Wally", "Zoe", "Kate", "Bill", "Dave", "Liz", "Jo"]),
("Hugo", "Martin Scorsese", 2011, ["Wally", "Sam"])]