Guest User

Untitled

a guest
Nov 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. package org.lu.ics.inlämningsuppgifter;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class Controller {
  6. PersonRegister persons;
  7. JFrame frame;
  8.  
  9. public Controller (PersonRegister personReg, JFrame frame) {
  10. this.persons=personReg;
  11. this.frame=frame;
  12. }
  13.  
  14. public void addPerson (String pNbr, String pName) {
  15. Person tmpPerson = new Person(pNbr, pName);
  16. persons.addPerson(tmpPerson);
  17. }
  18.  
  19. public void addPersonAccount (String pNbr, String pName, String accNbr) {
  20. Person tmpPerson = new Person(pNbr, pName);
  21. Account account = new Account(accNbr);
  22. tmpPerson.setAccounts(account);
  23.  
  24. }
  25. public String[] findPerson(String pNbrFind) {
  26. Person p;
  27. String[] aPerson = null;
  28. p = persons.findPerson(pNbrFind);
  29. if (p != null && p.getAccounts() != null) {
  30. aPerson = new String[4];
  31. aPerson[0] = p.getPNbr();
  32. aPerson[1] = p.getName();
  33. aPerson[2] = p.getAccounts();
  34. } else if (p != null) {
  35. aPerson = new String[2];
  36. aPerson[0] = p.getPNbr();
  37. aPerson[1] = p.getName();
  38. }
  39. return aPerson;
  40. }
  41.  
  42.  
  43. }
  44.  
  45. package org.lu.ics.inlämningsuppgifter;
  46.  
  47. public class Account {
  48.  
  49. private String nbr;
  50. private double balance;
  51. private Person owner;
  52.  
  53. public Person getOwner() {
  54. return owner;
  55. }
  56.  
  57. public void setOwner(Person newOwner) {
  58. owner = newOwner;
  59. }
  60.  
  61. public void setNbr(String newNbr) {
  62. nbr = newNbr;
  63. }
  64.  
  65. public String getNbr() {
  66. return nbr;
  67. }
  68.  
  69. public Account (String accNbr) {
  70. balance = 0.00;
  71. this.setNbr(accNbr);
  72.  
  73. }
  74.  
  75. public void setBalance(double newBalance) {
  76. balance = newBalance;
  77. this.setBalance(newBalance);
  78.  
  79. }
  80.  
  81. public double getBalance() {
  82. return balance;
  83. }
  84.  
  85. public void credit(double amount) {
  86. balance = balance + amount;
  87. }
  88.  
  89. public void withdraw(double amount) {
  90. balance = balance - amount;
  91. }
  92.  
  93.  
  94. }
  95.  
  96. import java.util.*;
  97.  
  98. public class Person {
  99.  
  100. private String pNbr;
  101. private String name;
  102. private ArrayList<Account> accounts = new ArrayList<Account>();
  103.  
  104. public Person(String pNbr, String pName) {
  105. this.setPNbr(pNbr);
  106. this.setName(pName);
  107. }
  108.  
  109. public ArrayList<Account> getAccount() {
  110. return accounts;
  111. }
  112.  
  113. public void setPNbr(String newPNbr) {
  114. pNbr = newPNbr;
  115. }
  116.  
  117. public String getPNbr() {
  118. return pNbr;
  119. }
  120.  
  121. public void setName(String newName) {
  122. name = newName;
  123. }
  124.  
  125. public String getName() {
  126. return name;
  127. }
  128.  
  129. public void setAccounts(ArrayList<Account> newAccounts) {
  130. accounts = newAccounts;
  131. }
  132.  
  133. public ArrayList<Account> getAccounts() {
  134. return accounts;
  135. }
  136.  
  137. public void addAccount(Account anAccount) {
  138. this.getAccounts().add(anAccount);
  139. }
  140.  
  141.  
  142. }
  143.  
  144. package org.lu.ics.inlämningsuppgifter;
  145.  
  146. import java.awt.EventQueue;
  147.  
  148. import javax.swing.JFrame;
  149. import javax.swing.JButton;
  150. import javax.swing.JTable;
  151. import javax.swing.table.DefaultTableModel;
  152.  
  153. import java.awt.event.ActionListener;
  154. import java.awt.event.ActionEvent;
  155. import javax.swing.JScrollPane;
  156. import javax.swing.JFrame;
  157. import javax.swing.JLabel;
  158. import javax.swing.JPanel;
  159. import javax.swing.JTextField;
  160. public class PersonApplication {
  161.  
  162. private JFrame frame;
  163. private final JTable tblAccounts = new JTable();
  164. private JTextField textField;
  165. private JTextField textField_1;
  166. private JTextField textField_AccNbr;
  167.  
  168. private Controller controller;
  169. private PersonRegister personRegister;
  170.  
  171. /**
  172. * Launch the application.
  173. */
  174. public static void main(String[] args) {
  175. EventQueue.invokeLater(new Runnable() {
  176. public void run() {
  177. try {
  178. PersonApplication window = new PersonApplication();
  179. window.frame.setVisible(true);
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. }
  183. }
  184. });
  185. }
  186.  
  187. /**
  188. * Create the application.
  189. */
  190. public PersonApplication() {
  191. initialize();
  192. }
  193.  
  194. /**
  195. * Initialize the contents of the frame.
  196. */
  197. private void initialize() {
  198. JFrame frame = new JFrame("Accounts");
  199. frame.setBounds(100, 100, 840, 524);
  200. frame.getContentPane();
  201. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  202. frame.getContentPane().setLayout(null);
  203. String col[] = {"Account", "Balance"};
  204.  
  205. DefaultTableModel tableModel = new DefaultTableModel(col, 2);
  206.  
  207. JTable table = new JTable(tableModel);
  208.  
  209. JButton btnAddPerson = new JButton("Add person");
  210. btnAddPerson.addActionListener(new ActionListener() {
  211. public void actionPerformed(ActionEvent arg0) {
  212. }
  213. });
  214. btnAddPerson.setBounds(35, 296, 105, 23);
  215. frame.getContentPane().add(btnAddPerson);
  216.  
  217. JButton btnRemovePerson = new JButton("Remove person");
  218. btnRemovePerson.addActionListener(new ActionListener() {
  219. public void actionPerformed(ActionEvent arg0) {
  220. }
  221. });
  222. btnRemovePerson.setBounds(35, 330, 107, 23);
  223. frame.getContentPane().add(btnRemovePerson);
  224.  
  225. JButton btnFindPerson = new JButton("Find person");
  226. btnFindPerson.addActionListener(new ActionListener() {
  227. public void actionPerformed(ActionEvent arg0) {
  228. String accNumber = textField_AccNbr.getText();
  229. //String[] tmpCustomer = controller.
  230. }
  231. });
  232. btnFindPerson.setBounds(35, 364, 91, 23);
  233. frame.getContentPane().add(btnFindPerson);
  234.  
  235. JButton btnAddAccount = new JButton("Add Account");
  236. btnAddAccount.addActionListener(new ActionListener() {
  237. public void actionPerformed(ActionEvent arg0) {
  238. }
  239. });
  240. btnAddAccount.setBounds(269, 296, 99, 23);
  241. frame.getContentPane().add(btnAddAccount);
  242.  
  243. JButton btnShowAccount = new JButton("Show Accounts");
  244. btnShowAccount.addActionListener(new ActionListener() {
  245. public void actionPerformed(ActionEvent arg0) {
  246. DefaultTableModel model=(DefaultTableModel)
  247. tblAccounts.getModel();
  248.  
  249. }
  250. });
  251. btnShowAccount.setBounds(439, 451, 105, 23);
  252. frame.getContentPane().add(btnShowAccount);
  253. tblAccounts.setBounds(439, 300, 352, 132);
  254. frame.getContentPane().add(tblAccounts);
  255.  
  256. JLabel lblName = new JLabel("Name:");
  257. lblName.setBounds(47, 29, 31, 14);
  258. frame.getContentPane().add(lblName);
  259.  
  260. textField = new JTextField();
  261. textField.setBounds(139, 26, 86, 20);
  262. frame.getContentPane().add(textField);
  263. textField.setColumns(10);
  264.  
  265. JLabel lblPnmbr = new JLabel("PNmbr:");
  266. lblPnmbr.setBounds(47, 57, 35, 14);
  267. frame.getContentPane().add(lblPnmbr);
  268.  
  269. textField_1 = new JTextField();
  270. textField_1.setBounds(139, 51, 86, 20);
  271. frame.getContentPane().add(textField_1);
  272. textField_1.setColumns(10);
  273.  
  274. JLabel lblAccountNumber = new JLabel("Account number:");
  275. lblAccountNumber.setBounds(47, 90, 82, 14);
  276. frame.getContentPane().add(lblAccountNumber);
  277.  
  278. textField_AccNbr = new JTextField();
  279. textField_AccNbr.setBounds(139, 87, 86, 20);
  280. frame.getContentPane().add(textField_AccNbr);
  281. textField_AccNbr.setColumns(10);
  282. }
  283. }
  284.  
  285. package org.lu.ics.inlämningsuppgifter;
  286.  
  287. import java.util.*;
  288.  
  289. public class PersonRegister {
  290.  
  291. private ArrayList<Person> persons = new ArrayList<Person>();
  292.  
  293. public ArrayList<Person> getPersoner() {
  294. return persons;
  295. }
  296.  
  297. public void setPersons(ArrayList<Person> newPersons) {
  298. persons = newPersons;
  299. }
  300.  
  301. public void addPerson(Person newPerson) {
  302. persons.add(newPerson);
  303. }
  304.  
  305. public Person findPerson(String pNbr) {
  306. for (Person p : this.persons) {
  307. if (p.getPNbr().equals(pNbr)) {
  308. return p;
  309. }
  310. }
  311. return null;
  312. }
  313.  
  314. public Person removePerson(String pNbr) {
  315. Person p = findPerson(pNbr);
  316. if (p != null) {
  317. persons.remove(p);
  318. }
  319. return null;
  320. }
  321. }
Add Comment
Please, Sign In to add comment