Advertisement
CR7CR7

phoneBill

Apr 12th, 2023
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. // Use const instead of let for variables that don't change
  2. const input = [
  3.     `31`,
  4.     '115'
  5. ];
  6.  
  7. // Use descriptive names for variables and functions
  8. const printOutput = this.print || console.log;
  9. const getInput = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  10.  
  11. // Use camelCase for variable names
  12. let messages = Number(getInput());
  13. let minutes = Number(getInput());
  14. let additional = 0;
  15. let taxes;
  16.  
  17. // Use constants for fixed values
  18. const FREE_MESSAGES = 20;
  19. const FREE_MINUTES = 60;
  20. const MESSAGE_FEE = 0.06;
  21. const MINUTE_FEE = 0.10;
  22. const TAX_RATE = 0.2;
  23. const BASE_FEE = 12;
  24.  
  25. // Use a function to calculate the additional fee and print the message
  26. function calculateAdditionalFee(used, free, fee, unit) {
  27.     let extra = used - free;
  28.     if (extra > 0) {
  29.         let charge = extra * fee;
  30.         additional += charge;
  31.         printOutput(`${extra} additional ${unit} for ${charge.toFixed(2)} levas`);
  32.     } else {
  33.         printOutput(`0 additional ${unit} for 0.00 levas`);
  34.     }
  35. }
  36.  
  37. // Call the function for messages and minutes
  38. calculateAdditionalFee(messages, FREE_MESSAGES, MESSAGE_FEE, 'messages');
  39. calculateAdditionalFee(minutes, FREE_MINUTES, MINUTE_FEE, 'minutes');
  40.  
  41. // Calculate and print the taxes and final bill
  42. if (additional > 0) {
  43.     taxes = additional * TAX_RATE;
  44.     printOutput(`${taxes.toFixed(2)} additional taxes`);
  45. } else {
  46.     printOutput('0.00 additional taxes');
  47. }
  48. let final = BASE_FEE + additional + taxes;
  49. printOutput(`${final.toFixed(2)} total bill`);
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement