Testing and Integration
Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 61 of 65.
Challenge
MediumFinish the system with returns, then let the tests run every part of it together. Keep everything you wrote in the previous steps; library.gd and user.gd change.
In Library, write return_book(isbn: String, user_id: String) -> String. Check in this order:
- no book with that ISBN: return
Book not found - no user with that id: return
User not found - the book is available, or its
borrower_idis another user: returnThis book was not borrowed by this user - otherwise make the book available again, set its
borrower_idto"", remove the ISBN from the user'sborrowed_isbnsand returnBook returned successfully
In User, write return_all_books(library: Library) -> int. It returns every book the user borrowed through library.return_book() and returns how many were returned. The user receives the library as an argument and does not store it, so no two objects point at each other.
The supplied code adds the commands return ISBN|ID and return_all ID. For this input:
Community
book The Great Gatsby|F. Scott Fitzgerald|123456
book 1984|George Orwell|345678
user U001|Alice
user U002|Bob
borrow 123456|U001
borrow 345678|U001
return 123456|U002
return_all U001
users
statsthe output is:
Book borrowed successfully
Book borrowed successfully
This book was not borrowed by this user
Books returned by Alice: 2
User: Alice (ID: U001) - Books borrowed: 0
User: Bob (ID: U002) - Books borrowed: 0
Total Books: 2
Available Books: 2
Borrowed Books: 0
Total Users: 2
Users with Books: 0Your code goes in book.gd, user.gd and library.gd. main.gd holds the supplied input/output code and cannot be edited.
Try it yourself
extends Node
func _ready():
# Supplied input/output code: keep it as it is
var library := Library.new(OS.read_string_from_stdin())
var line := OS.read_string_from_stdin()
while line != "":
var parts := line.split(" ", true, 1)
var fields: PackedStringArray = parts[1].split("|") if parts.size() > 1 else PackedStringArray()
match parts[0]:
"book":
library.add_book(Book.new(fields[0], fields[1], fields[2]))
"user":
library.add_user(User.new(fields[0], fields[1]))
"borrow":
print(library.borrow_book(fields[0], fields[1]))
"return":
print(library.return_book(fields[0], fields[1]))
"return_all":
var reader: User = library.find_user(fields[0])
var count: int = reader.return_all_books(library)
print("Books returned by ", reader.user_name, ": ", count)
"search":
var term: String = parts[1] if parts.size() > 1 else ""
var found: Array = library.search_by_title(term)
print("Results for '%s': %d" % [term, found.size()])
for book in found:
print("- ", book.title, " by ", book.author)
"stats":
var stats: Dictionary = library.get_statistics()
print("Total Books: ", stats["total_books"])
print("Available Books: ", stats["available_books"])
print("Borrowed Books: ", stats["borrowed_books"])
print("Total Users: ", stats["total_users"])
print("Users with Books: ", stats["users_with_books"])
"loans":
var reader: User = library.find_user(fields[0])
print(reader.user_name, " has borrowed:")
for isbn in reader.borrowed_isbns:
print("- ", library.find_book(isbn).title)
"holder":
var book: Book = library.find_book(fields[0])
if book.available:
print(book.title, " is on the shelf")
else:
print(book.title, " is borrowed by ", library.find_user(book.borrower_id).user_name)
"books":
for book in library.books:
print(book)
"users":
for user in library.users:
print(user)
"show":
print(library)
line = OS.read_string_from_stdin()
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self KeywordMethodsPropertiesConstructor (_init)Recap - Simple Calculator4Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator2Class Properties
Instance vs Static VariablesStatic FunctionsProperty SettersProperty GettersPrivate by ConventionRecap - Bank Account Manager5Encapsulation
Public and Private MembersValidation with SettersInformation HidingRead-Only PropertiesRecap - Student Records System8Advanced OOP Concepts
Composition vs InheritanceInner ClassesNodes as ComponentsRefCounted vs NodeEnums in Classes11Project: Library Management
Project OverviewBook and User Classes3Inheritance
Basic InheritanceThe super KeywordMethod OverridingInheritance ChainsType Checks with isRecap - Employee Hierarchy6Special Methods
Virtual Methods IntroductionThe _to_string MethodCustom IteratorsRecap - Custom List9Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy PatternPractice on your own: Online GDScript compiler