Java For both parts of this assignment, you\'ll implement the following UML diag
ID: 3777214 • Letter: J
Question
Java
For both parts of this assignment, you'll implement the following UML diagram:
Part I: Generic Classes
Write a generic class named PaymentBatchProcessor. You should be able to instantiate a PaymentBatchProcessor to work with any of the three types shown above. The PaymentBatchProcessor class should maintain a list of payments (using an ArrayList), based upon the type it was instantiated to work with. This class should include the following methods:
a method named add that adds the appropriate payment type to the ArrayList.
a method named getMax() that returns the payment with the largest amount
a method named getTotal() that returns the sum of all the payments
a method named getSize(), that returns how many payments are in the ArrayList.
Part II: Abstract Methods
For this exercise, you will write two generic methods getSum() and getMax(). Put these two methods in a class named PaymentProcessorHelper. These two methods should be static, and should be able to be used with any Payment type. The logic for these two methods are the same as the previous exercise, so you should be able to reuse the code from Part I. The primary purpose is to write the correct static method signatures for these two methods, and to call them correctly.
All classes for this exercise should be in the same package.
I've included a JUnit test class for your convenience. As with last week, this is an optional part of the assignment, but it will help you verify that your code is correct before submitting.
Payment +setAmount double): void +getAmounto: double Check IOU -amt: double -amt: double +check(double amt) +IOU(dobule amt)Explanation / Answer
package com.gp.classes;
public interface Payment {
public void setAmount(double amount);
public double getAmount();
}
---------------
package com.gp.classes;
public class Check implements Payment {
private double amount;
public Check(double amount) {
this.amount = amount;
}
/**
* @return the amount
*/
public double getAmount() {
return amount;
}
/**
* @param amount
* the amount to set
*/
public void setAmount(double amount) {
this.amount = amount;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Check [amount=" + amount + "]";
}
}
-------------------
package com.gp.classes;
public class IOU implements Payment {
private double amount;
public IOU(double amount) {
this.amount = amount;
}
/**
* @return the amount
*/
public double getAmount() {
return amount;
}
/**
* @param amount
* the amount to set
*/
public void setAmount(double amount) {
this.amount = amount;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "IOU [amount=" + amount + "]";
}
}
--------------------------------
package com.gp.classes;
import java.util.ArrayList;
import java.util.List;
public class PaymentBatchProcessor {
private Payment payment;
List<Payment> paymentslist = null;
public PaymentBatchProcessor(Payment payment) {
this.payment = payment;
this.paymentslist = new ArrayList<Payment>();
}
/*
* a method named getMax() that returns the payment with the largest amount
* a method named getTotal() that returns the sum of all the payments a
* method named getSize(), that returns how many payments are in the
* ArrayList.
*/
public double getMax() {
double gettingAmount = 0.0;
for (Payment payment : paymentslist) {
// System.out.println(payment.getAmount());
if (payment.getAmount() > gettingAmount) {
gettingAmount = payment.getAmount();
}
}
return gettingAmount;
}
public double getTotal()
{
double totalamount = 0;
for (Payment payment : paymentslist) {
totalamount += payment.getAmount();
}
return totalamount;
}
public int getSize() {
int size = paymentslist.size();
return size;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return " paymentslist=" + paymentslist + " ";
}
public static void main(String[] args) {
Payment payment = new Check(25000.63);
Payment payment1 = new IOU(3600.56);
Payment payment2 = new Check(250000.64);
Payment payment3 = new IOU(36000.56);
Payment payment4 = new Check(2500.63);
Payment payment5 = new IOU(36007.56);
PaymentBatchProcessor processor = new PaymentBatchProcessor(payment);
// add to the list
processor.paymentslist.add(payment);
processor.paymentslist.add(payment1);
processor.paymentslist.add(payment2);
processor.paymentslist.add(payment3);
processor.paymentslist.add(payment4);
processor.paymentslist.add(payment5);
System.out.println(processor.paymentslist);
double amount = processor.getMax();
System.out.println("the max payment is" + amount);
double total = processor.getTotal();
System.out.println("the total payment is" + total);
int size = processor.getSize();
System.out.println("the no of payments are=" + size);
}
}
output
the max payment is250000.64
the total payment is353110.58
the no of payments are=6
------------------------
package com.gp.classes;
import java.util.ArrayList;
import java.util.List;
public class PaymentProcessorHelper {
private Payment payment;
static List<Payment> paymentslist = null;
public PaymentProcessorHelper(Payment payment) {
this.payment = payment;
this.paymentslist = new ArrayList<Payment>();
}
/*
* a method named getMax() that returns the payment with the largest amount
* a method named getTotal() that returns the sum of all the payments a
* method named getSize(), that returns how many payments are in the
* ArrayList.
*/
public static double getMax() {
double gettingAmount = 0.0;
for (Payment payment : paymentslist) {
// System.out.println(payment.getAmount());
if (payment.getAmount() > gettingAmount) {
gettingAmount = payment.getAmount();
}
}
return gettingAmount;
}
public static double getSum()
{
double totalamount = 0;
for (Payment payment : paymentslist) {
totalamount += payment.getAmount();
}
return totalamount;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return " paymentslist=" + paymentslist + " ";
}
public static void main(String[] args) {
Payment payment = new Check(25000.63);
Payment payment1 = new IOU(3600.56);
Payment payment2 = new Check(250000.64);
Payment payment3 = new IOU(36000.56);
Payment payment4 = new Check(2500.63);
Payment payment5 = new IOU(36007.56);
PaymentProcessorHelper processor = new PaymentProcessorHelper(payment);
// add to the list
processor.paymentslist.add(payment);
processor.paymentslist.add(payment1);
processor.paymentslist.add(payment2);
processor.paymentslist.add(payment3);
processor.paymentslist.add(payment4);
processor.paymentslist.add(payment5);
System.out.println(processor.paymentslist);
double amount = processor.getMax();
System.out.println("the max payment is" + amount);
double total = processor.getSum();
System.out.println("the total payment is" + total);
}
}
output
the max payment is250000.64
the total payment is353110.58
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.