To write a program that performs data analysis from class ob
To write a program that performs data analysis from class objects
PROJECT DESCRIPTION
Bank is in desperate need of analytics from its clients for its loan application process. Currently, records show 600 clients exist and the bank is hoping to expand it’s clientele by offering premium loans to deserved grantees.
Perform the data analysis as follows for this lab.
Project Details
-Add to your existing project files from lab 2, a new class called Records. Have the class extend the BankRecords class to grab hold its instance methods plus the BankRecord object array.
-Use Comparator class(es) where necessary to sort your BankRecord objects by specific fields for quick processing and retrieval.
-Perform the following analysis requirements and output outcomes for the Records class
Display the following data analytic results to the console:
overall average income
max and min age per location
number of females with mortgages
number of males with both a car and 1 child per location
-Write all displayed data to a text file called bankrecords.txt relative to your project path as well. Appendyour name to the end of the file plus the date.
BankRecords.class
/*
02/09/2017
BankRecords.java
Lab 02
*/
/*
* This program will read a CSV file to capture the data and print it in a more readable fashion
*/
package lab02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class BankRecords implements Client {
//decleration of all variables holding data.... private to only allow class to access them
private String id;
private int age;
private String sex;
private String region;
private double income;
private String married;
private int children;
private String car;
private String save_acct;
private String current_act;
private String mortgage;
private String pep;
ArrayList<StringBuffer> list; // ArrayList Collection of type StringBuffer to store each record
// Setters and getters of above attributes
public String getId() { //Getter
return id;
}
public void setId(String id) { //Setter
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public int getChildren() {
return children;
}
public void setChildren(int children) {
this.children = children;
}
public String getMarried() {
return married;
}
public void setMarried(String married) {
this.married = married;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public String getSave_act() {
return save_acct;
}
public void setSave_act(String save_act) {
this.save_acct = save_act;
}
public String getCurrent_act() {
return current_act;
}
public void setCurrent_act(String current_act) {
this.current_act = current_act;
}
public String getMortgage() {
return mortgage;
}
public void setMortgage(String mortgage) {
this.mortgage = mortgage;
}
public String getPep() {
return pep;
}
public void setPep(String pep) {
this.pep = pep;
}
//read file for data
public void readData() {
try{
list = new ArrayList<StringBuffer>(); // Creating object for ArrayList
FileReader fr = new FileReader(\"/Users/shanshahzad/Desktop/Eclipse/lab02/src/bank-Detail.csv\"); // REading File bank-Details from D:\\ Drive
BufferedReader br = new BufferedReader(fr); // Passing FileReader into BufferedReader
String record;
while((record=br.readLine())!=null){ // Accessing each record
StringBuffer sb = new StringBuffer();
sb.append(record); // Appending to StringBuffer
list.add(sb); // Adding to ArrayList
}
} catch(Exception e){
e.printStackTrace(); // Catching Exception
}
}
//read ALL variables incase we need to print for REFRENCE
public void processData() {
Iterator<StringBuffer> itr = list.iterator(); // Processing ArrayList using Iterator
int i=0;
while(itr.hasNext()){ // Iterating each record from list
if(i==25){
break;
}
StringBuffer sb = itr.next();
String col[] = sb.toString().split(\",\"); // Splitting each attribute with comma seperator
this.setId(col[0]); // Setting each value to Setters
this.setAge(Integer.parseInt(col[1]));
this.setSex(col[2]);
this.setRegion(col[3]);
this.setIncome(Double.parseDouble(col[4]));
this.setMarried(col[5]);
this.setChildren(Integer.parseInt(col[6]));
this.setCar(col[7]);
this.setSave_act(col[8]);
this.setCurrent_act(col[9]);
this.setMortgage(col[10]);
this.setPep(col[11]);
this.printer(); // Calling printer for every record
i++;
}
}
//prints required info into lines
public void printer() {
System.out.println(\"[ID=\" + this.getId() + \", Age=\" + this.getAge() + \", Sex=\"
+ this.getSex() + \", Region=\" + this.getRegion() + \", Income=$\" + this.getIncome() +
\", Mortgage=\" + this.getMortgage() + \"\ --------------------------------------\");
}
public static void main(String[] args) {
BankRecords banker = new BankRecords(); //new object
banker.readData();
banker.processData();
}
}
Client.java
/*
02/09/2017
Client.java
Lab 02
*/
package lab02;
public interface Client {
public static void readData(){} //read file detail
public static void processData() {} //process file detail
public static void printData() {} //print file detail
}
Solution
/*
02/09/2017
BankRecords.java
Lab 02
*/
/*
* This program will read a CSV file to capture the data and print it in a more readable fashion
*/
//package lab02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class BankRecords implements Client {
// decleration of all variables holding data.... private to only allow class
// to access them
private String id;
private int age;
private String sex;
private String region;
private double income;
private String married;
private int children;
private String car;
private String save_acct;
private String current_act;
private String mortgage;
private String pep;
ArrayList<StringBuffer> list; // ArrayList Collection of type StringBuffer
// to store each record
ArrayList<BankRecords> listOfBankRecords =new ArrayList<>();
// Setters and getters of above attributes
public String getId() { // Getter
return id;
}
public void setId(String id) { // Setter
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public int getChildren() {
return children;
}
public void setChildren(int children) {
this.children = children;
}
public String getMarried() {
return married;
}
public void setMarried(String married) {
this.married = married;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public String getSave_act() {
return save_acct;
}
public void setSave_act(String save_act) {
this.save_acct = save_act;
}
public String getCurrent_act() {
return current_act;
}
public void setCurrent_act(String current_act) {
this.current_act = current_act;
}
public String getMortgage() {
return mortgage;
}
public void setMortgage(String mortgage) {
this.mortgage = mortgage;
}
public String getPep() {
return pep;
}
public void setPep(String pep) {
this.pep = pep;
}
// read file for data
public void readData() {
try {
list = new ArrayList<StringBuffer>(); // Creating object for
// ArrayList
FileReader fr = new FileReader(\"/Users/shanshahzad/Desktop/Eclipse/lab02/src/bank-Detail.csv\"); // REading
// File
// bank-Details
// from
// D:\\
// Drive
BufferedReader br = new BufferedReader(fr); // Passing FileReader
// into BufferedReader
String record;
while ((record = br.readLine()) != null) { // Accessing each record
StringBuffer sb = new StringBuffer();
sb.append(record); // Appending to StringBuffer
list.add(sb); // Adding to ArrayList
}
} catch (Exception e) {
e.printStackTrace(); // Catching Exception
}
}
// read ALL variables incase we need to print for REFRENCE
public void processData() {
// Iterator<StringBuffer> itr = list.iterator(); // Processing ArrayList
// // using Iterator
// int i = 0;
// while (itr.hasNext()) { // Iterating each record from list
// if (i == 25) {
// break;
// }
for(StringBuffer sb:list){ // >>>>>>>>>>>>>>>
BankRecords banker = new BankRecords();
String col[] = sb.toString().split(\",\"); // Splitting each attribute
// with comma seperator
banker.setId(col[0]); // Setting each value to Setters
banker.setAge(Integer.parseInt(col[1]));
banker.setSex(col[2]);
banker.setRegion(col[3]);
banker.setIncome(Double.parseDouble(col[4]));
banker.setMarried(col[5]);
banker.setChildren(Integer.parseInt(col[6]));
banker.setCar(col[7]);
banker.setSave_act(col[8]);
banker.setCurrent_act(col[9]);
banker.setMortgage(col[10]);
banker.setPep(col[11]);
banker.printer(); // Calling printer for every record
listOfBankRecords.add(banker);
}
}
// prints required info into lines
public void printer() {
System.out.println(\"[ID=\" + this.getId() + \", Age=\" + this.getAge() + \", Sex=\" + this.getSex() + \", Region=\"
+ this.getRegion() + \", Income=$\" + this.getIncome() + \", Mortgage=\" + this.getMortgage()
+ \"\ --------------------------------------\");
}
public static void main(String[] args) {
BankRecords banker = new BankRecords(); // new object
banker.readData();
banker.processData();
}
}
// Client.java
/*
* 02/09/2017 Client.java Lab 02
*/
public interface Client {
public static void readData() {
} // read file detail
public static void processData() {
} // process file detail
public static void printData() {
} // print file detail
}
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class Recards extends BankRecords {
PrintStream out;
public void dataAnalytic() {
// overall average income
// max and min age per location
// number of females with mortgages
//
// -Write all displayed data to a text file called bankrecords.txt
try {
out = new PrintStream(\"file_name_what_you_want\");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Collections.sort(listOfBankRecords, new BankRecordsComp()); // sorting
// records
// based on
// location
overallAverageIncome();
maxAndMinAgePerLocation();
numberOfFemalesWithMortgages();
maleAndCar();
out.println(\"you can print you name and date here\");
}
public void overallAverageIncome() {
double sum = 0;
for (BankRecords br : listOfBankRecords) {
sum = sum + br.getIncome();
}
double avg = sum / listOfBankRecords.size();
System.out.println(\"overall average income: \" + avg);
out.println(\"overall average income: \" + avg);
}
public void maxAndMinAgePerLocation() {
HashMap<String, Integer> minMap = new HashMap<>();
HashMap<String, Integer> maxMap = new HashMap<>();
for (BankRecords br : listOfBankRecords) {
if (!minMap.containsKey(br.getRegion())) {
minMap.put(br.getRegion(), br.getAge());
} else {
if (minMap.get(br.getRegion()) > br.getAge())
minMap.put(br.getRegion(), br.getAge());
}
if (!maxMap.containsKey(br.getRegion())) {
maxMap.put(br.getRegion(), br.getAge());
} else {
if (maxMap.get(br.getRegion()) < br.getAge())
maxMap.put(br.getRegion(), br.getAge());
}
}
System.out.println(\" max age per location : \" + maxMap);
System.out.println(\" min age per location : \" + minMap);
out.println(\" max age per location : \" + maxMap);
out.println(\" min age per location : \" + minMap);
}
public void numberOfFemalesWithMortgages() {
int sum = 0;
for (BankRecords br : listOfBankRecords) {
if (br.getSex().contentEquals(\"female\") && br.getMortgage() != null)
sum++;
}
System.out.println(\"number of females with mortgages: \" + sum);
out.println(\"number of females with mortgages: \" + sum);
}
public void maleAndCar() {
// number of males with both a car and 1 child per location
HashMap<String, Integer> maleCarChild = new HashMap<>();
for (BankRecords br : listOfBankRecords) {
if (br.getSex().contentEquals(\"male\") && br.getCar() != null && getChildren() == 1) {
if (!maleCarChild.containsKey(br.getRegion())) {
maleCarChild.put(br.getRegion(), 1);
} else {
maleCarChild.put(br.getRegion(), maleCarChild.get(br.getRegion()) + 1);
}
}
}
System.out.println(\" number of males with both a car and 1 child per location: \" + maleCarChild);
out.println(\" number of males with both a car and 1 child per location: \" + maleCarChild);
}
}
class BankRecordsComp implements Comparator<BankRecords> {
@Override
public int compare(BankRecords o1, BankRecords o2) {
return o1.getRegion().compareToIgnoreCase(o2.getRegion());
}
}














