Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.98 KB | None | 0 0
  1. class SimpleDate(private var d:Int,private var m:Int) {
  2.   checkInputAndFix()
  3.  
  4.   private var dayInYear  = (d-1 + ((m-1)*30))
  5.  
  6.   def day:Int = dayInYear%30 + 1
  7.   def day_=(x:Int){
  8.     if(0<x && x<31){
  9.     dayInYear = (dayInYear%30+x)%360 + 1
  10.     }else{
  11.       throw new wrongDate("Wrong day!")
  12.     }
  13.   }
  14.   def month:Int = dayInYear/30 + 1
  15.   def month_=(x:Int){
  16.     if(0<x && x<13){
  17.     dayInYear = (dayInYear%30+x*30)%360
  18.     }else{
  19.       throw new wrongDate("Wrong month!")
  20.     }
  21.   }
  22.  
  23.   def przesun(liczbaDni:Int){
  24.     dayInYear = (dayInYear + liczbaDni) % 360
  25.   }
  26.  
  27.   private def checkInputAndFix(){
  28.     if(d<1){
  29.     d = 1
  30.     }
  31.     if(d>30){
  32.       d=30
  33.     }
  34.     if(m<1){
  35.       m=1
  36.     }
  37.     if(m>12){
  38.       m=12
  39.     }
  40.   }
  41. }
  42.  
  43. object SimpleDate {
  44. def apply(d: Int, m: Int) = new SimpleDate(d,m)
  45. }
  46. class wrongDate(message:String) extends Exception{
  47.   print(message)
  48. }
  49.  
  50.  
  51. var date = new SimpleData(15,11)
  52. date.day
  53.  
  54. var date2 = new SimpleData(29,12)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement