Java and integrated GUI revision.
The class hierarchy I have sent you is in 4 files – These represent a program named CreditCard.
Methods have to be added to Class CreditCardFrame for all requirements to work.
The 4 classes are
Class Person – used to model a person
Class CreditCard – used to model a CreditCard
Class CreditCardTester – contains the main method for the prog to run.
Class CreditCardFrame – used to model a JFrame; The CreditCard selected from the JoptionPane input dialog box is passed into the constructor and is used to set up the curentCreditCard instance variable; currentCreditCard is displayed in the JFrame using a JLabel.
The required code is needed to be added to this (Class CreditCardFrame) frame to allow the user to work with currentCreditCard.
The options that have to be added, so the user can carry out on currentCreditCard form the frame are:-
makePurchase () - the user should be able to enter the purchase amount for currentCreditCard from the JFrame. This value must be passed into the makePurchase () method to increase the balance of currentCreditCard.
makePayment () – the user should be able to enter the payment amount for currentCreditCard in the JFrame. This value should be passed into the makePayment() method to decrease the balance of currentCreditCard.
changeCreditLimit() – the user should be able to enter a new credit limit in the JFrame. This value should be passed into the changeCreditLimit() method to change the creditLimit of the currentCreditCard. Include a security check on this operation so it can be only carried out if the correct password is entered.
The above are all methods in the attached class CreditCard file – The user must invoke these methods for currentCreditCard from the JFrame to get this to work.
// Class to model a Person
public class Person{
protected String name;
// Constructor 1
public Person(){
this.name = new String();
}
// Constructor 2
public Person(String name){
this.name = name;
}
// getName() method
public String getName(){
return name;
}
// toString() method
public String toString(){
return name;
}
// equals() method
public boolean equals(Person personIn){
if(name.equals(personIn.name))
return true;
else
return false;
}
}
// Class to model CreditCard objects
public class CreditCard{
// Instance Variables
private int accountNumber;
private Person cardHolder; // CreditCard HAS-A Person ==> Composition
private double balance;
private double creditLimit;
private static int nextUniqueNumber=1; // Next available unique CreditCard number
// static - means nextUniqueNumber is SHARED
// amongst all CreditCard objects, so if one
// of them change it, it is changed for all.
private final int DEFAULT_CREDIT_LIMIT = 1500;
private final int MAX_CREDIT_LIMIT = 3000;
// Constructor 1
public CreditCard(){
// Set accountNumber to nextUniqueNumber, then increment it for next CreditCard
this.accountNumber=nextUniqueNumber++;
this.cardHolder = new Person();
this.balance = 0.0;
this.creditLimit = DEFAULT_CREDIT_LIMIT;
}
// Constructor 2
public CreditCard(String name, double balance, double creditLimit){
// Set accountNumber to nextUniqueNumber, then increment it for next CreditCard
this.accountNumber=nextUniqueNumber++;
this.cardHolder = new Person(name);
this.balance = balance;
if(creditLimit <= MAX_CREDIT_LIMIT)
this.creditLimit=creditLimit;
else
creditLimit = MAX_CREDIT_LIMIT;
}
// getCardHolder() method
public Person getCardHolder(){
return cardHolder;
}
// getBalance() method
public double getBalance(){
return balance;
}
// getCreditLimit() method
public double getCreditLimit(){
return creditLimit;
}
// toString() method
public String toString(){
return "CARD NUMBER:" + accountNumber + " - " + cardHolder.toString() + ", " + balance + " , CREDIT LIMIT:" + creditLimit;
}
// equals() method
public boolean equals(CreditCard creditCardIn){
if(accountNumber == creditCardIn.accountNumber &&
cardHolder.equals(creditCardIn.cardHolder))
return true;
else
return false;
}
// makePurchase() method
public String makePurchase(double purchaseAmount){
if(balance + purchaseAmount < creditLimit){
balance += purchaseAmount;
// return message to indicate balance changed
return (purchaseAmount + " added to balance.");
}
else
// return message to indicate purchaseAmount exceeds credit limit
return (purchaseAmount + " exceeds credit limit.");
}
// makePayment() method
public void makePayment(double paymentAmount){
balance -= paymentAmount;
}
// changeCreditLimit() method
public String changeCreditLimit(double newCreditLimit){
if(newCreditLimit <= MAX_CREDIT_LIMIT){
creditLimit = newCreditLimit;
// return message to indicate creditLimit changed
return (" Credit limit changed to " + creditLimit);
}
else
// return message to indicate creditLimit exceeds allowable credit limit
return (creditLimit + " exceeds allowable credit limit.");
}
}
import javax.swing.*;
import java.awt.*;
// CreditCardFrame IS-A JFrame ==> Inheritance
public class CreditCardFrame extends JFrame{
// Instance Variable to keep track of currentCreditCard
private CreditCard currentCreditCard;
// Declare GUI Components here
private JLabel jlblOne, jlblTwo, jlblThree;
// Constructor - SetLayout & Add Components here...
public CreditCardFrame(CreditCard creditCardIn){
// Set up currentCreditCard
currentCreditCard=creditCardIn;
// Construct Components and
jlblOne = new JLabel(currentCreditCard.toString(), JLabel.CENTER);
jlblTwo = new JLabel("You must create a GUI to work with this CREDIT CARD here", JLabel.CENTER);
jlblThree = new JLabel("You need to add options to makePurchase(), makePayment() and changeCreditLimit() for " + currentCreditCard.getCardHolder().getName(), JLabel.CENTER);
// ...add them to the JFrame
add(jlblOne,BorderLayout.NORTH);
add(jlblTwo,BorderLayout.CENTER);
add(jlblThree,BorderLayout.SOUTH);
}
}
// Class: Object Oriented GUI Programming
// Description: A CreditCardTester class, to create and run CreditCard class
// Date: 14/03/2013
// Author: Maria Boyle
import java.util.*;
import javax.swing.*;
public class CreditCardTester{
public static void main(String args[]){
// Declare an Array of 5 CreditCards called creditCards
final int NUMBER_OF_CREDIT_CARD_ACCOUNTS = 5;
CreditCard[] creditCards = new CreditCard[NUMBER_OF_CREDIT_CARD_ACCOUNTS];
// Create 5 new CreditCard objects with initial values...
CreditCard cc1 = new CreditCard("Patrick Doyle", 100.00, 1000.00);
CreditCard cc2 = new CreditCard("Dylan Sweeney", 350.00, 500.00);
CreditCard cc3 = new CreditCard("Louise Coyle", 0.00, 1500.00);
CreditCard cc4 = new CreditCard("Mark White", 980.00, 1000.000);
CreditCard cc5 = new CreditCard("Emma Logan", 440.00, 500.00);
//...and add them to the array called creditCards
creditCards[0]=cc1;
creditCards[1]=cc2;
creditCards[2]=cc3;
creditCards[3]=cc4;
creditCards[4]=cc5;
// showInputDialog() passing in the creditCards array
// returnedValue will get the selected CreditCard
CreditCard returnedValue = (CreditCard)JOptionPane.showInputDialog(null, "Choose a CreditCard", "CREDIT CARDS",
JOptionPane.INFORMATION_MESSAGE, null, creditCards, creditCards[0]);
// CONSTRUCT a CreditCardFrame object called frame, passing the selected CreditCard into the constrctor
CreditCardFrame frame = new CreditCardFrame(returnedValue);
// Set up the frame object
frame.setTitle("CREDIT CARD OPTIONS");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Need Solution Email me: topsolutions8@gmail.com