Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% BM20A5001 Principles of Technical Computing
- % Matylda Jablonska-Sabuka
- % Exercise list 4, Task 4
- clc
- clear all
- close all
- % ask for the car class
- fprintf('Which car class would you like to rent?\n')
- fprintf('B - economy\n')
- fprintf('C - family\n')
- fprintf('D - luxury\n')
- c = input('Choose the class: ','s');
- % the 's' at the end of input function tells Matlab to trear whatever input
- % as a string
- % If the entered class type is completely wrong
- while c~='B'&c~='C'&c~='D'
- % inform the user and ask for the class again:
- fprintf('Class %s does not exist in our selection.\n',c)
- fprintf('Choose one of the available classes:\n')
- fprintf('B - economy\n')
- fprintf('C - family\n')
- fprintf('D - luxury\n')
- c = input('Choose the class: ','s');
- end
- % ask for the rental period
- p = input('For how many days would you like to rent the car? ');
- % option variable o will be used later
- o = 1;
- % looping for correct input from the user
- % as long as the user puts in too long rental period or class D with too
- % short period
- while p>60 | (p<7 & c=='D')
- % if the rental period is too long
- if p>60
- % inform the user and ask for input again
- p = input('Rental for over 60 days is not possible.\nInsert the period again: ');
- % otherwise it means that the class+rental period combination was not
- % right, so we ask the user what he wants to do:
- else
- fprintf('\nClass D cannot be rented for less than 7 days.\n')
- fprintf('Would you like to:\n')
- fprintf('- Change the car class (choose c)\n')
- fprintf('- Change the rental period (choose p)\n')
- fprintf('- Quit (choose q)\n')
- o = input('\nChoose the option: ','s');
- if o == 'c'
- c = input('\nWhich car class would you like to rent? ','s');
- elseif o == 'p'
- p = input('\nFor how many days would you like to rent the car? ');
- elseif o == 'q'
- fprintf('\nThank you for using our service. Goodbye!\n');
- break
- else
- while o ~= 'c' | o ~= 'p'
- fprintf('\nWrong option.\n')
- o = input('Choose the option again: ','s');
- end
- end
- end
- end
- if o ~='q'
- % define the rental price components
- d = [27 25 23
- 34 31 28
- 0 43 38];
- period = [162 662
- 204 884
- 276 1136];
- t1 = 7;
- t2 = 28;
- if p<t1 & c == 'B'
- rental = p*d(1,1);
- elseif p<t1 & c =='C'
- rental = p*d(2,1);
- elseif p<t2 & c == 'B'
- rental = period(1,1) + (p-t1)*d(1,2);
- elseif p<t2 & c == 'C'
- rental = period(2,1) + (p-t1)*d(2,2);
- elseif p<t2 & c == 'D'
- rental = period(3,1) + (p-t1)*d(3,2);
- elseif p>=t2 & c == 'B'
- rental = period(1,2) + (p-t2)*d(1,3);
- elseif p>=t2 & c == 'C'
- rental = period(2,2) + (p-t2)*d(2,3);
- elseif p>=t2 & c == 'D'
- rental = period(3,2) + (p-t2)*d(3,3);
- end
- fprintf('\nThe rental of class %s for %d day(s) will cost %d euros.\n',c,p,rental)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement