fork download
  1. "============================================================"
  2. " 大学図書館貸出システム"
  3. " Ideone / GNU Smalltalk用"
  4. "============================================================"
  5.  
  6.  
  7. "------------------------------------------------------------"
  8. " Bookクラス"
  9. "------------------------------------------------------------"
  10.  
  11. Object subclass: #Book
  12. instanceVariableNames: 'isbn title author available'
  13. classVariableNames: ''
  14. poolDictionaries: ''
  15. category: 'UniversityLibrary'!
  16.  
  17.  
  18. !Book methodsFor: 'initialization'!
  19.  
  20. initializeISBN: anISBN title: aTitle author: anAuthor
  21. isbn := anISBN.
  22. title := aTitle.
  23. author := anAuthor.
  24. available := true.
  25. ^self
  26. !
  27.  
  28. ! !
  29.  
  30.  
  31. !Book methodsFor: 'accessing'!
  32.  
  33. isbn
  34. ^isbn
  35. !
  36.  
  37. title
  38. ^title
  39. !
  40.  
  41. author
  42. ^author
  43. !
  44.  
  45. isAvailable
  46. ^available
  47. !
  48.  
  49. statusText
  50. available ifTrue: [^'貸出可能'].
  51. ^'貸出中'
  52. !
  53.  
  54. ! !
  55.  
  56.  
  57. !Book methodsFor: 'loan operations'!
  58.  
  59. borrow
  60. available ifFalse: [
  61. self error: 'この図書はすでに貸出中です'
  62. ].
  63.  
  64. available := false.
  65. ^self
  66. !
  67.  
  68. returnBook
  69. available ifTrue: [
  70. self error: 'この図書は貸出されていません'
  71. ].
  72.  
  73. available := true.
  74. ^self
  75. !
  76.  
  77. ! !
  78.  
  79.  
  80. !Book methodsFor: 'printing'!
  81.  
  82. printOn: aStream
  83. aStream nextPutAll: title.
  84. aStream nextPutAll: ' / '.
  85. aStream nextPutAll: author.
  86. aStream nextPutAll: ' [ISBN: '.
  87. aStream nextPutAll: isbn.
  88. aStream nextPutAll: '] '.
  89. aStream nextPutAll: self statusText
  90. !
  91.  
  92. ! !
  93.  
  94.  
  95. "------------------------------------------------------------"
  96. " Memberクラス"
  97. "------------------------------------------------------------"
  98.  
  99. Object subclass: #Member
  100. instanceVariableNames: 'memberId memberName memberType'
  101. classVariableNames: ''
  102. poolDictionaries: ''
  103. category: 'UniversityLibrary'!
  104.  
  105.  
  106. !Member methodsFor: 'initialization'!
  107.  
  108. initializeId: anId name: aName type: aType
  109. memberId := anId.
  110. memberName := aName.
  111. memberType := aType.
  112. ^self
  113. !
  114.  
  115. ! !
  116.  
  117.  
  118. !Member methodsFor: 'accessing'!
  119.  
  120. memberId
  121. ^memberId
  122. !
  123.  
  124. name
  125. ^memberName
  126. !
  127.  
  128. memberType
  129. ^memberType
  130. !
  131.  
  132. typeName
  133. memberType = #student ifTrue: [^'学生'].
  134. memberType = #teacher ifTrue: [^'教職員'].
  135. ^'その他'
  136. !
  137.  
  138. loanLimit
  139. memberType = #student ifTrue: [^3].
  140. memberType = #teacher ifTrue: [^10].
  141. ^2
  142. !
  143.  
  144. loanPeriod
  145. memberType = #teacher ifTrue: [^30].
  146. ^14
  147. !
  148.  
  149. ! !
  150.  
  151.  
  152. !Member methodsFor: 'printing'!
  153.  
  154. printOn: aStream
  155. aStream nextPutAll: memberName.
  156. aStream nextPutAll: ' [利用者番号: '.
  157. aStream nextPutAll: memberId.
  158. aStream nextPutAll: ', 区分: '.
  159. aStream nextPutAll: self typeName.
  160. aStream nextPutAll: ', 貸出上限: '.
  161. aStream nextPutAll: self loanLimit printString.
  162. aStream nextPutAll: '冊]'
  163. !
  164.  
  165. ! !
  166.  
  167.  
  168. "------------------------------------------------------------"
  169. " Loanクラス"
  170. " 日付は整数で表現する"
  171. "------------------------------------------------------------"
  172.  
  173. Object subclass: #Loan
  174. instanceVariableNames: 'loanBook loanMember loanDay dueDay returnedDay'
  175. classVariableNames: ''
  176. poolDictionaries: ''
  177. category: 'UniversityLibrary'!
  178.  
  179.  
  180. !Loan methodsFor: 'initialization'!
  181.  
  182. initializeBook: aBook member: aMember day: aDay
  183. loanBook := aBook.
  184. loanMember := aMember.
  185. loanDay := aDay.
  186. dueDay := aDay + aMember loanPeriod.
  187. returnedDay := nil.
  188. ^self
  189. !
  190.  
  191. ! !
  192.  
  193.  
  194. !Loan methodsFor: 'accessing'!
  195.  
  196. book
  197. ^loanBook
  198. !
  199.  
  200. member
  201. ^loanMember
  202. !
  203.  
  204. loanDay
  205. ^loanDay
  206. !
  207.  
  208. dueDay
  209. ^dueDay
  210. !
  211.  
  212. returnedDay
  213. ^returnedDay
  214. !
  215.  
  216. isActive
  217. ^returnedDay isNil
  218. !
  219.  
  220. ! !
  221.  
  222.  
  223. !Loan methodsFor: 'testing'!
  224.  
  225. isOverdueOn: currentDay
  226. self isActive ifFalse: [^false].
  227. ^currentDay > dueDay
  228. !
  229.  
  230. overdueDaysOn: currentDay
  231. (self isOverdueOn: currentDay) ifFalse: [^0].
  232. ^currentDay - dueDay
  233. !
  234.  
  235. ! !
  236.  
  237.  
  238. !Loan methodsFor: 'returning'!
  239.  
  240. returnOn: aDay
  241. self isActive ifFalse: [
  242. self error: 'この貸出記録はすでに返却済みです'
  243. ].
  244.  
  245. returnedDay := aDay.
  246. loanBook returnBook.
  247. ^self
  248. !
  249.  
  250. ! !
  251.  
  252.  
  253. !Loan methodsFor: 'printing'!
  254.  
  255. printOn: aStream
  256. aStream nextPutAll: loanMember name.
  257. aStream nextPutAll: ' -> '.
  258. aStream nextPutAll: loanBook title.
  259. aStream nextPutAll: ' / 貸出日: '.
  260. aStream nextPutAll: loanDay printString.
  261. aStream nextPutAll: ' / 返却期限: '.
  262. aStream nextPutAll: dueDay printString.
  263.  
  264. self isActive
  265. ifTrue: [
  266. aStream nextPutAll: ' / 貸出中'
  267. ]
  268. ifFalse: [
  269. aStream nextPutAll: ' / 返却日: '.
  270. aStream nextPutAll: returnedDay printString.
  271. aStream nextPutAll: ' / 返却済み'
  272. ]
  273. !
  274.  
  275. ! !
  276.  
  277.  
  278. "------------------------------------------------------------"
  279. " Libraryクラス"
  280. "------------------------------------------------------------"
  281.  
  282. Object subclass: #Library
  283. instanceVariableNames: 'libraryName books members loans'
  284. classVariableNames: ''
  285. poolDictionaries: ''
  286. category: 'UniversityLibrary'!
  287.  
  288.  
  289. !Library methodsFor: 'initialization'!
  290.  
  291. initializeName: aName
  292. libraryName := aName.
  293. books := OrderedCollection new.
  294. members := OrderedCollection new.
  295. loans := OrderedCollection new.
  296. ^self
  297. !
  298.  
  299. ! !
  300.  
  301.  
  302. !Library methodsFor: 'accessing'!
  303.  
  304. name
  305. ^libraryName
  306. !
  307.  
  308. books
  309. ^books
  310. !
  311.  
  312. members
  313. ^members
  314. !
  315.  
  316. loans
  317. ^loans
  318. !
  319.  
  320. ! !
  321.  
  322.  
  323. !Library methodsFor: 'registration'!
  324.  
  325. addBook: aBook
  326. (self findBookByISBN: aBook isbn) notNil ifTrue: [
  327. self error: '同じISBNの図書が登録されています'
  328. ].
  329.  
  330. books add: aBook.
  331. ^aBook
  332. !
  333.  
  334. registerMember: aMember
  335. (self findMemberById: aMember memberId) notNil ifTrue: [
  336. self error: '同じ利用者番号が登録されています'
  337. ].
  338.  
  339. members add: aMember.
  340. ^aMember
  341. !
  342.  
  343. ! !
  344.  
  345.  
  346. !Library methodsFor: 'searching'!
  347.  
  348. findBookByISBN: anISBN
  349. books do: [:each |
  350. each isbn = anISBN ifTrue: [^each]
  351. ].
  352.  
  353. ^nil
  354. !
  355.  
  356. findMemberById: anId
  357. members do: [:each |
  358. each memberId = anId ifTrue: [^each]
  359. ].
  360.  
  361. ^nil
  362. !
  363.  
  364. activeLoansFor: aMember
  365. | result |
  366.  
  367. result := OrderedCollection new.
  368.  
  369. loans do: [:each |
  370. each member == aMember ifTrue: [
  371. each isActive ifTrue: [
  372. result add: each
  373. ]
  374. ]
  375. ].
  376.  
  377. ^result
  378. !
  379.  
  380. activeLoanForBook: aBook
  381. loans do: [:each |
  382. each book == aBook ifTrue: [
  383. each isActive ifTrue: [^each]
  384. ]
  385. ].
  386.  
  387. ^nil
  388. !
  389.  
  390. ! !
  391.  
  392.  
  393. !Library methodsFor: 'loan operations'!
  394.  
  395. lendISBN: anISBN toMember: anId onDay: aDay
  396. | targetBook targetMember currentLoans newLoan |
  397.  
  398. targetBook := self findBookByISBN: anISBN.
  399.  
  400. targetBook isNil ifTrue: [
  401. self error: '指定された図書が見つかりません'
  402. ].
  403.  
  404. targetMember := self findMemberById: anId.
  405.  
  406. targetMember isNil ifTrue: [
  407. self error: '指定された利用者が見つかりません'
  408. ].
  409.  
  410. targetBook isAvailable ifFalse: [
  411. self error: '指定された図書はすでに貸出中です'
  412. ].
  413.  
  414. currentLoans := self activeLoansFor: targetMember.
  415.  
  416. currentLoans size >= targetMember loanLimit ifTrue: [
  417. self error: '貸出可能冊数を超えています'
  418. ].
  419.  
  420. targetBook borrow.
  421.  
  422. newLoan := Loan new
  423. initializeBook: targetBook
  424. member: targetMember
  425. day: aDay.
  426.  
  427. loans add: newLoan.
  428.  
  429. ^newLoan
  430. !
  431.  
  432. returnISBN: anISBN onDay: aDay
  433. | targetBook targetLoan |
  434.  
  435. targetBook := self findBookByISBN: anISBN.
  436.  
  437. targetBook isNil ifTrue: [
  438. self error: '指定された図書が見つかりません'
  439. ].
  440.  
  441. targetLoan := self activeLoanForBook: targetBook.
  442.  
  443. targetLoan isNil ifTrue: [
  444. self error: '指定された図書は現在貸出されていません'
  445. ].
  446.  
  447. targetLoan returnOn: aDay.
  448.  
  449. ^targetLoan
  450. !
  451.  
  452. ! !
  453.  
  454.  
  455. !Library methodsFor: 'reporting'!
  456.  
  457. showBooks
  458. '' printNl.
  459. '===== 図書一覧 =====' printNl.
  460.  
  461. books isEmpty ifTrue: [
  462. '登録されている図書はありません' printNl
  463. ].
  464.  
  465. books do: [:each |
  466. each printString printNl
  467. ]
  468. !
  469.  
  470. showMembers
  471. '' printNl.
  472. '===== 利用者一覧 =====' printNl.
  473.  
  474. members isEmpty ifTrue: [
  475. '登録されている利用者はいません' printNl
  476. ].
  477.  
  478. members do: [:each |
  479. each printString printNl
  480. ]
  481. !
  482.  
  483. showActiveLoans
  484. | count |
  485.  
  486. count := 0.
  487.  
  488. '' printNl.
  489. '===== 現在の貸出一覧 =====' printNl.
  490.  
  491. loans do: [:each |
  492. each isActive ifTrue: [
  493. each printString printNl.
  494. count := count + 1
  495. ]
  496. ].
  497.  
  498. count = 0 ifTrue: [
  499. '現在貸出中の図書はありません' printNl
  500. ]
  501. !
  502.  
  503. showOverdueLoansOn: currentDay
  504. | count |
  505.  
  506. count := 0.
  507.  
  508. '' printNl.
  509. '===== 延滞一覧 =====' printNl.
  510. ('確認日: ' , currentDay printString , '日目') printNl.
  511.  
  512. loans do: [:each |
  513. (each isOverdueOn: currentDay) ifTrue: [
  514. each printString print.
  515. ' / 延滞日数: ' print.
  516. (each overdueDaysOn: currentDay) print.
  517. '日' printNl.
  518. count := count + 1
  519. ]
  520. ].
  521.  
  522. count = 0 ifTrue: [
  523. '現在延滞中の貸出はありません' printNl
  524. ]
  525. !
  526.  
  527. showLoanHistory
  528. '' printNl.
  529. '===== 全貸出履歴 =====' printNl.
  530.  
  531. loans isEmpty ifTrue: [
  532. '貸出履歴はありません' printNl
  533. ].
  534.  
  535. loans do: [:each |
  536. each printString printNl
  537. ]
  538. !
  539.  
  540. ! !
  541.  
  542.  
  543. "============================================================"
  544. " メインプログラム"
  545. "============================================================"
  546.  
  547. Eval [
  548. | library book1 book2 book3 student teacher loan1 loan2 returnedLoan |
  549.  
  550. library := Library new
  551. initializeName: '○○大学図書館'.
  552.  
  553. book1 := Book new
  554. initializeISBN: '978-4-00-000001-1'
  555. title: 'Smalltalk入門'
  556. author: '山田太郎'.
  557.  
  558. book2 := Book new
  559. initializeISBN: '978-4-00-000002-8'
  560. title: 'オブジェクト指向設計'
  561. author: '佐藤花子'.
  562.  
  563. book3 := Book new
  564. initializeISBN: '978-4-00-000003-5'
  565. title: 'データベース基礎'
  566. author: '鈴木一郎'.
  567.  
  568. student := Member new
  569. initializeId: 'S2023001'
  570. name: '田中学生'
  571. type: #student.
  572.  
  573. teacher := Member new
  574. initializeId: 'T1001'
  575. name: '佐々木教授'
  576. type: #teacher.
  577.  
  578. library addBook: book1.
  579. library addBook: book2.
  580. library addBook: book3.
  581.  
  582. library registerMember: student.
  583. library registerMember: teacher.
  584.  
  585. '========================================' printNl.
  586. '大学図書館貸出システムを開始します' printNl.
  587. ('図書館名: ' , library name) printNl.
  588. '========================================' printNl.
  589.  
  590. library showBooks.
  591. library showMembers.
  592.  
  593. loan1 := library
  594. lendISBN: '978-4-00-000001-1'
  595. toMember: 'S2023001'
  596. onDay: 100.
  597.  
  598. '' printNl.
  599. '===== 貸出処理結果1 =====' printNl.
  600. loan1 printString printNl.
  601.  
  602. loan2 := library
  603. lendISBN: '978-4-00-000002-8'
  604. toMember: 'T1001'
  605. onDay: 105.
  606.  
  607. '' printNl.
  608. '===== 貸出処理結果2 =====' printNl.
  609. loan2 printString printNl.
  610.  
  611. library showActiveLoans.
  612. library showBooks.
  613.  
  614. library showOverdueLoansOn: 120.
  615.  
  616. returnedLoan := library
  617. returnISBN: '978-4-00-000001-1'
  618. onDay: 121.
  619.  
  620. '' printNl.
  621. '===== 返却処理結果 =====' printNl.
  622. returnedLoan printString printNl.
  623.  
  624. library showActiveLoans.
  625. library showBooks.
  626.  
  627. library showOverdueLoansOn: 140.
  628. library showLoanHistory.
  629.  
  630. '' printNl.
  631. '========================================' printNl.
  632. '大学図書館貸出システムを終了します' printNl.
  633. '========================================' printNl
  634. ]
Success #stdin #stdout 0.01s 9872KB
stdin
Standard input is empty
stdout
'========================================'
'大学図書館貸出システムを開始します'
'図書館名: ○○大学図書館'
'========================================'
''
'===== 図書一覧 ====='
'Smalltalk入門 / 山田太郎 [ISBN: 978-4-00-000001-1] 貸出可能'
'オブジェクト指向設計 / 佐藤花子 [ISBN: 978-4-00-000002-8] 貸出可能'
'データベース基礎 / 鈴木一郎 [ISBN: 978-4-00-000003-5] 貸出可能'
''
'===== 利用者一覧 ====='
'田中学生 [利用者番号: S2023001, 区分: 学生, 貸出上限: 3冊]'
'佐々木教授 [利用者番号: T1001, 区分: 教職員, 貸出上限: 10冊]'
''
'===== 貸出処理結果1 ====='
'田中学生 -> Smalltalk入門 / 貸出日: 100 / 返却期限: 114 / 貸出中'
''
'===== 貸出処理結果2 ====='
'佐々木教授 -> オブジェクト指向設計 / 貸出日: 105 / 返却期限: 135 / 貸出中'
''
'===== 現在の貸出一覧 ====='
'田中学生 -> Smalltalk入門 / 貸出日: 100 / 返却期限: 114 / 貸出中'
'佐々木教授 -> オブジェクト指向設計 / 貸出日: 105 / 返却期限: 135 / 貸出中'
''
'===== 図書一覧 ====='
'Smalltalk入門 / 山田太郎 [ISBN: 978-4-00-000001-1] 貸出中'
'オブジェクト指向設計 / 佐藤花子 [ISBN: 978-4-00-000002-8] 貸出中'
'データベース基礎 / 鈴木一郎 [ISBN: 978-4-00-000003-5] 貸出可能'
''
'===== 延滞一覧 ====='
'確認日: 120日目'
'田中学生 -> Smalltalk入門 / 貸出日: 100 / 返却期限: 114 / 貸出中'' / 延滞日数: '6'日'
''
'===== 返却処理結果 ====='
'田中学生 -> Smalltalk入門 / 貸出日: 100 / 返却期限: 114 / 返却日: 121 / 返却済み'
''
'===== 現在の貸出一覧 ====='
'佐々木教授 -> オブジェクト指向設計 / 貸出日: 105 / 返却期限: 135 / 貸出中'
''
'===== 図書一覧 ====='
'Smalltalk入門 / 山田太郎 [ISBN: 978-4-00-000001-1] 貸出可能'
'オブジェクト指向設計 / 佐藤花子 [ISBN: 978-4-00-000002-8] 貸出中'
'データベース基礎 / 鈴木一郎 [ISBN: 978-4-00-000003-5] 貸出可能'
''
'===== 延滞一覧 ====='
'確認日: 140日目'
'佐々木教授 -> オブジェクト指向設計 / 貸出日: 105 / 返却期限: 135 / 貸出中'' / 延滞日数: '5'日'
''
'===== 全貸出履歴 ====='
'田中学生 -> Smalltalk入門 / 貸出日: 100 / 返却期限: 114 / 返却日: 121 / 返却済み'
'佐々木教授 -> オブジェクト指向設計 / 貸出日: 105 / 返却期限: 135 / 貸出中'
''
'========================================'
'大学図書館貸出システムを終了します'
'========================================'