Home > OS >  Billing phone calculator
Billing phone calculator

Time:10-12

I have the code for my phone bill calculator. It charges users 0.25 for every minute they were over the plan and 15% tax on the subtotal. Now I have to separate the methods to calculate the tax, overage fees, and final total by printing the itemized bill. Can you help me to separate the methods?

import java.util.*

fun main(args: Array<String>) {
    print("Enter the Total Minutes of Calls Made this Month: ")
    val scan = Scanner(System.`in`)
    val numberOfMinutes = scan.nextInt()

    var excessMinutes = numberOfMinutes - 60
    if (excessMinutes < 0) {
        excessMinutes = 0
    }
    val bill = 10   excessMinutes * 0.25
    val taxedBill = bill * 1.15

    println("\nTelephone Bill this Month = $taxedBill")
}

CodePudding user response:

That's already a pretty short function, so I'm guessing refactoring it like that is a homework exercise! You need to work out how to break the steps down into separate tasks that return a result.

Try replacing this:

var excessMinutes = numberOfMinutes - 60
if (excessMinutes < 0) {
    excessMinutes = 0
}

with this:

val excessMinutes = someFunction()

That's a val now, so you can't change it once you get the result. someFunction needs to do all the calculation, and return a result. What needs to go in that function? What type of value does it need to return? Do you need to pass any values into that function as parameters, so it can use them in the calculation?

Once you work that out, the rest should be pretty straightforward!

CodePudding user response:

You can refactor it like that

import java.util.* 

fun main() {
    print("Enter the Minutes of Calls Made this Month: ") 
    val scan = Scanner(System.`in`)
    val numberOfMinutes = scan.nextInt()
    var excessMinutes = numberOfMinutes - 60 
    if (excessMinutes < 0) {
        excessMinutes = 0
    }
    val bill = 10   excessMinutes * 0.25
    val fees = excessMinutes * 0.25
    val taxedBill = bill * 1.15
    val tax = taxedBill - bill
        
    println("\nOverage fees = $fees")
    println("\nTax = $tax")
    println("\nFinal total = $taxedBill")
}
  • Related