Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. import java.awt.Container;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.BufferedReader;
  8. import java.io.BufferedWriter;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JComboBox;
  16. import javax.swing.JDialog;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextArea;
  22. import javax.swing.JTextField;
  23.  
  24. import java.awt.Container;
  25. import java.awt.Font;
  26. import java.awt.GridLayout;
  27.  
  28. import javax.swing.JButton;
  29. import javax.swing.JFrame;
  30. public class Porownanie extends JPanel implements ActionListener {
  31. private int p;
  32. private String odset = null;
  33. ArrayList<Double> ListaOprocentowania = new ArrayList<Double>();
  34. double proc;
  35. JDialog dialog = new JDialog();
  36.  
  37. JLabel pusty = new JLabel();
  38. JLabel pusty1= new JLabel();
  39. JLabel pusty2 = new JLabel();
  40. JLabel pusty3 = new JLabel();
  41. JLabel pusty4 = new JLabel();
  42. JLabel pusty5 = new JLabel();
  43. JLabel pusty6 = new JLabel();
  44. JLabel pusty7 = new JLabel();
  45.  
  46. JFrame frame = new JFrame();
  47. JLabel tekst = new JLabel("Wybierz lokate: ");
  48.  
  49. JLabel wprowadzKwote = new JLabel("Wprowadź kwotę:");
  50. static JTextField JTFwprowadzKwote1 = new JTextField();
  51.  
  52.  
  53. double dbZmienna;
  54.  
  55.  
  56. JButton oblicz = new JButton("Oblicz");
  57. JButton oblicz2 = new JButton("Oblicz2");
  58. JButton oblicz3 = new JButton("Oblicz3");
  59. JButton oblicz4 = new JButton("Oblicz4");
  60. JButton oblicz5 = new JButton("Oblicz5");
  61.  
  62. JLabel odst = new JLabel("Odsetki:");
  63. public static JTextField odsetki = new JTextField();
  64. public static JTextField odsetki2 = new JTextField();
  65. public static JTextField odsetki3 = new JTextField();
  66. public static JTextField odsetki4 = new JTextField();
  67. public static JTextField odsetki5 = new JTextField();
  68.  
  69. JLabel stanKonta = new JLabel("Na koniec:");
  70. public static JTextArea stanKonta1 = new JTextArea();
  71. public static JTextArea stanKonta2 = new JTextArea();
  72. public static JTextArea stanKonta3 = new JTextArea();
  73. public static JTextArea stanKonta4 = new JTextArea();
  74. public static JTextArea stanKonta5 = new JTextArea();
  75.  
  76. JButton wybieram1 = new JButton("Wybieram lokate banku numer 1");
  77. JButton wybieram2 = new JButton("Wybieram lokate banku numer 2");
  78. JButton wybieram3 = new JButton("Wybieram lokate banku numer 3");
  79. JButton wybieram4 = new JButton("Wybieram lokate banku numer 4");
  80. JButton wybieram5 = new JButton("Wybieram lokate banku numer 5");
  81.  
  82. JLabel coDoPlikuZapisane = new JLabel();
  83. JLabel wpisz = new JLabel("Wprowadź kwotę: ");
  84. JLabel koniec = new JLabel("Stan konta na koniec lokaty: ");
  85.  
  86. public Porownanie() {
  87.  
  88. try {
  89. BufferedReader br = new BufferedReader(new FileReader(
  90. "bin/BazaOfertBankowOprocentowanie.txt"));
  91.  
  92. String linia = null;
  93. while ((linia = br.readLine()) != null) {
  94. proc = Double.parseDouble(linia);
  95. ListaOprocentowania.add(proc);
  96. }
  97. } catch (Exception e1) {
  98. System.err.println("Wystapil blad przy wczytywaniu danych"+e1);
  99. e1.printStackTrace();
  100. }
  101.  
  102. oblicz.addActionListener(this); oblicz2.addActionListener(this);oblicz3.addActionListener(this);oblicz4.addActionListener(this);oblicz5.addActionListener(this);
  103. wybieram1.addActionListener(this);
  104. wybieram2.addActionListener(this);
  105. wybieram3.addActionListener(this);
  106. wybieram4.addActionListener(this);
  107. wybieram5.addActionListener(this);
  108. // dodane wierszami (6 w linii), puste są po to zeby ładnie wyglądało
  109. add(wprowadzKwote); add(JTFwprowadzKwote1);add(pusty1);add(pusty2);add(pusty3);
  110. add(pusty4);add(pusty5);add(oblicz);add(oblicz2);add(oblicz3);add(oblicz4);add(oblicz5);
  111. add(odst);add(odsetki);add(odsetki2);add(odsetki3);add(odsetki4);add(odsetki5);
  112. add(stanKonta); add(stanKonta1);add(stanKonta2);add(stanKonta3);add(stanKonta4);add(stanKonta5);
  113. add(pusty);add(wybieram1);add(wybieram2);add(wybieram3);add(wybieram4);add(wybieram5);
  114. add(coDoPlikuZapisane);
  115.  
  116. Dimension preferredSize = getPreferredSize();
  117. preferredSize.height = 50;
  118. preferredSize.width = 140;
  119.  
  120. wprowadzKwote.setPreferredSize(preferredSize);
  121. JTFwprowadzKwote1.setPreferredSize(preferredSize);
  122. oblicz.setPreferredSize(preferredSize);
  123. oblicz2.setPreferredSize(preferredSize);
  124. oblicz3.setPreferredSize(preferredSize);
  125. oblicz4.setPreferredSize(preferredSize);
  126. oblicz5.setPreferredSize(preferredSize);
  127. pusty1.setPreferredSize(preferredSize);
  128. pusty2.setPreferredSize(preferredSize);
  129. pusty3.setPreferredSize(preferredSize);
  130. pusty4.setPreferredSize(preferredSize);
  131. pusty5.setPreferredSize(preferredSize);
  132.  
  133. odst.setPreferredSize(preferredSize);
  134. odsetki.setPreferredSize(preferredSize);
  135. odsetki2.setPreferredSize(preferredSize);
  136. odsetki3.setPreferredSize(preferredSize);
  137. odsetki4.setPreferredSize(preferredSize);
  138. odsetki5.setPreferredSize(preferredSize);
  139.  
  140. stanKonta.setPreferredSize(preferredSize);
  141. stanKonta1.setPreferredSize(preferredSize);
  142. stanKonta2.setPreferredSize(preferredSize);
  143. stanKonta3.setPreferredSize(preferredSize);
  144. stanKonta4.setPreferredSize(preferredSize);
  145. stanKonta5.setPreferredSize(preferredSize);
  146.  
  147. pusty.setPreferredSize(preferredSize);
  148. wybieram1.setSize(200, 50);
  149. wybieram2.setSize(200, 50);
  150. wybieram3.setSize(200, 50);
  151. wybieram4.setSize(200, 50);
  152. wybieram5.setSize(200, 50);
  153.  
  154. }
  155.  
  156. public void actionPerformed(ActionEvent e) {
  157. Double ods = (double) 0;
  158. Double k = (double) 0;
  159. Double i=(double) 0;
  160.  
  161. String numStr = JTFwprowadzKwote1.getText();
  162. int num = Integer.parseInt(numStr);
  163.  
  164.  
  165.  
  166.  
  167.  
  168. String n;
  169. String kwotaPoczatkowa;
  170. if (e.getSource() == oblicz) {
  171.  
  172.  
  173. while(true)
  174. {
  175. if(numStr==null)
  176. break; // if you press cancel it will exit
  177. try {
  178. num=Integer.parseInt(numStr);
  179. break;
  180. } catch(NumberFormatException ex)
  181. {
  182. JOptionPane.showMessageDialog(frame, "Podałeś złą wartość w polu 'Wprowadź kwotę'");
  183. }
  184. }
  185. stanKonta1.setText("");
  186. kwotaPoczatkowa =JTFwprowadzKwote1.getText();
  187. p = Integer.parseInt(kwotaPoczatkowa);
  188. i= (double) (ListaOprocentowania.get(1)/ ListaOprocentowania.get(0)); //ListaOprocentowania.get(1) to 0.05
  189. k = (double) (p*(1 + ListaOprocentowania.get(0)* i)) ; //ListaOprocentowania.get(0) to 3 lata
  190. n = Double.toString(k);
  191. stanKonta1.append(n);
  192.  
  193. ods = (Double) (p * ListaOprocentowania.get(1));
  194. dbZmienna=Math.round(ods*100)/100;
  195.  
  196. odset =Double.toString(dbZmienna);
  197. odsetki.setText(odset);
  198.  
  199.  
  200. } else if (e.getSource() == oblicz2) {
  201. stanKonta2.setText("");
  202. kwotaPoczatkowa = JTFwprowadzKwote1.getText();
  203. p = Integer.parseInt(kwotaPoczatkowa);
  204. i= (double) (ListaOprocentowania.get(2));
  205. double z = (double) (p*(1+i));
  206. double k2=(double) (z*(1+i));
  207. k=(double) (k2*(1+i));
  208.  
  209.  
  210. dbZmienna=Math.round(k*100)/100;
  211. n = Double.toString(dbZmienna);
  212. stanKonta2.append(n);
  213.  
  214. double zmienna111 = (double) (dbZmienna-p);
  215. //dbZmienna = (double) (p*0.02);
  216. odset =Double.toString(zmienna111);
  217. odsetki2.setText(odset);
  218.  
  219.  
  220. } else if (e.getSource() == oblicz3) {
  221. stanKonta3.setText("");
  222. kwotaPoczatkowa = JTFwprowadzKwote1.getText();
  223. p = Integer.parseInt(kwotaPoczatkowa);
  224. double opro1 = (double) (p + p * ListaOprocentowania.get(3));
  225. double opro2 = (double) (opro1 + opro1 * ListaOprocentowania.get(4));
  226. double opro3 = (double) (opro2 + opro2 * ListaOprocentowania.get(5));
  227. double opro4 = (double) (opro3 + opro3 * ListaOprocentowania.get(6));
  228. double opro5 = (double) (opro4 + opro4 * ListaOprocentowania.get(7));
  229. //double opro6 = (double) (opro5 + opro5 * ListaOprocentowania.get(8));
  230. k = (double) (opro5 + opro5 * ListaOprocentowania.get(8));
  231.  
  232. dbZmienna=Math.round(k*100)/100;
  233.  
  234. n = Double.toString(dbZmienna);
  235. stanKonta3.append(n);
  236.  
  237. double zmienna2 = (double) dbZmienna-p;
  238. //dbZmienna = (double)k-p;
  239. odset =Double.toString(zmienna2);
  240. odsetki3.setText(odset);
  241.  
  242. }else if (e.getSource() == oblicz4) {
  243. stanKonta4.setText("");
  244. kwotaPoczatkowa = JTFwprowadzKwote1.getText();
  245. p = Integer.parseInt(kwotaPoczatkowa);
  246. if (p <= ListaOprocentowania.get(10)) {
  247. double oproc1 = (double) (p + p * ListaOprocentowania.get(11));
  248. k = (double) (oproc1 + oproc1 * ListaOprocentowania.get(11));
  249. } else {
  250. double oproc2 = (double) (p + p * ListaOprocentowania.get(12));
  251. k = (double) (oproc2 + oproc2 * ListaOprocentowania.get(12));
  252. }
  253. dbZmienna = Math.round(k*100)/100;
  254.  
  255. n = Double.toString(dbZmienna);
  256. stanKonta4.append(n);
  257.  
  258. double kw = (double) dbZmienna-p;
  259. odset = Double.toString(kw);
  260. odsetki4.setText(odset);
  261.  
  262. }else if (e.getSource() == oblicz5) {
  263. stanKonta5.setText("");
  264. kwotaPoczatkowa = JTFwprowadzKwote1.getText();
  265. p = Integer.parseInt(kwotaPoczatkowa);
  266. double kwotaK = p;
  267. for (int a = 1; a <= ListaOprocentowania.get(0)*12; a++) { //ListaOprocentowania.get(0)*12 zamiana lat na miesiące
  268. kwotaK = (double) (kwotaK + kwotaK * ListaOprocentowania.get(13));
  269. }
  270. k=kwotaK;
  271. dbZmienna = Math.round(k*100)/100;
  272. n = Double.toString(dbZmienna);
  273. stanKonta5.append(n);
  274. ods = (double) (dbZmienna-p);
  275. odset =Double.toString(ods);
  276. odsetki5.setText(odset);
  277.  
  278.  
  279. }
  280. if (e.getSource() == wybieram1) {
  281. FileWriter plik = null;
  282. try {
  283. plik = new FileWriter(
  284. "bin/NajlepszaLokata.txt");
  285. BufferedWriter bwrt = new BufferedWriter(plik);
  286. bwrt.write("Wpłacona kwota: " + p);
  287. bwrt.newLine();
  288. bwrt.newLine();
  289. bwrt.write("Wybrana lokata: Lokata numer 1");
  290. bwrt.close();
  291.  
  292. } catch (IOException e1) {
  293.  
  294. e1.printStackTrace();
  295. } finally {
  296. if (plik != null) {
  297. try {
  298. plik.close();
  299.  
  300. } catch (IOException e1) {
  301.  
  302. e1.printStackTrace();
  303. }
  304. }
  305. }
  306. coDoPlikuZapisane.setText("Wybrana lokata została zapisana do pliku.");
  307. } else if (e.getSource() == wybieram2) {
  308. FileWriter plik = null;
  309. try {
  310. plik = new FileWriter( "bin/NajlepszaLokata2.txt" );
  311. BufferedWriter bwrt = new BufferedWriter(plik);
  312. bwrt.write("Wpłacona kwota: " + p);
  313. bwrt.newLine();
  314. bwrt.newLine();
  315. bwrt.write("Wybrana lokata: Lokata numer 2");
  316. bwrt.close();
  317.  
  318. } catch (IOException e1) {
  319.  
  320. e1.printStackTrace();
  321. } finally {
  322. if (plik != null) {
  323. try {
  324. plik.close();
  325.  
  326. } catch (IOException e1) {
  327.  
  328. e1.printStackTrace();
  329. }
  330. }
  331. }
  332. coDoPlikuZapisane.setText("Wybrana lokata została zapisana do pliku.");
  333. }else if (e.getSource() == wybieram3) {
  334. FileWriter plik = null;
  335. try {
  336. plik = new FileWriter(
  337. "bin/NajlepszaLokata3.txt");
  338. BufferedWriter bwrt = new BufferedWriter(plik);
  339. bwrt.write("Wpłacona kwota: " + p);
  340. bwrt.newLine();
  341. bwrt.newLine();
  342. bwrt.write("Wybrana lokata: Lokata numer 3");
  343. bwrt.close();
  344.  
  345. } catch (IOException e1) {
  346.  
  347. e1.printStackTrace();
  348. } finally {
  349. if (plik != null) {
  350. try {
  351. plik.close();
  352.  
  353. } catch (IOException e1) {
  354.  
  355. e1.printStackTrace();
  356. }
  357. }
  358. }
  359. coDoPlikuZapisane.setText("Wybrana lokata została zapisana do pliku.");
  360. }else if (e.getSource() == wybieram4) {
  361. FileWriter plik = null;
  362. try {
  363. plik = new FileWriter(
  364. "bin/NajlepszaLokata4.txt" );
  365. BufferedWriter bwrt = new BufferedWriter(plik);
  366. bwrt.write("Wpłacona kwota: " + p+", odestki w tej lokacie wynoszą: "+ odset);
  367. bwrt.newLine();
  368. bwrt.write("Saldo konta na koniec lokaty: " + p+odset);
  369. bwrt.newLine();
  370. bwrt.write("Wybrana lokata: Lokata numer 4");
  371. bwrt.close();
  372.  
  373. } catch (IOException e1) {
  374.  
  375. e1.printStackTrace();
  376. } finally {
  377. if (plik != null) {
  378. try {
  379. plik.close();
  380.  
  381. } catch (IOException e1) {
  382.  
  383. e1.printStackTrace();
  384. }
  385. }
  386. }
  387. coDoPlikuZapisane.setText("Wybrana lokata została zapisana do pliku.");
  388. }else if (e.getSource() == wybieram5) {
  389. FileWriter plik = null;
  390. try {
  391. plik = new FileWriter(
  392. "bin/NajlepszaLokata5.txt");
  393. BufferedWriter bwrt = new BufferedWriter(plik);
  394. bwrt.write("Wpłacona kwota: " + p);
  395. bwrt.newLine();
  396. bwrt.newLine();
  397. bwrt.write("Wybrana lokata: Lokata numer 5 ");
  398. bwrt.close();
  399.  
  400. } catch (IOException e1) {
  401.  
  402. e1.printStackTrace();
  403. } finally {
  404. if (plik != null) {
  405. try {
  406. plik.close();
  407.  
  408. } catch (IOException e1) {
  409.  
  410. e1.printStackTrace();
  411. }
  412. }
  413. }
  414. coDoPlikuZapisane.setText("Wybrana lokata została zapisana do pliku.");
  415. }
  416.  
  417.  
  418.  
  419. }
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement