Diese Präsentation wurde erfolgreich gemeldet.

Party planner workshop

2

Teilen

Nächste SlideShare
Scala is for everyone
Scala is for everyone
Wird geladen in …3
×
1 von 28
1 von 28

Party planner workshop

2

Teilen

Herunterladen, um offline zu lesen

Beschreibung

Workshop description for https://github.com/DanyMariaLee/partyplanner

Transkript

  1. 1. with Scala Let’s plan a party © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  2. 2. github.com/DanyMariaLee/ partyplanner
  3. 3. The task Parse data Calculate Print result © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  4. 4. use worksheet or REPL
  5. 5. google it
  6. 6. Try again
  7. 7. Data models case class Price(product: String, value: Double) Price("cola", 1.5) case class Consumption(product: String, value: Double) Consumption("cake", 0.01) case class Party(name: String, products: List[String]) Party("birthday", List("cake", "balloon")) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  8. 8. Request case class Request(party: Party, numberOfPeople: Int, budget: Double) Request( Party("birthday", List("cake", "balloon")), 10, 100.5) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  9. 9. Receipt case class Receipt(request: Request, totalAmount: Double) Receipt( Request( Party("Party name", List("product")), 3, 100.0), 6.0) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  10. 10. Parse data package partyplanner.utils /** * @return List[String] containing lines from file with `name` * For example for file name.txt * * product1;value1 * product2;value2 * product3;value3 * * result will be * * List("product1;value1", "product2;value2", "product3;value3") * * @param name Is the name of a file you want to read (e.g. "price.txt") */ def loadDataFromFile(name: String): List[String] = { val stream: InputStream = getClass.getResourceAsStream(name) scala.io.Source.fromInputStream(stream).getLines.toList } © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  11. 11. Parse data import utils.Utils.loadDataFromFile All data is in resources folder: party.txt, price.txt, consumption.txt Use `split` function val arr = "boo:and:foo".split(":") // results to Array("boo", "and", "foo") Access each element by index arr(0) // results to "boo" Don't forget to transform String into Double type when necessary "2".toDouble // 2: Double "a".toDouble // throws an Exception © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  12. 12. price.txt /** * @return List[Price] containing prices from file in resource * For example for line * * "apple;1.5" * * price should be Price("apple", 1.5) */ def parsePrices: List[Price] = ??? © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  13. 13. price.txt banana;2 coffee;1 Price("banana", 2) Price("coffee", 1) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  14. 14. consumption.txt /** * @return List[Consumption] containing * consumptions from file in resource * For example for line * * "apple;0.5" * * consumption should be Consumption("apple", 0.5) */ def parseConsumptions: List[Consumption] = ??? © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  15. 15. consumption.txt banana;0.2 coffee;0.5 Consumption("banana", 0.2) Consumption("coffee", 0.5) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  16. 16. party.txt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence /** * @return List[Party] containing Party from file in resource * For example for lines * * "birthday;coffee" * "birthday;cola" * * party should be Party("birthday", List("coffee", "cola")) */ def parseParties: List[Party] = ???
  17. 17. party.txt birthday;banana birthday;coffee corporate;vine corporate;fish Party(“birthday", List(“coffee”, "coffee")) Party(“corporate", List(“vine”, “fish")) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  18. 18. Run test © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  19. 19. Calculation ONE product for ONE person ALL products for ONE person Total amount © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  20. 20. ONE product for ONE person consumption * price = product amount Price("coffee", 1)Consumption("banana", 0.2) C * P = A (product, person) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  21. 21. ALL products for ONE person A1 + A2 + A3 + … + An = A(person) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  22. 22. Total amount A(person) * number of people = total amount © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  23. 23. Console dialog party type number of people budget Request © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  24. 24. Console dialog © 2018 The Quantium Group Pty Ltd. In Commercial Confidence To get data from console we have scala.io.StdIn object with methods for each type. For example to get an Int scala.io.StdIn.readInt() ! This method throws an exception if user types a wrong type.
  25. 25. Console dialog © 2018 The Quantium Group Pty Ltd. In Commercial Confidence Hey there, it's a party planner app! We are offering different types of parties: 0 - birthday 1 - cocktail 2 - garden 3 - corporate Please, pick the type of your party Please, pick the type of your party 0 How many people will be at the party? 10 What is your budget for this party? 1000
  26. 26. Receipt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence -------------------- Birthday party for 10 people cake balloon cola icecream banana TOTAL $37.0 -------------------- THANK YOU! 10 February 2018
  27. 27. Contact me © 2018 The Quantium Group Pty Ltd. In Commercial Confidence twitter @besseifunction
  28. 28. Thank you! © 2018 The Quantium Group Pty Ltd. In Commercial Confidence

Beschreibung

Workshop description for https://github.com/DanyMariaLee/partyplanner

Transkript

  1. 1. with Scala Let’s plan a party © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  2. 2. github.com/DanyMariaLee/ partyplanner
  3. 3. The task Parse data Calculate Print result © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  4. 4. use worksheet or REPL
  5. 5. google it
  6. 6. Try again
  7. 7. Data models case class Price(product: String, value: Double) Price("cola", 1.5) case class Consumption(product: String, value: Double) Consumption("cake", 0.01) case class Party(name: String, products: List[String]) Party("birthday", List("cake", "balloon")) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  8. 8. Request case class Request(party: Party, numberOfPeople: Int, budget: Double) Request( Party("birthday", List("cake", "balloon")), 10, 100.5) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  9. 9. Receipt case class Receipt(request: Request, totalAmount: Double) Receipt( Request( Party("Party name", List("product")), 3, 100.0), 6.0) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  10. 10. Parse data package partyplanner.utils /** * @return List[String] containing lines from file with `name` * For example for file name.txt * * product1;value1 * product2;value2 * product3;value3 * * result will be * * List("product1;value1", "product2;value2", "product3;value3") * * @param name Is the name of a file you want to read (e.g. "price.txt") */ def loadDataFromFile(name: String): List[String] = { val stream: InputStream = getClass.getResourceAsStream(name) scala.io.Source.fromInputStream(stream).getLines.toList } © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  11. 11. Parse data import utils.Utils.loadDataFromFile All data is in resources folder: party.txt, price.txt, consumption.txt Use `split` function val arr = "boo:and:foo".split(":") // results to Array("boo", "and", "foo") Access each element by index arr(0) // results to "boo" Don't forget to transform String into Double type when necessary "2".toDouble // 2: Double "a".toDouble // throws an Exception © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  12. 12. price.txt /** * @return List[Price] containing prices from file in resource * For example for line * * "apple;1.5" * * price should be Price("apple", 1.5) */ def parsePrices: List[Price] = ??? © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  13. 13. price.txt banana;2 coffee;1 Price("banana", 2) Price("coffee", 1) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  14. 14. consumption.txt /** * @return List[Consumption] containing * consumptions from file in resource * For example for line * * "apple;0.5" * * consumption should be Consumption("apple", 0.5) */ def parseConsumptions: List[Consumption] = ??? © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  15. 15. consumption.txt banana;0.2 coffee;0.5 Consumption("banana", 0.2) Consumption("coffee", 0.5) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  16. 16. party.txt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence /** * @return List[Party] containing Party from file in resource * For example for lines * * "birthday;coffee" * "birthday;cola" * * party should be Party("birthday", List("coffee", "cola")) */ def parseParties: List[Party] = ???
  17. 17. party.txt birthday;banana birthday;coffee corporate;vine corporate;fish Party(“birthday", List(“coffee”, "coffee")) Party(“corporate", List(“vine”, “fish")) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  18. 18. Run test © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  19. 19. Calculation ONE product for ONE person ALL products for ONE person Total amount © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  20. 20. ONE product for ONE person consumption * price = product amount Price("coffee", 1)Consumption("banana", 0.2) C * P = A (product, person) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  21. 21. ALL products for ONE person A1 + A2 + A3 + … + An = A(person) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  22. 22. Total amount A(person) * number of people = total amount © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  23. 23. Console dialog party type number of people budget Request © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  24. 24. Console dialog © 2018 The Quantium Group Pty Ltd. In Commercial Confidence To get data from console we have scala.io.StdIn object with methods for each type. For example to get an Int scala.io.StdIn.readInt() ! This method throws an exception if user types a wrong type.
  25. 25. Console dialog © 2018 The Quantium Group Pty Ltd. In Commercial Confidence Hey there, it's a party planner app! We are offering different types of parties: 0 - birthday 1 - cocktail 2 - garden 3 - corporate Please, pick the type of your party Please, pick the type of your party 0 How many people will be at the party? 10 What is your budget for this party? 1000
  26. 26. Receipt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence -------------------- Birthday party for 10 people cake balloon cola icecream banana TOTAL $37.0 -------------------- THANK YOU! 10 February 2018
  27. 27. Contact me © 2018 The Quantium Group Pty Ltd. In Commercial Confidence twitter @besseifunction
  28. 28. Thank you! © 2018 The Quantium Group Pty Ltd. In Commercial Confidence

Weitere Verwandte Inhalte

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

×