This file contains hidden or 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
@Test | |
fun `buy two book should 10 percent discount`() { | |
val book1 = Book("1", 100) | |
val book2 = Book("2", 100) | |
cart.addBook(book1) | |
cart.addBook(book2) | |
priceShouldBe(180) | |
} |
This file contains hidden or 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 ShoppingCartTest { | |
private val cart = ShoppingCart() | |
@Test | |
fun `buy one book should no discount`() { | |
val book = Book("1", 100) | |
cart.addBook(book) | |
priceShouldBe(100) | |
} |
This file contains hidden or 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
@Test | |
fun `buy one book should no discount`() { | |
val cart = ShoppingCart() | |
cart.addBook(Book("1", 100)) | |
Assert.assertEquals(100, cart.calculateTotalPrice()) | |
} |
This file contains hidden or 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
@Test | |
fun `calculateTotalPrice() should return 180 when 2 books are added`() { | |
val cart = ShoppingCart() | |
cart.addBook(Book("1", 100)) | |
cart.addBook(Book("2", 100)) | |
assert(cart.calculateTotalPrice() == 180) | |
} |
This file contains hidden or 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
@Test | |
fun `calculateTotalPrice() should return 180 when 2 books are added`() { | |
val cart = ShoppingCart() | |
cart.addBook(Book("1", 100)) | |
cart.addBook(Book("2", 80)) | |
assert(cart.calculateTotalPrice() == 180) | |
} |
This file contains hidden or 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
@Test | |
fun `calculateTotalPrice() should return 100 when 1 book is added`() { | |
val cart = ShoppingCart() | |
cart.addBook(Book("1", 100)) | |
assert(cart.calculateTotalPrice() == 100) | |
} |
This file contains hidden or 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
@Test | |
fun `calculateTotalPrice() should return 0 when no book is added`() { | |
val cart = ShoppingCart() | |
assert(cart.calculateTotalPrice() == 0) | |
} |
This file contains hidden or 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 ShoppingCart { | |
private val books = mutableListOf<Book>() | |
fun addBook(book: Book) { | |
books.add(book) | |
} | |
fun calculateTotalPrice(): Int { | |
if (books.count() >= 3) { | |
return (books.sumOf { it.price } * 0.8).toInt() |
This file contains hidden or 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
/** | |
*計算購物車總金額 | |
* 300元以上打八折 | |
* 200元以上打七折 | |
*/ | |
fun calculateTotalPrice(): Int { | |
val prices = books.sumOf { it.price } | |
var discount = 1.0 | |
if (prices >= 300) { |
This file contains hidden or 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
//add glide dependency | |
implementation 'com.github.bumptech.glide:glide:4.12.0' |
NewerOlder