Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package kalendarzkr;
- /**
- *
- * @author student
- */
- public class KalendarzKR {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try{
- data d = new data(2015,2,29);
- System.out.println(d);
- }catch (Exception e){
- System.out.println(e.getMessage());
- }
- }
- }
- //------------------
- package kalendarzkr;
- /*
- klasa - data,
- klasa - miesiac,
- klasa - miesiace,
- */
- public class Miesiac {
- private int maxDay;
- private int number;
- private String name;
- public Miesiac(int maxDay, int number , String name){
- this.maxDay = maxDay;
- this.number = number;
- this.name = name;
- }
- public int getMaxDay() {
- return maxDay;
- }
- public int getNumber() {
- return number;
- }
- public String getName() {
- return name;
- }
- }
- //------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package kalendarzkr;
- /**
- *
- * @author student
- */
- public class Data {
- private int year;
- private int mounth;
- private int day;
- private int dayLimit;
- public Data(int y, int m, int d) throws Exception{
- this.year = y;
- if(m >=0 && m <=12){
- this.mounth = m;
- checkDayLimit();
- if(d <= this.dayLimit && d > 0){
- this.day = d;
- } else
- throw new Exception("Day 1 >= x <= " + this.dayLimit);
- }
- else throw new Exception("mounth 1 >= x <= 12");
- }
- private void checkDayLimit(){
- this.dayLimit = Miesiace.getMounth(this.mounth).getMaxDay();
- if(getCheckYear(this.year) && this.mounth == 2)
- this.dayLimit += 1;
- }
- public void addDays(int d){
- int daysum = this.day + d; //27+3 = 2
- if(d > 0){
- if ((daysum) < this.dayLimit){
- this.day += d;
- } else {
- while(daysum > this.dayLimit){
- if(this.mounth == 12){
- this.year++;
- this.mounth = 1;
- } else {
- this.mounth++;
- }
- daysum -= this.dayLimit;
- checkDayLimit();
- }
- this.day = daysum;
- }
- }
- }
- public boolean getCheckYear(int y)
- {
- return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
- }
- @Override
- public String toString() {
- return this.year + "-" + this.mounth + "("+ Miesiace.getMounth(this.mounth).getName() + ")" + "-" + this.day;
- }
- }
- //--------
- package kalendarzkr;
- /*
- klasa - data,
- klasa - miesiac,
- klasa - miesiace,
- */
- public class Miesiace {
- private static Miesiac[] miesiace = {
- new Miesiac(31, 1, "Styczen"),
- new Miesiac(28, 2, "Luty"),
- new Miesiac(31, 3, "Marzec"),
- new Miesiac(30, 4, "Kwiecien"),
- new Miesiac(31, 5, "Maj"),
- new Miesiac(30, 6, "Czerwiec"),
- new Miesiac(31, 7, "Lipiec"),
- new Miesiac(31, 8, "Sierpien"),
- new Miesiac(30, 9, "Wrzesien"),
- new Miesiac(31, 10, "Pazdziernik"),
- new Miesiac(30, 11, "Listopad"),
- new Miesiac(31, 12, "Grudzien"),
- };
- public static Miesiac getMounth(int nrMiesiaca){
- return miesiace[nrMiesiaca - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement