In: Computer Science
Design a set of classes that keeps track of demographic information about a set of people, such as age, nationality, occupation, income, and so on. Design each class to focus on a particular aspect of data collection. Assume the input data is coming from a text file. Create a main driver class to instantiate several of the classes.
Place demographic.txt file in project structure with below details in it:
0001,George,26,NYC,US,123,Engineer,10000,FedEx,Dinklage,Christ
0022,John,22,IL,US,144,Sr.
Engineer,15000,Macy's,Boston,Christ
0033,Ram,29,CH,US,567,Engineer,11000,Delloitte,Sharma,Hindu
0056,Bryan,20,NYC,US,67,Worker,8000,Shop,Page,Christ
0011,Thomas,45,MH,US,898,Doctor,30000,Central
Hospital,Lewis,Christ
Driver class: Demographic.java
package com.demographic;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Demographic {
public static void main(String[] args) {
// TODO Auto-generated method
stub
BufferedReader reader;
try {
reader = new
BufferedReader(new FileReader("demographic.txt"));
String line =
reader.readLine();
while(line!=null)
{
String[] str = line.split(",");
Personal p = new Personal();
Professional pr = new Professional();
Social s = new Social();
line = reader.readLine();
p.setId(Integer.parseInt(str[0]));
p.setName(str[1]);
p.setAge(Integer.parseInt(str[2]));
p.setAddress(str[3]);
p.setNationality(str[4]);
pr.setEmpid(Integer.parseInt(str[5]));
pr.setOccupation(str[6]);
pr.setIncome(Double.parseDouble(str[7]));
pr.setOrg(str[8]);
s.setFamily(str[9]);
s.setReligon(str[10]);
System.out.println("Demographic details for id
"+p.getId()+" is registered!");
}
}
catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
catch (Exception e) {
// TODO
Auto-generated catch block
System.out.println("Other exception");
e.printStackTrace();
}
}
}
Personal.java
package com.demographic;
public class Personal {
private int id;
private String name;
private int age;
private String address;
private String nationality;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
}
Professional.java
package com.demographic;
public class Professional {
private int id;
private int empid;
private String occupation;
private double income;
private String org;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
}
Social.java
package com.demographic;
public class Social {
private int id;
private String family;
private String religon;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getReligon() {
return religon;
}
public void setReligon(String religon) {
this.religon = religon;
}
}
Output:
Demographic details for id 1 is registered!
Demographic details for id 22 is registered!
Demographic details for id 33 is registered!
Demographic details for id 56 is registered!
Demographic details for id 11 is registered!