Compare commits

..

No commits in common. 'lab2' and 'lab2.2' have entirely different histories.
lab2 ... lab2.2

BIN
.DS_Store vendored

Binary file not shown.

@ -1,88 +1,4 @@
# Lab 2 # Lab 2
> "И тут я обнаружил что случайно сделал 3-5 задачи в 1-2"
## Диаграмма классов:
```mermaid
classDiagram
class BinarySearchTree {
- root : Node
+ add(student : Student) : void
+ each(&block) : void
- insert(node : Node, student : Student) : Node
- in_order_traversal(node : Node, &block) : void
}
class Node {
- student : Student
- left : Node
- right : Node
+ Node(student : Student)
}
class Person {
- id : String
- git : String
- phone : String?
- telegram : String?
- email : String?
+ Person(id : String, git : String, phone: String, telegram: String, email: String)
+ phone=(String) : Boolean
+ telegram=(String) : Boolean
+ email=(String) : Boolean
+ surname_initials() : NotImplementedError
+ valid_phone_number() : Boolean <<class>>
+ valid_telegram() : Boolean <<class>>
+ valid_email() : Boolean <<class>>
+ git_present() : Boolean
+ contact_present() : Boolean
+ get_first_contact() : String
+ git=(git : String) : void
+ validate_id(id: String) : void <<class>>
+ validate_git(git : String) : void <<class>>
- valid_git?(git : String) : Boolean
- valid_id?(id : String) : Boolean
}
class StudentRepository {
+ read_from_txt(file_path : String) : List~Student~ <<class>>
+ write_to_txt(file_path : String, students : List~Student~) <<class>>
}
class StudentShort {
- surname_initials : String
+ get_surname_initials() : String
+ StudentShort(id : String, surname_initials : String, phone: String, telegram: String, email: String)
+ from_student(student : Student) : StudentShort <<class>>
+ from_string(id : String, info_string : String) : StudentShort <<class>>
+ to_s() : String
- parse_contact_string(contact_string: String) : Array<String> <<class>>
}
class Student {
- surname : String
- name : String
- patronymic : String
- birth_date : Date
- const NAME_REGEX : String
+ Student(id : String, git : String, phone: String, telegram: String, email: String, surname : String, name : String, patronymic : String, birth_date : Date)
+ from_string(student_string : String) : Student <<class>>
+ surname_initials() : String
+ to_s() : String
+ get_info() : String
+ surname=(surname : String)
+ name=(name : String)
+ patronymic=(patronymic : String)
+ birth_date=(birthdate : String)
+ valid_name?(name : String) : Boolean <<class>>
- name_initial(name : String) : String
- patronymic(patronymic : String) : String
}
BinarySearchTree o-- Node
Node o-- Student
Person <|-- Student
Person <|-- StudentShort
Student <.. StudentRepository
```

@ -1,46 +0,0 @@
class BinarySearchTree
include Enumerable
class Node
attr_accessor :student, :left, :right
def initialize(student)
@student = student
@left = nil
@right = nil
end
end
def initialize
@root = nil
end
def add(student)
@root = insert(@root, student)
end
def each(&block)
in_order_traversal(@root, &block)
end
private
def insert(node, student)
return Node.new(student) if node.nil?
if student.birth_date < node.student.birth_date
node.left = insert(node.left, student)
else
node.right = insert(node.right, student)
end
node
end
def in_order_traversal(node, &block)
return if node.nil?
in_order_traversal(node.left, &block)
yield node.student
in_order_traversal(node.right, &block)
end
end

