Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GaelBamana opened this issue Aug 30, 2024 · 0 comments
Open

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

GaelBamana opened this issue Aug 30, 2024 · 0 comments

Comments

@GaelBamana
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant