Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 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 data;
  7. import gui.*;
  8. /**
  9. *
  10. * @author Student
  11. */
  12. class ZłyNumerMiesiaca extends RuntimeException{
  13. ZłyNumerMiesiaca(String s){
  14. super(s);
  15. }
  16. ZłyNumerMiesiaca(Throwable t){
  17. super(t);
  18. }
  19. ZłyNumerMiesiaca(String s, Throwable t){
  20. super(s, t);
  21. }
  22. ZłyNumerMiesiaca(){};
  23. }
  24. class ZłyNumerDnia extends RuntimeException{
  25. ZłyNumerDnia(String s){
  26. super(s);
  27. }
  28. ZłyNumerDnia(Throwable t){
  29. super(t);
  30. }
  31. ZłyNumerDnia(String s, Throwable t){
  32. super(s, t);
  33. }
  34. ZłyNumerDnia(){};
  35. }
  36. class miesiac{
  37. private int liczbaDni;
  38. private String nazwa,rzymskie;
  39. public miesiac (int liczbaDni, String nazwa, String rzymskie){
  40. this.liczbaDni=liczbaDni;
  41. this.nazwa=nazwa;
  42. this.rzymskie=rzymskie;
  43. }
  44. public int getLiczbaDni() {
  45. return liczbaDni;
  46. }
  47. public String getNazwa() {
  48. return nazwa;
  49. }
  50. public String getRzymskie(){
  51. return rzymskie;
  52. }
  53. }
  54. class miesiace{
  55. miesiac miesiac[]={
  56. new miesiac(31,"Styczen","I"),
  57. new miesiac(28,"Luty","II"),
  58. new miesiac(31,"Marzec","III"),
  59. new miesiac(30,"Kwiecien","IV"),
  60. new miesiac(31,"Maj","V"),
  61. new miesiac(30,"Czerwiec","VI"),
  62. new miesiac(31,"Lipiec","VII"),
  63. new miesiac(31,"Sierpien","VIII"),
  64. new miesiac(30,"Wrzesien","IX"),
  65. new miesiac(31,"Pazdziernik","X"),
  66. new miesiac(30,"Listopad","XI"),
  67. new miesiac(31,"Grudzien","XII"),
  68. };
  69. public int getIlośćDni(int n) throws ZłyNumerMiesiaca{
  70. try{
  71. return miesiac[n-1].getLiczbaDni();
  72. }catch(ArrayIndexOutOfBoundsException e){
  73. throw new ZłyNumerMiesiaca("Błędny miesiąc.");
  74. }
  75. }
  76. public String getNazweMiesiaca(int n){
  77. return miesiac[n-1].getNazwa();
  78. }
  79. public String getRzymskie(int n){
  80. return miesiac[n-1].getRzymskie();
  81. }
  82. }
  83. public class data extends miesiace {
  84. private int dzien,miesiac,rok;
  85. static final int FORMAT_KROTKI = 991;
  86. static final int FORMAT_DLUGI = 992;
  87. static final int FORMAT_SREDNI = 993;
  88. static final String tydzien[] = {"Niedziela","Poniedzialek","Wtorek","Sroda","Czwartek","Piatek","Sobota"};
  89. static final data POCZATEK_TYGODNIA = new data(9,4,2018);
  90.  
  91. /**
  92. * @param args the command line arguments
  93. */
  94. public data(int dzien, int miesiac, int rok) throws ZłyNumerDnia {
  95. this.miesiac = miesiac;
  96. if(sprawdzDzien(dzien, miesiac)){
  97. this.dzien = dzien;
  98. }else{
  99. throw new ZłyNumerDnia("Zły dzień");
  100. }
  101. this.rok = rok;
  102. }
  103. private boolean isPrzestepny(int rok)
  104. {
  105. return (this.rok % 4 == 0 && this.rok % 100 != 0 || this.rok % 400 == 0);
  106. }
  107. public int getDzien(){
  108. return this.dzien;
  109. }
  110. public int getMiesiac(){
  111. return this.miesiac;
  112. }
  113. public String getNazweMiesiaca(){
  114. return getNazweMiesiaca(this.miesiac);
  115. }
  116. public String getRzymskie(){
  117. return getRzymskie(this.miesiac);
  118. }
  119. public int getRok(){
  120. return this.rok;
  121. }
  122. public int compateTo(data data){
  123.  
  124. return 0;
  125. }
  126. public String getNazweDnia(){
  127. //Innaczej nie potrafie
  128. int year = this.rok;
  129. int day = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year) / 400) + 1) % 7;
  130. return tydzien[day];
  131. }
  132. public void tydzienPlus(){
  133. if(isPrzestepny(this.rok) && this.miesiac ==2){
  134. if(this.dzien + 7 < getIlośćDni(miesiac)+1){
  135. this.dzien +=7;
  136. }else{
  137. this.dzien = this.dzien+7-getIlośćDni(miesiac);
  138. if(this.miesiac < 12){
  139. this.miesiac = this.miesiac+1;
  140. }else{
  141. this.rok+=1;
  142. this.miesiac = 1;
  143. }
  144. }
  145. }else{
  146. if(this.dzien + 7 < getIlośćDni(miesiac)){
  147. this.dzien +=7;
  148. }else{
  149. this.dzien = this.dzien+7-getIlośćDni(miesiac);
  150. if(this.miesiac < 12){
  151. this.miesiac = this.miesiac+1;
  152. }else{
  153. this.rok+=1;
  154. this.miesiac = 1;
  155. }
  156. }
  157. }
  158. }
  159. public void tydzienMinus(){
  160. if(isPrzestepny(this.rok) && this.miesiac ==2){
  161. if(this.dzien - 7 > getIlośćDni(miesiac)+1){
  162. this.dzien -=7;
  163. }else{
  164. this.dzien = this.dzien-7+getIlośćDni(miesiac);
  165. if(this.miesiac > 1){
  166. this.miesiac = this.miesiac-1;
  167. }else{
  168. this.rok-=1;
  169. this.miesiac = 12;
  170. }
  171. }
  172. }else{
  173. if(this.dzien - 7 > getIlośćDni(miesiac)){
  174. this.dzien -=7;
  175. }else{
  176. this.dzien = this.dzien-7+getIlośćDni(miesiac);
  177. if(this.miesiac > 1){
  178. this.miesiac = this.miesiac-1;
  179. }else{
  180. this.rok-=1;
  181. this.miesiac = 12;
  182. }
  183. }
  184. }
  185.  
  186. }
  187. public String toString(int typ){
  188. if(typ == FORMAT_KROTKI){
  189. return dzien+"/"+miesiac+"/"+rok;
  190. }
  191. else if(typ == FORMAT_DLUGI){
  192. return this.getNazweDnia()+","+dzien+" "+this.getNazweMiesiaca()+" "+rok;
  193. }
  194. else if(typ == FORMAT_SREDNI){
  195. return dzien+" "+this.getRzymskie()+" "+rok;
  196. }
  197. else{
  198. return "Nieznany typ";
  199. }
  200. }
  201. public boolean sprawdzDzien(int dzien, int miesiac){
  202. if(dzien < getIlośćDni(miesiac)+1 && dzien > 0){
  203. return true;
  204. }else{
  205. return false;
  206. }
  207. }
  208. public static void main(String[] args) {
  209. // TODO code application logic here
  210. //int MarginLeft = 10;
  211. //int MarginTop = 10;
  212. /*
  213. NewJFrame frame;
  214. frame = new NewJFrame();
  215. frame.setVisible(true);
  216.  
  217. frame.setBounds(100, 100, 480, 150);
  218. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  219. frame.getContentPane().setLayout(null);
  220. frame.setResizable(false);
  221. JTextField textField_1 = new JTextField();
  222. textField_1.setBounds(0+MarginLeft, 30, 86, 20);
  223. JTextField textField_2 = new JTextField();
  224. textField_2.setBounds(128+MarginLeft, 30, 86, 20);
  225. JTextField textField_3 = new JTextField();
  226. textField_3.setBounds(256+MarginLeft, 30, 86, 20);
  227. JTextField textField_4 = new JTextField();
  228. textField_4.setBounds(120, 90, 120, 20);
  229. JLabel label1 = new JLabel("Rok");
  230. label1.setBounds(30+MarginLeft, 0+MarginTop, 46, 14);
  231. JLabel label2 = new JLabel("Miesiac");
  232. label2.setBounds(150+MarginLeft, 0+MarginTop, 46, 14);
  233. JLabel label3 = new JLabel("Dzien");
  234. label3.setBounds(285+MarginLeft, 0+MarginTop, 46, 14);
  235. frame.getContentPane().add(label1);
  236. JButton btn1 = new JButton("Ustaw");
  237. btn1.setBounds(380, 28, 89, 23);
  238. JButton btn2 = new JButton("Cofnij tydzien");
  239. btn2.setBounds(50, 60, 110, 23);
  240. JButton btn3 = new JButton("Dodaj tydzien");
  241. btn3.setBounds(180, 60, 110, 23);
  242. frame.getContentPane().add(textField_1);
  243. frame.getContentPane().add(textField_2);
  244. frame.getContentPane().add(textField_3);
  245. frame.getContentPane().add(textField_4);
  246. frame.getContentPane().add(label1);
  247. frame.getContentPane().add(label2);
  248. frame.getContentPane().add(label3);
  249. frame.getContentPane().add(btn1);
  250. frame.getContentPane().add(btn2);
  251. frame.getContentPane().add(btn3);
  252. frame.setVisible(true);
  253. */
  254. data d = new data(31,12,1997);
  255. System.out.print(d.toString(FORMAT_KROTKI)+"\n"+d.toString(FORMAT_SREDNI)+"\n"+d.toString(FORMAT_DLUGI));
  256. }
  257. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  258. // TODO add your handling code here:
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement