Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions examples/objects/Swift/book.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

import Foundation

/// A Book Object that holds the price, author, and title of a book
class Book {



var author : String
var title : String
var price : Float

init(author : String, title : String, price : Float=0.00) {

/**
Constructor to build a book object

- Parameters:
- author: The full name of the author of the book
- title: The full title of the book
- price: The price of the book in dollars and cents (example format ###.##)

*/

self.author = author
self.price = price
self.title = title

}

func getAuthor() -> String {

/**
Returns the author of the book

- Returns: The author of the book as a String

*/



return self.author
}

func getPrice() -> Float {

/**
Returns the price of the book

- Returns: The price of the book as a Float

*/

return self.price
}

func getTitle() -> String {

/**
Returns the title of the book to the console

- Returns: The title of the book

*/

return self.title
}

func increasePrice(_ increase : Float) -> Bool {

/**
Attempts to increase the price of the book

- Parameters:
- increase: The value to increase the price by. This value can be negative; however, it will never lower the value below zero. If this happens, the function will return false

- Returns: true if the method was successful
- Returns: false if the method attempted to bring the value below zero




*/

if (self.price + increase) > 0 {
self.price += increase
return true
}

return false

}

}




23 changes: 23 additions & 0 deletions examples/objects/Swift/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import Foundation

let bookOne = Book(author: "Terry Pratchett", title: "Guards! Guards!", price: 5.99)
let bookTwo = Book(author: "Robert Jordan", title: "The Eye of the World", price: 8.99)

print(bookOne.getAuthor())
print(bookOne.getPrice())
print(bookOne.getTitle())
bookOne.increasePrice(1.00)
print(bookOne.getPrice())

print(bookTwo.getAuthor())
print(bookTwo.getPrice())
print(bookTwo.getTitle())
bookTwo.increasePrice(-6.00)
print(bookTwo.getPrice())