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