Created
July 7, 2020 03:42
-
-
Save kazimdsaidul/fb6ca92bf2bc18c1c0d4515476aa0738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleUnitTest { | |
@Test | |
fun test8Hours1Min(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:01", "2020-07-06 08:01:00") | |
assertEquals(calBill, 602.0, 0.0) | |
} | |
@Test | |
fun test1Second(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:00", "2020-07-06 00:00:01") | |
assertEquals(calBill, 1.0, 0.0) | |
} | |
@Test | |
fun test10Second(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:00", "2020-07-06 00:00:10") | |
assertEquals(calBill, 1.0, 0.0) | |
} | |
@Test | |
fun test2MinuteAnd10Second(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:00", "2020-07-06 00:02:10") | |
assertEquals(calBill, 3.0, 0.0) | |
} | |
@Test | |
fun test24Hours(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:00", "2020-07-06 23:59:01") | |
assertEquals(calBill, 3600.0, 0.0) | |
} | |
@Test | |
fun test2Days(){ | |
val calBill = calculateMobileBill("2020-07-06 00:00:00", "2020-07-07 23:59:01") | |
assertEquals(calBill, (3600.0 *2), 0.0) | |
} | |
fun calculateMobileBill(startDateAndTime: String?, endDateAndTime: String?): Double { | |
val format: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") | |
val start: LocalDateTime = LocalDateTime.parse(startDateAndTime, format) | |
val end: LocalDateTime = LocalDateTime.parse(endDateAndTime, format) | |
var totalBill = 0.0 | |
Stream.iterate(start, { date -> date.plusDays(1) }) | |
.limit(ChronoUnit.DAYS.between(start, end) + 1) | |
.forEach { | |
println("start "+start) | |
println("end "+end) | |
println("--------------") | |
val hourEnd = end.toLocalTime().hour | |
val minuteEnd = end.toLocalTime().minute | |
val secondEnd = end.toLocalTime().second | |
if (end.toLocalTime().hour<6){ | |
totalBill = calculateBlockBill(totalBill, secondEnd, minuteEnd, hourEnd, 1) | |
}else if (end.toLocalTime().hour<12){ | |
val remainHours = hourEnd - 6 | |
totalBill = calculateBlockBill( totalBill = totalBill, hour = 6, prices = 1) | |
totalBill = calculateBlockBill(totalBill = totalBill, hour = remainHours, minute = minuteEnd, second = secondEnd, prices = 2) | |
}else if (end.toLocalTime().hour<18){ | |
val remainHours = hourEnd - 12 | |
totalBill = calculateBlockBill( totalBill = totalBill, hour = 12, prices = 2) | |
totalBill = calculateBlockBill(totalBill = totalBill, hour = remainHours, minute = minuteEnd, second = secondEnd, prices = 3) | |
}else if (end.toLocalTime().hour<24){ | |
val remainHours = hourEnd - 18 | |
totalBill = calculateBlockBill( totalBill = totalBill, hour = 12, prices = 3) | |
totalBill = calculateBlockBill(totalBill = totalBill, hour = remainHours, minute = minuteEnd, second = secondEnd, prices = 4) | |
} | |
} | |
return totalBill | |
} | |
private fun calculateBlockBill(totalBill: Double = 0.0, second: Int=0, minute: Int=0, hour: Int=0, prices: Int=0): Double { | |
var bill = totalBill | |
bill += if (second == 0) 0 else 1 * prices | |
bill += if (minute == 0) 0 else minute * prices | |
bill += if (hour == 0) 0 else hour * prices * 60 | |
return bill | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment