"--- Class Definition: Book ---"
Object subclass: Book [
| title isBorrowed |
Book class >> newWithTitle: aString [
^ (super new) initializeWithTitle: aString
]
initializeWithTitle: aString [
title := aString.
isBorrowed := false.
]
title [ ^ title ]
isBorrowed [ ^ isBorrowed ]
borrowBook [
isBorrowed ifTrue: [ ^ false ]. "Already borrowed"
isBorrowed := true.
^ true.
]
returnBook [
isBorrowed ifFalse: [ ^ false ]. "Already returned"
isBorrowed := false.
^ true.
]
]
"--- Class Definition: User ---"
Object subclass: User [
| name |
User class >> newWithName: aString [
^ (super new) initializeWithName: aString
]
initializeWithName: aString [
name := aString.
]
name [ ^ name ]
borrow: aBook [
Transcript show: name; show: ' tries to borrow "'; show: aBook title; show: '"... '.
(aBook borrowBook)
ifTrue: [ Transcript show: 'Success!'; cr ]
ifFalse: [ Transcript show: 'Failed (Already borrowed).'; cr ].
]
return: aBook [
Transcript show: name; show: ' returns "'; show: aBook title; show: '"... '.
(aBook returnBook)
ifTrue: [ Transcript show: 'Success!'; cr ]
ifFalse: [ Transcript show: 'Failed (Not borrowed).'; cr ].
]
]
"--- Main Simulation Script ---"
| book1 book2 studentA studentB |
"1. Create instances (Objects)"
book1 := Book newWithTitle: 'Smalltalk-80 Practice'.
book2 := Book newWithTitle: 'Introduction to AI 2026'.
studentA := User newWithName: 'Taro'.
studentB := User newWithName: 'Hanako'.
Transcript show: '=== Library System Simulation ==='; cr; cr.
"2. Student A borrows Book 1"
studentA borrow: book1.
"3. Student B tries to borrow the same Book 1 (Should fail)"
studentB borrow: book1.
"4. Student B borrows Book 2"
studentB borrow: book2.
"5. Student A returns Book 1"
studentA return: book1.
"6. Student B tries to borrow Book 1 again (Should succeed now)"
studentB borrow: book1.