Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. %% BM20A5001 Principles of Technical Computing
  2. % Matylda Jablonska-Sabuka
  3. % Exercise list 4, Task 4
  4.  
  5. clc
  6. clear all
  7. close all
  8.  
  9.  
  10. % ask for the car class
  11. fprintf('Which car class would you like to rent?\n')
  12. fprintf('B - economy\n')
  13. fprintf('C - family\n')
  14. fprintf('D - luxury\n')
  15. c = input('Choose the class: ','s');
  16. % the 's' at the end of input function tells Matlab to trear whatever input
  17. % as a string
  18.  
  19. % If the entered class type is completely wrong
  20. while c~='B'&c~='C'&c~='D'
  21. % inform the user and ask for the class again:
  22. fprintf('Class %s does not exist in our selection.\n',c)
  23. fprintf('Choose one of the available classes:\n')
  24. fprintf('B - economy\n')
  25. fprintf('C - family\n')
  26. fprintf('D - luxury\n')
  27. c = input('Choose the class: ','s');
  28. end
  29.  
  30. % ask for the rental period
  31. p = input('For how many days would you like to rent the car? ');
  32. % option variable o will be used later
  33. o = 1;
  34.  
  35. % looping for correct input from the user
  36. % as long as the user puts in too long rental period or class D with too
  37. % short period
  38. while p>60 | (p<7 & c=='D')
  39. % if the rental period is too long
  40. if p>60
  41. % inform the user and ask for input again
  42. p = input('Rental for over 60 days is not possible.\nInsert the period again: ');
  43. % otherwise it means that the class+rental period combination was not
  44. % right, so we ask the user what he wants to do:
  45. else
  46. fprintf('\nClass D cannot be rented for less than 7 days.\n')
  47. fprintf('Would you like to:\n')
  48. fprintf('- Change the car class (choose c)\n')
  49. fprintf('- Change the rental period (choose p)\n')
  50. fprintf('- Quit (choose q)\n')
  51. o = input('\nChoose the option: ','s');
  52. if o == 'c'
  53. c = input('\nWhich car class would you like to rent? ','s');
  54. elseif o == 'p'
  55. p = input('\nFor how many days would you like to rent the car? ');
  56. elseif o == 'q'
  57. fprintf('\nThank you for using our service. Goodbye!\n');
  58. break
  59. else
  60. while o ~= 'c' | o ~= 'p'
  61. fprintf('\nWrong option.\n')
  62. o = input('Choose the option again: ','s');
  63. end
  64. end
  65. end
  66. end
  67.  
  68. if o ~='q'
  69.  
  70. % define the rental price components
  71. d = [27 25 23
  72. 34 31 28
  73. 0 43 38];
  74.  
  75. period = [162 662
  76. 204 884
  77. 276 1136];
  78. t1 = 7;
  79. t2 = 28;
  80.  
  81. if p<t1 & c == 'B'
  82. rental = p*d(1,1);
  83. elseif p<t1 & c =='C'
  84. rental = p*d(2,1);
  85. elseif p<t2 & c == 'B'
  86. rental = period(1,1) + (p-t1)*d(1,2);
  87. elseif p<t2 & c == 'C'
  88. rental = period(2,1) + (p-t1)*d(2,2);
  89. elseif p<t2 & c == 'D'
  90. rental = period(3,1) + (p-t1)*d(3,2);
  91. elseif p>=t2 & c == 'B'
  92. rental = period(1,2) + (p-t2)*d(1,3);
  93. elseif p>=t2 & c == 'C'
  94. rental = period(2,2) + (p-t2)*d(2,3);
  95. elseif p>=t2 & c == 'D'
  96. rental = period(3,2) + (p-t2)*d(3,3);
  97. end
  98.  
  99. fprintf('\nThe rental of class %s for %d day(s) will cost %d euros.\n',c,p,rental)
  100.  
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement