Skip to content

Commit 7b36fdc

Browse files
committed
#26 - TypeScript
1 parent 5251d01 commit 7b36fdc

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Invalid way
2+
class User {
3+
name: string;
4+
email: string;
5+
6+
constructor(name: string, email: string) {
7+
this.name = name;
8+
this.email = email;
9+
}
10+
11+
getUserInfo(): { name: string; email: string } {
12+
return {
13+
name: this.name,
14+
email: this.email
15+
};
16+
}
17+
18+
formatUserInfo(): string {
19+
return `Name: ${this.name}, Email: ${this.email}`;
20+
}
21+
}
22+
23+
const user = new User('Isaac Morales', '[email protected]');
24+
console.log(user.formatUserInfo());
25+
26+
// Correct way
27+
class UserInfo {
28+
name: string;
29+
email: string;
30+
31+
constructor(name: string, email: string) {
32+
this.name = name;
33+
this.email = email;
34+
}
35+
36+
getUserInfo(): { name: string; email: string } {
37+
return {
38+
name: this.name,
39+
email: this.email
40+
};
41+
}
42+
}
43+
44+
class FormatUser {
45+
static format(user: UserInfo): string {
46+
return `Name: ${user.name}, Email: ${user.email}`;
47+
}
48+
}
49+
50+
const newUser = new UserInfo('Isaac Cortés', '[email protected]');
51+
console.log(FormatUser.format(newUser));
52+
53+
// ** Extra Exercise ** //
54+
interface Book {
55+
title: string;
56+
author: string;
57+
copies: number;
58+
}
59+
60+
class BookManager {
61+
private books: Book[];
62+
63+
constructor() {
64+
this.books = [];
65+
}
66+
67+
addBook(title: string, author: string, copies: number): void {
68+
this.books.push({ title, author, copies });
69+
}
70+
71+
findBook(title: string): Book | undefined {
72+
return this.books.find(book => book.title === title);
73+
}
74+
}
75+
76+
interface UserDetails {
77+
name: string;
78+
id: number;
79+
email: string;
80+
}
81+
82+
class UserManager {
83+
private users: UserDetails[];
84+
85+
constructor() {
86+
this.users = [];
87+
}
88+
89+
addUser(name: string, id: number, email: string): void {
90+
this.users.push({ name, id, email });
91+
}
92+
93+
findUser(id: number): UserDetails | undefined {
94+
return this.users.find(user => user.id === id);
95+
}
96+
}
97+
98+
class LoanManager {
99+
private bookManager: BookManager;
100+
private userManager: UserManager;
101+
private loans: { userId: number; bookTitle: string }[];
102+
103+
constructor(bookManager: BookManager, userManager: UserManager) {
104+
this.bookManager = bookManager;
105+
this.userManager = userManager;
106+
this.loans = [];
107+
}
108+
109+
borrowBook(userId: number, bookTitle: string): void {
110+
const user = this.userManager.findUser(userId);
111+
const book = this.bookManager.findBook(bookTitle);
112+
113+
if (user && book && book.copies > 0) {
114+
book.copies--;
115+
this.loans.push({ userId, bookTitle });
116+
console.log(`${user.name} borrowed "${book.title}"`);
117+
} else {
118+
console.log(`Book is unavailable or user not found`);
119+
}
120+
}
121+
122+
returnBook(userId: number, bookTitle: string): void {
123+
const loanIndex = this.loans.findIndex(
124+
loan => loan.userId === userId && loan.bookTitle === bookTitle
125+
);
126+
const book = this.bookManager.findBook(bookTitle);
127+
128+
if (loanIndex !== -1 && book) {
129+
book.copies++;
130+
this.loans.splice(loanIndex, 1);
131+
console.log(`"${book.title}" has been returned`);
132+
} else {
133+
console.log('Loan not found or book not available');
134+
}
135+
}
136+
}
137+
138+
const bookManager = new BookManager();
139+
const userManager = new UserManager();
140+
const loanManager = new LoanManager(bookManager, userManager);
141+
142+
bookManager.addBook('The Great Gatsby', 'F. Scott Fitzgerald', 5);
143+
userManager.addUser('Isaac', 1, '[email protected]');
144+
145+
loanManager.borrowBook(1, 'The Great Gatsby');
146+
loanManager.returnBook(1, 'The Great Gatsby');

0 commit comments

Comments
 (0)