SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Object-oriented Design
Exercise:
Exercises
Question:15 | ISBN:9781118771334 | Edition: 6

Question

If the parameter to the makePayment method of the CreditCard class (see Code Fragment 1.5) were a negative number, that would have the effect of raising the balance on the account. Revise the implementation so that it throws an IllegalArgumentException if a negative amount is sent as a parameter.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package java_problems_datastructures;


public class CreditCard {
	// Instance variables:
	private String customer; // name of the customer (e.g., ”John Bowman”)
	private String bank; // name of the bank (e.g., ”California Savings”)
	private String account; // account identifier (e.g., ”5391 0375 9387 5309”)
	private int limit; // credit limit (measured in dollars)
	protected double balance; // current balance (measured in dollars)
// Constructors:

	public CreditCard(String cust, String bk, String acnt, int lim, double initialBal) {
		customer = cust;
		bank = bk;
		account = acnt;
		limit = lim;
		balance = initialBal;
	}

	public CreditCard(String cust, String bk, String acnt, int lim) {
		this(cust, bk, acnt, lim, 0.0); // use a balance of zero as default
	}

	// Accessor methods:
	public String getCustomer() {
		return customer;
	}

	public String getBank() {
		return bank;
	}

	public String getAccount() {
		return account;
	}

	public int getLimit() {
		return limit;
	}

	public double getBalance() {
		return balance;
	}

	// Update methods:
	public boolean charge(double price) { // make a charge
		if (price + balance > limit) // if charge would surpass limit
			return false; // refuse the charge
		// at this point, the charge is successful
		balance += price; // update the balance
		return true; // announce the good news
	}

	// Modified method to reject negative payments
	// below method won't accept the negative payments
	// and throws the IllegalArgumentException
	public void makePayment(double amount) { // make a payment

		if (amount < 0) {
			throw new IllegalArgumentException("Can not pay negative amount :( ");
		} else
			balance -= amount;

	}

	// Utility method to print a card's information
	public static void printSummary(CreditCard card) {
		System.out.println("Customer = " + card.customer);
		System.out.println("Bank = " + card.bank);
		System.out.println("Account = " + card.account);
		System.out.println("Balance = " + card.balance); // implicit cast
		System.out.println("Limit = " + card.limit); // implicit cast
	}

	public static void main(String[] args) {
		CreditCard[] wallet = new CreditCard[3];
		wallet[0] = new CreditCard("John Bowman", "California Savings", "5391 0375 9387 5309", 5000);
		wallet[1] = new CreditCard("John Bowman", "California Federal", "3485 0399 3395 1954", 3500);
		wallet[2] = new CreditCard("John Bowman", "California Finance", "5391 0375 9387 5309", 2500, 300);

		for (int val = 1; val <= 16; val++) {
			wallet[0].charge(3 * val);
			wallet[1].charge(2 * val);
			wallet[2].charge(val);
		}

		for (CreditCard card : wallet) {
			CreditCard.printSummary(card); // calling static method
			if (card.getBalance() > 200.0) {
				// below line cause to throw an IllegalArgumentException as we passing negative amount
				card.makePayment(-200);
				System.out.println("New balance = " + card.getBalance());
			}
		}
	}
}

Output:

Customer = John Bowman
Bank = California Savings
Account = 5391 0375 9387 5309
Balance = 408.0
Limit = 5000
Exception in thread "main" java.lang.IllegalArgumentException: Can not pay negative amount :( 
    at java_problems_datastructures.CreditCard.makePayment(CreditCard.java:61)
    at java_problems_datastructures.CreditCard.main(CreditCard.java:92)


Modified the makePayment() method so it will throw IllegalArgumentException if we attemp to pass negative amount

0 0

Discussions

Post the discussion to improve the above solution.