Created
January 8, 2024 11:29
-
-
Save UkiDelly/ca6b7cfa36501dec6def5a19f96f873a to your computer and use it in GitHub Desktop.
과제
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
| package backend_challenge | |
| fun main() { | |
| val martOwner = MartOwner() | |
| val customer = Customer(1) | |
| customer.addToCart(Ramen(), Milk(), Water()) | |
| val payment = martOwner.createPayment(customer.cart) | |
| val money = customer.pay(payment) | |
| martOwner.receivePayment(payment, money) | |
| } |
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
| package backend_challenge | |
| class Cart { | |
| val products = mutableListOf<Product>() | |
| } |
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
| package backend_challenge | |
| class Customer(val id: Int) { | |
| val cart = Cart() | |
| private var card = 1000000 | |
| fun addToCart(vararg product: Product) { | |
| cart.products.addAll(product) | |
| } | |
| fun removeFromCart(product: Product) { | |
| cart.products.remove(product) | |
| } | |
| fun pay(payment: Payment): Int { | |
| card -= payment.amount | |
| println("${payment.amount}를 결제합니다.") | |
| println("카드 잔액은 ${card}입니다.") | |
| return payment.amount | |
| } | |
| } | |
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
| package backend_challenge | |
| class MartOwner { | |
| private var money = 0 | |
| fun createPayment(cart: Cart): Payment { | |
| val amount = cart.products.sumOf { it.price } | |
| return Payment(1, amount) | |
| } | |
| fun receivePayment(payment: Payment, money: Int) { | |
| this.money += money | |
| println("${payment.amount}를 결제받았습니다.") | |
| } | |
| } |
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
| package backend_challenge | |
| class Payment(val id: Int, val amount: Int) |
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
| package backend_challenge | |
| abstract class Product( | |
| val id: Int, | |
| val name: String, | |
| val price: Int | |
| ) | |
| class Ramen : Product(1, "라면", 3000) | |
| class Milk : Product(2, "우유", 2500) | |
| class Water : Product(3, "생수", 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment