首页 > 解决方案 > 我想不仅在“do catch”中使用“shop”,怎么办?

问题描述

此代码适用于除“商店”之外的任何地方

enum PossibleErrors: Error {
    case notInStock
    case notEnoughMoney
}
    
struct Item {
    var price: Int
    var count: Int
}

protocol forShop {
    func buyGood(withName name: String) throws -> Item
}

class Shop: forShop {
    var moneyInBank : Int
    var stocks = ["Item1" : Item(price: 13, count: 4),"Item2" : Item(price: 12, count: 1), "Item3" : Item(price: 10, count: 0), "Item4" : Item(price: 50, count: 12)]
    
    init(moneyInBank: Int) throws {
        self.moneyInBank = moneyInBank
        if moneyInBank <= 9  {
            throw PossibleErrors.notEnoughMoney
        }
    }
}

extension Shop {
    func buyGood(withName name: String) throws -> Item {
        guard var Item = stocks[name] else {
            throw PossibleErrors.notInStock
        }
        guard Item.count > 0 else {
            throw PossibleErrors.notInStock
        }
        guard moneyInBank >= Item.price else {
            throw PossibleErrors.notEnoughMoney
        }
        moneyInBank -= Item.price
        Item.count -= 1
        return Item
    }
}

do {
    let shop = try Shop(moneyInBank: 8)
} catch PossibleErrors.notEnoughMoney {
    print("Work harder to buy it!")
}
do {
    try shop.buyGood(withName: "Item4")
} catch PossibleErrors.notInStock {
    print("Shop hasn't any")
} catch PossibleErrors.notEnoughMoney {
    print("Work harder to buy it!")
}

请告诉我如何解决它。谢谢堆栈告诉我代码太多但我真的不知道如何修复它。请帮助我,我该怎么做才能发布它。堆栈给了 mu “主要是代码”,但是大声笑,这一切都需要理解

标签: swift

解决方案


寻找类似的东西:

struct Item: Hashable {
    var name: String
    var price: Int
}

class Shop {
    enum PossibleErrors: Error { case notInStock, notEnoughMoney }
    var stock: [Item: Int] = [ 
        // switched to item(name, price):quantity because that
        // is a better conceptual match for real stock records
        // than name:item(price, quantity)
        Item(name: "Item1", price: 13): 4,
        Item(name: "Item2", price: 12): 1,
        Item(name: "Item3", price: 10): 0,
        Item(name: "Item4", price: 50): 12,
    ]
    var balance: Int
    
    init(balance: Int) throws {
        self.balance = balance
        if balance <= 9  {
            throw PossibleErrors.notEnoughMoney
        }
    }
    
    func buyItem(named name: String) throws -> Item {
        // check over everything in stock
        for (item, quantity) in stock {
            // if this is the one you're looking for
            if (item.name == name && quantity > 0) {
                // if you have enough money, buy it
                if (balance > item.price) {
                    balance -= item.price
                    stock[item]? -= 1
                    return item
                }
                // if you don't have enough money
                throw PossibleErrors.notEnoughMoney
            }
        }
        // if it doesn't exist or there are 0 in stock
        throw PossibleErrors.notInStock
    }
}

do {
    let shop = try Shop(balance: 100)
    // this loop has to be nested in the other do-try-catch
    // or else the `shop` variable doesn't exist for sure
    do {
        let boughtItems = try shop.buyItem(named: "Item4")
        print("Bought: \(boughtItems)")
    } catch Shop.PossibleErrors.notInStock {
        print("Shop hasn't any")
    }
} catch Shop.PossibleErrors.notEnoughMoney {
    print("Work harder to buy it!")
}

推荐阅读