forked from werhereitacademy/Python_Modul_Week_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_2.py
More file actions
218 lines (135 loc) · 6.92 KB
/
Question_2.py
File metadata and controls
218 lines (135 loc) · 6.92 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
""" Question 2: Film Library Management System Project
Project Description: This project aims to create an application that will help the user manage their movie collection. Users can add, edit, delete movies and view their collection.
Data Structures Used: Dictionaries (to store movies and related information), lists (to display movie collection)
Basic Functions:
1-Create a movie data by taking information such as movie name, director, release year and genre from the user and store it in a dictionary.
2-Give the user the option to edit or delete a movie. (For this, a suitable function must be written for whatever data they want to change about the movie.)
3-Allow the user to view their collection. List all movies or filter by criteria such as genre or year of release.
4-Save the movie data in a file and restore this data when you start the program.
# """
import os.path # importing os module
import json # importing json module
from pprint import pprint # (pretty-print) This module indents and smooths your output
#film={}
film = {
1:{'name': 'John', "Derector": "Tomas","Year":1980,"Genre" : "Trillend" },
2:{'name': 'John', "Derector": "Tomas","Year":1981 ,"Genre" : "Animasyon" },
3:{'name': 'John', "Derector": "Tomas","Year":1982 ,"Genre" : "Family" },
4:{'name': 'John', "Derector": "Tomas","Year":1983 ,"Genre" : "Trillend" }
}
file_name = "Film.json" # It can also be ".txt"
fNumber = 0
#There is a nested dictionary structure here, each film is recorded under a number(from 1 to infinity),
#That is, the first key number value is again the dictionary.
def addFilm(fName :str,fDerector: str ,fYear : int, fGenre :str) -> None:
film[fNumber] = {
"Name" :fName,
"Derector":fDerector,
"Year" :fYear,
"Genre" : fGenre
}
print(F" Your film {fName} input has been made successfully.")
uploodDictJsonFile(file_name)
def updateFilm(choise :int,fName :None,fDerector: None ,fYear : None, fGenre :None)-> None:
if choise in film:
if fName is not None:
film[choise].update({"Name" : fName})
#film[choise][fName] = fName
if fDerector is not None :
film[choise].update({"Derector" :fDerector})
if fYear is not None :
film[choise].update({"Year" :fYear})
if fGenre is not None :
film[choise].update({"Genre" :fGenre})
print("An update has been made.")
uploodDictJsonFile(file_name)
def searchFilm(chose):
if chose in film:
print (F"{chose} was found {film[chose]}")
else:
print("Item not found.")
def displayFilm():
pprint(film, indent=4) # With this module I will get the output indented and smooth.
#print(film) # This is an normaly format
#print(json.dumps(film, indent=4)) # With this modele I will get the output in dictionaru format.
def removeFilm(chose):
if chose in film:
del film["chose"]
else :
print("Item not found.")
#This function converts the data in dictionary format to json file format.
def uploodDictJsonFile(file_name):
with open(file_name,"a") as f_json :
json.dump(film,f_json,indent=4)
def checkJsonFile():
if os.path.isfile(file_name):
print("\ndosya mevcut.\n")
with open(file_name,"r") as f_json:
mydict = json.load(f_json)
print(mydict)
else:
print("\nFalse, dosya mevcut değil.")
with open("Film.json", "w",encoding = 'utf-8') as f_json:
print("\nDosya olusturuldu.\n")
print("\nFilim ler sozlugumdeki veriler json dosyasina ekleniyor...\n")
json.dump(film,f_json,indent=4)
"""When the program runs automatically, the first function that will open is our main menu."""
def mainFilmMenu():
checkJsonFile()
while True :
print(
"""
---- Film Library Management System Project----
1. Add film
2. Update film
3. Delete film
4. Search film
5. list film
0. exit
"""
)
select = int(input("Please select the option you want to make between 1-5 :"))
match select :
case 1:
print("Your choice is : (1) Film add \n")
print("Please fill in the required information to add a movie.")
global fNumber # If we do not specify this variable as "global" we get an error.
fNumber+=1 # Automatically generates film number
#These are all film library input data.
fName =input("Enter name of the film :")
fDerector = input("Enter name of the filmdrector :")
fYear = input("Enter release year of the film :")
fGenre = input("Enter genre of the film :")
addFilm(fName,fDerector,fYear,fGenre) #We call the add film function.
case 2 :
print("Your choice is : (2) Film update , Please enter your current information..")
#If there is no movie in the entered number, the selection continues until the correct choice is made.
while True:
choise = int(input("Enter number of film you want to change :"))
if choise in film:
print (F"{choise} number of film wil be update ")
fName =input("Enter name of the film :") or None
fDerector = input("Enter name of the filmdrector :") or None
fYear = input("Enter release year of the film :") or None
fGenre = input("Enter genre of the film :") or None
updateFilm(choise,fName,fDerector,fYear,fGenre)
break
else :
filmList = film.keys()
print(filmList , "Enter one of the film numbers among them.")
case 3:
choise = input("Enter jour film code (1 - 5)")
removeFilm(choise)
case 4 :
choise = input("Enter jour film code :")
searchFilm(choise)
case 5 :
displayFilm()
case 0 :
print(json.dumps(film, indent=4))
break
case _:
print("Number not between 1 and 5 ")
# It is required to run the specified function automatically when the program is run.
if __name__ == "__main__":
mainFilmMenu()