Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kalendarz;
- public class BrakDnia extends Exception {
- public BrakDnia() {
- }
- /**
- * Constructs an instance of <code>BrakDnia</code> with the specified detail
- * message.
- *
- * @param msg the detail message.
- */
- public BrakDnia(String msg) {
- super(msg);
- }
- }
- /---------------------------------------------------------------------------------------------/
- package kalendarz;
- public class BrakMiesiaca extends Exception {
- public BrakMiesiaca() {
- }
- /**
- * Constructs an instance of <code>BrakMiesiaca</code> with the specified
- * detail message.
- *
- * @param msg the detail message.
- */
- public BrakMiesiaca(String msg) {
- super(msg);
- }
- }
- /---------------------------------------------------------------------------------------------/
- package zadanie5;
- public class Data {
- private int dzien, rok, numerMies;
- private String miesiac;
- @Override
- public String toString() {
- return dzien + " " + miesiac + " " + rok;
- }
- public Data(int dz, int mies, int rok)throws BrakDnia, BrakMiesiaca{
- isPrzestepny(rok);
- if(mies < 1 || mies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
- if(dz < 1 || dz > Miesiace.getLiczba(mies)) throw new BrakDnia("Podano niepoprawny dzien");
- this.dzien = dz;
- this.numerMies=mies;
- this.miesiac = Miesiace.getNzw(mies);
- this.rok = rok;
- }
- private void isPrzestepny(int rok){
- if(rok % 4 == 0 && rok % 100 != 0 || rok % 400 == 0)
- Miesiace.edytujMies(2, 29,"Luty");
- else{
- Miesiace.edytujMies(2, 28,"Luty");
- }
- }
- public void tydzienPlus(){
- if(this.dzien + 7 <= Miesiace.getLiczba(numerMies)){
- this.dzien +=7;
- }else{
- this.dzien = this.dzien+7-Miesiace.getLiczba(numerMies);
- if(this.numerMies < 12){
- this.miesiac = Miesiace.getNzw(numerMies+1);
- this.numerMies = this.numerMies+1;
- }else{
- this.rok+=1;
- this.miesiac = Miesiace.getNzw(1);
- this.numerMies = 1;
- }
- }
- }
- public void tydzienMinus(){
- if(this.dzien - 7 > 0){
- this.dzien -=7;
- }else{
- this.dzien = this.dzien-7+Miesiace.getLiczba(numerMies);
- if(this.numerMies > 1){
- this.miesiac = Miesiace.getNzw(numerMies-1);
- this.numerMies = this.numerMies-1;
- }else{
- this.rok-=1;
- this.miesiac = Miesiace.getNzw(12);
- this.numerMies = 12;
- }
- }
- }
- /**
- * @return the dzien
- */
- public int getDzien() {
- return dzien;
- }
- /**
- * @param dzien the dzien to set
- * @throws kalendarz.BrakDnia
- * @throws kalendarz.BrakMiesiaca
- */
- public void setDzien(int dzien)throws BrakDnia, BrakMiesiaca {
- if(this.numerMies < 1 || this.numerMies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
- if(dzien < 1 || dzien > Miesiace.getLiczba(this.numerMies)) throw new BrakDnia("Podano niepoprawny dzien dla tego miesiaca");
- this.dzien = dzien;
- }
- /**
- * @return the miesiac
- */
- public String getMiesiac() {
- return miesiac;
- }
- /**
- * @return the rok
- */
- public int getRok() {
- return rok;
- }
- /**
- * @param rok the rok to set
- */
- public void setRok(int rok) {
- this.rok = rok;
- }
- /**
- * @return the numerMies
- */
- public int getNumerMies() {
- return numerMies;
- }
- /**
- * @param numerMies the numerMies to set
- * @throws kalendarz.BrakMiesiaca
- * @throws kalendarz.BrakDnia
- */
- public void setNumerMies(int numerMies)throws BrakMiesiaca,BrakDnia {
- if(numerMies <0 || numerMies > 12) throw new BrakMiesiaca("Podano niepoprawny miesiac");
- if(this.dzien < 1 || this.dzien > Miesiace.getLiczba(numerMies)) throw new BrakDnia("Podano niepoprawny dzien dla tego miesiaca");
- this.numerMies = numerMies;
- this.miesiac = Miesiace.getNzw(numerMies);
- }
- }
- /------------------------------------------------------------------------------------/
- package kalendarz;
- package kalendarz;
- public class Main {
- public static void main(String[] args){
- try{
- Data d1 = new Data(25,12,2019);
- d1.tydzienPlus();
- System.out.println(d1);
- }
- catch(BrakDnia | BrakMiesiaca bd){
- bd.printStackTrace(System.out);
- }
- }
- }
- /------------------------------------------------------------------------------------------------------------/
- package kalendarz;
- public class Miesiac {
- private int dni;
- private String nazwa;
- public Miesiac(int dni, String nazwa){
- this.dni = dni;
- this.nazwa = nazwa;
- }
- /**
- * @return the dni
- */
- public int getDni() {
- return dni;
- }
- /**
- * @param dni the dni to set
- */
- public void setDni(int dni) {
- this.dni = dni;
- }
- /**
- * @return the nazwa
- */
- public String getNazwa() {
- return nazwa;
- }
- /**
- * @param nazwa the nazwa to set
- */
- public void setNazwa(String nazwa) {
- this.nazwa = nazwa;
- }
- }
- /-------------------------------------------------------------------------------------------------------------------/
- package kalendarz;
- public class Miesiace{
- private static Miesiac[] tabl ={
- new Miesiac(31,"Styczen"),
- new Miesiac(0,""),
- new Miesiac(31,"Marzec"),
- new Miesiac(30,"Kwiecien"),
- new Miesiac(31,"Maj"),
- new Miesiac(30,"Czerwiec"),
- new Miesiac(31,"Lipiec"),
- new Miesiac(31,"Sierpien"),
- new Miesiac(30,"Wrzesien"),
- new Miesiac(31,"Pazdziernik"),
- new Miesiac(30,"Listopad"),
- new Miesiac(31,"Grudzien")
- };
- /**
- * @param n
- * @return the tabl
- */
- public static int getLiczba(int n) {
- return tabl[n-1].getDni();
- }
- public static String getNzw(int n){
- return tabl[n-1].getNazwa();
- }
- public static void edytujMies(int n,int dni, String nazwa){
- tabl[n-1] = new Miesiac(dni,nazwa);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement