키오스크의 구조를 만드는 프로젝트를 시작했다.
스타벅스의 주문 절차를 만들기로 했고, 우선 따로 만들고 리뷰하며 합치기로 했다.
먼저 내가 처음으로 만든 코드다.
import Foundation
+print("스타벅스 입니다. 주문하려면 1을 눌러주세요")
+func showMenu() {
+ print("1.음료\n2.푸드\n3.상품\n0.종료")
+}
+
+
+func showBeverage() { //음료
+ print("1.커피\n2.피지오\n3.프라푸치노")
+}
+
+ func showCoffee() { //음료-커피
+ print("1.아메리카노 4,500원 아이스/ 핫\n2.카페라떼 5,000원 아이스/ 핫\n3.콜드브루 4,900원 Iced Only")
+ }
+ func showFizzio() { //음료-피지오
+ print("1.유자 패션 피지오 5,900원\n2.쿨 라임 피지오 5,900원\n3.피치 딸기 피지오 5,700원")
+ }
+ func showFra() { //음료-프라푸치노
+ print("1.자바 칩 프라푸치노 6,300원\n2.카라멜 프라푸치노 5,900\n3.그린티 프라푸치노 6,300원")
+ }
-if pressedNum == 1 {
- print("1.아메리카노\n2.카페라떼\n3.에스프레소")
+func showFood() { //푸드
+ print("1.바비큐 치킨 치즈 치아바타 5,800원원\n2.바질 토마토 그림치즈 베이글 5,300원\n3.베이컨 치즈 토스트 4,900원")
+}
+
+
+func showGoods() {
+ print("텀블러")
+}
+
+while let input = readLine(), let pressedNum = Int(input){
+ if pressedNum == 0 {
+ break
+ }
+ else if pressedNum == 1 {
+ showMenu()
+ if let menuInput = readLine(), let menuNum = Int(menuInput){
+ if menuNum == 1 {
+ showBeverage()
+ if let bevInput = readLine(), let bevNum = Int(bevInput){
+
+ if bevNum == 1 {
+ showCoffee()
+ }
+ else if bevNum == 2 {
+ showFizzio()
+ }
+ else if bevNum == 3 {
+ showFra()
+ }
+ else if bevNum == 0 {
+ }
+ }
+ else if menuNum == 2 {
+ showFood()
+ }
+ else if menuNum == 3 {
+ showGoods()
+ }
+ }
+ }
+ }
}
처음에는 우선 메뉴, 음료, 커피, 피지오, 프라푸치노, 푸드, 상품을 프린트 하는 함수를 따로 만든다음 조작하도록 했다.
이 코드에서 두가지 문제가 있었는데
1. 뒤로가기를 구현하지 못했고
2. 금액이 string으로 되어있어 이후에 금액을 비교하는 작업을 할 수 없었다.
이후 코드 역시 뒤로가기는 구현하지 못했지만 struct를 이용했기에 금액을 Int로 접근할 수 있다.
<수정된 코드>
import Foundation
struct menuItem {
let name: String
let price: Int
}
var coffees : [menuItem] = [
menuItem(name: "아메리카노", price: 4500),
menuItem(name: "카페라떼", price: 5900),
menuItem(name: "콜드브루", price: 4900)
]
var fizzios : [menuItem] = [
menuItem(name: "유자 패션 피지오", price: 5900),
menuItem(name: "쿨 라임 피지오", price: 5900),
menuItem(name: "피치 딸기 피지오", price: 5700)
]
var frappuccinos : [menuItem] = [
menuItem(name: "자바 칩 프라푸치노", price: 6300),
menuItem(name: "카라멜 프라푸치노", price: 5900),
menuItem(name: "그린티 프라푸치노", price: 6300)
]
var foods : [menuItem] = [
menuItem(name: "바비큐 치킨 치즈 치아바타", price: 5800),
menuItem(name: "바질 토마토 크림치즈 베이글", price: 5300),
menuItem(name: "베이컨 치즈 토스트", price: 4900)
]
var goods : [menuItem] = [
menuItem(name: "SS 스탠리 그린 캔처 텀블러 591ml", price: 39000),
menuItem(name: "SS 스탠리 크림 캔처 텀블러 591ml", price: 39000),
menuItem(name: "SS 엘마 블랙 텀블러 473ml", price: 33000)
]
func showMenu() {
print("1.음료\n2.푸드\n3.상품\n0.종료")
}
func showBeverage() { //음료
print("1.커피\n2.피지오\n3.프라푸치노\n0.뒤로가기")
}
func showCoffees() {
for (index, item) in coffees.enumerated(){
print("\(index+1). \(item.name) \(item.price)원")
}
}
func showFizzios(){
for (index, item) in fizzios.enumerated(){
print("\(index+1). \(item.name) \(item.price)원")
}
}
func showFrappuccions(){
for (index, item) in frappuccinos.enumerated(){
print("\(index+1). \(item.name) \(item.price)원")
}
}
func showFoods(){
for (index, item) in foods.enumerated(){
print("\(index+1). \(item.name) \(item.price)원")
}
}
func showGoods(){
for (index, item) in goods.enumerated(){
print("\(index+1). \(item.name) \(item.price)원")
}
}
print("스타벅스 입니다. 주문하려면 1을 눌러주세요")
while let input = readLine(), let pressedNum = Int(input){
if pressedNum == 0 {
break
}
else if pressedNum == 1{
showMenu()
if let menuInput = readLine(), let menuNum = Int(menuInput){
if menuNum == 1{
showBeverage()
if let inputBevNum = readLine(), let bevNum = Int(inputBevNum){
if bevNum == 1{
showCoffees()
print("0. 뒤로가기")
}
else if bevNum == 2{
showFizzios()
print("0. 뒤로가기")
}
else if bevNum == 3{
showFrappuccions()
print("0. 뒤로가기")
}
}
}
else if menuNum == 2{
showFoods()
print("0. 뒤로가기")
}
else if menuNum == 3{
showGoods()
print("0. 뒤로가기")
}
}
}
}