Solution to Three employees in a company are up for a special pay increase. You are given … - Sikademy

Sept. 17, 2019

Archangel Macsika Sikademy Image

Archangel Macsika

Three employees in a company are up for a special pay increase. You are given a file, say Data.txt, with the following data: Miller Andrew 65789.87 5 Green Sheila 75892.56 6 Sethi Amit 74900.50 6.1 Each input line consists of an employee's last name, first name, current salary, and percent pay increase. For example, in the first input line, the last name of the employee is Miller, the first name is Andrew, the current salary is 65789.87, and pay increase is 5%. Write a program that reads data from the specified file and stores the output in the file Output.dat. For each employee, the data must be output in the following form: firstName lastName updatedSalary. Format the output of decimal numbers to two decimal places.

Solution in Ruby Programming Language


def data_transfer(from_path, to_path)
	if (File.exist?(from_path))
			File.open(from_path, "r") do |read_all_data|
				to_file = File.open(to_path, "w+")
				to_file.puts "First Name | Last Name | Updated Salary"
					while each_line = read_all_data.gets
						file_contents = (each_line[/\w+\s\w+\s(\d+[.]\d+)\s+(\d+[.]\d+|\d+)\Z/]).split(" ")
						to_file.puts "#{file_contents[0]}     |  #{file_contents[1]}   |   #{(file_contents[2].to_f * (file_contents[3].to_f/100)).round(2)}"
					end
				puts "Operation successful!"
			end
	else
		puts "Sorry, the requested file does not exist."
	end
end
data_transfer("Data.txt",  "output.dat")

If you would like to see a different solution or an implementation in a different programming language Kindly let us know