Skip to content

Commit 7eaa1c9

Browse files
committed
OOP ile yapılan örnek uygulamanın çözümü eklendi.
1 parent f9f7afa commit 7eaa1c9

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

OOP - Example (Serhat)/crud.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import json
2+
import os
3+
import uuid
4+
5+
class User:
6+
def __init__(self):
7+
self.isim = ""
8+
self.soyisim = ""
9+
self.numara = ""
10+
11+
class DatabaseMethods:
12+
def __init__(self):
13+
self.path = "database.json"
14+
self.Create_Database_If_Not_Exists()
15+
16+
def Check_UserID(self, user_id):
17+
with open(self.path, "r") as f:
18+
js = json.load(f)
19+
20+
#if user_id in js:
21+
# return True
22+
#else:
23+
# return False
24+
25+
#return True if user_id in js else False
26+
27+
return user_id in js
28+
29+
def Create_Database_If_Not_Exists(self):
30+
if not os.path.exists(self.path):
31+
with open(self.path, "w") as f:
32+
json.dump({}, f)
33+
34+
def add(self, user):
35+
with open(self.path, "r") as f:
36+
js = json.load(f)
37+
38+
js.update({
39+
str(uuid.uuid1()) : user.__dict__
40+
})
41+
42+
with open(self.path, "w") as f:
43+
json.dump(js, f)
44+
45+
def update(self, user_id, user):
46+
with open(self.path, "r") as f:
47+
js = json.load(f)
48+
49+
# exist_check = js.get(user_id, False)
50+
# if not exist_check:
51+
# return False
52+
53+
if user_id in js:
54+
js[user_id] = user.__dict__
55+
56+
with open(self.path, "w") as f:
57+
json.dump(js, f)
58+
59+
def get(self):
60+
with open(self.path) as f:
61+
js = json.load(f)
62+
return js
63+
64+
def delete(self, user_id):
65+
result = False
66+
with open(self.path, "r") as f:
67+
js = json.load(f)
68+
69+
if user_id in js:
70+
del js[user_id]
71+
result = True
72+
73+
with open(self.path, "w") as f:
74+
json.dump(js, f)
75+
76+
return result

OOP - Example (Serhat)/main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import sys
3+
from pprint import pprint
4+
from crud import User, DatabaseMethods
5+
6+
class ConsoleProgram:
7+
def __init__(self):
8+
self.main_choice = ""
9+
self.choices_list = ["1", "2", "3", "4"]
10+
11+
def Main_Page(self):
12+
print("Hoşgeldiniz...")
13+
while True:
14+
print("1. Kayıtları Göster")
15+
print("2. Ekle")
16+
print("3. Güncelle")
17+
print("4. Sil")
18+
self.main_choice = input()
19+
if self.main_choice in self.choices_list:
20+
break
21+
self.Clear_Screen()
22+
print("Girdiniz değer yanlıştır!")
23+
24+
def Goto_Choice(self):
25+
db = DatabaseMethods()
26+
27+
if self.main_choice == "1": # Get
28+
pprint(db.get())
29+
30+
elif self.main_choice == "2": # Add
31+
db.add(self.Get_User())
32+
pprint(db.get())
33+
34+
elif self.main_choice == "3": # Update
35+
user_id = input("User ID : ")
36+
if db.Check_UserID(user_id):
37+
db.update(user_id, self.Get_User())
38+
print("Kullanıcı güncellendi.")
39+
else:
40+
print("Kullanıcı bulunamadı.")
41+
42+
pprint(db.get())
43+
44+
elif self.main_choice == "4": # Delete
45+
user_id = input("Kullanıcı ID giriniz : ")
46+
47+
if db.delete(user_id):
48+
print("Kullanıcı silindi.")
49+
else:
50+
print("Böyle bir kullanıcı bulunamadı.")
51+
52+
pprint(db.get())
53+
54+
@staticmethod
55+
def Clear_Screen():
56+
os.system("cls" if sys.platform == "nt" else "clear")
57+
58+
@staticmethod
59+
def Get_User():
60+
user = User()
61+
user.isim = input("isim : ")
62+
user.soyisim = input("soyisim : ")
63+
user.numara = input("numara : ")
64+
return user
65+
66+
def Run(self):
67+
self.Main_Page()
68+
self.Goto_Choice()
69+
70+
if __name__ == "__main__":
71+
n = ConsoleProgram()
72+
n.Run()

0 commit comments

Comments
 (0)