Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This programming assignment involves writing a Java application which allows the

ID: 3804460 • Letter: T

Question

This programming assignment involves writing a Java application which allows the user to CREATE and VERIFY credit card accounts. The user types textual commands which operate as shown in the examples below. (This sample list is provided to aid your understanding. It is not an exhaustive list of test cases for the program.) create v create AE In this example, the user is requesting to CREATE a new “Visa” credit card account. The program should create a new account and assign it a credit limit as described later in this document. In this example, the user is requesting to CREATE a new “American Express” credit card account. The program should create a new account and assign it a credit limit as described later in this document. verify 5439123412345678 250.00 In this example, the user is requesting to process a purchase of $250.00. First, the software must VERIFY that account 5439123412345678 has at least $250.00 of available credit. If the account does have at least $250.00 of available credit, then the available credit is reduced by $250.00 and the program outputs a message indicating that the transaction was approved. This type of transaction is called a “debit”. verify 5439123412345678 -125.00 In this example, the user is requesting to process a return of $125.00. First, the software must VERIFY that adding $125.00 to the available credit of account 5439123412345678 will not raise the available credit above the Maximum Credit Limit for this account. If the transaction is allowed, then the available credit is increased by $125.00, and the program outputs a message indicating that the transaction was approved. This type of transaction is called a “credit”. Credit Card Account Numbers and Issuer Codes Each credit card account number should be represented in the program by a String object. The text we place in this String consists of 16 decimal digits: The first digit is assigned according to the table on the next page. The remaining 15 digits should be generated randomly. When initially creating each account, the maximum credit limit is based on the last digit of the credit card account number. That is, if the last digit is in the range of 0-4, then a maximum credit limit of $1000.00 should be assigned. Otherwise, the maximum credit limit of $500.00 should be assigned. The table below lists several credit card issuers, the first digit of their account numbers and the symbol we will use on the program command-line to select that particular issuer. Data File Format The credit card data should be stored in a text file. Each line of text in the file represents one credit card account, and the format of the data should be as follows: account_Number | current_available_credit| maximum_credit_limit The field delimiter character should be the vertical bar (“|”) character. (This character is sometimes referred to as the “pipe” character.) This is a good choice because it will never occur in the data fields, and also makes the data file easy to browse manually with a text editor. Processing The user enters text commands from the keyboard. The user transaction either creates a new data record (in memory) or modifies an existing data record. After the transaction has been completed, then all data must be written out to the data file before the program exits. This requirement can pose a challenge during the debug process: if the program writes bad data to the file during one test, then that could mean the data file becomes unusable for the next test. One simple way to deal with this is for the tester (you) to manually copy the data file to a different file name or different folder before executing each test. The following section describes an automated approach to handle this issue. Card Issuer Symbol First Digit American Express AE 3 Visa V 4 MasterCard MC 5 Discover DIS 6 Diners Club DINE 7

Explanation / Answer

Here is the code for the question. Please don't forget to rate the answer if it helped. Thank you very much

CreditCard.java

//class representiing a credit card
class CreditCard
{
//Card types supported
static String AMERICAN_EXPRESS="AE";
static String VISA="V";
static String MASTER_CARD="MC";
static String DISCOVER="DIS";
static String DINERS_CLUB="DINE";
  
protected String cardNumber; //16 digit card number stored as a string
protected double availableCredit; //available credit in this account of card
protected double maxLimit; //the maximum limit for the card
protected String cardType; //
public CreditCard(String type,String cardNum)
{
   cardType=type;
cardNumber=cardNum;
     
   char ch=cardNum.charAt(15);//get the last digit
     
   if(ch>='0' && ch<='4') //cards ending with digit 0-4
       availableCredit=maxLimit=1000; //to begin with available credit and max are same
   else
       availableCredit=maxLimit=500;
      
}
  
public CreditCard(String cardNum,double availabe,double max)
{
   cardNumber=cardNum;
   availableCredit=availabe;
   maxLimit=max;
     
   //check the 1st digit and assign the cardtype
   char ch=cardNum.charAt(0);
     
   if(ch=='3')
       cardType=AMERICAN_EXPRESS;
   else if(ch=='4')
       cardType=VISA;
   else if(ch=='5')
       cardType=MASTER_CARD;
   else if (ch=='6')
       cardType=DISCOVER;
   else if (ch=='7')
       cardType=DINERS_CLUB;
     
   }
  
String getType()
{
return cardType;
}
String getCardNumber()
{
return cardNumber;
}
double getAvailableCredit()
{
return availableCredit;
}
double getMaxLimit()
{
   return maxLimit;
}
//function to make a purchase on the card with specified amount.
//the function returns false if the purchase can not made if the limit is below the purchase amount
//otherwise , it will update the credit limit and return true
boolean purchase(double amount)
{
if(amount>availableCredit)
return false; //purchase can not be made because the limit is low

availableCredit=availableCredit-amount; //update the credit limit
return true;

  
}
//increases the credit limit by the amount specified. Increase will happen only if the total available credit amount does not cross
// the maximum credit limit set for the card. Returns true if the credit amount was allowed, false otherwise
boolean credit(double amount)
{
   if(availableCredit+amount<=maxLimit)
   {
       availableCredit=availableCredit+amount;
       return true;
   }
   else
   {
       return false;
   }
     
}
}

