import Foundation
// Fig03-01-11: Account.swift
// Account class with name and balance properties,
// an initializer adn deposit and withdraw methods
public class Account {
public var name: String = ""
public private(set) var balance: Double = 0.0
public init(name: String, balance: Double) {
self.name = name
if balance > 0.0 {
self.balance = balance
}
}
public func deposit(amount: Double) {
if amount > 0.0 {
balance = balance + amount
}
}
public func withdraw(amount: Double) {
if amount > 0.0 {
if balance - amount >= 0.0 {
balance = balance - amount
}
}
}
} // end class Account
/***********************************************************/
import Foundation
// fig03-01-11: main.swift
// Using class Account's init method to initialize and Account's
// name property when the Account object is created
var formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
func formatAccountString(account: Account) -> String {
return account.name + "'s balance: " +
formatter.stringFromNumber(account.balance)!
}
let account1 = Account(name: "Jane Green", balance: 50.00)
let account2 = Account(name: "John Blue", balance: -7.53)
// display initial balance of each Account
println(formatAccountString(account1))
println(formatAccountString(account2))
var depositAmount = 25.53
println("\ndepositing " + formatter.stringFromNumber(depositAmount) +
" into accounting1\n") // error here
account1.deposit(depositAmount)
depositAmount = 123.45
println("\ndepositing " + formatter.stringFromNumber(depositAmount) +
" into account2\n") // error here
account2.deposit(depositAmount)
println(formatAccountString(account1))
println(formatAccountString(account2))
var withdrawalAmount = 14.27
println("\nwithdrawing " + formatter.stringFromNumber(withdrawalAmount) +
" from account1\n") // error here
account1.withdraw(withdrawalAmount)
println(formatAccountString(account1))
println(formatAccountString(account2))
withdrawalAmount = 100.00
println("\nwithdrawing " + formatter.stringFromNumber(withdrawalAmount) +
" from account2\n") // error here
account2.withdraw(withdrawalAmount)
println(formatAccountString(account1))
println(formatAccountString(account2))
/* how do i fix this and why does it not just work */
/* code from deitel and deitel swift for programmers */
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/fdfdc0a3-1326-489e-aac8-b6556dca15bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.