Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First Scala example code running

  • Loading branch information...
commit 2581a47bae6f04ccb11ffd382a625946b9a8b927 1 parent 25f75a1
@stefanroock authored
View
2  1_Java_Examples/README.txt
@@ -4,4 +4,4 @@ The examples and the learnings can be easily transfered to other statically type
To execute the examples you need a JVM (Java Virtual Machine) installed on your system.
-To run the example just execute the run.sh file in the example directory. If you are using Windows you have to create a run.cmd equivalent.
+To run the example just execute the run.sh file in the example directory. To build an example run build.sh. If you are using Windows you have to create a build.cmd and a run.cmd equivalent.
View
7 3_Scala_Examples/1_ConferenceAttendees_SRP/Attendee.scala
@@ -0,0 +1,7 @@
+import AttendeeType._
+
+class Attendee(aName: String, anAttendeeType: AttendeeType) {
+
+ var name: String = aName
+ var attendeeType: AttendeeType = anAttendeeType
+}
View
4 3_Scala_Examples/1_ConferenceAttendees_SRP/AttendeeType.scala
@@ -0,0 +1,4 @@
+object AttendeeType extends Enumeration {
+ type AttendeeType = Value
+ val Speaker, Organizer, Participant = Value
+}
View
5 3_Scala_Examples/1_ConferenceAttendees_SRP/Exercise.txt
@@ -0,0 +1,5 @@
+We want to introduce Students as discounted participants. They should pay only 20% of the regular price.
+
+1. What has to be changed in the code to implement the new feature?
+2. The existing code violates the SRP (Single Responsibility Principle). Why?
+3. What are possible refactorings to make the code conform to the SRP? Is the implementation of the Student feature now easier?
View
13 3_Scala_Examples/1_ConferenceAttendees_SRP/Main.scala
@@ -0,0 +1,13 @@
+import PriceCalculator._
+
+object Main {
+ def main(args: Array[String]) {
+ val participant = new Attendee("Stefan", AttendeeType.Participant)
+ val speaker = new Attendee("Markus", AttendeeType.Speaker)
+ val organizer = new Attendee("Andreas", AttendeeType.Organizer)
+ System.out.println("Price for regular participant: " + PriceCalculator.calculatePrice(participant))
+ System.out.println("Price for speaker: " + PriceCalculator.calculatePrice(speaker))
+ System.out.println("Price for organizer: " + PriceCalculator.calculatePrice(organizer))
+ }
+}
+
View
11 3_Scala_Examples/1_ConferenceAttendees_SRP/PriceCalculator.scala
@@ -0,0 +1,11 @@
+object PriceCalculator {
+ def calculatePrice(attendee:Attendee):Int = {
+ val REGULAR_PRICE = 1000
+ attendee.attendeeType match {
+ case AttendeeType.Participant => return REGULAR_PRICE
+ case AttendeeType.Speaker => return REGULAR_PRICE / 2
+ case AttendeeType.Organizer => return 0
+ case _ => throw new IllegalStateException()
+ }
+ }
+}
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/Attendee.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/AttendeeType$.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/AttendeeType.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/Main$.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/Main.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/PriceCalculator$.class
Binary file not shown
View
BIN  3_Scala_Examples/1_ConferenceAttendees_SRP/bin/PriceCalculator.class
Binary file not shown
View
2  3_Scala_Examples/1_ConferenceAttendees_SRP/build.sh
@@ -0,0 +1,2 @@
+rm bin/*.class
+scalac -classpath bin -d bin *.scala
View
1  3_Scala_Examples/1_ConferenceAttendees_SRP/run.sh
@@ -0,0 +1 @@
+scala -cp bin Main
View
7 3_Scala_Examples/README.txt
@@ -0,0 +1,7 @@
+This directory contains several Scala code examples to learn the SOLID principles applied to statically typed functional programming languages.
+
+The examples and the learnings can be easily transfered to other statically typed functional languages like F#, Miranda etc.
+
+To execute the examples you need a JVM (Java Virtual Machine) and Scala installed on your system.
+
+To run the example just execute the run.sh file in the example directory. To build an example run build.sh. If you are using Windows you have to create a build.cmd and a run.cmd equivalent.
View
BIN  SOLIDforyourlanguage.pdf
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.