Read the code carefully before you run it.
class User(private val firstName: String, private val lastName: String) {
fun fullName(): String = firstName + " " + lastName
fun saveToDatabase() {
println("INSERT INTO users (name) VALUES ('" + fullName() + "')")
}
}
fun main() {
val user = User("Lena", "Fischer")
println(user.fullName())
user.saveToDatabase()
}
What does this print? The class holds user data: which of the two methods introduces a second reason to change, and why?
The function printOrderSummary does two jobs: it calculates the order total and it formats the output. Extract the calculation into a top-level function orderTotal(lines: List<OrderLine>): Double so the pricing rule has its own reason to change, and make printOrderSummary call it. The total is the sum of unitPrice * quantity; if that sum is greater than 100.0, apply a 5% discount. The printed output must stay exactly the same. Then call printOrderSummary with two lines in main.
Before you get a hint: what have you tried, and where exactly are you stuck?
1. What does the single-responsibility principle say a class should have?
2. A class both calculates a price and renders a PDF invoice. Why is that a problem?
3. Which question best helps you find responsibilities to separate?