当前位置:网站首页>Swift Error Handling
Swift Error Handling
2022-07-03 12:34:00 【iWillook】
//
// main.swift
// Ultimate
//
// Created by Mewlan Musajan on 3/20/21.
//
enum VendingMachineError: Error {
case invalidSelection
case insufficientFunds(coinsNeeded: Int)
case outOfStock
}
struct Item {
var price: Int
var count: Int
}
class VendingMachine {
var inventory = [
"Candy Bar": Item(price: 12, count: 7),
"Chips": Item(price: 10, count: 4),
"Pretzels": Item(price: 7, count: 11)
]
var coinsDeposited = 0
init(deposite: Int) {
coinsDeposited = deposite
}
func vend(itemNamed name: String) throws {
guard let item = inventory[name] else {
throw VendingMachineError.invalidSelection
}
guard item.count > 0 else {
throw VendingMachineError.outOfStock
}
guard item.price <= coinsDeposited else {
throw VendingMachineError.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
}
coinsDeposited -= item.price
var newItem = item
newItem.count -= 1
inventory[name] = newItem
print("Dispensing \(name)")
if coinsDeposited != 0 {
print("Returing extra \(coinsDeposited) BTC")
}
}
}
let favoriteSnacks = [
"Alice": "Chips",
"Bob": "Licorice",
"Eve": "Pretzels",
]
func buyFavoriteSnack(person: String, vendingMachine: VendingMachine) throws {
let snackName = favoriteSnacks[person] ?? "Candy Bar"
try vendingMachine.vend(itemNamed: snackName)
}
try buyFavoriteSnack(person: "Eve", vendingMachine: VendingMachine(deposite: 12))
struct PurchasedSnack {
let name: String
init(name: String, vendingMachine: VendingMachine) throws {
try vendingMachine.vend(itemNamed: name)
self.name = name
}
}
print("\(try PurchasedSnack(name: "Candy Bar", vendingMachine: VendingMachine(deposite: 12)).name) was vended")
var vendingMachine = VendingMachine(deposite: 8)
do {
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
print("Success! Yum.")
} catch VendingMachineError.invalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.outOfStock {
print("Out of Stock.")
} catch VendingMachineError.insufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
} catch {
print("Unexpected error: \(error).")
}
func nourish(with item: String) throws {
do {
try vendingMachine.vend(itemNamed: item)
} catch is VendingMachineError {
print("Couldn't buy that from the vending machine.")
}
}
do {
try nourish(with: "Beet-Flavored Chips")
} catch {
print("Unexpected non-vending-machine-realted error: \(error)")
}
边栏推荐
- ES6 standard
- (construction notes) learning experience of MIT reading
- Eureka self protection
- 2.6 preliminary cognition of synergetic couroutines
- 232. Implement queue with stack
- Unicode encoding table download
- RedHat5 安装Socket5代理服务器
- 剑指Offer04. 二维数组中的查找【中等】
- MySQL time zone solution
- Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
猜你喜欢
剑指Offer06. 从尾到头打印链表
Sword finger offer05 Replace spaces
T430 toss and install OS majave 10.14
阿里大于发送短信(用户微服务--消息微服务)
LeetCode 0556. Next bigger element III - end of step 4
Sword finger offer03 Repeated numbers in the array [simple]
Sword finger offer09 Implementing queues with two stacks
Integer int compare size
New features of ES6
TOGAF认证自学宝典V2.0
随机推荐
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
Prompt unread messages and quantity before opening chat group
Shutter: overview of shutter architecture (excerpt)
Sword finger offer06 Print linked list from end to end
启用MemCached的SASL认证
Write a simple nodejs script
[download attached] password acquisition tool lazagne installation and use
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Sword finger offer03 Repeated numbers in the array [simple]
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
Kubectl_ Command experience set
Sqoop1.4.4原生增量导入特性探秘
网上炒股开户安不安全?谁给回答一下
MySQL time zone solution
239. Sliding window maximum
2.6 preliminary cognition of synergetic couroutines
2020-09_ Shell Programming Notes
Day 1 of kotlin learning: simple built-in types of kotlin
ES6 standard
(construction notes) ADT and OOP