@ -1,92 +1,59 @@
require 'date'
require_relative 'person'
require_relative 'student' require_relative 'student'
require_relative 'student_short' require_relative 'student_short'
require_relative 'student_repository'
require_relative 'binary_search_tree'
def test_classes begin
person = Person.new(id: '1', git: 'https://github.com/example', phone: '+798912465', telegram: '@example_user', email: 'test@example.com') student_string = "Норакет Норакет Норакет | ID: 2 | Phone: +1234567890 | Telegram: @noracat | Email: nora@example.com | Git: https://github.com/nora"
puts "Person Contact Info: #{person.get_first_contact}" student_from_string = Student.from_string(student_string)
puts "Создан объект из строки:\n#{student_from_string}"
student = Student.new(
id: '2',
git: 'https://github.com/student_example',
surname: 'Иванов',
name: 'Иван',
patronymic: 'Иванович',
birth_date: Date.new(2000, 5, 15),
phone: '+79891234567',
telegram: '@student_ivan',
email: 'ivanov@example.com'
)
puts "Student Full Info: #{student.to_s}"
puts "Student Initials: #{student.surname_initials}"
puts "Student Contact Info: #{student.get_first_contact}"
student_short = StudentShort.from_student(student)
puts "Student Short Info: #{student_short.to_s}"
file_path = 'students.txt'
students = StudentRepository.read_from_txt(file_path)
puts "Read Students from File:"
students.each { |s| puts s.to_s }
output_path = 'output_students.txt'
StudentRepository.write_to_txt(output_path, students)
puts "Students written to file: #{output_path}"
end
def test_parsing
student_string = 'Иванов Иван Иванович | ID: 3 | Phone: +79876543210 | Telegram: @ivan_user | Email: ivan@example.com | Git: https://github.com/ivanov | Birth Date: 2001-01-01'
student = Student.from_string(student_string)
puts "Parsed Student: #{student.to_s}"
short_string = 'Иванов И.И., Contact: Phone: +79876543210'
student_short = StudentShort.from_string('4', short_string)
puts "Parsed StudentShort: #{student_short.to_s}"
end
def test_binary_search_tree
bst = BinarySearchTree.new
student1 = Student.new( student1 = Student.new(
id: '1', surname: 'Алексеевич',
git: 'https://github.com/student1', name: 'Артем-Дариус',
surname: 'Смирнов', patronymic: 'Вебер',
name: 'Алексей', id: 1,
patronymic: 'Иванович', git: 'https://github.com/space-creator'
birth_date: Date.new(1999, 2, 15),
phone: '+123456789',
telegram: '@student1',
email: 'student1@example.com'
)
student2 = Student.new(
id: '2',
git: 'https://github.com/student2',
surname: 'Иванов',
name: 'Иван',
patronymic: 'Сергеевич',
birth_date: Date.new(2001, 5, 10),
phone: '+987654321',
telegram: '@student2',
email: 'student2@example.com'
) )
bst.add(student1) student1.set_contacts(
bst.add(student2) phone: '+79891242223',
telegram: '@alstroemeria22',
email: 'no-replay@djft.ru'
)
puts "Students in BST (sorted by birth_date):" puts student1.get_info
bst.each { |student| puts student.to_s } puts "Surname and Initials: #{student1.surname_and_initials}"
end puts "Git Info: #{student1.git_info}"
puts "Contact Info: #{student1.contact_info}"
if __FILE__ == $0 student2 = Student.new(
puts "=== Testing Classes ===" surname: 'Норакет',
test_classes name: 'Норакет',
patronymic: 'Фамилия'
)
puts "\n=== Testing Parsing ===" student2.set_contacts(
test_parsing phone: '+70000000000'
)
puts "\n=== Testing Binary Search Tree ===" puts student1
test_binary_search_tree puts '-' * 40
puts student2
student_short_from_student = StudentShort.new(student1)
puts "StudentShort from Student object:"
puts "ID: #{student_short_from_student.id}"
puts "Surname and Initials: #{student_short_from_student.surname_initials}"
puts "Git: #{student_short_from_student.git}"
puts "Contact: #{student_short_from_student.contact}"
student_short_from_string = StudentShort.from_string(5, 'Skye A.A., Git: https://github.com/skye, Contact: Phone: +4923467890')
puts "StudentShort from string:"
puts "ID: #{student_short_from_string.id}"
puts "Surname and Initials: #{student_short_from_string.surname_initials}"
puts "Git: #{student_short_from_string.git}"
puts "Contact: #{student_short_from_string.contact}"
rescue ArgumentError => e
puts "Ошибка: #{e.message}"
end end

