Kaynağa Gözat

Account event formatting and cancellation

Tankernn 8 yıl önce
ebeveyn
işleme
15eb1ec4fc

+ 15 - 6
src/eu/tankernn/accounts/AccountEvent.java

@@ -2,17 +2,26 @@ package eu.tankernn.accounts;
 
 /**
  * Describes an event in an account's history.
+ * 
  * @author frans
  *
  */
 public class AccountEvent {
-	
+
 	private final double balanceChange;
 	private final String description;
-	
-	public AccountEvent(double balanceChange, String description) {
+
+	/**
+	 * Creates a new account event.
+	 * 
+	 * @param balanceChange The change in account balance.
+	 * @param descriptionFormat
+	 *            A string that will be used for a <code>String.format()</code>
+	 *            call, along with the absolute balance chage value.
+	 */
+	public AccountEvent(double balanceChange, String descriptionFormat) {
 		this.balanceChange = balanceChange;
-		this.description = description;
+		this.description = String.format(descriptionFormat, Math.abs(balanceChange));
 	}
 
 	public double getBalanceChange() {
@@ -22,9 +31,9 @@ public class AccountEvent {
 	public String getDescription() {
 		return description;
 	}
-	
+
 	public String toString() {
 		return description;
 	}
-	
+
 }

+ 17 - 10
src/eu/tankernn/accounts/frame/AccountPanel.java

@@ -119,19 +119,24 @@ public class AccountPanel extends JPanel implements ActionListener {
 		if (src.equals(transferFrom)) {
 			double amount = showAmountDialog("transfer");
 
-			if (amount <= 0 || amount > currentAccount.calculateBalance()) {
+			if (amount <= 0) {
+				return;
+			} else if (amount > currentAccount.calculateBalance()) {
 				JOptionPane.showMessageDialog(null, "Invalid amount.");
 				return;
 			}
 
-			JOptionPane.showConfirmDialog(null,
+			int result = JOptionPane.showConfirmDialog(null,
 					new JComponent[] { new JLabel("Please select receiver account."), otherAccounts },
-					"Select account.", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE);
-
+					"Select account.", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
+			
+			if (result != JOptionPane.OK_OPTION)
+				return;
+			
 			Account sender = currentAccount, receiver = (Account) otherAccounts.getSelectedItem();
 
-			sender.getHistory().add(new AccountEvent(-amount, "Transferred " + amount + " to " + receiver + "."));
-			receiver.getHistory().add(new AccountEvent(amount, "Received " + amount + " from " + sender + "."));
+			sender.getHistory().add(new AccountEvent(-amount, "Transferred %.2f to " + receiver + "."));
+			receiver.getHistory().add(new AccountEvent(amount, "Received %.2f from " + sender + "."));
 		} else if (src.equals(deposit)) {
 			double amount = showAmountDialog("deposit");
 
@@ -140,7 +145,7 @@ public class AccountPanel extends JPanel implements ActionListener {
 				return;
 			}
 
-			currentAccount.getHistory().add(new AccountEvent(amount, "User deposited " + amount + "."));
+			currentAccount.getHistory().add(new AccountEvent(amount, "User deposited %.2f."));
 		} else if (src.equals(withdraw)) {
 			double amount = showAmountDialog("withdraw");
 
@@ -152,21 +157,23 @@ public class AccountPanel extends JPanel implements ActionListener {
 				return;
 			}
 
-			currentAccount.getHistory().add(new AccountEvent(-amount, "User withdrew " + amount + "."));
+			currentAccount.getHistory().add(new AccountEvent(-amount, "User withdrew %.2f."));
 		}
 
 		this.updatePanel(currentAccount);
 	}
 
 	private double showAmountDialog(String action) {
-		String amountStr = JOptionPane.showInputDialog("Amount to " + action + ":");
-
+		String amountStr;
 		double amount = -1;
 		while (amount == -1)
 			try {
+				amountStr = JOptionPane.showInputDialog("Amount to " + action + ":");
 				amount = Double.parseDouble(amountStr);
 			} catch (NumberFormatException ex) {
 				JOptionPane.showMessageDialog(null, "Please enter a valid number value.");
+			} catch (NullPointerException ex) {
+				break;
 			}
 		return amount;
 	}