package sanguo.timesheet
import helpers._
abstract class Project(val name: String, val rate: Int)
case class BillableProject(override val name: String) extends Project(name, 1)
case class SIPProject(override val name: String) extends Project(name, 0)
case class Holiday() extends Project("休假", 0)
class Level(val chargePerHour: Int)
object Level {
def apply(chargePerHour: Int) = new Level(chargePerHour)
}
class Consultant(val name: String, val level: Level) {
def chargePerHour = level.chargePerHour
}
object Consultant {
def apply(name: String, level: Level) = new Consultant(name, level)
}
class TimeSheetItem(
val consultant: Consultant,
val project: Project,
mon: Int, tue: Int, wed: Int, thu: Int, fri: Int) {
def working_hours = mon + tue + wed + thu + fri
def charge = consultant.chargePerHour * project.rate * working_hours
}
object TimeSheetItem {
def apply(c: Consultant, p: Project, mon: Int, tue: Int, wed: Int, thu: Int, fri: Int) = new TimeSheetItem(c, p, mon, tue, wed, thu, fri)
}
class Timesheet(val items: Set[TimeSheetItem]) {
private var consultants = Set.empty[Consultant]
private var projects = Set.empty[Project]
items foreach { item =>
consultants += item.consultant
projects += item.project
}
private val sum_hours = (total: Int, item: TimeSheetItem) => total + item.working_hours
private val sum_charges = (total: Int, item: TimeSheetItem) => total + item.charge
def total_hours(_filter: (TimeSheetItem => Boolean)): Int = {
(0 /: (items filter _filter)) { sum_hours }
}
def total_charge(_filter: (TimeSheetItem => Boolean)): Int = {
(0 /: (items filter _filter)) { sum_charges }
}
def print_report = {
projects foreach { p =>
p match {
case BillableProject(name) => {
println("项目名称:" + name)
println("咨询师\t工作时间\t应收费用")
var hours_all = 0
var charge_all = 0
consultants foreach { c =>
val filter = c at p
val hours = total_hours(filter)
val charge = total_charge(filter)
if (hours > 0) println(c.name + "\t" + hours + "\t" + charge)
hours_all += hours
charge_all += charge
}
println("总计:\t" + hours_all + "\t" + charge_all)
}
case Holiday() => {
println("休假情况:")
println("咨询师\t时间")
var hours_all = 0
consultants foreach { c =>
val filter = c at p
val hours = total_hours(filter)
if (hours > 0) println(c.name + "\t" + hours)
}
println("总计:\t" + hours_all)
}
case SIPProject(name) => {
println("项目名称:" + name + " - 公益项目")
println("咨询师\t工作时间")
var hours_all = 0
consultants foreach { c =>
val filter = c at p
val hours_of_cons = total_hours(filter)
hours_all += hours_of_cons
if (hours_of_cons > 0) println(c.name + "\t" + hours_of_cons)
}
println("总计:\t" + hours_all)
}
}
println("-------------------------------------------------")
}
}
}
object helpers {
implicit def consultant2Helper(consultant: Consultant): Consultant2TimesheetItem = new Consultant2TimesheetItem(consultant)
implicit def helper(name: String): String2Consultant = new String2Consultant(name)
}
class Consultant2TimesheetItem(consultant: Consultant) {
def at(project: Project): (TimeSheetItem => Boolean) = {
item: TimeSheetItem => item.consultant == consultant && item.project == project
}
}
class String2Consultant(name: String) {
def level(_level: Level) = {
Consultant(name, _level)
}
}
object Timesheet extends App {
val junior = Level(chargePerHour = 1000)
val senior = Level(chargePerHour = 2000)
val 张飞 = "张飞" level junior
val 关羽 = "关羽" level junior
val 刘备 = "刘备" level senior
val 赵云 = "赵云" level junior
val 火烧赤壁 = BillableProject("火烧赤壁")
val 七擒孟获 = BillableProject("七擒孟获")
val 都江堰 = SIPProject("都江堰")
val 休假 = Holiday()
import sanguo.timesheet.{TimeSheetItem => Item}
val timesheet = new Timesheet(
Set(
Item(张飞, 火烧赤壁, 8, 0, 0, 8, 4),
Item(张飞, 七擒孟获, 0, 8, 8, 0, 0),
Item(张飞, 休假, 0, 8, 8, 0, 0),
Item(关羽, 都江堰, 8, 8, 8, 0, 0),
Item(关羽, 火烧赤壁, 8, 8, 8, 0, 0),
Item(刘备, 七擒孟获, 8, 8, 0, 8, 8),
Item(刘备, 都江堰, 0, 0, 8, 0, 0),
Item(赵云, 休假, 8, 8, 8, 8, 8)))
timesheet print_report
}
分享到:
相关推荐
【课程大纲】第1讲-Spark的前世今生第2讲-课程介绍、特色与价值第3讲-Scala编程:基础语法第4讲-Scala编程:条件控制与循环第5讲-Scala编程:函数入门第6讲-Scala编程:函数入门之默认参数和带名参数第7讲-Scala编程...
赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
【课程大纲】第1讲-Spark的前世今生 共12页第2讲-课程介绍、特色与价值 共13页第3讲-Scala编程详解:基础语法 共8页第4讲-Scala编程详解:条件控制与循环 共7页第5讲-Scala编程详解:函数入门 共5页第6讲-Scala编程...
赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...
赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.1.pom; 包含翻译后的API...
Scala,作为一种将面向对象编程和函数式编程完美结合的语言,提供了强大的函数式编程特性。Scala的函数式编程(FP)不仅包括高阶函数、匿名函数、递归和模式匹配等,还提供了不可变数据结构和纯函数等核心概念。本文...
使用Scala的Leetcode解决方案 ID 问题 解决方案 测试用例 58 345 Scala99: 将Scala-test和Junit集成到Scala中 编写此应用程序时,我发现它可以在本地Intellij IDEA+Junit运行测试用例,但是如果尝试执行sbt test...