@ -1,85 +1,65 @@
class Person class Person
attr_reader :id, :git, :phone, :telegram, :email attr_accessor :id, :git
def initialize(id:, git: nil, phone: nil, telegram: nil, email: nil) def initialize(args = {})
self.class.validate_id(id) @id = args[:id] || nil
self.phone = phone @git = args[:git]
self.telegram = telegram validate
self.email = email
raise ArgumentError, "Необходимо указать хотя бы один контакт (телефон, Telegram или email)" unless contact_present?
@id = id
@git = git
end end
def surname_initials def set_contacts(phone: nil, telegram: nil, email: nil)
raise NotImplementedError, "#{self.class} must implement the 'surname_initials' method" @phone = phone
end raise ArgumentError, "Invalid phone number format: #{@phone}" if @phone && !self.class.valid_phone_number?(@phone)
def git_present? @telegram = telegram
!@git.nil? && !@git.empty? raise ArgumentError, "Invalid telegram format: #{@telegram}" if @telegram && !self.class.valid_telegram?(@telegram)
end
def contact_present? @email = email
@phone || @telegram || @email raise ArgumentError, "Invalid email format: #{@email}" if @email && !self.class.valid_email?(@email)
end end
def get_first_contact def self.valid_phone_number?(phone)
[@phone, @telegram, @email].compact.first phone.match?(/\A\+?[0-9]{10,15}\z/)
end end
def self.valid_phone_number?(phone) def self.valid_name?(name)
/\A\+?[0-9]{9,15}\z/.match?(phone) name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
end end
def self.valid_telegram?(telegram) def self.valid_telegram?(telegram)
/\A@[A-Za-z0-9_]{5,32}\z/.match?(telegram) telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
end end
def self.valid_email?(email) def self.valid_email?(email)
/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/.match?(email) email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
end
def phone=(phone)
if phone && !self.class.valid_phone_number?(phone)
raise ArgumentError, "Недопустимый номер телефона: #{phone}"
end
@phone = phone
end end
def telegram=(telegram) def self.valid_git?(git)
if telegram && !self.class.valid_telegram?(telegram) git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
raise ArgumentError, "Некорректный Telegram: #{telegram}"
end
@telegram = telegram
end end
def email=(email) def git_present?
if email && !self.class.valid_email?(email) !@git.nil? && !@git.empty?
raise ArgumentError, "Некорректный email: #{email}"
end
@email = email
end end
def git=(git) def contact_present?
self.class.validate_git(git) !(@phone.nil? || @phone.empty?) || !(@telegram.nil? || @telegram.empty?) || !(@email.nil? || @email.empty?)
@git = git
end end
def self.validate_id(id) def validate
raise ArgumentError, 'ID is required and must be a non-empty string' unless valid_id?(id) raise ArgumentError, "Git link is required" unless git_present?
raise ArgumentError, "At least one contact (phone, telegram, or email) is required" unless contact_present?
end end
def self.validate_git(git) def contact_info
raise ArgumentError, 'Git link is required' if git.nil? || git.strip.empty? return "Phone: #{@phone}" if @phone
raise ArgumentError, 'Invalid Git link format' unless valid_git?(git) return "Telegram: #{@telegram}" if @telegram
return "Email: #{@email}" if @email
'No contact available'
end end
def self.valid_git?(git) private
/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/.match?(git)
end
def self.valid_id?(id) attr_reader :phone, :telegram, :email
id.is_a?(String) && !id.strip.empty? attr_writer :phone, :telegram, :email
end
end end

