Code gets data for age in regions etc... but doesnt display data for women with
ID: 3799596 • Letter: C
Question
Code gets data for age in regions etc... but doesnt display data for women with mortgage and males with car and 1 child...
BankRecords.java:
import java.io.BufferedReader; //import necessary classes for program
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
public class BankRecords implements Client //will utilize Client abstract methods
{
//create objects for processing & handling data
static BankRecords robjs[]= new BankRecords[600]; //600 fields in csv file
static List> array = new ArrayList<>(); //array list
//declaring each instance field (these will be the columns on the csv file
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_act;
private String current_act;
private String mortgage;
private String pep;
//constructors
public BankRecords()
{
}
public BankRecords(String id, int age, String sex, String region, double income, String married, int children, String car, String save_act, String current_act, String mortgage, String pep)
{
super();
this.id = id;
this.age = age;
this.sex = sex;
this.region = region;
this.income = income;
this.married = married;
this.children = children;
this.car = car;
this.save_act = save_act;
this.current_act = current_act;
this.mortgage = mortgage;
this.pep = pep;
}
//generated getters and setters through Eclipse
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public String getMarried() {
return married;
}
public void setMarried(String married) {
this.married = married;
}
public int getChildren() {
return children;
}
public void setChildren(int children) {
this.children = children;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public String getSave_act() {
return save_act;
}
public void setSave_act(String save_act) {
this.save_act = 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_act) {
this.mortgage = mortgage_act;
}
public String getPep() {
return pep;
}
public void setPep(String pep) {
this.pep = pep;
}
public static void readData() //method for reading data in csv file
{
String line = "";
//open file in order to read and process
try (BufferedReader br = new BufferedReader(new FileReader("bank-Detail.csv"))) { //try catch
while ((line=br.readLine())!=null) //read from file
array.add(Arrays.asList(line.split(","))); //parse each record into array
} catch (IOException e) { //catch exception
e.printStackTrace();
}
processData(); //next step is to process data
}
public static void processData() { //method for processing data
int idx=0;
for (List rowData: array){
//initialize array
//use setters to populate your array of objects
robjs[idx] = new BankRecords();
robjs[idx].setId(rowData.get(0));
robjs[idx].setAge(Integer.parseInt(rowData.get(1))); //parse int to string
robjs[idx].setSex(rowData.get(2));
robjs[idx].setRegion(rowData.get(3));
robjs[idx].setIncome(Double.parseDouble(rowData.get(4))); //parse double to string
robjs[idx].setMarried(rowData.get(5));
robjs[idx].setChildren(Integer.parseInt(rowData.get(6))); //parse int to string
robjs[idx].setCar(rowData.get(7));
robjs[idx].setSave_act(rowData.get(8));
robjs[idx].setCurrent_act(rowData.get(9));
robjs[idx].setMortgage(rowData.get(10));
robjs[idx].setPep(rowData.get(11));
idx++;
}
printData(); //next step is to print data
}
public static void printData() //method for printing the data
{
System.out.printf("%-13s%13s%13s%13s%13s%13s%n", "ID" , "AGE", "SEX", "REGION", "INCOME", "MORTGAGE", "CAR");
//display the headings for columns
//create for loop to display each robjs object value
for(int i=0;i<25;i++) //for loop for first 25 records
//display
System.out.printf("%-13s%13s%13s%13s%13s%13s%n", robjs[i].getId(), robjs[i].getAge(), robjs[i].getSex(), robjs[i].getRegion(), robjs[i].getIncome(), robjs[i].getMortgage(), robjs[i].getCar());
//columnar format with 6 columns for id, age, sex, region, income, and mortgage
System.out.println("------------------------------------------------------------------");
Records foo = new Records();
foo.dataAnalytic();
}
public static void main(String[] args) //main method
{
readData(); //start with readData method
System.out.println(); //white space
String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()); //format time and date
System.out.println("Cur dt=" + timeStamp + " Programmed "); //display name, date, time
} //end main
} //end class
Records.java
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class Records extends BankRecords {
PrintStream out;
public static void main (String[] args) {
}
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("bankrecords.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Arrays.sort(BankRecords.robjs, new BankRecordsComp()); // sorting
// records
// based on
// location
overallAverageIncome();
maxAndMinAgePerLocation();
numberOfFemalesWithMortgages();
maleAndCar();
out.println("STAMP: IZAAZ KOTHAWALA | 2/27/2017");
}
public void overallAverageIncome() {
double sum = 0;
for (BankRecords br : robjs) {
sum = sum + br.getIncome();
}
double avg = sum / robjs.length;
System.out.println("overall average income: " + avg);
out.println("overall average income: " + avg);
}
public void maxAndMinAgePerLocation() {
HashMap minMap = new HashMap<>();
HashMap maxMap = new HashMap<>();
for (BankRecords br : this.robjs) {
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 : robjs) {
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
int mcsum = 0;
HashMap maleCarChild = new HashMap<>();
for (BankRecords br : robjs) {
if (br.getSex().equalsIgnoreCase("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);
}
*/
mcsum++;
}
}
System.out.println("-number of males with both a car and 1 child per location: " + mcsum);
out.println("-number of males with both a car and 1 child per location: " + maleCarChild);
}
}
class BankRecordsComp implements Comparator {
@Override
public int compare(BankRecords o1, BankRecords o2) {
return o1.getRegion().compareToIgnoreCase(o2.getRegion());
}
}
Client.java
/*
Izaaz Kothawala
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
}
bank-Detail.csv
terminated B am Files (x86)Maya 27, 2017, 3:57:34 aww.exe 15538.8 ID12124 27 TOWN FEMALE YES ID12125 22 MALE INNER CITY 12640.3 overall average income: 27936.072716666695 -max age per location {RURAL 67, SUBURBAN-67, TOWN 67, INNER CITY-67) -min age per location RURAL-18, SUBURBAN-18, TOWN-19, INNER CITY-18) -number of females with mortgages 0 -number of males with both a car and 1 child per location: 0 Cur dt 2017/02/27 15:57:34 ProgrammedExplanation / Answer
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class Records extends BankRecords {
PrintStream out;
public static void main (String[] args) {
}
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("bankrecords.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Arrays.sort(BankRecords.robjs, new BankRecordsComp()); // sorting
// records
// based on
// location
overallAverageIncome();
maxAndMinAgePerLocation();
numberOfFemalesWithMortgages();
maleAndCar();
out.println("STAMP: IZAAZ KOTHAWALA | 2/27/2017");
}
public void overallAverageIncome() {
double sum = 0;
for (BankRecords br : robjs) {
sum = sum + br.getIncome();
}
double avg = sum / robjs.length;
System.out.println("overall average income: " + avg);
out.println("overall average income: " + avg);
}
public void maxAndMinAgePerLocation() {
HashMap minMap = new HashMap<>();
HashMap maxMap = new HashMap<>();
for (BankRecords br : this.robjs) {
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 : robjs) {
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
int mcsum = 0;
HashMap maleCarChild = new HashMap<>();
for (BankRecords br : robjs) {
if (br.getSex().equalsIgnoreCase("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);
}
*/
mcsum++;
}
}
System.out.println("-number of males with both a car and 1 child per location: " + mcsum);
out.println("-number of males with both a car and 1 child per location: " + maleCarChild);
}
}
class BankRecordsComp implements Comparator {
@Override
public int compare(BankRecords o1, BankRecords o2) {
return o1.getRegion().compareToIgnoreCase(o2.getRegion());
}
}