Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TDD-Restaurant-Billing-System
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Dependencies._
lazy val root = (project in file(".")).
settings(
inThisBuild(List(
organization := "com.lagosscala",
organization := "com.example",

Copy link
Copy Markdown
Contributor

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

scalaVersion := "2.12.7",
version := "0.1.0-SNAPSHOT"
)),
name := "tdd-restuarant-billing-system",
name := "restaurant-billing-system",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do revert this back also

libraryDependencies += scalaTest % Test
)
100 changes: 100 additions & 0 deletions src/main/scala/com/restaurant/Restaurant.scala
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()



}
9 changes: 0 additions & 9 deletions src/test/scala/lagosscala/HelloSpec.scala

This file was deleted.

74 changes: 74 additions & 0 deletions src/test/scala/restaurant/RestaurantSpec.scala
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)



}


}