-
Notifications
You must be signed in to change notification settings - Fork 2
TDD restaurant billing system #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
horlahlekhon
wants to merge
2
commits into
LagosScala:master
Choose a base branch
from
horlahlekhon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # TDD-Restaurant-Billing-System |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,10 @@ import Dependencies._ | |
| lazy val root = (project in file(".")). | ||
| settings( | ||
| inThisBuild(List( | ||
| organization := "com.lagosscala", | ||
| organization := "com.example", | ||
| scalaVersion := "2.12.7", | ||
| version := "0.1.0-SNAPSHOT" | ||
| )), | ||
| name := "tdd-restuarant-billing-system", | ||
| name := "restaurant-billing-system", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do revert this back also |
||
| libraryDependencies += scalaTest % Test | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.restaurant | ||
|
|
||
| import java.util.Scanner | ||
|
|
||
| import Console.{GREEN, MAGENTA, RED, RESET, UNDERLINED, WHITE, YELLOW, BOLD} | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
|
|
||
| case class Good(typeOf:String,temperature:Boolean, name: String, price:Double) | ||
| class Restaurant { | ||
|
|
||
| val temperatures = Map(1 -> false, 2 -> true) | ||
| val goodTypes = Map(1 -> "Coffee", 2 -> "Cola", 3 -> "Cheese Sandwich",4 -> "Steak Sandwich") | ||
| val prices = Map("Coffee" -> 1.00, "Cheese Sandwich" -> 2.00 , "Cola" -> 0.50 , "Steak Sandwich" -> 4.50) | ||
|
|
||
| def addGoods(goods: List[Good]): Double = goods.map(e => e.price).reduce((x, y) => x + y) | ||
|
|
||
| def calculateServiceCharge(goods: List[Good], hot: Double, defaultCharge: Double): Double = { | ||
| def charge(sum: Double, charge: Double) = charge * sum / 100 | ||
|
|
||
| if (goods.filter(e => e.typeOf.equals("Food") && e.temperature == true).length > 0) { | ||
| return if(charge(addGoods(goods), hot) > 20) 20 else charge(addGoods(goods), hot) | ||
| } else if (goods.filter(e => e.typeOf.equals("Food")).length > 0) { | ||
| return if(charge(addGoods(goods), defaultCharge) > 20) 20 else charge(addGoods(goods), defaultCharge) | ||
| } else return 0.0 | ||
| } | ||
|
|
||
| def detectTypeOf(typee: String) : String = { | ||
| typee match { | ||
| case "Coffee" | "Cola" => "Drink" | ||
| case "Cheese Sandwich" | "Steak Sandwich" => "Food" | ||
| case _ => "Food" | ||
| } | ||
| } | ||
| //i dont like this because string matching is a messy business | ||
| def resolveInput(goods :List[String]) : List[Good] = { | ||
| var lOGoods : ArrayBuffer[Good] = ArrayBuffer() | ||
| goods.map( e => e.trim match { | ||
| case "Cola" => | ||
| lOGoods += Good(detectTypeOf(goodTypes.apply(2)),temperatures.apply(1),e,prices.apply("Cola")) | ||
| case "Coffee" => | ||
| lOGoods += Good(detectTypeOf(goodTypes.apply(1)),temperatures.apply(2),e,prices.apply("Coffee")) | ||
| case "Cheese Sandwich" => | ||
| lOGoods += Good(detectTypeOf(goodTypes.apply(3)),temperatures.apply(2),e,prices.apply("Cheese Sandwich")) | ||
| case "Steak Sandwich" => | ||
| lOGoods += Good(detectTypeOf(goodTypes.apply(4)),temperatures.apply(1),e,prices.apply("Steak Sandwich")) | ||
|
|
||
| }) | ||
| lOGoods.toList | ||
| } | ||
|
|
||
| def total(goods :List[Good]): Double ={ | ||
| val charge = calculateServiceCharge(goods,20,10) | ||
| addGoods(goods) + charge | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| object Restaurant extends App { | ||
|
|
||
| // val goods = List(Good("Drink", false, "Cola - Cold", 0.50), Good("Drink", true, "cofee", 1.00), Good("Drink", false, "Cola - cold", 0.50)) | ||
| // val res = goods.filter(e => e.typeOf.equals("Food") && e.temperature == true) | ||
|
|
||
| Console.println(s"Welcome to ${MAGENTA}Restaurant X, ${RESET}our menu includes the following are\n the foods and drinks, which are available as cold or hot ") | ||
| val restaurant = new Restaurant() | ||
| Console.println() | ||
| // restaurant.goodTypes.foreach(Console.println) | ||
| Console.println(s"Be noted that hot foods attract${YELLOW} 20% ${RESET}of the total\n as service charge, and cold food ${YELLOW}10%${RESET}, kindly bear with us to serve you better") | ||
| Console.println(s"you can choose the goods based on their number on the menu${RED} enter F to finish and get you order and bill${RESET}") | ||
|
|
||
| restaurant.goodTypes.foreach(Console.println) | ||
| var order : ArrayBuffer[Good]= scala.collection.mutable.ArrayBuffer() | ||
| val sc = new Scanner(System.in) | ||
| while(sc.hasNextInt){ | ||
| val selected = sc.nextInt() | ||
| Console.println(s" kindly choose : ${WHITE}Cold -> 1, ${RED}hot -> 2${RESET}") | ||
| val temp = sc.nextInt() | ||
| selected match { | ||
| case 1 | 2 | 3 | 4 => | ||
| temp match { | ||
| case 1 | 2 => order += Good(restaurant.detectTypeOf(restaurant.goodTypes.apply(selected)),restaurant.temperatures.apply(temp),restaurant.goodTypes.apply(selected),restaurant.prices.apply(restaurant.goodTypes.apply(selected))) | ||
| } | ||
| case _ => Console.println(s"${RED}that good is not in our menu, kindly select from the list${RESET}") | ||
| } | ||
| Console.println(s"order: ${restaurant.goodTypes.apply(selected)}\tprice: £${restaurant.prices.apply(restaurant.goodTypes.apply(selected))}") | ||
| } | ||
| Console.println(s"This is your bill ${WHITE}${UNDERLINED}${BOLD}£${restaurant.total(order.toList)}") | ||
|
|
||
|
|
||
| // while(scala.io.StdIn.readInt() - 1 > 0){ | ||
| // | ||
| // } | ||
|
|
||
| // val a = scala.io.StdIn.readInt() | ||
|
|
||
|
|
||
|
|
||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package restaurant | ||
|
|
||
| import com.restaurant.{Good, Restaurant} | ||
| import org.scalatest._ | ||
|
|
||
| class RestaurantSpec extends FlatSpec with Matchers { | ||
|
|
||
| "calculateServiceCharge" should | ||
| "calculate Accurately the amount of charges to add to a the total good given the list of goods that contains food" in { | ||
|
|
||
| val restaurant = new Restaurant() | ||
| val goods = List(Good("Food",true,"cheese - Sandwch",2.00),Good("Drink",true,"cofee",1.00),Good("Food",true,"Steak - sandwich",4.50),Good("Drink", false, "Cola - cold", 0.50)) | ||
| val serviceChargeWithFood = restaurant.calculateServiceCharge(goods,20,10) | ||
|
|
||
| serviceChargeWithFood should be(1.6 ) | ||
|
|
||
| } | ||
|
|
||
| "calculateServiceCharge" should | ||
| "calculate Accurately the amount of charges to add to a the total good given the list of goods that doesnt contains food" in { | ||
|
|
||
| val restaurant = new Restaurant() | ||
| val goods = List(Good("Drink", false, "Cola - Cold", 0.50), Good("Drink", true, "cofee", 1.00), Good("Drink", false, "Cola - cold", 0.50)) | ||
| val serviceChargeWithoutFood = restaurant.calculateServiceCharge(goods, 20, 10) | ||
|
|
||
| serviceChargeWithoutFood should be(0.0) | ||
|
|
||
| } | ||
|
|
||
| "calculateServiceCharge" should | ||
| "calculate Accurately the amount of charges to add to a the total good given the list and should never default the max service charge to $20" in { | ||
|
|
||
| val restaurant = new Restaurant() | ||
| val goods = List(Good("Food",true,"cheese - Sandwch",2000.09),Good("Drink",true,"cofee",200.0),Good("Food",false,"Steak - sandwich",100.0)) | ||
| val serviceChargeWithoutFood = restaurant.calculateServiceCharge(goods, 20, 10) | ||
|
|
||
| serviceChargeWithoutFood should be(20.0) | ||
|
|
||
| } | ||
|
|
||
| "Restaurant.total" should | ||
| "return the total bill of of what was consumed without any sundry charges..LOL when there isnt any food in the order" in { | ||
| val restaurant = new Restaurant() | ||
| val goods = List(Good("Drink", false, "Cola - Cold", 0.50), Good("Drink", true, "cofee", 1.00), Good("Drink", false, "Cola - cold", 0.50)) | ||
| val total = restaurant.total(goods) | ||
|
|
||
| total should be(2) | ||
|
|
||
| } | ||
| "Restaurant.total" should | ||
| "return the total bill of of what was consumed with charges ..LOL but the charges shouldnt be more than 20 when there is food in the order" in { | ||
| val restaurant = new Restaurant() | ||
| val goods = List(Good("Food",true,"cheese - Sandwch",2000.09),Good("Drink",true,"cofee",200.0),Good("Food",false,"Steak - sandwich",100.0)) | ||
| val total = restaurant.total(goods) | ||
|
|
||
| total should be(2320.09) | ||
|
|
||
| } | ||
| "Restaurant.resolveInput" should | ||
| "return a structured list of goods" in { | ||
| val restaurant = new Restaurant() | ||
| val rawGoods = List("Coffee" ,"Cheese Sandwich","Cola", "Steak Sandwich" ) | ||
| val resolved = restaurant.resolveInput(rawGoods) | ||
|
|
||
| val goods = List(Good("Drink",true,"Coffee",1.00),Good("Food",true,"Cheese Sandwich",2.00),Good("Drink",false,"Cola",0.50),Good("Food",false,"Steak Sandwich",4.50)) | ||
|
|
||
| resolved should equal(goods) | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this back to lagosscala