编辑代码

import Foundation  
  
// 定义三个菜单的字典  
let menu1: [String: Double] = ["电视": 2000.0, "冰箱": 3000.0, "洗衣机": 1500.0] // 家用品  
let menu2: [String: Double] = ["苹果": 5.0, "香蕉": 3.0, "面包": 10.0] // 食品  
let menu3: [String: Double] = ["笔": 2.0, "纸张": 1.0, "文件夹": 10.0] // 办公用品  
  
// 定义下单情况的字典  
var mymenu: [String: Int] = [:]  
  
// 无限循环,等待用户输入  
while true {  
    // 读取用户输入  
    let userInput = readLine()?.trimmingCharacters(in: .whitespacesAndNewlines)  
    if userInput == "y" {  
        break // 退出循环  
    }  
      
    // 输出菜单供用户选择  
    print("1. 家用品")  
    for (product, price) in menu1 {  
        print("\(product): \(price)")  
    }  
    print("2. 食品")  
    for (product, price) in menu2 {  
        print("\(product): \(price)")  
    }  
    print("3. 办公用品")  
    for (product, price) in menu3 {  
        print("\(product): \(price)")  
    }  
      
    // 读取用户选择的菜单类型  
    if let menuChoice = Int(readLine()?.trimmingCharacters(in: .whitespacesAndNewlines)) {  
        switch menuChoice {  
        case 1:  
            let menu = menu1  
            // 读取商品编号  
            if let productID = readLine()?.trimmingCharacters(in: .whitespacesAndNewlines) {  
                if let product = menu.first(where: { $0.key == productID }) {  
                    let productName = product.key  
                    let productPrice = product.value  
                      
                    // 读取商品名称(这里其实已经知道了商品名称,但为了符合题目要求)  
                    let inputProductName = readLine()?.trimmingCharacters(in: .whitespacesAndNewlines)  
                    if inputProductName == productName {  
                        // 读取购买数量  
                        if let numb = Int(readLine()?.trimmingCharacters(in: .whitespacesAndNewlines)) {  
                            mymenu[productName, default: 0] += numb  
                            print("已将\(productName)添加到您的订单中,数量为\(numb)个。")  
                        } else {  
                        print("输入无效,请输入一个有效的购买数量。")  
                        }  
                    } else {  
                        print("商品名称不匹配,请重新输入。")  
                    }  
                } else {  
                    print("无效的商品编号,请重新输入。")  
                }  
            } else {  
                print("输入无效,请输入一个有效的商品编号。")  
            }  
              
        case 2:  
            let menu = menu2  
            // ... 同上处理食品菜单  
              
        case 3:  
            let menu = menu3  
            // ... 同上处理办公用品菜单  
              
        default:  
            print("无效的菜单选择,请重新输入。")  
        }  
    } else {  
        print("输入无效,请输入一个有效的菜单编号。")  
    }  
}  
  
// 打印小票  
print("====小票=====")  
print("名称\t数量\t*\t单价\t=\t价格")  
print("--------------------")  
  
var totalCost: Double = 0.0  
for (product, numb) in mymenu {  
    if let price = (menu1[product] ?? menu2[product] ?? menu3[product]) {  
        let cost = price * Double(numb)  
        totalCost += cost  
        print("\(product)\t\(numb)\t*\t\(price)\t=\t\(cost)")  
    }  
}  
  
print("--------------------")  
print("总价: \(totalCost)")  
print("感谢您的光临,祝您生活愉快!")