Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.89 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 kkalendarz;
  7.  
  8. /**
  9. *
  10. * @author Student
  11. */
  12. public class WyjatekMiesiac extends RuntimeException{
  13.  
  14. WyjatekMiesiac(String s){
  15. super(s);
  16. }
  17.  
  18. WyjatekMiesiac(){
  19.  
  20. }
  21. }
  22. ////////////////
  23. /*
  24. * To change this license header, choose License Headers in Project Properties.
  25. * To change this template file, choose Tools | Templates
  26. * and open the template in the editor.
  27. */
  28. package kkalendarz;
  29.  
  30. /**
  31. *
  32. * @author Student
  33. */
  34. public class WyjatekDzien extends RuntimeException{
  35.  
  36.  
  37.  
  38. public WyjatekDzien()
  39. {
  40. super();
  41. }
  42.  
  43. public WyjatekDzien(String s){
  44. super(s);
  45. }
  46.  
  47.  
  48. }
  49. ////////////////
  50. /*
  51. * To change this license header, choose License Headers in Project Properties.
  52. * To change this template file, choose Tools | Templates
  53. * and open the template in the editor.
  54. */
  55. package kkalendarz;
  56.  
  57.  
  58. public class Miesiace {
  59.  
  60. private static Miesiac miesiace[] = {
  61. new Miesiac("01", 31, "Styczen"),
  62. new Miesiac("02", 28, "Luty"),
  63. new Miesiac("03", 31, "Marzec"),
  64. new Miesiac("04", 30, "Kwiecien"),
  65. new Miesiac("05", 31, "Maj"),
  66. new Miesiac("06", 30, "Czerwiec"),
  67. new Miesiac("07", 31, "Lipiec"),
  68. new Miesiac("08", 31, "Sierpien"),
  69. new Miesiac("09", 30, "Wrzesien"),
  70. new Miesiac("10", 31, "Pazdziernik"),
  71. new Miesiac("11", 30, "Listopad"),
  72. new Miesiac("12", 31, "Grudzien")
  73. //new Miesiac("02", 29, "Luty")
  74. };
  75. public static Miesiac getMiesiac( int nrMiesiaca) {
  76. try {
  77. return miesiace[nrMiesiaca - 1];
  78. } catch (Exception e){
  79. throw new WyjatekMiesiac("Wprowadzono nieprawidłowy numer miesiaca!");
  80. }
  81. }
  82. }
  83. //////////////////
  84. /*
  85. * To change this license header, choose License Headers in Project Properties.
  86. * To change this template file, choose Tools | Templates
  87. * and open the template in the editor.
  88. */
  89.  
  90. package kkalendarz;
  91.  
  92.  
  93.  
  94. public class Miesiac {
  95. private String numerMiesiaca;
  96. private int liczbaDni;
  97. private String nazwa;
  98.  
  99. public Miesiac(String numerMiesiaca, int liczbaDni, String nazwa) {
  100. this.numerMiesiaca = numerMiesiaca;
  101. this.liczbaDni = liczbaDni;
  102. this.nazwa = nazwa;
  103. }
  104.  
  105. public String getNumerMiesiaca() {
  106. return numerMiesiaca;
  107. }
  108.  
  109. public int getLiczbaDni() {
  110. return liczbaDni;
  111. }
  112.  
  113. public String getNazwa() {
  114. return nazwa;
  115. }
  116.  
  117.  
  118. }
  119. //////////////////
  120. /*
  121. * To change this license header, choose License Headers in Project Properties.
  122. * To change this template file, choose Tools | Templates
  123. * and open the template in the editor.
  124. */
  125.  
  126. package kkalendarz;
  127.  
  128.  
  129. public class Kalendarzmain {
  130.  
  131.  
  132. public static void main(String[] args) throws Exception{
  133. try {
  134. Kalendarz k = new Kalendarz(2019,12,31);
  135. System.out.println(k.dzienTygodnia());
  136. System.out.println(k.toString(k.NUM));
  137. } catch (Exception e){
  138. System.out.println(e.getMessage());
  139. e.printStackTrace(System.out);
  140. }
  141. //System.out.println(k.getMaxday());
  142.  
  143. }
  144. }
  145. /////////////////////
  146. package kkalendarz;
  147.  
  148. public class Kalendarz {
  149.  
  150. private int rok;
  151. private Miesiac miesiac;
  152. private int dzien;
  153.  
  154. public static final int LONG = 753;
  155. public static final int MID = 951;
  156. public static final int NUM = 852;
  157.  
  158. private static String[] day = {"Poniedzialek","Wtorek","Sroda","Czwartek","Piatek","Sobota","Niedziela"};
  159.  
  160. public Kalendarz(int rok, int miesiac, int dzien) throws Exception {
  161. if (rok > 0){
  162. this.rok = rok;
  163. }else{
  164. throw new WyjatekDzien("Wprowadzono nieprawidłowy numer roku!");
  165. }
  166.  
  167.  
  168. if (miesiac==2 && czyPrzestepny(rok)){
  169. this.miesiac = new Miesiac("02", 29, "Luty");
  170. }else{
  171. this.miesiac = Miesiace.getMiesiac(miesiac);
  172. }
  173.  
  174.  
  175. if (dzien > 0 && dzien <= this.miesiac.getLiczbaDni()){
  176. this.dzien = dzien;
  177. }else{
  178. throw new WyjatekDzien("Wprowadzono nieprawidłowy dzień! - Maksymalna liczba dni dla miesiąca "+this.miesiac.getNazwa()+" to "+this.miesiac.getLiczbaDni()+".");
  179. }
  180.  
  181. }
  182. public String toString(int format){
  183. switch (format){
  184. case 753:
  185. return dzien+"."+miesiac.getNazwa()+"."+rok;
  186. case 951:
  187. return dzien+"."+miesiac.getNazwa()+"/"+rok;
  188. case 852:
  189. return dzien+"/"+miesiac.getNumerMiesiaca()+"/"+rok;
  190. }
  191. return dzien+"/"+miesiac.getNumerMiesiaca()+"/"+rok;
  192. }
  193.  
  194.  
  195. private boolean czyPrzestepny(int rok)
  196. {
  197. return (rok % 4 == 0 && rok % 100 != 0 || rok % 400 == 0);
  198. }
  199.  
  200. public String dzienTygodnia() throws Exception{
  201. Kalendarz pref = new Kalendarz(2018,01,01);
  202. int save = 0;
  203. //save = this.dzien - pref.dzien;
  204. //int roz = Integer.parseInt(this.miesiac.getNumerMiesiaca()) - Integer.parseInt(pref.miesiac.getNumerMiesiaca());
  205. //if compareTo
  206.  
  207. //if (compareTo(pref) == 1){
  208. // int roz = this.rok - pref.rok;
  209. // return Integer.toString(roz);
  210. //}
  211.  
  212. return Integer.toString(save);
  213. //return day[0];
  214. }
  215.  
  216. public int compareTo(Kalendarz por){
  217.  
  218. if (this.rok > por.rok){
  219. return 1;
  220. }
  221. if (this.rok < por.rok){
  222. return -1;
  223. }
  224. if (this.rok == por.rok){
  225.  
  226. if (Integer.parseInt(this.miesiac.getNumerMiesiaca()) > Integer.parseInt(por.miesiac.getNumerMiesiaca())){
  227. return 1;
  228. }
  229. if (Integer.parseInt(this.miesiac.getNumerMiesiaca()) < Integer.parseInt(por.miesiac.getNumerMiesiaca())){
  230. return -1;
  231. }
  232. if (Integer.parseInt(this.miesiac.getNumerMiesiaca()) == Integer.parseInt(por.miesiac.getNumerMiesiaca())){
  233. if (this.dzien > por.dzien){
  234. return 1;
  235. }
  236. if (this.dzien > por.dzien){
  237. return -1;
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. }
  244. ////////////////////////*
  245. * To change this license header, choose License Headers in Project Properties.
  246. * To change this template file, choose Tools | Templates
  247. * and open the template in the editor.
  248. */
  249. package gui;
  250.  
  251. /**
  252. *
  253. * @author Student
  254. */
  255. public class NewJFrame extends javax.swing.JFrame {
  256.  
  257. /**
  258. * Creates new form NewJFrame
  259. */
  260. public NewJFrame() {
  261. initComponents();
  262. }
  263.  
  264. /**
  265. * This method is called from within the constructor to initialize the form.
  266. * WARNING: Do NOT modify this code. The content of this method is always
  267. * regenerated by the Form Editor.
  268. */
  269. @SuppressWarnings("unchecked")
  270. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  271. private void initComponents() {
  272.  
  273. jScrollBar1 = new javax.swing.JScrollBar();
  274. jButton1 = new javax.swing.JButton();
  275. jButton2 = new javax.swing.JButton();
  276. jScrollPane1 = new javax.swing.JScrollPane();
  277. jTextArea1 = new javax.swing.JTextArea();
  278. jButton3 = new javax.swing.JButton();
  279. jButton4 = new javax.swing.JButton();
  280. jButton5 = new javax.swing.JButton();
  281. jTextField1 = new javax.swing.JTextField();
  282.  
  283. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  284.  
  285. jButton1.setText("Przycisk 1");
  286. jButton1.addActionListener(new java.awt.event.ActionListener() {
  287. public void actionPerformed(java.awt.event.ActionEvent evt) {
  288. jButton1ActionPerformed(evt);
  289. }
  290. });
  291.  
  292. jButton2.setText("Przycisk 2");
  293. jButton2.addActionListener(new java.awt.event.ActionListener() {
  294. public void actionPerformed(java.awt.event.ActionEvent evt) {
  295. jButton2ActionPerformed(evt);
  296. }
  297. });
  298.  
  299. jTextArea1.setColumns(20);
  300. jTextArea1.setRows(5);
  301. jScrollPane1.setViewportView(jTextArea1);
  302.  
  303. jButton3.setText("W tył");
  304.  
  305. jButton4.setText("W przód");
  306.  
  307. jButton5.setText("OK");
  308.  
  309. jTextField1.setText("Przesuń tydzień");
  310.  
  311. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  312. getContentPane().setLayout(layout);
  313. layout.setHorizontalGroup(
  314. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  315. .addGroup(layout.createSequentialGroup()
  316. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  317. .addGroup(layout.createSequentialGroup()
  318. .addGap(28, 28, 28)
  319. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  320. .addGroup(layout.createSequentialGroup()
  321. .addComponent(jButton3)
  322. .addGap(48, 48, 48)
  323. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  324. .addGap(12, 12, 12))
  325. .addGroup(layout.createSequentialGroup()
  326. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
  327. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  328. .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
  329. .addGap(32, 32, 32)
  330. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  331. .addComponent(jButton4)
  332. .addComponent(jButton5)))
  333. .addGroup(layout.createSequentialGroup()
  334. .addGap(136, 136, 136)
  335. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
  336. .addContainerGap(64, Short.MAX_VALUE))
  337. );
  338. layout.setVerticalGroup(
  339. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  340. .addGroup(layout.createSequentialGroup()
  341. .addGap(54, 54, 54)
  342. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  343. .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  344. .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  345. .addComponent(jButton5))
  346. .addGap(46, 46, 46)
  347. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  348. .addComponent(jButton4)
  349. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  350. .addComponent(jButton3))
  351. .addGap(15, 15, 15)
  352. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
  353. .addContainerGap(54, Short.MAX_VALUE))
  354. );
  355.  
  356. pack();
  357. }// </editor-fold>
  358.  
  359. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  360. // TODO add your handling code here:
  361. System.out.println("Wcisnieto " + jButton1.getText());
  362. jButton1.setText("Przycisk 2");
  363. jTextArea1.setText("1");
  364.  
  365. }
  366.  
  367. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  368. // TODO add your handling code here:
  369. System.out.println("Wcisnieto " + jButton2.getText());
  370. jTextArea1.setText("2");
  371.  
  372.  
  373. }
  374.  
  375. /**
  376. * @param args the command line arguments
  377. */
  378. public static void main(String args[]) {
  379. /* Set the Nimbus look and feel */
  380. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  381. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  382. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  383. */
  384. try {
  385. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  386. if ("Nimbus".equals(info.getName())) {
  387. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  388. break;
  389. }
  390. }
  391. } catch (ClassNotFoundException ex) {
  392. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  393. } catch (InstantiationException ex) {
  394. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  395. } catch (IllegalAccessException ex) {
  396. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  397. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  398. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  399. }
  400. //</editor-fold>
  401.  
  402. /* Create and display the form */
  403. java.awt.EventQueue.invokeLater(new Runnable() {
  404. public void run() {
  405. new NewJFrame().setVisible(true);
  406. }
  407. });
  408. }
  409.  
  410. // Variables declaration - do not modify
  411. private javax.swing.JButton jButton1;
  412. private javax.swing.JButton jButton2;
  413. private javax.swing.JButton jButton3;
  414. private javax.swing.JButton jButton4;
  415. private javax.swing.JButton jButton5;
  416. private javax.swing.JScrollBar jScrollBar1;
  417. private javax.swing.JScrollPane jScrollPane1;
  418. private javax.swing.JTextArea jTextArea1;
  419. private javax.swing.JTextField jTextField1;
  420. // End of variables declaration
  421. }
  422. ////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement