我正在尝试制作一个简单的图书馆应用程序,并且我正在尝试实现一种人们可以借书的方法。到目前为止,我有 2 节课,一门是针对上述人员的,另一门是针对书籍/文件的。
person(adherant) 类(包括一个测试用例):
require_relative 'document'
class Adherant
attr_reader :id,:nom, :prenom,:emprunt
def initialize(id,nom, prenom)
@id=id
@nom = nom
@prenom = prenom
end
def emprunt
@emprunt =0
end
def to_s
"Adherant N°: #{@id}- #{@nom} #{@prenom}"
end
def emprunter(other)
if other.empruntable==true && @emprunt <5
puts "Vous avez emprunter: #{other.titre}"
other.empruntable==false #there was a trailing `=` causing problems too
@emprunt+=1
else
puts "#{@emprunt.inspect} #{other.empruntable.inspect}"
puts "#{other.titre} est deja emprunter!"
end
end
end
livr= Document.new("Harry Potter And The Goblet Of Fire",12345,"J.K Rowling","livre")
test = Adherant.new(123,"Doe","John")
test.emprunter(livr)
文档类:
class Document
attr_accessor :titre, :isbn, :auteur,:type,:empruntable
def initialize(titre, isbn, auteur,type)
@titre = titre
@isbn = isbn
@auteur = auteur
@type = type
end
def self.empruntable
@empruntable = true
end
def to_s
"#{@type.capitalize}:#{@titre.capitalize}- #{@auteur.capitalize} #ISBN: #{@isbn.to_s.capitalize}"
end
end
输出:
nil nil
Harry Potter And The Goblet Of Fire est deja emprunter!
我不明白为什么坚持者@emprunt和文件@empruntable的价值为零,而我确保给他们一个价值。