google-developer-training / basic-android-kotlin-compose-training-dice-roller

Apache License 2.0
57 stars 49 forks source link

Practice problems Unit 2 Pathway 1: Android Basics with Compose #154

Open GaelBamana opened 2 months ago

GaelBamana commented 2 months ago

A --- name: Practice problems unit 2 pathway 1 issue template about: Issue template for Unit 1 | Pathway 1 | Practice Problems title: 'Practice problems Unit 2 Pathway 1: Android Basics with Compose' labels: '' assignees: ''


URL of codelab https://developer.android.com/codelabs/basic-android-kotlin-compose-kotlin-fundamentals-practice-problems?hl=fr&continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-2-pathway-1%3Fhl%3Dfr%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-kotlin-fundamentals-practice-problems#7

In which task and step of the codelab can this issue be found? Training: basic principles of Kotlin, POO and Lambda

Describe the problem

Hello, My question i's about the codelab : Special auction.

In the context of an auction, if the bid (the bid amount) is lower than the minimum price, the program should return the minimum price rather than the bid amount. This ensures that the item is never sold below the minimum price.

The current solution code of the Code Lab returns bid.amount if the auction exists, regardless of the amount, which is not what the statement seems to require. In the code Lab excecice we can read : "At an auction, the highest bid generally determines the price of an item."

Here is the code solution of the Codelab :

fun main() { val winningBid = Bid(5000, "Private Collector")

println("Item A is sold at ${auctionPrice(winningBid, 2000)}.")
println("Item B is sold at ${auctionPrice(null, 3000)}.")

}

class Bid(val amount: Int, val bidder: String)

fun auctionPrice(bid: Bid?, minimumPrice: Int): Int { return bid?.amount ?: minimumPrice }

// Out put : // Item A is sold at 5000. // Item B is sold at 3000.

this is my proposal for a possible solution :

fun main() { val winningBid = Bird(1900, "Private Collector")

println("Item A is sold at ${auctionPrice(winningBid, 2000)}.")
println("Item B is sold at ${auctionPrice(null, 3000)}.")

}

class Bird(val amount: Int, val bidder: String)

fun auctionPrice(bird: Bird?, minimumPrice: Int): Int { return if (bird != null && bird.amount > minimumPrice) { bird.amount } else { minimumPrice } }

// Out put : // Item A is sold at 1900. // Item B is sold at 3000.

Explanation:

Checking the auction : Condition bid != null && bid.amount > minimumPrice : This condition checks if bid is not null and that bid.amount is greater than the minimum price. If is true, this means that there is a valid bid that exceeds the minimum price, and this bid is therefore used as the sale price.

Return of the minimum price : Otherwise, if bid is zero or the bid amount is less than or equal to the minimum price, the function returns the minimum price. This ensures that the item is never sold below the minimum price.

What do you think about ? Thank you for your answer.

Steps to reproduce?

  1. Use IntelliJ IDEA or Kotlin Playground