5. A travelling salesman records his mileage at the end of each week in order to make an accurate claim for expenses.
Write a Java code:
* With a class called Travelling which has a calculateExpenses method which is to prompt the user to input the date and the number of kilometres travelled.
* Perform a validation to ensure that the number of kilometres is an integer greater than 0.
* The method should be written within the same class:
- The first 100 kilometres is paid at R1.20 per kilometre.
- If the of kilometres is more than 100, all additional kilometres are paid at R1.50 per kilometre
* Output:
- The date : 04/28/2022
- The number of kilometres travelled: 200
- The total expenses due : R270.0
class Travelling {
calculateExpenses (date, number) {
if(number <= 0) {
console.log('Incorrect number of km.')
return;
}
let total = 0;
for(let i = 1; i <= number; i++) {
total += i < 100 ? 1.20 : 1.50;
}
console.log('The date: ' + date);
console.log('The number of kilometres travelled: ' + number);
console.log('The total expenses due : R' + total);
}
}
Comments
Leave a comment