<" /> <" /> <"/>

Is there a better way to do this?

64 Views Asked by At

so i have this piece of xml code

<schedule>
  <teacher name="Charles Xavier">
    <activities>
      <activity init="7:00" end="7:50" alias="1st period">
        <days>
          <monday></monday>
          <tuesday>room 102</tuesday>
          <wednesday></wednesday>
          <thursday></thursday>
          <friday>room 101</friday>
        </days>
      </activity>
      <activity init="8:00" end="8:50" alias="2nd period">
        <days>
          <monday></monday>
          <tuesday></tuesday>
          <wednesday>room 101</wednesday>
          <thursday></thursday>
          <friday>room 103</friday>
        </days>
      </activity>
    </activities>
  </teacher>
  <teacher name="Moira McTaggert">
    <activities>
      <activity init="7:00" end="7:50" alias="1st period">
        <days>
          <monday></monday>
          <tuesday>room 102</tuesday>
          <wednesday>room 102</wednesday>
          <thursday>room 104</thursday>
          <friday>room 101</friday>
        </days>
      </activity>
      <activity init="8:00" end="8:50" alias="2nd period">
        <days>
          <monday>room 102</monday>
          <tuesday>room 102</tuesday>
          <wednesday>room 103</wednesday>
          <thursday>room 104</thursday>
          <friday>room 103</friday>
        </days>
      </activity>
    </activities>
  </teacher>
</schedule>

and scala code so far is

object WeekDay extends Enumeration("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") {
  type WeekDay = Value
  val Monday, Tuesday, Wednesday, Thursday, Friday = Value
}

class Teacher {
  var name: String = _
  var activities: ListBuffer[Activity] = _
}

class Activity {
  var init: String = _
  var end: String = _
  var alias: String = _
  var days: List[Day] = List(new Day(WeekDay.Monday), new Day(WeekDay.Tuesday), new Day(WeekDay.Wednesday),
    new Day(WeekDay.Thursday), new Day(WeekDay.Friday))
}

class Day(val day: WeekDay) {
  var room: String = _     
}

i think this way feels very "imperative" can it be more scala code?

in the end what im trying to accomplish is a method(see below) like this, so any hints about best way to tackle this would be appreciated.

var overlappedRooms = teachers.getOverlappedRooms("7:00", WeekDay.Tuesday)

this should give us some info saying that Charles xavier's room 102 is overlapped by Moira's room 102

Note. this is just a learning purposes project. :)

1

There are 1 best solutions below

1
Sam Stainsby On

Many Scala proponents try to avoid mutable objects, although some go overboard IMHO. You could try this sort of thing:

class Teacher(val name:String)

class Activity(
  val init:String,
  val end:String,
  val alias:String,
  val days:List[Day])

type Roster: Map[Teacher, List[Activity]]

Note I've moved rostering out of Teacher, since it is not really a property of a teacher. You might consider a new class for it. Possibly Activity needs to be considered separately from the days it is on too. I'm not sure why your Day class has 'room' in it. I suspect your object modelling needs some help here.