当前位置:网站首页>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)")
}
边栏推荐
- Lambda表达式
- Itext7 uses iexternalsignature container for signature and signature verification
- 145. Post order traversal of binary tree
- ES6新特性
- Introduction to concurrent programming (I)
- Applet wxss introduction
- 剑指Offer10- I. 斐波那契数列
- Shutter: about inheritedwidget
- Kubectl_ Command experience set
- Adult adult adult
猜你喜欢

【附下载】密码获取工具LaZagne安装及使用

Eureka self protection

Itext7 uses iexternalsignature container for signature and signature verification
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

What is more elegant for flutter to log out and confirm again?

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

Unicode encoding table download

Togaf certification self-study classic v2.0

剑指Offer10- I. 斐波那契数列

ES6新特性
随机推荐
(construction notes) grasp learning experience
Flutter Widget : Flow
Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
023(【模板】最小生成树)(最小生成树)
Wechat applet - basic content
Sword finger offer09 Implementing queues with two stacks
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
Dart: About zone
232. Implement queue with stack
剑指Offer06. 从尾到头打印链表
Itext7 uses iexternalsignature container for signature and signature verification
剑指Offer05. 替换空格
145. Post order traversal of binary tree
剑指Offer07. 重建二叉树
记录自己vulnhub闯关记录
elastic_ L02_ install
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Integer string int mutual conversion
225. Implement stack with queue