Advertisement
kaburen

kalendarz

Apr 28th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1. package kalendarz;
  2.  
  3. public class BrakDnia extends Exception {
  4.  
  5.     public BrakDnia() {
  6.     }
  7.  
  8.     /**
  9.      * Constructs an instance of <code>BrakDnia</code> with the specified detail
  10.      * message.
  11.      *
  12.      * @param msg the detail message.
  13.      */
  14.     public BrakDnia(String msg) {
  15.         super(msg);
  16.     }
  17. }
  18. /---------------------------------------------------------------------------------------------/
  19.  
  20. package kalendarz;
  21.  
  22. public class BrakMiesiaca extends Exception {
  23.  
  24.    
  25.     public BrakMiesiaca() {
  26.     }
  27.  
  28.     /**
  29.      * Constructs an instance of <code>BrakMiesiaca</code> with the specified
  30.      * detail message.
  31.      *
  32.      * @param msg the detail message.
  33.      */
  34.     public BrakMiesiaca(String msg) {
  35.         super(msg);
  36.     }
  37. }
  38.  
  39. /---------------------------------------------------------------------------------------------/
  40.  
  41. package zadanie5;
  42.  
  43. public class Data {
  44.     private int dzien, rok, numerMies;
  45.     private String miesiac;    
  46.    
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return dzien + " " + miesiac + " " + rok;
  51.     }
  52.    
  53.     public  Data(int dz, int mies, int rok)throws BrakDnia, BrakMiesiaca{
  54.         isPrzestepny(rok);
  55.         if(mies < 1 || mies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
  56.         if(dz < 1 || dz > Miesiace.getLiczba(mies)) throw new BrakDnia("Podano niepoprawny dzien");        
  57.         this.dzien = dz;
  58.         this.numerMies=mies;
  59.         this.miesiac = Miesiace.getNzw(mies);
  60.         this.rok = rok;
  61.                
  62.     }
  63.    
  64.     private void isPrzestepny(int rok){        
  65.         if(rok % 4 == 0 && rok % 100 != 0 || rok % 400 == 0)
  66.             Miesiace.edytujMies(2, 29,"Luty");
  67.         else{
  68.             Miesiace.edytujMies(2, 28,"Luty");
  69.         }
  70.     }
  71.    
  72.     public void tydzienPlus(){        
  73.             if(this.dzien + 7 <= Miesiace.getLiczba(numerMies)){
  74.                 this.dzien +=7;
  75.             }else{
  76.                this.dzien = this.dzien+7-Miesiace.getLiczba(numerMies);
  77.                if(this.numerMies < 12){
  78.                     this.miesiac = Miesiace.getNzw(numerMies+1);
  79.                     this.numerMies = this.numerMies+1;
  80.                }else{
  81.                     this.rok+=1;
  82.                     this.miesiac = Miesiace.getNzw(1);
  83.                    this.numerMies = 1;                    
  84.                }
  85.             }
  86.         }    
  87.    
  88.     public void tydzienMinus(){        
  89.             if(this.dzien - 7 > 0){
  90.                     this.dzien -=7;
  91.             }else{
  92.                this.dzien = this.dzien-7+Miesiace.getLiczba(numerMies);
  93.                 if(this.numerMies > 1){
  94.                     this.miesiac = Miesiace.getNzw(numerMies-1);
  95.                     this.numerMies = this.numerMies-1;                  
  96.                 }else{
  97.                    this.rok-=1;
  98.                    this.miesiac = Miesiace.getNzw(12);
  99.                    this.numerMies = 12;
  100.                 }
  101.             }
  102.         }    
  103.    
  104.     /**
  105.      * @return the dzien
  106.      */
  107.     public int getDzien() {
  108.         return dzien;
  109.     }
  110.  
  111.     /**
  112.      * @param dzien the dzien to set
  113.      * @throws kalendarz.BrakDnia
  114.      * @throws kalendarz.BrakMiesiaca
  115.      */
  116.     public void setDzien(int dzien)throws BrakDnia, BrakMiesiaca {
  117.         if(this.numerMies < 1 || this.numerMies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
  118.         if(dzien < 1 || dzien > Miesiace.getLiczba(this.numerMies)) throw new BrakDnia("Podano niepoprawny dzien dla tego miesiaca");
  119.         this.dzien = dzien;
  120.     }
  121.  
  122.     /**
  123.      * @return the miesiac
  124.      */
  125.     public String getMiesiac() {
  126.         return miesiac;
  127.     }  
  128.  
  129.     /**
  130.      * @return the rok
  131.      */
  132.     public int getRok() {
  133.         return rok;
  134.     }
  135.  
  136.     /**
  137.      * @param rok the rok to set
  138.      */
  139.     public void setRok(int rok) {
  140.         this.rok = rok;
  141.     }
  142.  
  143.     /**
  144.      * @return the numerMies
  145.      */
  146.     public int getNumerMies() {
  147.         return numerMies;
  148.     }
  149.  
  150.     /**
  151.      * @param numerMies the numerMies to set
  152.      * @throws kalendarz.BrakMiesiaca
  153.      * @throws kalendarz.BrakDnia
  154.      */
  155.     public void setNumerMies(int numerMies)throws BrakMiesiaca,BrakDnia {
  156.         if(numerMies <0 || numerMies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
  157.         if(this.dzien < 1 || this.dzien > Miesiace.getLiczba(numerMies)) throw new BrakDnia("Podano niepoprawny dzien dla tego miesiaca");
  158.         this.numerMies = numerMies;
  159.         this.miesiac = Miesiace.getNzw(numerMies);
  160.     }
  161.    
  162.    
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /------------------------------------------------------------------------------------/
  170. package kalendarz;
  171.  
  172.  
  173. package kalendarz;
  174.  
  175. public class Main {
  176.     public static void main(String[] args){
  177.         try{
  178.         Data d1 = new Data(25,12,2019);
  179.         d1.tydzienPlus();
  180.         System.out.println(d1);
  181.         }
  182.         catch(BrakDnia | BrakMiesiaca bd){
  183.                 bd.printStackTrace(System.out);
  184.                 }        
  185.     }
  186.  
  187. }
  188.  
  189. /------------------------------------------------------------------------------------------------------------/
  190.  
  191. package kalendarz;
  192.  
  193.  
  194. public class Miesiac {
  195.     private int dni;
  196.     private String nazwa;
  197.    
  198.     public Miesiac(int dni, String nazwa){
  199.         this.dni = dni;
  200.         this.nazwa = nazwa;
  201.     }
  202.  
  203.     /**
  204.      * @return the dni
  205.      */
  206.     public int getDni() {
  207.         return dni;
  208.     }
  209.  
  210.     /**
  211.      * @param dni the dni to set
  212.      */
  213.     public void setDni(int dni) {
  214.         this.dni = dni;
  215.     }
  216.  
  217.     /**
  218.      * @return the nazwa
  219.      */
  220.     public String getNazwa() {
  221.         return nazwa;
  222.     }
  223.  
  224.     /**
  225.      * @param nazwa the nazwa to set
  226.      */
  227.     public void setNazwa(String nazwa) {
  228.         this.nazwa = nazwa;
  229.     }
  230.    
  231. }
  232. /-------------------------------------------------------------------------------------------------------------------/
  233.  
  234. package kalendarz;
  235.  
  236. public class Miesiace{
  237.     private static Miesiac[] tabl ={
  238.         new Miesiac(31,"Styczen"),
  239.         new Miesiac(0,""),
  240.         new Miesiac(31,"Marzec"),
  241.         new Miesiac(30,"Kwiecien"),
  242.         new Miesiac(31,"Maj"),
  243.         new Miesiac(30,"Czerwiec"),
  244.         new Miesiac(31,"Lipiec"),
  245.         new Miesiac(31,"Sierpien"),
  246.         new Miesiac(30,"Wrzesien"),
  247.         new Miesiac(31,"Pazdziernik"),
  248.         new Miesiac(30,"Listopad"),
  249.         new Miesiac(31,"Grudzien")
  250.     };  
  251.  
  252.     /**
  253.      * @param n
  254.      * @return the tabl
  255.      */
  256.     public static int getLiczba(int n) {
  257.         return tabl[n-1].getDni();        
  258.     }
  259.     public static String getNzw(int n){
  260.         return tabl[n-1].getNazwa();
  261.     }
  262.     public static void edytujMies(int n,int dni, String nazwa){
  263.         tabl[n-1] = new Miesiac(dni,nazwa);
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement