Guest User

Untitled

a guest
Feb 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Font;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.ItemEvent;
  6. import java.awt.event.ItemListener;
  7. import java.text.SimpleDateFormat;
  8. import java.time.DateTimeException;
  9. import java.time.LocalDateTime;
  10. import java.time.Month;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13.  
  14. import javax.swing.DefaultComboBoxModel;
  15. import javax.swing.JCheckBox;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JDialog;
  18. import javax.swing.JPanel;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTextArea;
  21. import javax.swing.border.EmptyBorder;
  22.  
  23. public class TransactionsDialog extends JDialog
  24. {
  25. private static final long serialVersionUID = 6939141004692809959L;
  26. private final JPanel contentPanel = new JPanel();
  27. private SimpleDateFormat monthDate = new SimpleDateFormat("MMMM");
  28. private JTextArea txtTransactions;
  29. private JComboBox<String> comboStartMonth;
  30. private JComboBox<Integer> comboStartDay;
  31. private JComboBox<Integer> comboStartYear;
  32. private JComboBox<String> comboEndMonth;
  33. private JComboBox<Integer> comboEndDay;
  34. private JComboBox<Integer> comboEndYear;
  35. private JCheckBox chckbxStartDate;
  36. private JCheckBox chckbxEndDate;
  37.  
  38. /**
  39. * Create the dialog.
  40. */
  41. public TransactionsDialog(BankAccount account)
  42. {
  43. setResizable(false);
  44. /**
  45. * Populating (Day, Month, Year) Arrays.
  46. */
  47. ArrayList<String> monthList = new ArrayList<String>();
  48. for(int month = 0; month < 12; month++)
  49. {
  50. Calendar calendar = Calendar.getInstance();
  51. calendar.set(Calendar.MONTH, month);
  52. String monthName = monthDate.format(calendar.getTime());
  53.  
  54. monthList.add(monthName);
  55. }
  56.  
  57. ArrayList<Integer> dayList = new ArrayList<Integer>();
  58. for(int day = 1; day <= 31; day++)
  59. {
  60. dayList.add(day);
  61. }
  62.  
  63. ArrayList<Integer> yearList = new ArrayList<Integer>();
  64. for(int year = 1980; year <= Calendar.getInstance().get(Calendar.YEAR); year++)
  65. {
  66. yearList.add(year);
  67. }
  68.  
  69. setTitle("Transactions of " + account);
  70. setBounds(100, 100, 404, 300);
  71. getContentPane().setLayout(new BorderLayout());
  72. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  73. getContentPane().add(contentPanel, BorderLayout.CENTER);
  74. contentPanel.setLayout(null);
  75.  
  76. comboStartMonth = new JComboBox<String>();
  77. comboStartMonth.addItemListener(new ItemListener() {
  78. public void itemStateChanged(ItemEvent e) {
  79. listTransactions(account);
  80. }
  81. });
  82. comboStartMonth.setModel(new DefaultComboBoxModel<String>(monthList.toArray(new String[monthList.size()])));
  83. comboStartMonth.setEnabled(false);
  84. comboStartMonth.setBounds(100, 8, 118, 20);
  85. contentPanel.add(comboStartMonth);
  86.  
  87. comboStartDay = new JComboBox<Integer>();
  88. comboStartDay.setModel(new DefaultComboBoxModel<Integer>(dayList.toArray(new Integer[dayList.size()])));
  89. comboStartDay.setEnabled(false);
  90. comboStartDay.setBounds(228, 8, 71, 20);
  91. comboStartDay.addItemListener(new ItemListener() {
  92. public void itemStateChanged(ItemEvent e) {
  93. listTransactions(account);
  94. }
  95. });
  96. contentPanel.add(comboStartDay);
  97.  
  98. comboStartYear = new JComboBox<Integer>();
  99. comboStartYear.setModel(new DefaultComboBoxModel<Integer>(yearList.toArray(new Integer[yearList.size()])));
  100. comboStartYear.setSelectedIndex(comboStartYear.getItemCount() - 1);
  101. comboStartYear.setEnabled(false);
  102. comboStartYear.setBounds(309, 8, 71, 20);
  103. comboStartYear.addItemListener(new ItemListener() {
  104. public void itemStateChanged(ItemEvent e) {
  105. listTransactions(account);
  106. }
  107. });
  108. contentPanel.add(comboStartYear);
  109.  
  110. comboEndMonth = new JComboBox<String>();
  111. comboEndMonth.setModel(new DefaultComboBoxModel<String>(monthList.toArray(new String[monthList.size()])));
  112. comboEndMonth.setEnabled(false);
  113. comboEndMonth.setBounds(100, 34, 119, 20);
  114. comboEndMonth.addItemListener(new ItemListener() {
  115. public void itemStateChanged(ItemEvent e) {
  116. listTransactions(account);
  117. }
  118. });
  119. contentPanel.add(comboEndMonth);
  120.  
  121. comboEndDay = new JComboBox<Integer>();
  122. comboEndDay.setModel(new DefaultComboBoxModel<Integer>(dayList.toArray(new Integer[dayList.size()])));
  123. comboEndDay.setEnabled(false);
  124. comboEndDay.setBounds(228, 34, 71, 20);
  125. comboEndDay.addItemListener(new ItemListener() {
  126. public void itemStateChanged(ItemEvent e) {
  127. listTransactions(account);
  128. }
  129. });
  130. contentPanel.add(comboEndDay);
  131.  
  132. comboEndYear = new JComboBox<Integer>();
  133. comboEndYear.setModel(new DefaultComboBoxModel<Integer>(yearList.toArray(new Integer[yearList.size()])));
  134. comboEndYear.setSelectedIndex(comboEndYear.getItemCount() - 1);
  135. comboEndYear.setEnabled(false);
  136. comboEndYear.setBounds(309, 34, 71, 20);
  137. comboEndYear.addItemListener(new ItemListener() {
  138. public void itemStateChanged(ItemEvent e) {
  139. listTransactions(account);
  140. }
  141. });
  142. contentPanel.add(comboEndYear);
  143.  
  144. txtTransactions = new JTextArea();
  145. txtTransactions.setFont(new Font("Courier New", Font.PLAIN, 11));
  146. txtTransactions.setEditable(false);
  147. txtTransactions.setBounds(10, 63, 368, 187);
  148.  
  149. JScrollPane txtTScrollPane = new JScrollPane(txtTransactions);
  150. contentPanel.add(txtTScrollPane);
  151. contentPanel.revalidate();
  152. contentPanel.repaint();
  153.  
  154. chckbxStartDate = new JCheckBox("Start Date:");
  155. chckbxStartDate.setBounds(6, 7, 89, 23);
  156. chckbxStartDate.addActionListener(new ActionListener() {
  157. public void actionPerformed(ActionEvent arg0) {
  158. if(chckbxStartDate.isSelected())
  159. {
  160. comboStartMonth.setEnabled(true);
  161. comboStartDay.setEnabled(true);
  162. comboStartYear.setEnabled(true);
  163. }
  164. else
  165. {
  166. comboStartMonth.setEnabled(false);
  167. comboStartDay.setEnabled(false);
  168. comboStartYear.setEnabled(false);
  169. }
  170.  
  171. listTransactions(account);
  172. }
  173. });
  174. contentPanel.add(chckbxStartDate);
  175.  
  176. chckbxEndDate = new JCheckBox("End Date:");
  177. chckbxEndDate.setBounds(6, 33, 89, 23);
  178. chckbxEndDate.addActionListener(new ActionListener() {
  179. public void actionPerformed(ActionEvent e) {
  180. if(chckbxEndDate.isSelected())
  181. {
  182. comboEndMonth.setEnabled(true);
  183. comboEndDay.setEnabled(true);
  184. comboEndYear.setEnabled(true);
  185. }
  186. else
  187. {
  188. comboEndMonth.setEnabled(false);
  189. comboEndDay.setEnabled(false);
  190. comboEndYear.setEnabled(false);
  191. }
  192.  
  193. listTransactions(account);
  194. }
  195. });
  196. contentPanel.add(chckbxEndDate);
  197.  
  198. listTransactions(account);
  199. }
  200.  
  201. private void listTransactions(BankAccount account)
  202. {
  203. LocalDateTime startTime;
  204. LocalDateTime endTime;
  205. String startMonthName = (String) comboStartMonth.getSelectedItem();
  206. int startDay = (int) comboStartDay.getSelectedItem();
  207. int startYear = (int) comboStartYear.getSelectedItem();
  208. String endMonthName = (String) comboEndMonth.getSelectedItem();
  209. int endDay = (int) comboEndDay.getSelectedItem();
  210. int endYear = (int) comboEndYear.getSelectedItem();
  211.  
  212. if(chckbxStartDate.isSelected())
  213. {
  214. int startMonth = Month.valueOf(startMonthName.toUpperCase()).getValue();
  215.  
  216. try
  217. {
  218. startTime = LocalDateTime.of(startYear, startMonth, startDay, 0, 0);
  219. }
  220. catch(DateTimeException e)
  221. {
  222. txtTransactions.setText("INVALID DATE");
  223. return;
  224. }
  225. }
  226. else
  227. {
  228. startTime = null;
  229. }
  230.  
  231. if(chckbxEndDate.isSelected())
  232. {
  233. int endMonth = Month.valueOf(endMonthName.toUpperCase()).getValue();
  234.  
  235. try
  236. {
  237. endTime = LocalDateTime.of(endYear, endMonth, endDay, 23, 59);
  238. }
  239. catch(DateTimeException e)
  240. {
  241. txtTransactions.setText("INVALID DATE");
  242. return;
  243. }
  244. }
  245. else
  246. {
  247. endTime = null;
  248. }
  249.  
  250. ArrayList<Transaction> transactionList = account.getTransactions(startTime, endTime);
  251. String output = "";
  252. int maxAmountDigits = 1;
  253.  
  254. for(Transaction t : transactionList)
  255. {
  256. String stringAmount = String.format("%.2f", t.getAmount());
  257.  
  258. if(stringAmount.length() > maxAmountDigits)
  259. maxAmountDigits = stringAmount.length();
  260. }
  261.  
  262. output += String.format("%-10s %-8s %-" + (maxAmountDigits + 1) + "s %sn", "Date", "Time", "Amount", "Description");
  263. //https://stackoverflow.com/questions/37791455/java-string-format-adding-spaces-to-integers
  264. output += String.format("%0" + 100 + "dn", 0).replace("0", "-");
  265.  
  266. for(Transaction t : transactionList)
  267. {
  268. output += String.format("%d.%02d.%02d %02d:%02d:%02d $%-" + maxAmountDigits + ".2f %sn", t.getTransactionTime().getYear(),
  269. t.getTransactionTime().getMonthValue(),
  270. t.getTransactionTime().getDayOfMonth(),
  271. t.getTransactionTime().getHour(),
  272. t.getTransactionTime().getMinute(),
  273. t.getTransactionTime().getSecond(),
  274. t.getAmount(),
  275. t.getDescription());
  276. }
  277.  
  278. txtTransactions.setText(output);
  279. }
  280. }
Add Comment
Please, Sign In to add comment