-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.scala
61 lines (49 loc) · 1.92 KB
/
Models.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import scala.beans.BeanProperty
abstract class Person {
def firstName: String
def lastName: String
}
class Employee(@BeanProperty val firstName:String,
@BeanProperty val lastName:String,
val title:String = "Programmer") extends Person{
require(firstName.nonEmpty, "First Name cannot be empty")
require(lastName.nonEmpty, "Last Name cannot be empty")
require(title.nonEmpty, "Title cannot be empty")
if(title.contains("Senior") || title.contains("Junior"))
throw new IllegalArgumentException("Title cannot contain Junior or Senior")
def fullName = s"$firstName $lastName"
def changeLastName(ln:String) = new Employee(firstName, ln, title)
def copy(firstName:String = this.firstName, lastName:String = this.lastName,
title:String = this.title ) = {
new Employee(firstName, lastName, title)
}
override def equals(x:Any):Boolean = {
if (!x.isInstanceOf[Employee]) false
else {
val other = x.asInstanceOf[Employee]
other.firstName == this.firstName &&
other.lastName == this.lastName &&
other.title == this.title
}
}
override def hashCode:Int = {
var result = 19
result = 31 * result + firstName.hashCode
result = 31 * result + lastName.hashCode
result = 31 * result + title.hashCode
result
}
override def toString:String = s"Employee($firstName, $lastName, $title)"
}
// Warning: Scala Programmers don't like vars
case class Department (name:String) {
override def toString = s"Department: $name"
}
class Manager(firstName:String, lastName:String, title:String, val department:Department) extends
Employee(firstName, lastName, title) {
override def fullName = s"$firstName $lastName, ${department.name} Manager"
override def copy(firstName:String = this.firstName, lastName:String = this.lastName,
title:String = this.title ) = {
new Manager(firstName, lastName, title, new Department("Toys"))
}
}