Code Monkey home page Code Monkey logo

swift-shopping-cart-ios-0916's Introduction

Swift โ€” Shopping Cart

Overview

In this lab, you'll practice creating classes and properties as well as writing methods that use a custom type.

Objectives

  1. Create a class file.
  2. Create a property for a custom class.
  3. Write methods on your custom class which utilize a custom type.

Instructions

Open swift-shopping-cart.xcworkspace and navigate to the Item.swift file.

Item.swift

The Item class is already set up for you:

//  Item.swift

import Foundation

class Item: Equatable, CustomDebugStringConvertible {
    var name = ""
    var priceInCents = 0
    var debugDescription: String { return name }
    
    init(name: String, priceInCents: Int) {
        self.name = name
        self.priceInCents = priceInCents
    }
}


func ==(lhs: Item, rhs: Item) -> Bool {
    let sameName = lhs.name == rhs.name
    let samePrice = lhs.priceInCents == rhs.priceInCents
    
    return sameName && samePrice
}

You can see that it has two settable properties, a string called name and an integer called priceInCents. It's also written with an initializer that takes an argument for either property.

The debugDescription property is set up as a "calculated property" which simply returns the name. This property is required for conformance with the CustomDebugStringConvertible protocol that customizes how the class is read by the debug tools.

You'll notice that the Item class also conforms to the Equatable protocol, and the class definition is accompanied by an override of the equality comparator == that determines whether two Items are equal. This has the effect of allowing you to use the equality comparator == on two instances of the Item class.

Cart.swift

  1. In a new file, create a new class named Cart.

  2. Give it one property, an empty array of the Item Type named items.

Methods

  1. Write a method named totalPriceInCents() that takes no arguments and returns an Int. This method should return the total cost of all the items in the items array.

  2. Write a method named add(item:) that takes one argument of type Item and provides no return. This method should add the argument to the end of the items property array.

  3. Write a method named remove(item:) that takes one argument of type Item and provides no return. This method should remove an instance from the items array that matches the argument item.

  4. Write a method named items(withName:) that takes one string argument (called name) and returns an array of type Item. This method should collect all of the items in the items property array whose name property matches the submitted string argument.

  5. Write a method named items(withMinPrice:) that takes one integer argument (called price) and returns an array of type Item. This method should collect all of the items in the items property array whose priceInCents property is greater than or equal to the submitted integer argument.

  6. Write a method named items(withMaxPrice:) that take one integer argument (called price) and return an array of type Item. The method should collect all of the items in the items property array whose priceInCents property is less than or equal to the submitted integer argument.

View this lesson on Learn.co

swift-shopping-cart-ios-0916's People

Contributors

annjohn avatar ianrahman avatar ipc103 avatar jimcampagno avatar johannkerr avatar markedwardmurray avatar misterfifths avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.