Created
January 27, 2024 13:04
-
-
Save kafri8889/61b5c0fd6cbd1d167b4aea456acba3d3 to your computer and use it in GitHub Desktop.
Tugas OOP Coding AP 1 B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
# OOP perpus | |
# @author: kafri8889 | |
# Template nama orang | |
nameTemplate = [ | |
"Wang Yiren", | |
"Bai Lu", | |
"Zhong Siyu", | |
"Lin Youyou", | |
"Yang Chaoyue", | |
"Jiang Shen", | |
"Lin Hui", | |
] | |
class Book: | |
def __init__(self, id: int, name: str, writerName: str): | |
self._id: int = id | |
self._name: str = name | |
self._writerName: str = writerName | |
def setId(self, id: int): | |
self._id = id | |
def setName(self, name: str): | |
self._name = name | |
def setWriterName(self, name: str): | |
self._writerName = name | |
def getId(self) -> int: | |
return self._id | |
def getName(self) -> str: | |
return self._name | |
def getWriterName(self) -> str: | |
return self._writerName | |
def __str__(self): | |
return self.getName() | |
# Parent class untuk visitor dan borrower | |
class Person: | |
def __init__(self, name: str): | |
self._name: str = name | |
def setName(self, name: str): | |
self._name = name | |
def getName(self) -> str: | |
return self._name | |
def __str__(self): | |
return self.getName() | |
# Class yg mempresentasikan pengunjung perpus | |
class Visitor(Person): | |
def __init__(self, name: str, id: int): | |
Person.__init__(self, name) | |
self._id: int = id | |
def setId(self, id: int): | |
self._id = id | |
def getId(self) -> int: | |
return self._id | |
def __str__(self): | |
return self.getName() | |
# Class yg mempresentasikan peminjam buku di perpus | |
class Borrower(Person): | |
def __init__(self, name: str, id: int, book: Book, returnDate: str): | |
Person.__init__(self, name) | |
self._id: int = id | |
self._book: Book = book | |
self._returnDate: str = returnDate | |
def setId(self, id: int): | |
self._id = id | |
def setBookId(self, book: Book): | |
self._book = book | |
def setReturnDate(self, date: str): | |
self._returnDate = date | |
def getId(self) -> int: | |
return self._id | |
def getBook(self) -> Book: | |
return self._book | |
def getReturnDate(self) -> str: | |
return self._returnDate | |
def __str__(self): | |
return "{" + self.getName() + ", " + self._book.getName() + "}" | |
if __name__ == '__main__': | |
# Available books | |
books = [ | |
Book(0, "Kotlin multiplatform", "Zhao Lusi"), | |
Book(1, "Kotlin for machine learning", "Lu Yuxiao"), | |
Book(2, "Build server with Kotlin", "Ju Jingyi"), | |
Book(3, "Build websites quickly with Kotlin", "Shen Xiaoting"), | |
] | |
# Current visitors in perpus | |
visitors = [] | |
# Orang yg minjem buku | |
borrowers = [] | |
print() | |
print("Welcome to Ning Yizhuo's library simulator!!!") | |
print() | |
print("Description") | |
print("1. Simulate visitor borrowing book.") | |
print("2. Simulate visitor returning book.") | |
print("3. Simulate visitor entering the perpus") | |
print("4. Simulate visitor exiting the perpus") | |
print("5. List all visitor in this perpus") | |
print("6. List all visitor who borrow book from this perpus") | |
print("7. End simulation safely") | |
while (True): | |
print() | |
print() | |
print() | |
print("1 >> Pinjam buku") | |
print("2 >> Balikin buku") | |
print("3 >> Pengunjung masuk") | |
print("4 >> Pengunjung keluar") | |
print("5 >> List pengunjung perpus") | |
print("6 >> List peminjam buku") | |
print("7 >> Akhiri simulasi") | |
num = 0 | |
try: | |
num = int(input("Pilih: ")) | |
print() | |
except ValueError: | |
print("Hanya boleh memasukkan angka!") | |
continue | |
match num: | |
case 1: | |
if len(visitors) == 0: | |
print("Ngga ada pengunjung, siapa yg mau minjem huhuhu T_T") | |
continue | |
if len(books) == 0: | |
print("Buku udah habis, ngga ada yg bisa dipinjem T_T.") | |
continue | |
book = books[random.randint(0, len(books) - 1)] | |
visitor = visitors[random.randint(0, len(visitors) - 1)] | |
borrower = Borrower(visitor.getName(), random.randint(0, 1000), book, "31-08-04") | |
borrowers.append(borrower) | |
# Remove book karna udah dipinjem | |
books.remove(book) | |
print(f"Buku \"{book.getName()}\" dipinjam oleh \"{visitor.getName()}\"") | |
case 2: | |
if len(borrowers) == 0: | |
print("Ngga ada yg minjem, mau balikin apaan T_T") | |
continue | |
borrower = borrowers[random.randint(0, len(borrowers) - 1)] | |
borrowers.remove(borrower) | |
# Remove book karna udah dipinjem | |
books.append(borrower.getBook()) | |
print(f"\"{borrower.getName()}\" mengembalikan buku \"{borrower.getBook().getName()}\"") | |
case 3: | |
# Jika elemen di nameTemplate habis, berarti ngga ada pengunjung lagi | |
if len(nameTemplate) == 0: | |
print("Tidak ada pengunjung lagi T_T") | |
continue | |
# Get random nameTemplate index | |
randomIndex = random.randint(0, len(nameTemplate) - 1) | |
# Buat objek visitor | |
visitor = Visitor(nameTemplate[randomIndex], random.randint(0, 1000)) | |
# Hapus nama dari nameTemplate, nanti di append pas keluar | |
nameTemplate.remove(nameTemplate[randomIndex]) | |
visitors.append(visitor) | |
print(f"{visitor.getName()} telah masuk perpus.") | |
case 4: | |
if len(visitors) == 0: | |
print("Ngga ada pengunjung siapa yg mau keluar T_T") | |
continue | |
randomIndex = random.randint(0, len(visitors) - 1) | |
visitor = visitors[randomIndex] | |
visitors.remove(visitor) | |
print(f"{visitor.getName()} telah keluar perpus.") | |
case 5: | |
print(f"Pengunjung saat ini adalah: {list(map(lambda vstr: vstr.getName(), visitors))}") | |
case 6: | |
print(f"Peminjam saat ini adalah: {list(map(lambda brwr: str(brwr), borrowers))}") | |
case 7: | |
print("Shutdown in 3") | |
time.sleep(1) | |
print("Shutdown in 2") | |
time.sleep(0.8) | |
print("Shutdown in 1") | |
time.sleep(0.6) | |
print("Simulation end :D") | |
break | |
case _: | |
print("Pilih antara 1 sampai 6.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment