calvin van
Posts: 3
Nickname: calvinvan
Registered: Mar, 2004
|
|
Need help for modify CreditCard program.
|
Posted: Jun 9, 2004 12:14 PM
|
|
Advertisement
|
THIS IS A CreditCard.java
// Excerpted from "Data structures and algorithms in Java, // by Michael T. Goodrich and Roberto Tamassia" // // Modified by Calvin Nguyen
public class CreditCard { // Instance variables: private String number; private String name; private String bank; private double balance; private int limit; // Constructor: CreditCard(String no, String nm, String bk, double bal, int lim) { number = no; name = nm; bank = bk; balance = bal; limit = lim; } // Accessor methods: public String getNumber() { return number; } public String getName() { return name; } public String getBank() { return bank; } public double getBalance() { return balance; } public int getLimit() { return limit; } // Action methods: public boolean chargeIt(double price) { // Make a charge if (price + balance > (double) limit) return false; // There is not enough money left to charge it balance += price; return true; // The charge goes through in this case } public void makePayment(double payment) { // Make a payment balance -= payment; } public static void printCard(CreditCard c) { // Print a card's information System.out.println("Number = " + c.getNumber()); System.out.println("Name = " + c.getName()); System.out.println("Bank = " + c.getBank()); System.out.println("Balance = " + c.getBalance()); // Implicit cast System.out.println("Limit = " + c.getLimit()); // Implicit cast }
public static void main(String[] args) { CreditCard wallet[] = new CreditCard[10]; wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", "California Savings", 0.0, 2500); wallet[1] = new CreditCard("3485 0399 3395 1954", "John Bowman", "California Federal", 0.0, 3500); wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", "California Finance", 0.0, 5000); for (int i=1; i<=16; i++) { wallet[0].chargeIt((double)i); wallet[1].chargeIt(2.0*i); // implicit cast wallet[2].chargeIt((double)3*i); // explicit cast } for (int i=0; i<3; i++) { CreditCard.printCard(wallet); while (wallet.getBalance() > 100.0) { wallet.makePayment(100.0); System.out.println("New balance = " + wallet.getBalance()); } } } }
// END
Above is the CreditCard.java program that had written out, but I can't Modify the CreditCard class to do the following:
1. To check that the price argument passed to method chargeIt and the payment argument passed to method mkaePayment are positive.
2. To check a late fee for any payment that is past its due month. You may want to add an instance variable like dueMonth.
3. To include the modifier methods, which allow a user to modify internal variables in a CreditCard class in a controlled manner.
Here is my main() , that I really need help to modify those statement above, many thanks for helping me.
public static void main(String[] args) { CreditCard wallet[] = new CreditCard[10]; wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", "California Savings", 0.0, 2500, 6); //6 means June card1.chargeIt(100.5); CreditCard.printCard(card1);
card1.chargeIt(-10); CreditCard.printCard(card1);
card1.makePayment(20.0, 6); // pay $20.0 in June CreditCard.printCard(card1);
card1.makePayment(-10, 6); CreditCard.printCard(card1);
card1.makePayment(10.0, 7); // pay $20.0 in July CreditCard.printCard(card1);
card1.setNumber( "5391 0375 9387 5308"); card1.setName( "Jane Bowman"); card1.setBank( "Chicago Savings"); card1.serLimit(900); card1.serDueMonth(7); CrediCard.printCard(card1 ); }
This is the out put that I need to get from the code above:
Number = 5391 0375 9387 5309 Name = John Bowman Bank = California Savings Balance = 100.5 Limit = 2500 dueMonth = 6
The price passed to method chargeIt() is not positive.
Number = 5391 0375 9387 5309 Name = John Bowman Bank = California Savings Balance = 100.5 Limit = 2500 dueMonth = 6
Number = 5391 0375 9387 5309 Name = John Bowman Bank = California Savings Balance = 80.5 Limit = 2500 dueMonth = 6
The payment passed to method chargeIt() is not positive.
Number = 5391 0375 9387 5309 Name = John Bowman Bank = California Savings Balance = 100.5 Limit = 2500 dueMonth = 6
The payment was past the due date. A late fee, $25.0, was charged.
Number = 5391 0375 9387 5309 Name = John Bowman Bank = California Savings Balance = 95.5 Limit = 2500 dueMonth = 6
Number = 5391 0375 9387 5308 Name = John Bowman Bank = Chicago Savings Balance = 1000.0 Limit = 2500 dueMonth = 7
|
|