CreditCardHelper.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;
import java.util.Scanner;


/*class to create and verify (i.e.update) purchase and credit to a card. Loads any existing data from file and updates */
public class CreditCardHelper {
  
Hashtable<String, String> prefixes; //stores the prefixes for various types of cards, each cardtype has one prefix digit
Random random; //used for generating random numbers
ArrayList<CreditCard> cards; //list of cards in sequence as appearing in the file
String filename; //file to store and load credit card details
  
  
  

/*We assume all cards have 16 digits
* AE cards begin with 3
* DINE cards begin wtih 7
* DIS begin with 6
* MC begin with 5
* V cards begin with 4
*/
  
public CreditCardHelper(String file) throws IOException
{
filename=file;
random=new Random();
prefixes=new Hashtable<String,String>();

//store prefixes for different types of cards
prefixes.put(CreditCard.AMERICAN_EXPRESS, "3");
prefixes.put(CreditCard.VISA,"4");
prefixes.put(CreditCard.MASTER_CARD, "5");
prefixes.put(CreditCard.DINERS_CLUB, "6");
prefixes.put(CreditCard.DISCOVER,"7");
  
cards=new ArrayList<CreditCard>();
  
//load data from file
loadData();
}
  
//generates a 16 digit card number by using appropriate prefix for the card type
private String generateCardNumber(String cardType)
{
  
String prefix=prefixes.get(cardType);
String cardnum=null;

if(prefix!=null)
{
cardnum=""+prefix;
for(int i=1;i<=15;i++) //randomly generate the next 15 digits
{
cardnum=cardnum+random.nextInt(9);
}
  
}
return cardnum;
  
}

  
public void loadData()
{
   Scanner scanner;
       try {
           scanner = new Scanner(new File(filename));
       } catch (FileNotFoundException e) {
           //file was not found 1st time the program runs. so no data to load just return
           return;
       }
//file contains cardnumber | available_credit | max_credit
  
String line;
while(scanner.hasNext())
{
   line=scanner.nextLine();
     
if(line.trim().equals("")) continue; //dont process empty line
Scanner lineScanner=new Scanner(line);
lineScanner.useDelimiter("\|"); //we need to escape using \ because '|' is a regex (regular expression) character
CreditCard card=new CreditCard(lineScanner.next(),lineScanner.nextDouble(),lineScanner.nextDouble());
cards.add(card);
lineScanner.close();
  
}
scanner.close(); //close the file
System.out.println("Loaded "+cards.size()+" records from file ");
displayCards();
  
}
  
//returns the index of the card in the array loaded from file. -1 if the card is not found
  
private int findCardIndex(String cardNum)
{
for(int i=0;i<cards.size();i++)
if(cards.get(i).getCardNumber().equals(cardNum))
return i;
return -1;
}
//return true if card is valid and has required credit limit, false otherwise.
public boolean recordPurchase(String cardNumber,double amount) throws IOException
{
int index=findCardIndex(cardNumber);
boolean ret;
if(index==-1)
{
System.out.println("Credit card "+cardNumber+" does not exist");
return false;
}
CreditCard card=cards.get(index);
if(ret=card.purchase(amount))
{
System.out.println("Purchase approved. Credit limit =$"+card.getAvailableCredit()+" for card "+card.getCardNumber());
  
}
else
{
System.out.println("Credit limit is low for purchase amount fo $"+amount);
}
return ret;
}
//credits an amount for the specified cardnum if the total does not exceed max . Returns false for invalid card number, amount exceeds max, return true if approved
public boolean creditAmount(String cardNum,double amount) throws IOException
{
int index=findCardIndex(cardNum);

if(index==-1)
{
System.out.println("Credit card "+cardNum+" does not exist");
return false;
}
CreditCard card=cards.get(index);
if(card.credit(amount)) {
  
   System.out.println("Credit approved. Credit limit =$"+card.getAvailableCredit()+" for card "+card.getCardNumber());
   return true;
}
else
{
   System.out.println("Credit NOT approved because the total credit exceeds the max limit");
   return false;
}
}

  
//creates a new card for the specified type
public CreditCard createCard(String type)
{
type=type.toUpperCase();//convert to upper case
String number=generateCardNumber(type);
if(number==null) return null;
CreditCard card=new CreditCard(type,number);
cards.add(card);   
return card;
  
}
//when the program closes this function is called. So it will write all records into the file
public void close() throws IOException
{
   PrintWriter writer=new PrintWriter(filename);
   CreditCard c;
   for(int i=0;i<cards.size();i++)
   {
       c=cards.get(i);
       writer.write(c.getCardNumber()+"|"+c.getAvailableCredit()+"|"+c.getMaxLimit()+" ");
   }
   writer.close();
   System.out.println(cards.size()+" records written to file "+filename);
}
  
public void displayCards()
{
System.out.println("Type CardNumber Credit Limit Max Limit");
System.out.println("________________________________________________________________");
CreditCard card;
for(int i=0;i<cards.size();i++)
{
card=cards.get(i);
System.out.println(card.getType()+" "+card.getCardNumber()+" "+card.getAvailableCredit()+" "+card.getMaxLimit());
}
}
  
public static void main(String[] args) {
String command;
Scanner scanner=new Scanner(System.in);
try {
CreditCardHelper helper=new CreditCardHelper("creditcards.txt");//filename to load and also store data
String input;
double amount;
CreditCard card;
  
while(true)
{
System.out.println("Command : ");
command=scanner.next().toUpperCase(); //get the command name first
if(command.equals("HELP"))
{
System.out.println("USAGE:");
System.out.println("HELP");
System.out.println("CREATE <cardtype>");
System.out.println("VERIFY <cardnumber> <amount>");
System.out.println("Q");
System.out.println("__________________________________");
}
else if(command.equals("CREATE"))
{
input=scanner.next().toUpperCase();//get the card type and convert to uppercase
card=helper.createCard(input);
if(card!=null)
{
System.out.println("New account created for credit card symbol "+card.getType()+
" ,Account number = "+card.getCardNumber()+", Credit limit = $"+card.getAvailableCredit());
}
else
{
System.out.println("Invalid card type "+input);
}
}
else if(command.equals("VERIFY"))
{
input=scanner.next();//get the cardnumber
amount=scanner.nextDouble(); //get the amount
if(amount<0) //its a return , credit
{

helper.creditAmount(input, -amount);//negate the amount, since we are just want +ve values
  
}
else if(amount>0) //its a purchase
{
helper.recordPurchase(input, amount);
}
}
else if(command.equals("Q")) //for quit
{
helper.displayCards();//display all cards information
helper.close();
break;
}
else
{
System.out.println("Invalid command. Try help.");
}
}
scanner.close();
} catch (IOException e) {
System.out.println("Exception occured "+e.getMessage());
}
}

}

