Advertisement
Voldemord

[Java] Kalendarz

Apr 8th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package kalendarzkr;
  7.  
  8. /**
  9.  *
  10.  * @author student
  11.  */
  12. public class KalendarzKR {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.         try{
  19.             data d = new data(2015,2,29);
  20.             System.out.println(d);
  21.         }catch (Exception e){
  22.             System.out.println(e.getMessage());
  23.         }
  24.        
  25.     }
  26.    
  27. }
  28.  
  29.  
  30. //------------------
  31.  
  32.  
  33. package kalendarzkr;
  34.  
  35. /*
  36.  klasa - data,
  37.  klasa - miesiac,
  38.  klasa - miesiace,
  39. */
  40. public class Miesiac {
  41.    
  42.     private int maxDay;
  43.     private int number;
  44.     private String name;
  45.    
  46.     public Miesiac(int maxDay, int number , String name){
  47.         this.maxDay = maxDay;
  48.         this.number = number;
  49.         this.name = name;
  50.     }
  51.    
  52.     public int getMaxDay() {
  53.         return maxDay;
  54.     }
  55.  
  56.     public int getNumber() {
  57.         return number;
  58.     }
  59.  
  60.     public String getName() {
  61.         return name;
  62.     }
  63.    
  64. }
  65.  
  66. //------------------
  67.  
  68. /*
  69.  * To change this license header, choose License Headers in Project Properties.
  70.  * To change this template file, choose Tools | Templates
  71.  * and open the template in the editor.
  72.  */
  73. package kalendarzkr;
  74.  
  75. /**
  76.  *
  77.  * @author student
  78.  */
  79. public class Data {
  80.     private int year;
  81.     private int mounth;
  82.     private int day;
  83.     private int dayLimit;
  84.    
  85.    
  86.     public Data(int y, int m, int d) throws Exception{
  87.         this.year = y;
  88.        
  89.         if(m >=0 && m <=12){
  90.             this.mounth = m;
  91.             checkDayLimit();
  92.  
  93.             if(d <= this.dayLimit && d > 0){
  94.                  this.day = d;
  95.             } else
  96.                 throw new Exception("Day 1 >= x <= " + this.dayLimit);
  97.         }
  98.         else throw new Exception("mounth 1 >= x <= 12");
  99.     }
  100.    
  101.     private void checkDayLimit(){
  102.         this.dayLimit = Miesiace.getMounth(this.mounth).getMaxDay();
  103.             if(getCheckYear(this.year) && this.mounth == 2)
  104.                 this.dayLimit += 1;
  105.     }
  106.    
  107.     public void addDays(int d){
  108.         int daysum = this.day + d; //27+3 = 2
  109.         if(d > 0){
  110.             if ((daysum) < this.dayLimit){
  111.                 this.day += d;
  112.             } else {
  113.                 while(daysum > this.dayLimit){
  114.                     if(this.mounth == 12){
  115.                         this.year++;
  116.                         this.mounth = 1;
  117.                     } else {
  118.                         this.mounth++;
  119.                     }
  120.                     daysum -= this.dayLimit;
  121.                     checkDayLimit();
  122.                 }
  123.                 this.day = daysum;
  124.             }
  125.         }
  126.        
  127.            
  128.     }
  129.    
  130.     public boolean getCheckYear(int y)
  131.     {
  132.         return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
  133.     }
  134.    
  135.     @Override
  136.     public String toString() {
  137.         return this.year + "-" + this.mounth + "("+ Miesiace.getMounth(this.mounth).getName() + ")" + "-" + this.day;
  138.     }
  139. }
  140.  
  141. //--------
  142.  
  143.  
  144. package kalendarzkr;
  145.  
  146. /*
  147.  klasa - data,
  148.  klasa - miesiac,
  149.  klasa - miesiace,
  150. */
  151.  
  152. public class Miesiace {
  153.    
  154.     private static Miesiac[] miesiace = {
  155.         new Miesiac(31, 1, "Styczen"),
  156.         new Miesiac(28, 2, "Luty"),
  157.         new Miesiac(31, 3, "Marzec"),
  158.         new Miesiac(30, 4, "Kwiecien"),
  159.         new Miesiac(31, 5, "Maj"),
  160.         new Miesiac(30, 6, "Czerwiec"),
  161.         new Miesiac(31, 7, "Lipiec"),
  162.         new Miesiac(31, 8, "Sierpien"),
  163.         new Miesiac(30, 9, "Wrzesien"),
  164.         new Miesiac(31, 10, "Pazdziernik"),
  165.         new Miesiac(30, 11, "Listopad"),
  166.         new Miesiac(31, 12, "Grudzien"),
  167.     };
  168.    
  169.     public static Miesiac getMounth(int nrMiesiaca){
  170.         return miesiace[nrMiesiaca - 1];
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement