This assignment will familiarize you with Interface and how to implement it. You
ID: 3721891 • Letter: T
Question
This assignment will familiarize you with Interface and how to implement it. You will have to read information from an external data file and add the information two different ArrayList. The list from the file will have students and teachers on it, their names, their gender, and their age. This list should be separated into 2 ArrayLists one for students and one for teachers. While reading the text each token should be added to a new person, which means their title (student, teacher), name, gender, and age, then the new person should be added to the appropriate list. You should also save new people to the data file (people.txt) if you have new one, then reload the file into the ArrayList again. There will be 3 methods used on each ArrrayList to print just male or female teachers/students, students or teachers over a certain age, and the average age for male/female students or teachers.
----people.txt-----
s John m 18
t William m 42
s Susan f 21
s Jack m 19
s Jennifer f 18
t Sophia f 35
t Emma f 37
s Olivia f 26
t Ava f 59
t Tom m 47
s Mia f 18
s Isabella f 29
s Zoe f 17
s Lily f 20
---------------------------------------------------------------------------------------------------------
Example:
students.list(f); // all the female students ( name only)
teachers.list(m);
student.age(f, 20); // female student age is greater than or equal to 20
student.age(m, 18);
teacher.age(f, 40);
student.avg(f); // average age of all the female students
student.avg(m);
teacher.avg(m);
addNewPerson(title, name, gender, age); // save new people to the data file (people.txt)
Create arraylist and read in text file.
Use scanner to read in file.
Separate the into two arraylists (students, teachers).
Use iterator to read the arraylist and add each person to the appropriate list.
In a separate java file create the super class.
Add method to separate gender with a parameter.
Add method to sperate gender and find a certain age limit with two parameters.
Add method to sperate gender and then find the average age.
In a separate java file create the sub classes.
Both classes should implement the super class.
Define each method for both sub classes.
Output the different lists.
Explanation / Answer
PeopleInterface .java
import java.util.List;
public interface PeopleInterface {
void addNewPerson(String title,String name, String gender,int age); // save new people to the data file (people.txt)
List<String> list(List<People> peoples, String gender);
List<People> age(List<People> peoples, String gender, int age);
double avg(List<People> peoples,String gender);
}
People.java
public class People{
private String title;
private String name;
private String gender;
private int age;
//Default constructor
public People() {
}
//Parameterized constructor
public People(String title, String name, String gender, int age) {
this.title = title;
this.name = name;
this.gender = gender;
this.age = age;
}
//Getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [title=" + title + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
}
Student .java
public class Student extends People{
public Student() {
}
public Student(String name, String gender, int age) {
super("s", name, gender, age);
}
}
Teacher .java
public class Teacher extends People {
public Teacher() {
}
public Teacher(String name, String gender, int age) {
super("t", name, gender, age);
}
}
DriverClass .java
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
//This class is main class
public class DriverClass implements PeopleInterface {
static List<People> students = new ArrayList<>();
static List<People> teachers = new ArrayList<>();
public static void main(String[] args) {
DriverClass caller = new DriverClass();
// To Store file data
ArrayList<String> fileData;
Scanner sc = new Scanner(System.in);
System.out.println(
"--------------------------------------------------------------------------------------------");
System.out.println(
"Please enter the name of the course file .......Either put your file in root directory of project or give filename with path");
String fileName = sc.nextLine();
if (fileName != null) {
fileData = readFile(fileName);
if (fileData.isEmpty()) {
System.out.println("There is no data in file ");
System.exit(0);
} else {
// Do the tasks .
// Parse the File contents to Student and Teacher Object
caller.parseFileToArrayLists(fileData);
}
}
// Print data
caller.printStudents();
caller.printTeachers();
// test other methods here
// System.out.println(caller.list(teachers, "m"));
}
private void parseFileToArrayLists(ArrayList<String> fileData) {
Iterator<String> itr = fileData.iterator();
while (itr.hasNext()) {
// read the line
String line = itr.next();
// split the data
String[] data = line.split(" ");
// First token will be title, based on title we will add data to different lists
if (data[0].equalsIgnoreCase("t")) {
Teacher t = new Teacher(data[1], data[2], Integer.parseInt(data[3]));
teachers.add(t);
} else if (data[0].equalsIgnoreCase("s")) {
Student s = new Student(data[1], data[2], Integer.parseInt(data[3]));
students.add(s);
}
}
}
@Override
public List<String> list(List<People> peoples, String gender) {
List<String> peopleWithGivenGender = new ArrayList<>();
Iterator<People> itr = peoples.iterator();
while (itr.hasNext()) {
// read the line
String name = itr.next().getName();
peopleWithGivenGender.add(name);
}
return null;
}
@Override
public List<People> age(List<People> peoples, String gender, int age) {
List<People> studentList = new ArrayList<>();
List<People> teacherList = new ArrayList<>();
Iterator<People> itr = peoples.iterator();
String title = null;
while (itr.hasNext()) {
// read the line
People p = itr.next();
if (p.getTitle().equalsIgnoreCase("t")) {
title = "t";
if (p.getGender().equalsIgnoreCase(gender) && p.getAge() >= age) {
teacherList.add((Teacher) p);
}
} else if (p.getTitle().equalsIgnoreCase("s")) {
title = "s";
if (p.getGender().equalsIgnoreCase(gender) && p.getAge() >= age) {
studentList.add((Student) p);
}
}
}
if (title.equalsIgnoreCase("t")) {
return teacherList;
} else {
return studentList;
}
}
@Override
public double avg(List<People> peoples, String gender) {
double avgStudents = 0;
double avgTeachers = 0;
Iterator<People> itr = peoples.iterator();
String title = null;
while (itr.hasNext()) {
// read the line
People p = itr.next();
if (p.getTitle().equalsIgnoreCase("t")) {
title = "t";
if (p.getGender().equalsIgnoreCase(gender)) {
avgTeachers += p.getAge();
}
} else if (p.getTitle().equalsIgnoreCase("s")) {
title = "s";
if (p.getGender().equalsIgnoreCase(gender)) {
avgStudents += p.getAge();
}
}
}
if (title.equalsIgnoreCase("t")) {
return avgTeachers / peoples.size();
} else {
return avgStudents / peoples.size();
}
}
@Override
public void addNewPerson(String title, String name, String gender, int age) {
if (title.equalsIgnoreCase("s")) {
students.add(new Student(name, gender, age));
} else if (title.equalsIgnoreCase("t")) {
teachers.add(new Teacher(name, gender, age));
}
}
// Read the file
private static ArrayList<String> readFile(String fileName) {
List<String> list = null;
try {
list = Files.readAllLines(new File(fileName).toPath(), Charset.defaultCharset());
} catch (IOException e) {
System.out.println("There is some error in reading the file ");
System.exit(0);
}
return (ArrayList<String>) list;
}
public void printTeachers() {
System.out.println(teachers);
}
public void printStudents() {
System.out.println(students);
}
}
Sample Output:
--------------------------------------------------------------------------------------------
Please enter the name of the course file .......Either put your file in root directory of project or give filename with path
people.txt
[People [title=s, name=John, gender=m, age=18], People [title=s, name=Susan, gender=f, age=21], People [title=s, name=Jack, gender=m, age=19], People [title=s, name=Jennifer, gender=f, age=18], People [title=s, name=Olivia, gender=f, age=26], People [title=s, name=Mia, gender=f, age=18], People [title=s, name=Isabella, gender=f, age=29], People [title=s, name=Zoe, gender=f, age=17], People [title=s, name=Lily, gender=f, age=20]]
[People [title=t, name=William, gender=m, age=42], People [title=t, name=Sophia, gender=f, age=35], People [title=t, name=Emma, gender=f, age=37], People [title=t, name=Ava, gender=f, age=59], People [title=t, name=Tom, gender=m, age=47]]