Java Assignment No coding is needed. Design a java class to represent a credit c
ID: 3891179 • Letter: J
Question
Java Assignment
No coding is needed. Design a java class to represent a credit card. Try to design the class such that it only fields and methods relevant to a credit card itself without branching into related functionality that that of a bank or a consumer. I need to have a list of the fields and methods. Fields will need to specify the type, accessibility (private, public, protected, default or package), whether it is a static or instance field and any limitations on the values (non-negative, always less than some other value, etc). For methods, I need to supply the return type and parameter type list. A Free-form description of what each field and method is meant to do with explanation of any potentially confusing design decisions.
Here is what I have so far.
Its attributes (member or instance variables) are:
network - An enum (CardNetwork) type consisting of one of the 4 major networks (AMEX, DISCOVER, MASTERCARD or VISA)
number - The credit card number (String)
cardHolder - The name of the credit card holder (owner) (String)
expirationDate - The expiration date of the credit card (String)
securityCode - The security code associated with the card (int)
The Credit class will have methods to:
Create a new CreditCard (given a network, card holder name, number, expiration date and security code) [constructor]
getCardHolder
getExpirationDate
getNetwork
getNumber
setCardHolder
equals [method to check if one CreditCard is the same as another]
toString [method to turn CreditCard into a string for display]
Explanation / Answer
The design of a class can be best understood by designing the class itself. The constructors, modifiers and instance variables can be best understood by observing the class design. Refer the code below. Each method has been commented for better understanding.
/**
* Class to represent a CreditCard with all the required instance variables
* methods, and required constructor(s)
* @author YourName
*
*/
public class CreditCard {
// defining enum to represent card network
public enum CardNetwork{
AMEX,
DISCOVER,
MASTERCARD,
VISA
}
// private instance variables
private CardNetwork network;
private String number;
private String cardHolder;
private String expirationDate;
private int securityCode;
// Default constructor to initialize the instance variables
public CreditCard(CardNetwork network, String number,
String cardHolder, String expirationDate, int securityCode) {
this.network = network;
this.number = number;
this.cardHolder = cardHolder;
this.expirationDate = expirationDate;
this.securityCode = securityCode;
}
// public method to return the card network
// @return an enum CardNetwork
public CardNetwork getNetwork() {
return network;
}
// public method to return the card number
// @return String
public String getNumber() {
return number;
}
// public method to return the card holder name
// @return String
public String getCardHolder() {
return cardHolder;
}
// public method to return the card expiry date
// @return String
public String getExpirationDate() {
return expirationDate;
}
// public method to return the card secure code
// @return integer
public int getSecurityCode() {
return securityCode;
}
// public overridden method to check equality of 2 cards
@Override
public boolean equals(Object obj) {
CreditCard other = (CreditCard) obj;
if (cardHolder == null) {
if (other.cardHolder != null) {
return false;
}
}
else if (!cardHolder.equals(other.cardHolder)) {
return false;
}
if (expirationDate == null) {
if (other.expirationDate != null) {
return false;
}
} else if (!expirationDate.equals(other.expirationDate)) {
return false;
}
if (network != other.network) {
return false;
}
if (number == null) {
if (other.number != null) {
return false;
}
} else if (!number.equals(other.number)) {
return false;
}
if (securityCode != other.securityCode) {
return false;
}
return true;
}
// public overridden method to return the String
// representation of a CreditCard object
@Override
public String toString()
{
return "Card Network: " + network + " " +
"Card number: " + number + " " +
"Card holder: " + cardHolder + " " +
"Expiry Date: " + expirationDate + " " +
"Secure Code: " + securityCode;
}
}
/**
* Class to represent a CreditCard with all the required instance variables
* methods, and required constructor(s)
* @author YourName
*
*/
public class CreditCard {
// defining enum to represent card network
public enum CardNetwork{
AMEX,
DISCOVER,
MASTERCARD,
VISA
}
// private instance variables
private CardNetwork network;
private String number;
private String cardHolder;
private String expirationDate;
private int securityCode;
// Default constructor to initialize the instance variables
public CreditCard(CardNetwork network, String number,
String cardHolder, String expirationDate, int securityCode) {
this.network = network;
this.number = number;
this.cardHolder = cardHolder;
this.expirationDate = expirationDate;
this.securityCode = securityCode;
}
// public method to return the card network
// @return an enum CardNetwork
public CardNetwork getNetwork() {
return network;
}
// public method to return the card number
// @return String
public String getNumber() {
return number;
}
// public method to return the card holder name
// @return String
public String getCardHolder() {
return cardHolder;
}
// public method to return the card expiry date
// @return String
public String getExpirationDate() {
return expirationDate;
}
// public method to return the card secure code
// @return integer
public int getSecurityCode() {
return securityCode;
}
// public overridden method to check equality of 2 cards
@Override
public boolean equals(Object obj) {
CreditCard other = (CreditCard) obj;
if (cardHolder == null) {
if (other.cardHolder != null) {
return false;
}
}
else if (!cardHolder.equals(other.cardHolder)) {
return false;
}
if (expirationDate == null) {
if (other.expirationDate != null) {
return false;
}
} else if (!expirationDate.equals(other.expirationDate)) {
return false;
}
if (network != other.network) {
return false;
}
if (number == null) {
if (other.number != null) {
return false;
}
} else if (!number.equals(other.number)) {
return false;
}
if (securityCode != other.securityCode) {
return false;
}
return true;
}
// public overridden method to return the String
// representation of a CreditCard object
@Override
public String toString()
{
return "Card Network: " + network + " " +
"Card number: " + number + " " +
"Card holder: " + cardHolder + " " +
"Expiry Date: " + expirationDate + " " +
"Secure Code: " + securityCode;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.