output run 1

Command :
create ae
New account created for credit card symbol AE ,Account number = 3731741547600302, Credit limit = $1000.0
Command :
create mc
New account created for credit card symbol MC ,Account number = 5525652202686328, Credit limit = $500.0
Command :
create v
New account created for credit card symbol V ,Account number = 4068758152356256, Credit limit = $500.0
Command :
create dis
New account created for credit card symbol DIS ,Account number = 7456835126365707, Credit limit = $500.0
Command :
create dine
New account created for credit card symbol DINE ,Account number = 6782614545570058, Credit limit = $500.0
Command :
create hello
Invalid card type HELLO
Command :
verify 5525652202686328 500
Purchase approved. Credit limit =$0.0 for card 5525652202686328
Command :
verify 5525652202686328 100
Credit limit is low for purchase amount fo $100.0
Command :
verify 5525652202686328 -200
Credit approved. Credit limit =$200.0 for card 5525652202686328
Command :
q
Type   CardNumber       Credit Limit       Max Limit
_____________________________________________
AE   3731741547600302   1000.0               1000.0
MC   5525652202686328   200.0               500.0
V   4068758152356256   500.0               500.0
DIS   7456835126365707   500.0               500.0
DINE   6782614545570058   500.0           500.0
5 records written to file creditcards.txt


output run 2

Loaded 5 records from file

Type   CardNumber       Credit Limit       Max Limit
________________________________________________________________
AE   3731741547600302   1000.0           1000.0
MC   5525652202686328   200.0           500.0
V   4068758152356256   500.0           500.0
DINE   7456835126365707   500.0           500.0
DIS   6782614545570058   500.0           500.0
Command :
verify 3731741547600302 700
Purchase approved. Credit limit =$300.0 for card 3731741547600302
Command :
verify 6782614545570058 200
Purchase approved. Credit limit =$300.0 for card 6782614545570058
Command :
verify 7456835126365707 -300
Credit NOT approved because the total credit exceeds the max limit
Command :
q
Type   CardNumber       Credit Limit       Max Limit
________________________________________________________________
AE   3731741547600302   300.0           1000.0
MC   5525652202686328   200.0           500.0
V   4068758152356256   500.0           500.0
DINE   7456835126365707   500.0           500.0
DIS   6782614545570058   300.0           500.0
5 records written to file creditcards.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote