Skip to content

Commit 24490e0

Browse files
authored
Merge pull request mouredev#4830 from qwik-zgheib/main
#26-27 - Go & Python
2 parents c37218a + 30fb4ec commit 24490e0

File tree

4 files changed

+581
-0
lines changed

4 files changed

+581
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/* -- Exercise */
6+
7+
type User struct {
8+
Name string
9+
Email string
10+
}
11+
12+
/* -- exercise -- incorrect */
13+
14+
func (u *User) Save() {
15+
fmt.Printf("Saving user %s to the database\n", u.Name)
16+
}
17+
18+
func (u *User) SendEmail() {
19+
fmt.Printf("Sending email to %s\n", u.Email)
20+
}
21+
22+
/* -- exercise -- correct */
23+
24+
type UserRepository struct{}
25+
26+
func (repo *UserRepository) Save(user User) {
27+
fmt.Printf("Saving user %s to the database\n", user.Name)
28+
}
29+
30+
type EmailService struct{}
31+
32+
func (service *EmailService) SendEmail(user User) {
33+
fmt.Printf("Sending email to %s\n", user.Email)
34+
}
35+
36+
/* -- Extra Challenge */
37+
38+
type Book struct {
39+
Title string
40+
Author string
41+
Copies int
42+
}
43+
44+
type UserT struct {
45+
Name string
46+
ID string
47+
Email string
48+
}
49+
50+
/* -- extra challenge - incorrect */
51+
52+
type Library struct {
53+
Books []Book
54+
Users []UserT
55+
Loans map[string]string // map[userID]bookTitle
56+
}
57+
58+
func (lib *Library) AddBook(book Book) {
59+
lib.Books = append(lib.Books, book)
60+
fmt.Printf("Book '%s' by '%s' added to the library.\n", book.Title, book.Author)
61+
}
62+
63+
func (lib *Library) AddUser(user UserT) {
64+
lib.Users = append(lib.Users, user)
65+
fmt.Printf("User '%s' added to the library.\n", user.Name)
66+
}
67+
68+
func (lib *Library) LoanBook(userID, bookTitle string) {
69+
lib.Loans[userID] = bookTitle
70+
fmt.Printf("User '%s' loaned book '%s'.\n", userID, bookTitle)
71+
}
72+
73+
/* -- extra challenge - correct */
74+
75+
type BookRepositoryC struct {
76+
Books []Book
77+
}
78+
79+
func (repo *BookRepositoryC) AddBook(book Book) {
80+
repo.Books = append(repo.Books, book)
81+
fmt.Printf("Book '%s' by '%s' added to the library.\n", book.Title, book.Author)
82+
}
83+
84+
type UserRepositoryC struct {
85+
Users []UserT
86+
}
87+
88+
func (repo *UserRepositoryC) AddUserC(user UserT) {
89+
repo.Users = append(repo.Users, user)
90+
fmt.Printf("User '%s' added to the library.\n", user.Name)
91+
}
92+
93+
type LoanService struct {
94+
Loans map[string]string // map[userID]bookTitle
95+
}
96+
97+
func (service *LoanService) LoanBook(userID, bookTitle string) {
98+
service.Loans[userID] = bookTitle
99+
fmt.Printf("User '%s' loaned book '%s'.\n", userID, bookTitle)
100+
}
101+
102+
func main() {
103+
/* Exercise */
104+
user := User{Name: "Legion Commander", Email: "legioncommander@gmail.com"}
105+
/* -- incorrect use */
106+
user.Save()
107+
user.SendEmail()
108+
109+
/* -- correct use */
110+
userRepo := UserRepository{}
111+
userRepo.Save(user)
112+
113+
emailService := EmailService{}
114+
emailService.SendEmail(user)
115+
116+
/* Extra Challenge */
117+
/* -- incorrect use */
118+
library := &Library{
119+
Books: []Book{},
120+
Users: []UserT{},
121+
Loans: make(map[string]string),
122+
}
123+
124+
library.AddBook(Book{Title: "1984", Author: "George Orwell", Copies: 3})
125+
library.AddUser(UserT{Name: "Legion Commander", ID: "xyz-123", Email: "legioncommander@gmail.com"})
126+
library.LoanBook("xyz-123", "1984")
127+
128+
/* -- correct use */
129+
bookRepoC := &BookRepositoryC{Books: []Book{}}
130+
userRepoC := &UserRepositoryC{Users: []UserT{}}
131+
loanService := &LoanService{Loans: make(map[string]string)}
132+
133+
bookRepoC.AddBook(Book{Title: "1984", Author: "George Orwell", Copies: 3})
134+
userRepoC.AddUserC(UserT{Name: "Legion Commander", ID: "xyz-123", Email: "legioncommander@gmail.com"})
135+
loanService.LoanBook("123", "1984")
136+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# -- exercise
2+
# incorrect
3+
class HeroI:
4+
def __init__(self, name, primary_attribute, base_damage, health):
5+
self.name = name;
6+
self.primary_attribute = primary_attribute;
7+
self.base_damage = base_damage;
8+
self.health = health;
9+
10+
def calculate_damage(self, additional_damage):
11+
return self.base_damage + additional_damage;
12+
13+
def display_info(self):
14+
info = f"Hero: {self.name}\n";
15+
info += f"Primary Attribute: {self.primary_attribute}\n";
16+
info += f"Base Damage: {self.base_damage}\n";
17+
info += f"Health: {self.health}";
18+
return info;
19+
20+
hero = HeroI("Axe", "Strength", 50, 1000);
21+
print(hero.display_info());
22+
print(f"Damage: {hero.calculate_damage(20)}");
23+
24+
# correct
25+
class HeroC:
26+
def __init__(self, name, primary_attribute, base_damage, health):
27+
self.name = name;
28+
self.primary_attribute = primary_attribute;
29+
self.base_damage = base_damage;
30+
self.health = health;
31+
32+
class DamageCalculator:
33+
def calculate_damage(self, hero, additional_damage):
34+
return hero.base_damage + additional_damage;
35+
36+
class HeroDisplay:
37+
def display_info(self, hero):
38+
info = f"Hero: {hero.name}\n"
39+
info += f"Primary Attribute: {hero.primary_attribute}\n";
40+
info += f"Base Damage: {hero.base_damage}\n";
41+
info += f"Health: {hero.health}";
42+
return info;
43+
44+
print("------------------------------------------------")
45+
hero = HeroC("Axe", "Strength", 50, 1000);
46+
damage_calculator = DamageCalculator();
47+
hero_display = HeroDisplay();
48+
49+
print(hero_display.display_info(hero))
50+
print(f"Damage: {damage_calculator.calculate_damage(hero, 20)}");
51+
52+
# -- extra challenge
53+
# incorrect
54+
class Library:
55+
def __init__(self):
56+
self.books = [];
57+
self.users = [];
58+
self.loans = [];
59+
60+
def add_book(self, title, author, copies):
61+
book = {"title": title, "author": author, "copies": copies};
62+
self.books.append(book);
63+
64+
def add_user(self, name, user_id, email):
65+
user = {"name": name, "user_id": user_id, "email": email};
66+
self.users.append(user);
67+
68+
def loan_book(self, user_id, book_title):
69+
for book in self.books:
70+
if book["title"] == book_title and book["copies"] > 0:
71+
self.loans.append({"user_id": user_id, "book_title": book_title});
72+
book["copies"] -= 1;
73+
return True;
74+
return False;
75+
76+
def return_book(self, user_id, book_title):
77+
for loan in self.loans:
78+
if loan["user_id"] == user_id and loan["book_title"] == book_title:
79+
self.loans.remove(loan);
80+
for book in self.books:
81+
if book["title"] == book_title:
82+
book["copies"] += 1;
83+
return True;
84+
return False;
85+
86+
library = Library()
87+
library.add_book("1996", "Ahmed Khedr", 4);
88+
library.add_user("Legion Commander", 1, "legioncommander@gmail.com");
89+
library.loan_book(1, "1996");
90+
library.return_book(1, "1996");
91+
92+
93+
# correct
94+
class BookManager:
95+
def __init__(self):
96+
self.books = [];
97+
98+
def add_book(self, title, author, copies):
99+
book = {"title": title, "author": author, "copies": copies};
100+
self.books.append(book);
101+
102+
def update_copies(self, book_title, copies):
103+
for book in self.books:
104+
if book["title"] == book_title:
105+
book["copies"] += copies;
106+
return True;
107+
return False;
108+
109+
def get_book(self, book_title):
110+
for book in self.books:
111+
if book["title"] == book_title:
112+
return book;
113+
return None;
114+
115+
class UserManager:
116+
def __init__(self):
117+
self.users = [];
118+
119+
def add_user(self, name, user_id, email):
120+
user = {"name": name, "user_id": user_id, "email": email};
121+
self.users.append(user);
122+
123+
def get_user(self, user_id):
124+
for user in self.users:
125+
if user["user_id"] == user_id:
126+
return user;
127+
return None;
128+
129+
class LoanManager:
130+
def __init__(self, book_manager):
131+
self.loans = [];
132+
self.book_manager = book_manager;
133+
134+
def loan_book(self, user_id, book_title):
135+
book = self.book_manager.get_book(book_title);
136+
if book and book["copies"] > 0:
137+
self.loans.append({"user_id": user_id, "book_title": book_title});
138+
self.book_manager.update_copies(book_title, -1);
139+
return True;
140+
return False;
141+
142+
def return_book(self, user_id, book_title):
143+
for loan in self.loans:
144+
if loan["user_id"] == user_id and loan["book_title"] == book_title:
145+
self.loans.remove(loan);
146+
self.book_manager.update_copies(book_title, 1);
147+
return True;
148+
return False;
149+
150+
# Uso correcto
151+
book_manager = BookManager();
152+
user_manager = UserManager();
153+
loan_manager = LoanManager(book_manager);
154+
155+
book_manager.add_book("1996", "Ahmed Khedr", 4);
156+
user_manager.add_user("Legion Commander", 1, "legioncommander@gmail.com");
157+
loan_manager.loan_book(1, "1996");
158+
loan_manager.return_book(1, "1996");
159+

0 commit comments

Comments
 (0)