Answered You can hire a professional tutor to get the answer.

QUESTION

It just looked very long, not so much work!(There are may some mistake in given code, If there is one, just skip that, and mind me that). We have...

It just looked very long, not so much work!(There are may some mistake in given code, If there is one, just skip that, and mind me that).

We have provided a program skeleton (TransactionProcessor.java) for you. It includes a method named getCardType(), which returns a String identifying the type of card that a given card number represents. Complete the TransactionProcessor class as follows:

1. Add a method with the header: public static BankCard convertToCard(String data) This method uses a Scanner to break up the input String into a long (the card number), a String (the cardholder name), and possibly a double (the card limit) followed by an int (the expiration date). It returns a subclass of BankCard built using that data:

a. Start by creating a Scanner to read from the parameter. Recall that you can initialize a Scanner with a String as its argument instead of the more commonly-used System.in. If you do, the Scanner will only collect input from that string, rather than from standard input (the keyboard). Use the Scanner method nextLong() to retrieve the card number.

b. Use the provided getCardType() method to get a String corresponding to the type of card this data belongs to. If the card type is null, skip the next step and move to the next line of the file.

c. Using an if-else chain, determine which of the three BankCard subclasses you are dealing with. Then read the remaining fields from the parameter (see the end of this document for details) and create and return a new card object of the appropriate type.

2. Add a method with the header: public static CardList loadCardData(String fName) This method takes the name of a card data file as its argument. It opens and processes that file, one line at a time, before returning a CardList of BankCards based on the data file contents (you may add the cards to the CardList in any order you want, not necessarily the order they are listed in the card data file). Use the convertToCard() method you defined in the previous step to process each individual line of the data file. If you encounter into an IOException, your method should return null to indicate failure.

3. Add a method with the header: public static void processTransactions(String filename, CardList c) This method opens the card transaction file named filename for reading. For each line in the file:

a. Use the String method split() to divide the line into an array of smaller substrings (use a single space as the separator/delimiter).

b. Use the Long.parseLong() utility method to convert the first substring (the card number) from a String into a long value.

c. Find the index of the card with that card number in your CardList.

d. The second substring indicates what type of transaction we are working with (you may assume that all transactions are performed on the correct type of card):

i. If the transaction type is "redeem":

1. Convert the third substring to an integer value (the number of points to redeem) with Integer.parseInt().

2. Retrieve the card at the specified index and cast it back to a RewardsCard.

3. Call the card's redeemPoints() method with the number of points.

ii. If the transaction type is "top-up":

1. Convert the third substring to a double value (the amount of money to add) with Double.parseDouble().

2. Retrieve the card at the specified index and cast it to a PrepaidCard.

3. Call the card's addFunds() method with the top-up value.

iii. Otherwise:

1. Create a new Transaction from the second, fourth, and third substrings (the third substring should be converted to a double value using Double.parseDouble()).

2. Retrieve the card at the specified index (as a BankCard object).

3. Call that card's addTransaction() method with the Transaction object you just created.

e. If you encounter an IOException, stop processing the file and return immediately from the method (do not try to reverse any transactions that you have already processed successfully).

4. Add a main() method to your TransactionProcessor class. main() should use your helper methods from the previous steps to perform the following sequence of tasks:

a. Read in the name of the card data file from the user.

b. Use loadCardData() to create a CardList from the contents of the card data file.

c. If loadCardData() successfully creates a list of cards:

i. Read in the name of a transaction data file from the user.

ii. Use processTransactions() to read the contents of the transaction file and apply them to the list of cards.

iii. Once you have finished processing the transaction file, print out a summary statement for each card in the CardList (using its printStatement() method).

Format:

The card data file is a plain text file with no blank lines. Each line of the file has two required values (the card number and cardholder name, in that order), possibly followed by one or two optional values. There is a single space between each field: card_number cardholder (for a PrepaidCard with the default starting balance) or card_number cardholder expiration (for a CreditCard or RewardsCard with the default credit limit) or card_number cardholder balance (for a PrepaidCard with a specific starting balance) or card_number cardholder expiration limit (for a CreditCard or RewardsCard with a specified credit limit) Every field has a specific type: • card_number is a long. • cardholder is a String where all spaces have been replaced by underscore ('_') characters (so, for example, "John Smith" will be represented as "John_Smith"). • expiration is an int. • balance and limit are both of type double. For example: 8434567812345678 John_Smith 318 12345.67 represents a CreditCard whose expiration date is March 2018 and whose credit limit is $12345.67. Likewise, 8902345682910224 Mary_Doe represents a PrepaidCard issued to "Mary Doe" with an initial balance of $0. The transaction data file is a plain text file with no blank lines. Each line of the file begins with two required fields (the card number and transaction type, in that order), followed by one or two additional fields (depending on the transaction type). Each field is separated by a single space. The card number is a long value. There are four transaction types (each of which is a String): redeem, top-up, debit, and credit redeem transactions have a third field (an int) representing the number of points being redeemed. top-up transactions include a third field (a double) representing the amount of money to add. debit and credit transactions include two additional fields representing the transaction amount (a double) and the merchant name (a String where all spaces have been replaced by underscore ('_') characters), respectively. Sample transaction records: 8634567812345678 redeem 2048 8853946610327745 top-up 500.00 8734621893246154 debit 397.45 Amazon 8419567820318835 credit -98.72 Gas_N_Go

Some code provided:

import java.util.ArrayList;

abstract class BankCard {

  private String name;

  protected long number;

  protected double balance;

  protected ArrayList<Transaction> lala = new ArrayList<Transaction>();

  public BankCard() {}

  public BankCard(String cardholderName, long cardNumber) {

     this.balance=0;

     this.setName(cardholderName);

     this.number = cardNumber;

  }

  public double balance() {

     return balance;

  }

  public long number() {

     return number;

  }

  public String cardholder() {

     return getName();

  }

  public String toString() {

     String s = "Card # "+number+"ttBalance: $"+balance;

     return s;

  }

  public abstract boolean addTransaction(Transaction t);

  public abstract void printStatement();

  public String getName() {

     return name;

  }

  public void setName(String name) {

     this.name = name;

  }

}

--------------------------------------------------------------------------------------------------------------------------------------------------

public class CreditCard extends BankCard{

  private int date;

  protected double limit;

  public CreditCard(String cardHolder, long cardNumber, int expiration, double limit) {

     this.number = cardNumber;

     this.limit = limit;

     this.setDate(expiration);

     this.setName(cardHolder);

  }

  public CreditCard(String cardHolder, long cardNumber, int expiration) {

     this.number = cardNumber;

     this.limit = 500.00;

     this.setDate(expiration);

     this.setName(cardHolder);

  }

  public CreditCard() {}

  public double limit() {

     return limit;

  }

  public double availableCredit() {

     return limit-balance;

  }

  public int expiration() {

     return getDate();

  }

  public String toString() {

     String s = "Card # "+number+"ttExpiration Date:"+getDate()+"ttBalance: $"+balance;

     return s;

  }

  @Override

  public boolean addTransaction(Transaction t) {

     if (t.type().equals("debit")&&t.amount()<=limit-balance) {

        balance+=t.amount();

        lala.add(t);

        return true;

     }

     else if (t.type().equals("debit")&&t.amount()>limit-balance) {

        return false;

     }

     else if (t.type().equals("credit")) {

        balance+=t.amount();

        lala.add(t);

        return true;

     }

     else {

        return false;

     }

  }

  @Override

  public void printStatement() {

     System.out.println("Cardholder's name: "+cardholder());

     System.out.println("Card Number: "+number());

     System.out.println("Expiraion date: "+expiration());

     System.out.println("balance: "+balance());

     for(int i=0; i<lala.size(); i++) {

  System.out.println("Transaction #" + i + ": " + lala.get(i));

  }

  }

  public int getDate() {

     return date;

  }

  public void setDate(int date) {

     this.date = date;

  }

}

--------------------------------------------------------------------------------------------------------------------------------------------------

public class RewardsCard extends CreditCard{

  private int rpoint;

  public RewardsCard(String holder, long number, int expiration, double limit) {

     this.setName(holder);

     this.number = number;

     this.setDate(expiration);

     this.limit = limit;

     this.rpoint = 0;

  }

  public RewardsCard(String holder, long number, int expiration) {

     this.setName(holder);

     this.number = number;

     this.setDate(expiration);

     this.limit = 500.00;

     this.rpoint = 0;

  }

  public int rewardPoints() {

     return rpoint;

  }

  public boolean redeemPoints(int points) {

     if(points<=rpoint) {

        balance -= points/100;

        rpoint -= points;

        Transaction a = new Transaction("redemption","CSEBank",points/100);

lala.add(a);

        return true;

     }

     else {

        return false;

     }

  }

  public String toString() {

     return super.toString()+"ttReward Point: "+rpoint;

  }

  public boolean addTransaction(Transaction t) {

     if (t.type().equals("debit")&&t.amount()<=limit-balance) {

        balance+=t.amount();

        rpoint += (int)t.amount()*100;

        lala.add(t);

        return true;

     }

     else if (t.type().equals("debit")&&t.amount()>limit-balance) {

        return false;

     }

     else if (t.type().equals("credit")) {

        balance+=t.amount();

        lala.add(t);

        return true;

     }

     else {

        return false;

     }

  }

  public void printStatement() {

     System.out.println("Cardholder's name: "+cardholder());

     System.out.println("Card Number: "+number());

     System.out.println("Expiraion date: "+expiration());

     System.out.println("balance: "+balance());

     for(int i=0; i<lala.size(); i++) {

  System.out.println("Transaction #" + i + ": " + lala.get(i));

  }

  }

--------------------------------------------------------------------------------------------------------------------------------------------------

public class PrepaidCard extends BankCard{

  public PrepaidCard(String cardHolder, long cardNumber, double balance){

     this.setName(cardHolder);

     this.number = cardNumber;

     this.balance = balance;

  }

  public PrepaidCard(String cardHolder, long cardNumber) {

     this.setName(cardHolder);

     this.number = cardNumber;

     this.balance = 0;

  }

  @Override

  public boolean addTransaction(Transaction t) {

     if (t.type().equals("debit")&&t.amount()<=balance) {

        balance-=t.amount();

        lala.add(t);

        return true;

     }

     else if (t.type().equals("debit")&&t.amount()>balance) {

        return false;

     }

     else if (t.type().equals("credit")) {

        balance-=t.amount();

        lala.add(t);

        return true;

     }

     else {

        return false;

     }

  }

  public boolean addFunds(double amount) {

     if(amount>0) {

        balance -= amount;

        Transaction m = new Transaction("top-up","User Payment",amount);

        lala.add(m);

        return true;

     }

     else {

        return false;

     }

  }

  public String toString() {

     String s = "Card # "+number+"ttBalance: $"+balance;

     return s;

  }

  @Override

  public void printStatement() {

     System.out.println("Cardholder's name: "+cardholder());

     System.out.println("Card Number: "+number());

     System.out.println("balance: "+balance());

     for(int i=0; i<lala.size(); i++) {

  System.out.println("Transaction #" + i + ": " + lala.get(i));

  }

  }

}

--------------------------------------------------------------------------------------------------------------------------------------------------

public class Transaction

{

  private String type;

  private double amount;

  private String merchant;

  private String notes;

  public Transaction(String type, String from, double amt)

  {

     this.type = type.toLowerCase(); // accept "Debit" as well as "debit", for example

     // Note: Since the majority of BankCard subtypes accumulate debit

     // amounts each billing cycle, debits are always positive values,

     // while credits are listed as negative values. We enforce that here.

     amount = roundToTwoPlaces(amt);

     if (type.equals("debit") && amount < 0)

     {

        amount = -1.0 * amount; // make the debit a positive value

     }

     if (type.equals("credit") && amount > 0)

     {

        amount = -1.0 * amount; // make the credit a negative value

     }

     if (type.equals("redemption") && amount > 0)

     {

        amount = -1.0 * amount; // redemptions are credits, and therefore negative

     }

     merchant = from;

     notes = "";

  }

  public String type() { return type; }

  public double amount() { return amount; }

  public String merchant() { return merchant; }

  public String notes() { return notes; }

  // Append date to the existing notes field

  public void addNotes(String newData)

  {

     if (notes.length() > 0)

     {

        notes += "n"; // start a new line for the new information

     }

     notes += newData;

  }

  // Replace the existing notes for this Transaction

  public void setNotes (String newData)

  {

     notes = newData;

  }

  public String toString()

  {

     String result = type + "t" + merchant + "t";

     String transAmount = "" + amount;

     // If the transaction amount ends with a single digit after the

     // decimal point, add a trailing 0 to force it to two decimal places

     if (transAmount.charAt(transAmount.length()-2) == '.')

     {

        // Add on a second trailing 0

        transAmount += "0";

     }

     result = result + transAmount;

     if (notes.length() > 0) // add notes on a new line, if present

     {

        result = result + "nt" + notes;

     }

     return result;

  }

  // Make sure that the Transaction's value has exactly two decimal places

  private double roundToTwoPlaces (double value)

  {

     value = value * 100;

     double truncatedValue = Math.floor(value);

     return truncatedValue / 100.0;

  }

}

--------------------------------------------------------------------------------------------------------------------------------------------------

// Driver class for the final project
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question