🍥구현 기능
🍥구현하기
- 🔗github link
- MenuItem 클래스, MenuItemCollection 클래스, MyMenu 클래스: Lv2와 동일
- Kiosk.kt: Lv2와 동일
Screen 클래스
open abstract class Screen {
open fun run():String = ""
open fun run(menuResult:String){}
protected abstract fun getInput():String
protected abstract fun printScreen()
}
➖
FirstScreen 클래스
class FirstScreen:Screen(){
override fun run(): String {
return getInput()
}
override fun getInput():String{
printScreen()
return readLine() ?: ""
}
override fun printScreen(){
}
}
TakeOutScreen 클래스
class TakeOutScreen :Screen() {
override fun run():String{
}
override fun getInput():String{
}
override fun printScreen(){
}
}
MainScreen 클래스
class MainScreen() : Screen() {
override fun run():String{
}
override fun getInput():String{
}
override fun printScreen(){
}
}
class SelectMenuScreen: Screen() {
var menuItemCollection: MenuItemCollection? = null
override fun run(menuResult:String){
}
override fun getInput():String{
}
override fun printScreen(){
}
}
class ShoppingCartScreen: Screen() {
var isShoppingCartEmpty = true
val deleteFromShoppingCart = DeleteFromShoppingCart()
override fun run():String{
var input = ""
while(true) {
isShoppingCartEmpty = MyKiosk.shoppingCart.isEmpty()
input = getInput()
if(input == "b") return ""
else if(input == "d") deleteFromShoppingCart.run()
}
}
override fun getInput():String{
}
override fun printScreen(){
}
private fun printCostSum(){
}
}
- ShoppingCartScreen의 내부(inner) 클래스 DeleteFromShoppingCart
-> DeleteFromShoppingCartScreen 클래스로 파일 분리
- Screen 클래스를 상속하도록 변경
[장바구니에서 항목 삭제하기]
삭제할 항목의 번호를 입력하세요.
-> 1
선택한 항목이 삭제되었습니다.
class DeleteFromShoppingCart() : Screen() {
override fun run(): String {
val input = getInput()
MyKiosk.shoppingCart.removeMenuItemAt(input.toInt() - 1)
println("선택한 항목이 삭제되었습니다.")
return ""
}
override fun getInput(): String {
}
override fun printScreen() {
}
}
OrderScreen 클래스
class OrderScreen : Screen(){
var isShoppingCartEmpty = true
override fun run():String{
isShoppingCartEmpty = MyKiosk.shoppingCart.isEmpty()
val input = getInput()
if(input == "b") return "false"
if(input == "p") return "true"
return ""
}
override fun getInput():String{
}
override fun printScreen(){
}
private fun printCostSum(){
}
}
OrderComplete 클래스
class OrderCompleteScreen : Screen() {
override fun run():String{
printScreen()
return ""
}
override fun getInput(): String {
return ""
}
override fun printScreen(){
}
}
➖
MyKiosk 클래스
- OrderScreen 클래스가 Screen 클래스를 상속하며, run():String 함수를 override
-> run 함수의 반환타입이 Boolean에서 String으로 변경됨
- MyKiosk 클래스에서 orderScreen.run()의 반환값을 사용하는 조건문 수정
val isOrderComplete = orderScreen.run()
if(isOrderComplete == "true") break@mainLoop