@ -1,103 +1,92 @@
require_relative 'person' require_relative 'person'
class Student < Person class Student < Person
attr_accessor :surname, :name, :patronymic, :birth_date attr_accessor :surname, :name, :patronymic
private def initialize(args = {})
super(args)
@surname = args.fetch(:surname)
raise ArgumentError, "Invalid surname format: #{@surname}" unless self.class.valid_name?(@surname)
NAME_REGEX = /\A[А-Яа-яЁёA-Za-z\-]+\z/ @name = args.fetch(:name)
raise ArgumentError, "Invalid name format: #{@name}" unless self.class.valid_name?(@name)
public @patronymic = args.fetch(:patronymic)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless self.class.valid_name?(@patronymic)
def initialize(id:, git:, surname:, name:, patronymic:, birth_date:, phone: nil, telegram: nil, email: nil) set_contacts(
super(id: id, git: git, phone: phone, telegram: telegram, email: email) phone: args[:phone],
self.surname = surname telegram: args[:telegram],
self.name = name email: args[:email]
self.patronymic = patronymic )
self.birth_date = birth_date
end end
def self.from_string(student_string) def self.from_string(student_string)
parts = student_string.split('|').map(&:strip) parts = student_string.split('|').map(&:strip)
raise ArgumentError, 'Invalid student string format' if parts.size < 7 surname, name, patronymic = parts[0].split(' ')
id = parts[1].split(': ').last.to_i
name_parts = parts[0].split(' ')
raise ArgumentError, 'Invalid name format' if name_parts.size != 3
surname, name, patronymic = name_parts
id = parts[1].split(': ').last
phone = parts[2].split(': ').last phone = parts[2].split(': ').last
telegram = parts[3].split(': ').last telegram = parts[3].split(': ').last
email = parts[4].split(': ').last email = parts[4].split(': ').last
git = parts[5].split(': ').last git = parts[5].split(': ').last
birth_date = Date.parse(parts[6].split(': ').last)
new( new(
id: id,
git: git,
surname: surname, surname: surname,
name: name, name: name,
patronymic: patronymic, patronymic: patronymic,
birth_date: birth_date, id: id,
phone: phone, phone: phone,
telegram: telegram, telegram: telegram,
email: email email: email,
git: git
) )
end end
def surname_initials def self.read_from_txt(file_path)
"#{@surname} #{name_initial(@name)}.#{patronymic_initial(@patronymic)}." raise IOError, "File path is invalid or file does not exist: #{file_path}" unless File.exist?(file_path)
end
def to_s
"#{@surname} #{@name} #{@patronymic} | ID: #{@id} | " \
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Email: #{@email || 'N/A'} | Git: #{@git} | Birth Date: #{@birth_date}"
end
def get_info students = []
"#{surname_initials}, Git: #{@git}, Contact: #{get_first_contact}, Birth Date: #{@birth_date}"
end
def surname=(surname) File.foreach(file_path) do |line|
raise ArgumentError, 'Surname is required' if surname.nil? || surname.strip.empty? line.strip!
raise ArgumentError, "Invalid surname format: #{surname}" unless self.class.valid_name?(surname) next if line.empty?
@surname = surname begin
student = from_string(line)
students << student
rescue ArgumentError => e
puts "Error processing line: '#{line}'. Reason: #{e.message}"
end end
def name=(name)
raise ArgumentError, 'Name is required' if name.nil? || name.strip.empty?
raise ArgumentError, "Invalid name format: #{name}" unless self.class.valid_name?(name)
@name = name
end end
def patronymic=(patronymic) students
raise ArgumentError, 'Patronymic is required' if patronymic.nil? || patronymic.strip.empty?
raise ArgumentError, "Invalid patronymic format: #{patronymic}" unless self.class.valid_name?(patronymic)
@patronymic = patronymic
end end
def birth_date=(birth_date) def self.write_to_txt(file_path, students)
raise ArgumentError, 'Birth date is required' if birth_date.nil? raise ArgumentError, "Expected an array of Student objects" unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
raise ArgumentError, "Invalid birth date: #{birth_date}" unless birth_date.is_a?(Date)
@birth_date = birth_date File.open(file_path, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end end
def self.valid_name?(name) puts "Data successfully written to #{file_path}"
NAME_REGEX.match?(name) rescue IOError => e
puts "An error occurred while writing to the file: #{e.message}"
end end
private def surname_and_initials
"#{@surname} #{name[0]}.#{patronymic[0]}."
end
def name_initial(name) def to_s
name[0].upcase "#{@surname} #{@name} #{@patronymic} | ID: #{@id || 'N/A'} | " \
"Phone: #{@phone || 'N/A'} | Telegram: #{@telegram || 'N/A'} | " \
"Email: #{@email || 'N/A'} | Git: #{@git || 'N/A'}"
end end
def patronymic_initial(patronymic) def get_info
patronymic[0].upcase "#{surname_and_initials}, Git: #{git_info}, Contact: #{contact_info}"
end end
end end

@ -1,39 +0,0 @@
require_relative 'student'
class StudentRepository
def self.read_from_txt(file_path)
raise IOError, "File does not exist: #{file_path}" unless File.exist?(file_path)
students = []
File.foreach(file_path) do |line|
line.strip!
next if line.empty?
begin
student = Student.from_string(line)
students << student
rescue ArgumentError => e
puts "Error processing line: '#{line}'. Reason: #{e.message}"
end
end
students
end
def self.write_to_txt(file_path, students)
unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
raise ArgumentError, 'Expected an array of Student objects'
end
File.open(file_path, 'w') do |file|
students.each do |student|
file.puts student.to_s
end
end
puts "Data successfully written to #{file_path}"
rescue IOError => e
puts "An error occurred while writing to the file: #{e.message}"
end
end

@ -1,59 +1,31 @@
require_relative 'person' require_relative 'person'
class StudentShort < Person class StudentShort < Person
attr_reader :surname_initials attr_reader :surname_initials, :contact
def initialize(id:, surname_initials:, phone: nil, telegram: nil, email: nil) def initialize(student)
super(id: id, phone: phone, telegram: telegram, email: email) super(id: student.id, git: student.git_info)
@surname_initials = surname_initials @surname_initials = student.surname_and_initials
freeze @contact = student.contact_info
end
def self.from_student(student)
new(
id: student.id,
surname_initials: student.surname_initials,
phone: student.phone,
telegram: student.telegram,
email: student.email
)
end end
def self.from_string(id, info_string) def self.from_string(id, info_string)
parts = info_string.split(', ').map(&:strip) parts = info_string.split(', ').map(&:strip)
raise ArgumentError, 'Invalid info string format' if parts.size < 2
surname_initials = parts[0] surname_initials = parts[0]
contact_string = parts[1].split(': ', 2).last.strip git = parts[1].split(': ').last
contact = parts[2].split(': ').last
phone, telegram, email = parse_contact_string(contact_string)
new( new_instance = allocate
id: id, new_instance.send(:initialize_from_data, id, surname_initials, git, contact)
surname_initials: surname_initials, new_instance
phone: phone,
telegram: telegram,
email: email
)
end
def to_s
contact_info = get_first_contact()
"#{@surname_initials}, Contact: #{contact_info}"
end end
private private
def self.parse_contact_string(contact_string) def initialize_from_data(id, surname_initials, git, contact)
case contact_string @id = id
when /\APhone: (.+)\z/i @surname_initials = surname_initials
[Regexp.last_match(1).strip, nil, nil] @git = git
when /\ATelegram: (.+)\z/i @contact = contact
[nil, Regexp.last_match(1).strip, nil]
when /\AEmail: (.+)\z/i
[nil, nil, Regexp.last_match(1).strip]
else
raise ArgumentError, "Invalid contact string format: #{contact_string}"
end
end end
end end

@ -1,3 +0,0 @@
Иванов Иван Иванович | ID: 1 | Phone: +12345678901 | Telegram: @ivanov_user | Email: ivanov@example.com | Git: https://github.com/ivanov
Петров Петр Петрович | ID: 2 | Phone: +98765432101 | Telegram: @petrov_user | Email: petrov@example.com | Git: https://github.com/petrov
Сидоров Сидор Сидорович | ID: 3 | Phone: +56789012345 | Telegram: @sidorov_user | Email: sidorov@example.com | Git: https://github.com/sidorov
Loading…
Cancel
Save