Q:

Write a menu-driven program that will read in an employee data base for a company from a file, and do specified operations on the data. The file stores the following information for each employee:

0

 Name

 Department

 Birth Date

 Date Hired

 Annual Salary

 Office Phone Extension

You are to decide exactly how this information is to be stored in the 

file. Design the layout of the file, and then create a sample data file in 

this format to use when testing your program. The format of the file is 

up to you. However, space is critical. Do not use any more characters 

in your file than you have to! Your program is to read the information 

from the file into a data structure, and then display a menu of options 

for operations to be done on the data. You may not assume in your 

program that you know the length of the data file. The menu options 

are:

1. Print all of the information in an easy-to-read format to a new file.

2. Print the information for a particular department.

3. Calculate the total payroll for the company (the sum of the salaries).

4. Find out how many employees have been with the company for N 

years (N might be 10, for example).

5. Exit the program.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch9Ex31.m

% This script creates an employee data base for a company

% and performs some operations on the data

% Read the info from a file

employees = reademployees;

% Call a function to display a menu and get choice

choice = options;

while choice ~= 5

 switch choice

 case 1

 % Prints all of the info to a file

 printall(employees)

 case 2

 % Prints info for one department

 printdept(employees)

 case 3

 % Prints total payroll

 payroll([employees.salary])

 case 4

 % Prints employees >= N years

 nyears(employees)

 end

 % Display menu again and get user's choice

 choice = options;

end

reademployees.m

function emp = reademployees

% Function stub

emp(2).name = 'Smith, Jane';

emp(2).dept = 'Service';

emp(2).birth = '072267';

emp(2).hired = '121298';

emp(2).salary = 87333;

emp(2).phone = '5388';

emp(1).name = 'Smith, Joe';

emp(1).dept = 'Sales';

emp(1).birth = '072267';

emp(1).hired = '121288';

emp(1).salary = 77333;

emp(1).phone = '5389';

end

options.m

function choice = options

% options prints the menu of options and error-checks

% until the user pushes one of the buttons

choice = menu('Choose an option', 'Print all', ...

 'Print dept', 'Payroll', 'N years', 'Exit Program');

% If the user closes the menu box rather than 

% pushing one of the buttons, choice will be 0 

while choice == 0

disp('Error-please choose one of the options.') 

choice = menu('Choose an option', 'Print all', ...

 'Print dept', 'Payroll', 'N years', 'Exit Program');

end

end

printall.m

function printall(emp)

% Write to screen; could change to write to file

fprintf('%-15s%-8s%11s%11s %-10s %5s\n\n', 'Name', 'Dept', ...

 'Birth Date', 'Hire Date', 'Salary', 'Phone')

for i = 1:length(emp)

 fprintf('%-15s%-8s', emp(i).name, emp(i).dept)

 b = emp(i).birth;

 birthdate = sprintf('%s-%s-19%s',b(1:2),b(3:4),b(5:6));

 h = emp(i).hired;

 hiredate = sprintf('%s-%s-19%s',h(1:2),h(3:4),h(5:6));

 fprintf('%11s%11s', birthdate, hiredate)

 fprintf(' $%9.2f x%s\n', emp(i).salary, emp(i).phone)

end

end

printdept.m

function printdept(emp)

choice = menu('Choose Dept', 'Sales', ...

 'Service', 'Trucking');

% If the user closes the menu box rather than 

% pushing one of the buttons, choice will be 0 

while choice == 0

 disp('Error-please choose one of the options.') 

 choice = menu('Choose Dept', 'Sales', ...

 'Service', 'Trucking');

end

ca = {'Sales','Service','Trucking'};

chosen = ca{choice};

fprintf('%-15s%-8s%11s%11s %-10s %5s\n\n', 'Name', 'Dept', ...

 'Birth Date', 'Hire Date', 'Salary', 'Phone')

for i = 1:length(emp)

 if strcmp(emp(i).dept, chosen)

 fprintf('%-15s%-8s', emp(i).name, emp(i).dept)

 b = emp(i).birth;

 birthdate = sprintf('%s-%s-19%s',b(1:2),b(3:4),b(5:6));

 h = emp(i).hired;

 hiredate = sprintf('%s-%s-19%s',h(1:2),h(3:4),h(5:6));

 fprintf('%11s%11s', birthdate, hiredate)

 fprintf(' $%9.2f x%s\n', emp(i).salary, emp(i).phone)

 end

end

end

payroll.m

function payroll(salaries)

fprintf('The total of the salaries is $%.2f.\n\n', ...

 sum(salaries))

end

nyears.m

function nyears(emps)

% Only considers years

hiredyears = zeros(1, length(emps));

for i = 1:length(emps)

 hiredyears(i) = str2num(emps(i).hired(5:6)) + 1900;

end

current_year = 2013;

N = 20;

fy = find(current_year-hiredyears >= N);

fprintf('%d employees have worked at least %d years.\n\n', ...

 length(fy), N)

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now