feat: add constructor for Student class to parse object from string representation

- Updated `to_s` method to standardize string representation of Student object.
- Added `from_string` constructor that parses a formatted string and initializes the object with parsed data.
- Modified `set_contacts` to ensure contacts are only set via the defined method.
- Added validation for Git presence and contact details presence, ensuring data integrity.
- Updated tests in main.rb to validate the new constructor and string parsing logic.
pull/4/head
Artem-Darius Weber 4 months ago
parent 1407d3e6fd
commit 8858731565

@ -1,33 +1,38 @@
require_relative 'student' require_relative 'student'
begin begin
student1 = Student.new( student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @nora | Email: nora@example.com | Git: https://github.com/nora"
surname: 'Алексеевич', student_from_string = Student.from_string(student_string)
name: 'Артем-Дариус', puts "Создан объект из строки:\n#{student_from_string}"
patronymic: 'Вебер',
id: 1,
git: 'https://git.djft.ru'
)
student1.set_contacts(
phone: '+79891242223',
telegram: '@alstroemeria22',
email: 'no-replay@djft.ru'
)
student2 = Student.new( student1 = Student.new(
surname: 'Норакет', surname: 'Алексеевич',
name: 'Норакет', name: 'Артем-Дариус',
patronymic: 'Фамилия' patronymic: 'Вебер',
) id: 1,
git: 'https://git.djft.ru'
)
student2.set_contacts( student1.set_contacts(
phone: '+70000000000' phone: '+79891242223',
) telegram: '@alstroemeria22',
email: 'no-replay@djft.ru'
)
puts student1 student2 = Student.new(
puts '-' * 40 surname: 'Норакет',
puts student2 name: 'Норакет',
patronymic: 'Фамилия'
)
student2.set_contacts(
phone: '+70000000000'
)
puts student1
puts '-' * 40
puts student2
rescue ArgumentError => e rescue ArgumentError => e
puts "Ошибка: #{e.message}" puts "Ошибка: #{e.message}"
end end

@ -44,6 +44,27 @@ class Student
validate validate
end end
def self.from_string(student_string)
parts = student_string.split('|').map(&:strip)
surname, name, patronymic = parts[0].split(' ')
id = parts[1].split(': ').last.to_i
phone = parts[2].split(': ').last
telegram = parts[3].split(': ').last
email = parts[4].split(': ').last
git = parts[5].split(': ').last
new(
surname: surname,
name: name,
patronymic: patronymic,
id: id,
phone: phone,
telegram: telegram,
email: email,
git: git
)
end
def set_contacts(phone: nil, telegram: nil, email: nil) def set_contacts(phone: nil, telegram: nil, email: nil)
@phone = phone @phone = phone
@ -70,13 +91,10 @@ class Student
end end
def to_s def to_s
"Student: #{@surname} #{@name} #{@patronymic}\n" \ "#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \
"ID: #{@id || 'N/A'}\n" \ "Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Phone: #{@phone || 'N/A'}\n" \ "Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}"
"Telegram: #{@telegram || 'N/A'}\n" \ end
"Email: #{@email || 'N/A'}\n" \
"Git: #{@git || 'N/A'}"
end
private private

Loading…
Cancel
Save