Analyse the imperative program code as shown in Listing 1. Adapt each of the programming methods using functional programming concepts. Your solution code should consider higher-order function, method references, functional interfaces or lambda expression, function pipelining, nullable object, collection of objects and etc import java.io.IOException; import java.nio.file.*; import java.util.*; import java.util.regex.Pattern; public class OrderHandler { private static final String FILENAME = "data/deliveryitem.txt"; List data; public OrderHandler() { try { data = Files.readAllLines( Paths.get( FILENAME ) ); } catch (IOException e) { e.printStackTrace(); } } //display all order information except the heading text void print( List> orderLst ) { for (int i = 0; i < orderLst.size(); i++) { System.out.println( orderLst.get(i) ); } } //collect all orders and split them with '\' List> collect(){ List> orderLst = new ArrayList(); for (int i = 0; i < data.size(); i++) { if( !data.get(i).startsWith("PARCELITEM") ) { String[] split = data.get(i).split( Pattern.quote("\\") ); orderLst.add( Arrays.asList( split ) ); } } return orderLst; } //compute the total payment of order placed double computePaymnt( List> orderLst ) { double sum = 0; for (int i = 0; i < orderLst.size(); i++) { int count = orderLst.get(i).size(); double charge = Double.parseDouble( orderLst.get(i).get( count-1 ) ); sum += charge; } return sum; } //export to order object comprises name and charges only List populate( List> orderLst ){ List items = new ArrayList(); for (int i = 0; i < orderLst.size(); i++) { int count = orderLst.get(i).size(); String first = orderLst.get(i).get(0); String last = orderLst.get(i).get( count-1 ); items.add( new Item( first, last ) ); } return items; } }
Things i want is in picture attached
Analyse the imperative program code as shown in Listing 1. Adapt each of the
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.regex.Pattern;
public class OrderHandler {
private static final String FILENAME = "data/deliveryitem.txt";
List<String> data;
public OrderHandler() {
try {
data = Files.readAllLines( Paths.get( FILENAME ) );
} catch (IOException e) {
e.printStackTrace();
}
}
//display all order information except the heading text
void print( List<List<String>> orderLst ) {
for (int i = 0; i < orderLst.size(); i++) {
System.out.println( orderLst.get(i) );
}
}
//collect all orders and split them with '\'
List<List<String>> collect(){
List<List<String>> orderLst = new ArrayList();
for (int i = 0; i < data.size(); i++) {
if( !data.get(i).startsWith("PARCELITEM") ) {
String[] split = data.get(i).split( Pattern.quote("\\") );
orderLst.add( Arrays.asList( split ) );
}
}
return orderLst;
}
//compute the total payment of order placed
double computePaymnt( List<List<String>> orderLst ) {
double sum = 0;
for (int i = 0; i < orderLst.size(); i++) {
int count = orderLst.get(i).size();
double charge = Double.parseDouble( orderLst.get(i).get( count-1 ) );
sum += charge;
}
return sum;
}
//export to order object comprises name and charges only
List<Item> populate( List<List<String>> orderLst ){
List<Item> items = new ArrayList();
for (int i = 0; i < orderLst.size(); i++) {
int count = orderLst.get(i).size();
String first = orderLst.get(i).get(0);
String last = orderLst.get(i).get( count-1 );
items.add( new Item( first, last ) );
}
return items;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps