Skip to content

Instantly share code, notes, and snippets.

@UkiDelly
Created January 8, 2024 11:29
Show Gist options
  • Select an option

  • Save UkiDelly/ca6b7cfa36501dec6def5a19f96f873a to your computer and use it in GitHub Desktop.

Select an option

Save UkiDelly/ca6b7cfa36501dec6def5a19f96f873a to your computer and use it in GitHub Desktop.
과제
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)
}
package backend_challenge
class Cart {
val products = mutableListOf<Product>()
}
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
}
}
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}를 결제받았습니다.")
}
}
package backend_challenge
class Payment(val id: Int, val amount: Int)
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