Skip to content

Latest commit

 

History

History
213 lines (162 loc) · 5.49 KB

1.5-README.md

File metadata and controls

213 lines (162 loc) · 5.49 KB

<< back to main index

Lab 1.5 : Implement A Vending Machine Class In Scala

Overview

Implement a full fledged Scala class

Depends On

None

Run time

20 mins

Recommended Setup (In order of preference)

  • If you are using an IDE (like IntelliJ) please open the project as an SBT project.
  • If you are using a UI editor like 'Sublime' open the 'project root directory' in editor (so you can navigate to all files easily).
  • If using command line, please open 2 terminals - 1 for editing files and other for running commands.

Step 0 : Instructor

  • Get the lab started on your screen.
  • Show how to complete TODO-1 & 2 (upto STEP 5) on screen.
  • Then let the class complete rest of the TODO items.

Step 1 : Project Directory

Make sure you are in the project top level directory.

    $   cd   ~/spark-labs/01-scala/vending-machine

    # see the project structure
    $   find .  | grep -v target

Step 2 : SBT

Using one terminal, launch SBT from project root directory.

    $   cd   ~/spark-labs/01-scala/vending-machine

    $   sbt

You would enter SBT prompt like follows:

    [info] Loading global plugins from /Users/sujee/.sbt/0.13/plugins
    [info] Loading project definition from /Users/sujee/ElephantScale/spark-labs/01-scala/vending-machine/project
    [info] Set current project to vending-machine (in build file:/Users/sujee/ElephantScale/spark-labs/01-scala/vending-machine/)

    > 

Quick Tip: Arrange SBT & Edit windows

Here is a tip to arrange terminal windows.

Step 3 : VendingMachine trait

Inspect the file src/main/scala/x/VendingMachine.scala.
This file defines the trait for a vending machine

If using command line, you can use the following command:

    $   vi src/main/scala/x/VendingMachine.scala
    # or
    $  nano src/main/scala/x/VendingMachine.scala

Step 4 : VendingMachine 'Test Driver'

Inspect the file src/main/scala/x/QuickTest.scala.
This file calls methods on VendingMachine.

If using command line, you can use the following command:

    $   vi src/main/scala/x/QuickTest.scala
    # or
    $  nano src/main/scala/x/QuickTest.scala

Let's try this test driver.
On your SBT terminal type

     > run

You will see following output from our test driver class.

    > run
    [info] compiling 1 scala source to /users/sujee/elephantscale/spark-labs/01-scala/vending-machine/target/scala-2.11/classes...
    [info] Running x.QuickTest
    Stocking 'coke', price = $1, qty = 10
        coke stock : 0
    Stocking 'm&m', price = $2, qty = 20
        m&m stock : 0
    Stocking 'oreo', price = $3, qty = 20
        oreo stock : 0
    stock qty for coke 0
    price for oreo 0
    depositing $2
    trying to buy oreo @ $3 : Success
    trying to buy coke @ $1 : Success
    balance : 0
    [success] Total time: 2 s, completed May 16, 2016 4:00:55 PM

Step 5 : VendingMachine Implementation

Edit the file : src/main/scala/MyVendingMachine.scala.
This file contains partial implementation of VendingMachine trait.
Please complete the first TODO item in the file.

If using command line use the following command:

    $   vi src/main/scala/x/MyVendingMachine.scala
    # or
    $  nano src/main/scala/x/MyVendingMachine.scala

And here is how we fix TODO-1 & TODO-2:

    override def addStockItem(item: String, price: Int, qty: Int): Int = {
        if (store.contains(item)) {
          // adding an item with a new price automatically re-prices existing stock
          val newStock = store(item).add(price, qty)

          // TODO-1 : put new stock in 'store'
          store.put(item, newStock)
          newStock.getQty
        } else {
           store.put(item, new Stock(item, price, qty))
           // TODO-2 : return qty
           qty // fix this
    }

=> Save the file.
=> And run on SBT terminal.
Notice how SBT will compile the file and run it again.

Inspect the output. The addStockItem should work now.

    > run
    [info] compiling 1 scala source to /users/sujee/elephantscale/spark-labs/01-scala/vending-machine/target/scala-2.11/classes...
    [info] Running x.QuickTest
    Stocking 'coke', price = $1, qty = 10
        coke stock : 10
    Stocking 'm&m', price = $2, qty = 20
        m&m stock : 20
    Stocking 'oreo', price = $3, qty = 20
        oreo stock : 30

    stock qty for coke 0
    price for oreo 0
    depositing $2
    trying to buy oreo @ $3 : Success
    trying to buy coke @ $1 : Success
    balance : 0
    [success] Total time: 2 s, completed May 16, 2016 4:00:55 PM

Step 6 : Complete The Implementation of 'MyVendingMachine'

Edit the file : src/main/scala/MyVendingMachine.scala.
Please complete the all TODO items in the file.

If using command line use the following command:

    $   vi src/main/scala/x/MyVendingMachine.scala
    # or
    $  nano src/main/scala/x/MyVendingMachine.scala

**=> Edit --> Save --> Run **
**=> Rinse and Repeat **

Step 7 : SBT Tidbits

SBT Shell

You can issue sbt commands from shell as follows

    $   sbt  clean compile run

For iterative development, sbt shell is much faster !

    $   sbt
    > clean
    > compile run
    > run

Specifying A Main Class To Run

    $  sbt  "run-main x.Foo"
    # in sbt shell
    >   runMain  x.foo