/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); if (session.getAttribute("user") != null) { try { Group staff = Database.getInstance().getGroup(2); User user = (User) session.getAttribute("user"); if (Database.getInstance().isInGroup(user, staff)) { String strroundid = request.getParameter("id"); int roundid = Integer.parseInt(strroundid); String title = request.getParameter("title"); String description = request.getParameter("description"); String strenddate = request.getParameter("enddate"); String endtime = request.getParameter("endtime"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date endDate = sdf.parse(strenddate); java.sql.Date endSqlDate = new Date(endDate.getTime()); java.sql.Time endSqlTime = java.sql.Time.valueOf(endtime); Round round = new Round(0, title, description, "Closed"); Round dbRound = Database.getInstance().getRound(roundid); Countdown dbCountdown = Database.getInstance().getCountdown(dbRound); Countdown countdown = new Countdown( dbCountdown.getId(), round.getTitle(), "Countdown of " + round.getTitle() + ", which is is about: " + round.getDescription(), endSqlDate, endSqlTime); if (Database.getInstance().updateRound(round, countdown)) response.sendRedirect("round.jsp?id=" + roundid); else response.getOutputStream().print("Error while saving the round!"); } else { response.getOutputStream().print("you are not staff"); } } catch (Exception e) { response.getOutputStream().print("Error while saving the round!"); } } else { response.getOutputStream().print("Not logged in"); } }
public class Query { static Database database = Database.getInstance(); public static LinkedHashMap<String, AccountOwnerData> getAllOwners() { LinkedHashMap<String, AccountOwnerData> list = new LinkedHashMap<String, AccountOwnerData>(); for (Entry<String, Object> entry : database.data.entrySet()) { String key = entry.getKey(); Object obj = entry.getValue(); if (obj instanceof AccountOwnerData) { AccountOwnerData recordOwner = (AccountOwnerData) obj; list.put(key, recordOwner); } } return (list); } public static LinkedHashMap<String, AccountData> getAllAccounts() { LinkedHashMap<String, AccountData> list = new LinkedHashMap<String, AccountData>(); for (Entry<String, Object> entry : database.data.entrySet()) { Object obj = entry.getValue(); String key = entry.getKey(); if (obj instanceof AccountData) { AccountData recordAccount = (AccountData) obj; list.put(key, recordAccount); } } return (list); } public static LinkedHashMap<String, ArrayList<Object>> getAllTransactionsByAccount() { LinkedHashMap<String, ArrayList<Object>> list = new LinkedHashMap<String, ArrayList<Object>>(); for (Entry<String, Object> entry : database.data.entrySet()) { Object obj = entry.getValue(); if ((obj instanceof String) || (obj instanceof AccountOwnerData) || (obj instanceof AccountData)) { continue; } if (obj instanceof DepositData) { DepositData deposit = (DepositData) obj; ArrayList<Object> members = (list.get(deposit.accountId) == null ? new ArrayList<Object>() : list.get(deposit.accountId)); members.add(deposit); list.put(deposit.accountId, members); } if (obj instanceof WithdrawalData) { WithdrawalData withdraw = (WithdrawalData) obj; ArrayList<Object> members = (list.get(withdraw.accountId) == null ? new ArrayList<Object>() : list.get(withdraw.accountId)); members.add(withdraw); list.put(withdraw.accountId, members); } if (obj instanceof TransferData) { TransferData transfer = (TransferData) obj; ArrayList<Object> members = (list.get(transfer.fromAccountId) == null ? new ArrayList<Object>() : list.get(transfer.fromAccountId)); members.add(transfer); list.put(transfer.fromAccountId, members); members = (list.get(transfer.toAccountId) == null ? new ArrayList<Object>() : list.get(transfer.toAccountId)); members.add(transfer); list.put(transfer.toAccountId, members); } } return (list); } public static LinkedHashMap<String, Object> getAllTransactions() { LinkedHashMap<String, Object> list = new LinkedHashMap<String, Object>(); for (Entry<String, Object> entry : database.data.entrySet()) { Object obj = entry.getValue(); String key = entry.getKey(); if ((obj instanceof String) || (obj instanceof AccountOwnerData) || (obj instanceof AccountData)) { continue; } list.put(key, obj); } return (list); } public void getAll() { System.out.println("All Owners:"); for (Entry<String, AccountOwnerData> entry : getAllOwners().entrySet()) { Object obj = entry.getKey(); System.out.println(obj + " = " + entry.getValue()); } System.out.println("All Accounts:"); for (Entry<String, AccountData> entry : getAllAccounts().entrySet()) { Object obj = entry.getKey(); System.out.println(obj + " = " + entry.getValue()); } System.out.println("All Transactions by Account:"); for (Entry<String, ArrayList<Object>> entry : getAllTransactionsByAccount().entrySet()) { Object obj = entry.getKey(); System.out.println(obj + " = " + entry.getValue()); } } }
public class WithdrawalTest { Database dataBase = Database.getInstance(); @Before public void setUp() throws Exception { Database.setFileName("test.dat"); dataBase.eraseFile(); dataBase.load(); AccountOwner newAccountOwner = new AccountOwner("Michael Powell", "P$1111"); newAccountOwner.put(); AccountOwner newAccountOwner2 = new AccountOwner("Ann Singh", "P$2222"); newAccountOwner2.put(); Account newAccount = new Account("O1001", "Checking", "50.00"); newAccount.put(); Account newAccount2 = new Account("O1001", "Savings", "50.00"); newAccount2.put(); Account newAccount3 = new Account("O1002", "Savings", "50.00"); newAccount3.put(); Account newAccount4 = new Account("O1002", "Checking", "250.00"); newAccount4.put(); Assert.assertEquals("A1004", Account.get("A1004").getId()); Assert.assertEquals("O1002", AccountOwner.get("O1002").getId()); } @After public void tearDown() throws Exception {} @Test public void UpdateWithdrawal() { // Testing proper password and withdrawal amount updates account to correct new balance. UAT // 5.1, 5.3 & 5.4 Withdrawal newWithdrawal = new Withdrawal("O1002", "A1004", "50.00"); Assert.assertEquals("Invalid Password", newWithdrawal.updateBalance("P@1234")); Assert.assertEquals("valid", newWithdrawal.updateBalance("P$2222")); Account newAccount4 = Account.get(newWithdrawal.getAccountId()); Assert.assertEquals("200.00", newAccount4.getBalance()); newWithdrawal.put(); newWithdrawal = new Withdrawal("O1002", "A1004", "100.00"); Assert.assertEquals("valid", newWithdrawal.updateBalance("P$2222")); newAccount4 = Account.get(newWithdrawal.getAccountId()); Assert.assertEquals("100.00", newAccount4.getBalance()); newWithdrawal = new Withdrawal("O1002", "A1004", "50.25"); Assert.assertEquals("valid", newWithdrawal.updateBalance("P$2222")); newAccount4 = Account.get(newWithdrawal.getAccountId()); Assert.assertEquals("49.75", newAccount4.getBalance()); } /* @Test public void WithdrawalNotNegative() { //Testing withdrawals do not contain a negative amount. UAT 5.2 Withdrawal withdrawal = new Withdrawal("01002", "A1004", "-100"); //Assert.assertEquals("valid", withdrawal.validateWithdrawalAmount("-100")); Assert.assertEquals("Withdrawal amount cannot be negative", withdrawal.validateWithdrawalAmount(withdrawal.getWithdrawalAmount())); } @Test public void amountNotGreaterThanBalance() { //Testing withdrawal amounts are not greater than balances, so there cannot be negative balances after withdrawing. UAT 5.5 Withdrawal newWithdrawal = new Withdrawal("O1002", "A1004", "350.00"); Assert.assertEquals("Withdrawal amount cannot be greater than balance", newWithdrawal.validateWithdrawalAmount(newWithdrawal.getWithdrawalAmount())); } @Test public void notEmpty() { //Testing withdrawals do not contain empty values. UAT 5.6 Withdrawal newWithdrawal = new Withdrawal("O1001","A1001",""); Assert.assertEquals("Withdrawal amount cannot be empty", newWithdrawal.validateWithdrawalAmount(newWithdrawal.getWithdrawalAmount())); } @Test public void testWithdrawalAmountIsNumeric() { //Testing withdrawals do not contain any char or string data. UAT 5.7 Withdrawal withdrawal1 = new Withdrawal ("O1002","A1004","23w"); Assert.assertEquals("Withdrawal amount must be numeric", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004","23.00"); Assert.assertEquals("valid", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); } @Test public void testwithdrawalAmountMustBeDollarsAndCents () { //Testing withdrawals do not contain more than two decimal places. UAT 5.8 Withdrawal withdrawal1 = new Withdrawal ("O1002","A1004","1.234"); Assert.assertEquals("Amount must be dollars and cents", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004","1.23"); Assert.assertEquals("valid", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); } @Test public void TestWithdrawalAmountCannotBeBlank() { //Testing withdrawals do not contain spaces (blanks). UAT 5.9 Withdrawal withdrawal1 = new Withdrawal ("O1002","A1004"," "); Assert.assertEquals("Withdrawal amount cannot be blank", withdrawal1.validateWithdrawalAmount(" ")); withdrawal1 = new Withdrawal ("O1002","A1004"," "); Assert.assertEquals("Withdrawal amount cannot be blank", withdrawal1.validateWithdrawalAmount(" ")); withdrawal1 = new Withdrawal ("O1002","A1004","50.00"); Assert.assertEquals("valid", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); } @Test public void TestWithdrawalAmountCannotBeZero() { //Testing withdrawals do not contain a zero amount. UAT 5.10 Withdrawal withdrawal1 = new Withdrawal ("O1002","A1004","0.00"); Assert.assertEquals("Withdrawal amount cannot be zero", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004","0.0"); Assert.assertEquals("Withdrawal amount cannot be zero", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004","0"); Assert.assertEquals("Withdrawal amount cannot be zero", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004",".00"); Assert.assertEquals("Withdrawal amount cannot be zero", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); withdrawal1 = new Withdrawal ("O1002","A1004","1.00"); Assert.assertEquals("valid", withdrawal1.validateWithdrawalAmount(withdrawal1.getWithdrawalAmount())); } */ } // End DepositTest
/** * build the {@link Transaction} list and filter also for {@link #acc}, {@link * #paymentReason},{@link #from} and {@link #until} * * @return the filtered {@link Transaction} list */ public List<Transaction> calculateTransactions() { List<Account> accounts = accountController.getAccounts(); if (accounts == null) return null; Account fromAcc = null; for (Account account : accounts) { if (account.toString().equals(acc)) { fromAcc = account; break; } } List<Transaction> result = new ArrayList<Transaction>(); List<Transaction> transactions; try { transactions = Database.getInstance().getTransactions(fromAcc); } catch (Throwable e) { return null; } if (transactions == null) return null; Pattern payment = null; if (paymentReason != null) { payment = Pattern.compile(".*" + Pattern.quote(paymentReason.toLowerCase()) + ".*"); } if (from == null && until == null) result = transactions; if (from != null && until == null) for (Transaction transaction : transactions) if (transaction.getDate().after(from)) result.add(transaction); if (from != null && until != null) for (Transaction transaction : transactions) if (transaction.getDate().after(from) && transaction.getDate().before(until)) result.add(transaction); if (from == null && until != null) for (Transaction transaction : transactions) if (transaction.getDate().before(until)) result.add(transaction); List<Transaction> endResult = new LinkedList<Transaction>(); if (paymentReason == null) { endResult = result; } else { for (Transaction transaction : result) { if (payment.matcher(transaction.getPaymentReason().toLowerCase()).matches()) { endResult.add(transaction); } } } // for (Transaction transaction : endResult) { // if(transaction.getFromAccount().getAccountID() == fromAcc.getAccountID()){ // if(transaction.getAmount().floatValue() > 0) // transaction.setAmount(transaction.getAmount().negate()); // } // } return endResult; }
/** Create the panel. */ public AccountOwnerCreateView() { database.load(); setMinimumSize(new Dimension(600, 400)); addContainerListener( new ContainerAdapter() { @Override public void componentAdded(ContainerEvent arg0) {} }); Database.getInstance().load(); setLayout(null); ownerIdLabel = new JLabel(); ownerIdLabel.setBounds(211, 55, 63, 29); ownerIdLabel.setFont(new Font("Lucida Grande", Font.BOLD, 18)); ownerIdLabel.setText(AccountOwner.getNextId()); add(ownerIdLabel); ownerIdLabel.setName("ownerIdLabel"); JLabel passwordLabel = new JLabel("Password"); passwordLabel.setBounds(16, 127, 94, 24); add(passwordLabel); passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT); passwordLabel.setFont(new Font("Arial", Font.BOLD, 20)); passwordTextField = new JTextField(); passwordTextField.setMinimumSize(new Dimension(600, 400)); passwordTextField.setText(""); passwordTextField.setColumns(5); passwordTextField.setBounds(211, 122, 59, 29); passwordTextField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String password = passwordTextField.getText(); String result = PasswordManager.validate(password); if (!result.equals("valid")) { getErrorMessage().setText(result); getErrorMessage().setVisible(true); } } }); add(passwordTextField); passwordTextField.setName("passwordTextField"); passwordLabel.setLabelFor(passwordTextField); JLabel titleLabel = new JLabel("Create Account Owner"); titleLabel.setBounds(74, 8, 324, 36); add(titleLabel); titleLabel.setFont(new Font("Arial", Font.BOLD, 30)); JButton submitButton = new JButton("Submit"); submitButton.setBounds(16, 175, 88, 29); submitButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { String password = passwordTextField.getText(); String name = accountOwnerNameTextField.getText(); AccountOwner accountOwner = new AccountOwner(name, password); String result = accountOwner.validate(); if (result.equals("valid")) { getErrorMessage().setText(""); getErrorMessage().setVisible(false); accountOwner.put(); ownerIdLabel.setText(AccountOwner.getNextId()); } else { getErrorMessage().setText(result); getErrorMessage().setVisible(true); } } }); add(submitButton); submitButton.setHorizontalAlignment(SwingConstants.LEFT); JButton clearButton = new JButton("Clear"); clearButton.setBounds(390, 175, 76, 29); clearButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { getErrorMessage().setText(""); passwordTextField.setText(""); accountOwnerNameTextField.setText(""); } }); add(clearButton); clearButton.setHorizontalAlignment(SwingConstants.LEFT); clearButton.setAlignmentX(Component.RIGHT_ALIGNMENT); errorMessageLabel = new JLabel(""); errorMessageLabel.setBounds(16, 155, 324, 16); errorMessageLabel.setName("errorMessageLabel"); add(errorMessageLabel); JLabel accountOwnerIdLabel = new JLabel("Account Owner ID"); accountOwnerIdLabel.setBounds(16, 56, 191, 24); accountOwnerIdLabel.setFont(new Font("Arial", Font.BOLD, 20)); add(accountOwnerIdLabel); JLabel accountOwnerNameLabel = new JLabel("Name"); accountOwnerNameLabel.setBounds(16, 94, 191, 24); accountOwnerNameLabel.setFont(new Font("Arial", Font.BOLD, 20)); add(accountOwnerNameLabel); accountOwnerNameTextField = new JTextField(); accountOwnerNameTextField.setText(""); accountOwnerNameTextField.setBounds(211, 89, 255, 29); accountOwnerNameTextField.setName("accountOwnerNameTextField"); accountOwnerNameTextField.setColumns(30); add(accountOwnerNameTextField); }
public class AccountOwnerCreateView extends JPanel { private static final long serialVersionUID = 1L; Database database = Database.getInstance(); public static void main(String[] args) { java.awt.EventQueue.invokeLater( new Runnable() { public void run() { JFrame jFrame = new JFrame(); AccountOwnerCreateView newContentPane = new AccountOwnerCreateView(); newContentPane.setPreferredSize(new Dimension(600, 400)); newContentPane.setOpaque(true); jFrame.setContentPane(newContentPane); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); jFrame.setLocationRelativeTo(null); } }); } private JTextField passwordTextField; private JLabel ownerIdLabel; private JLabel errorMessageLabel; private JTextField accountOwnerNameTextField; /** Create the panel. */ public AccountOwnerCreateView() { database.load(); setMinimumSize(new Dimension(600, 400)); addContainerListener( new ContainerAdapter() { @Override public void componentAdded(ContainerEvent arg0) {} }); Database.getInstance().load(); setLayout(null); ownerIdLabel = new JLabel(); ownerIdLabel.setBounds(211, 55, 63, 29); ownerIdLabel.setFont(new Font("Lucida Grande", Font.BOLD, 18)); ownerIdLabel.setText(AccountOwner.getNextId()); add(ownerIdLabel); ownerIdLabel.setName("ownerIdLabel"); JLabel passwordLabel = new JLabel("Password"); passwordLabel.setBounds(16, 127, 94, 24); add(passwordLabel); passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT); passwordLabel.setFont(new Font("Arial", Font.BOLD, 20)); passwordTextField = new JTextField(); passwordTextField.setMinimumSize(new Dimension(600, 400)); passwordTextField.setText(""); passwordTextField.setColumns(5); passwordTextField.setBounds(211, 122, 59, 29); passwordTextField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String password = passwordTextField.getText(); String result = PasswordManager.validate(password); if (!result.equals("valid")) { getErrorMessage().setText(result); getErrorMessage().setVisible(true); } } }); add(passwordTextField); passwordTextField.setName("passwordTextField"); passwordLabel.setLabelFor(passwordTextField); JLabel titleLabel = new JLabel("Create Account Owner"); titleLabel.setBounds(74, 8, 324, 36); add(titleLabel); titleLabel.setFont(new Font("Arial", Font.BOLD, 30)); JButton submitButton = new JButton("Submit"); submitButton.setBounds(16, 175, 88, 29); submitButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { String password = passwordTextField.getText(); String name = accountOwnerNameTextField.getText(); AccountOwner accountOwner = new AccountOwner(name, password); String result = accountOwner.validate(); if (result.equals("valid")) { getErrorMessage().setText(""); getErrorMessage().setVisible(false); accountOwner.put(); ownerIdLabel.setText(AccountOwner.getNextId()); } else { getErrorMessage().setText(result); getErrorMessage().setVisible(true); } } }); add(submitButton); submitButton.setHorizontalAlignment(SwingConstants.LEFT); JButton clearButton = new JButton("Clear"); clearButton.setBounds(390, 175, 76, 29); clearButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { getErrorMessage().setText(""); passwordTextField.setText(""); accountOwnerNameTextField.setText(""); } }); add(clearButton); clearButton.setHorizontalAlignment(SwingConstants.LEFT); clearButton.setAlignmentX(Component.RIGHT_ALIGNMENT); errorMessageLabel = new JLabel(""); errorMessageLabel.setBounds(16, 155, 324, 16); errorMessageLabel.setName("errorMessageLabel"); add(errorMessageLabel); JLabel accountOwnerIdLabel = new JLabel("Account Owner ID"); accountOwnerIdLabel.setBounds(16, 56, 191, 24); accountOwnerIdLabel.setFont(new Font("Arial", Font.BOLD, 20)); add(accountOwnerIdLabel); JLabel accountOwnerNameLabel = new JLabel("Name"); accountOwnerNameLabel.setBounds(16, 94, 191, 24); accountOwnerNameLabel.setFont(new Font("Arial", Font.BOLD, 20)); add(accountOwnerNameLabel); accountOwnerNameTextField = new JTextField(); accountOwnerNameTextField.setText(""); accountOwnerNameTextField.setBounds(211, 89, 255, 29); accountOwnerNameTextField.setName("accountOwnerNameTextField"); accountOwnerNameTextField.setColumns(30); add(accountOwnerNameTextField); } public JLabel getErrorMessage() { return errorMessageLabel; } public JTextField getPasswordField() { return passwordTextField; } }