first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
#BlueJ class context
comment0.params=
comment0.target=AnimalMonitor()
comment0.text=\n\ Create\ an\ AnimalMonitor.\n
comment1.params=filename
comment1.target=void\ addSightings(java.lang.String)
comment1.text=\n\ Add\ the\ sightings\ recorded\ in\ the\ given\ filename\ to\ the\ current\ list.\n\ @param\ filename\ A\ CSV\ file\ of\ Sighting\ records.\n
comment2.params=
comment2.target=void\ printList()
comment2.text=\n\ Print\ details\ of\ all\ the\ sightings.\n
comment3.params=animal
comment3.target=void\ printSightingsOf(java.lang.String)
comment3.text=\n\ Print\ the\ details\ of\ all\ the\ sightings\ of\ the\ given\ animal.\n\ @param\ animal\ The\ type\ of\ animal.\n
comment4.params=spotter
comment4.target=void\ printSightingsBy(int)
comment4.text=\n\ Print\ all\ the\ sightings\ by\ the\ given\ spotter.\n\ @param\ spotter\ The\ ID\ of\ the\ spotter.\n
comment5.params=animalNames\ dangerThreshold
comment5.target=void\ printEndangered(java.util.ArrayList,\ int)
comment5.text=\n\ Print\ a\ list\ of\ the\ types\ of\ animal\ considered\ to\ be\ endangered.\n\ @param\ animalNames\ A\ list\ of\ animals\ names.\n\ @param\ dangerThreshold\ Counts\ less-than\ or\ equal-to\ to\ this\ level\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ are\ considered\ to\ be\ dangerous.\n
comment6.params=animal
comment6.target=int\ getCount(java.lang.String)
comment6.text=\n\ Return\ a\ count\ of\ the\ number\ of\ sightings\ of\ the\ given\ animal.\n\ @param\ animal\ The\ type\ of\ animal.\n\ @return\ The\ count\ of\ sightings\ of\ the\ given\ animal.\n
comment7.params=
comment7.target=void\ removeZeroCounts()
comment7.text=\n\ Remove\ from\ the\ sightings\ list\ all\ of\ those\ records\ with\n\ a\ count\ of\ zero.\n
comment8.params=animal\ area
comment8.target=java.util.ArrayList\ getSightingsInArea(java.lang.String,\ int)
comment8.text=\n\ Return\ a\ list\ of\ all\ sightings\ of\ the\ given\ type\ of\ animal\n\ in\ a\ particular\ area.\n\ @param\ animal\ The\ type\ of\ animal.\n\ @param\ area\ The\ ID\ of\ the\ area.\n\ @return\ A\ list\ of\ sightings.\n
comment9.params=animal
comment9.target=java.util.ArrayList\ getSightingsOf(java.lang.String)
comment9.text=\n\ Return\ a\ list\ of\ all\ the\ sightings\ of\ the\ given\ animal.\n\ @param\ animal\ The\ type\ of\ animal.\n\ @return\ A\ list\ of\ all\ sightings\ of\ the\ given\ animal.\n
numComments=10

View File

@@ -0,0 +1,153 @@
import java.util.ArrayList;
import java.util.Iterator;
/**
* Monitor counts of different types of animal.
* Sightings are recorded by spotters.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29 (imperative)
*/
public class AnimalMonitor
{
// Records of all the sightings of animals.
private ArrayList<Sighting> sightings;
/**
* Create an AnimalMonitor.
*/
public AnimalMonitor()
{
this.sightings = new ArrayList<>();
}
/**
* Add the sightings recorded in the given filename to the current list.
* @param filename A CSV file of Sighting records.
*/
public void addSightings(String filename)
{
SightingReader reader = new SightingReader();
sightings.addAll(reader.getSightings(filename));
}
/**
* Print details of all the sightings.
*/
public void printList()
{
for(Sighting record : sightings) {
System.out.println(record.getDetails());
}
}
/**
* Print the details of all the sightings of the given animal.
* @param animal The type of animal.
*/
public void printSightingsOf(String animal)
{
for(Sighting record : sightings) {
if(animal.equals(record.getAnimal())) {
System.out.println(record.getDetails());
}
}
}
/**
* Print all the sightings by the given spotter.
* @param spotter The ID of the spotter.
*/
public void printSightingsBy(int spotter)
{
for(Sighting record : sightings) {
if(record.getSpotter() == spotter) {
System.out.println(record.getDetails());
}
}
}
/**
* Print a list of the types of animal considered to be endangered.
* @param animalNames A list of animals names.
* @param dangerThreshold Counts less-than or equal-to to this level
* are considered to be dangerous.
*/
public void printEndangered(ArrayList<String> animalNames,
int dangerThreshold)
{
for(String animal : animalNames) {
if(getCount(animal) <= dangerThreshold) {
System.out.println(animal + " is endangered.");
}
}
}
/**
* Return a count of the number of sightings of the given animal.
* @param animal The type of animal.
* @return The count of sightings of the given animal.
*/
public int getCount(String animal)
{
int total = 0;
for(Sighting sighting : sightings) {
if(animal.equals(sighting.getAnimal())) {
total = total + sighting.getCount();
}
}
return total;
}
/**
* Remove from the sightings list all of those records with
* a count of zero.
*/
public void removeZeroCounts()
{
Iterator<Sighting> it = sightings.iterator();
while(it.hasNext()) {
Sighting record = it.next();
if(record.getCount() == 0) {
it.remove();
}
}
}
/**
* Return a list of all sightings of the given type of animal
* in a particular area.
* @param animal The type of animal.
* @param area The ID of the area.
* @return A list of sightings.
*/
public ArrayList<Sighting> getSightingsInArea(String animal, int area)
{
ArrayList<Sighting> records = new ArrayList<>();
for(Sighting record : sightings) {
if(animal.equals(record.getAnimal())) {
if(record.getArea() == area) {
records.add(record);
}
}
}
return records;
}
/**
* Return a list of all the sightings of the given animal.
* @param animal The type of animal.
* @return A list of all sightings of the given animal.
*/
public ArrayList<Sighting> getSightingsOf(String animal)
{
ArrayList<Sighting> filtered = new ArrayList<>();
for(Sighting record : sightings) {
if(animal.equals(record.getAnimal())) {
filtered.add(record);
}
}
return filtered;
}
}

View File

@@ -0,0 +1,13 @@
Project: animal-monitoring-v1
Authors: David J. Barnes and Michael Kölling
This project is part of the material for chapter 5 of the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is the starting version for lambda and stream exercises;
here, the code is written in the traditional imperative style, using
for-each and while loops.

View File

@@ -0,0 +1,23 @@
#BlueJ class context
comment0.params=animal\ spotter\ count\ area\ period
comment0.target=Sighting(java.lang.String,\ int,\ int,\ int,\ int)
comment0.text=\n\ Create\ a\ record\ of\ a\ sighting\ of\ a\ particular\ type\ of\ animal.\n\ @param\ animal\ The\ animal\ spotted.\n\ @param\ spotter\ The\ ID\ of\ the\ spotter.\n\ @param\ count\ How\ many\ were\ seen\ (>\=\ 0).\n\ @param\ area\ The\ ID\ of\ the\ area\ in\ which\ they\ were\ seen.\n\ @param\ period\ The\ reporting\ period.\n
comment1.params=
comment1.target=java.lang.String\ getAnimal()
comment1.text=\n\ Return\ the\ type\ of\ animal\ spotted.\n\ @return\ The\ animal\ type.\n
comment2.params=
comment2.target=int\ getSpotter()
comment2.text=\n\ Return\ the\ ID\ of\ the\ spotter.\n\ @return\ The\ spotter's\ ID.\n
comment3.params=
comment3.target=int\ getCount()
comment3.text=\n\ Return\ how\ many\ were\ spotted.\n\ @return\ The\ number\ seen.\n
comment4.params=
comment4.target=int\ getArea()
comment4.text=\n\ Return\ the\ ID\ of\ the\ area\ in\ which\ they\ were\ seen.\n\ @return\ Where\ they\ were\ seen.\n
comment5.params=
comment5.target=int\ getPeriod()
comment5.text=\n\ Return\ the\ period\ in\ which\ they\ were\ seen.\n\ @return\ When\ they\ were\ seen.\n
comment6.params=
comment6.target=java.lang.String\ getDetails()
comment6.text=\n\ Return\ a\ string\ containing\ details\ of\ the\ animal,\ the\ number\ seen,\n\ where\ they\ were\ seen,\ who\ spotted\ them\ and\ when.\n\ @return\ A\ string\ giving\ details\ of\ the\ sighting.\n
numComments=7

View File

@@ -0,0 +1,96 @@
/**
* Details of a sighting of a type of animal by an individual spotter.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class Sighting
{
// The animal spotted.
private final String animal;
// The ID of the spotter.
private final int spotter;
// How many were seen.
private final int count;
// The ID of the area in which they were seen.
private final int area;
// The reporting period.
private final int period;
/**
* Create a record of a sighting of a particular type of animal.
* @param animal The animal spotted.
* @param spotter The ID of the spotter.
* @param count How many were seen (>= 0).
* @param area The ID of the area in which they were seen.
* @param period The reporting period.
*/
public Sighting(String animal, int spotter, int count, int area, int period)
{
this.animal = animal;
this.spotter = spotter;
this.count = count;
this.area = area;
this.period = period;
}
/**
* Return the type of animal spotted.
* @return The animal type.
*/
public String getAnimal()
{
return animal;
}
/**
* Return the ID of the spotter.
* @return The spotter's ID.
*/
public int getSpotter()
{
return spotter;
}
/**
* Return how many were spotted.
* @return The number seen.
*/
public int getCount()
{
return count;
}
/**
* Return the ID of the area in which they were seen.
* @return Where they were seen.
*/
public int getArea()
{
return area;
}
/**
* Return the period in which they were seen.
* @return When they were seen.
*/
public int getPeriod()
{
return period;
}
/**
* Return a string containing details of the animal, the number seen,
* where they were seen, who spotted them and when.
* @return A string giving details of the sighting.
*/
public String getDetails()
{
return animal +
", count = " + count +
", area = " + area +
", spotter = " + spotter +
", period = " + period;
}
}

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=SightingReader()
comment0.text=\n\ Create\ a\ SightingReader.\n
comment1.params=filename
comment1.target=java.util.ArrayList\ getSightings(java.lang.String)
comment1.text=\n\ Read\ sightings\ in\ CSV\ format\ from\ the\ given\ file.\n\ Return\ an\ ArrayList\ of\ Sighting\ objects\ created\ from\n\ the\ information\ in\ the\ file.\n\ \n\ @param\ filename\ The\ file\ to\ be\ read\ -\ should\ be\ in\ CSV\ format.\n\ @return\ A\ list\ of\ Sightings.\n
numComments=2

View File

@@ -0,0 +1,81 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* A class to read CSV-style records of animal sighting reports.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class SightingReader
{
// How many fields are expected.
private static final int NUMBER_OF_FIELDS = 5;
// Index values for the fields in each record.
private static final int SPOTTER = 0,
ANIMAL = 1,
COUNT = 2,
AREA = 3,
PERIOD = 4;
/**
* Create a SightingReader.
*/
public SightingReader()
{
}
/**
* Read sightings in CSV format from the given file.
* Return an ArrayList of Sighting objects created from
* the information in the file.
*
* @param filename The file to be read - should be in CSV format.
* @return A list of Sightings.
*/
public ArrayList<Sighting> getSightings(String filename)
{
// Create a Sighting from a CSV input line.
Function<String, Sighting> createSighting =
record -> {
String[] parts = record.split(",");
if(parts.length == NUMBER_OF_FIELDS) {
try {
int spotter = Integer.parseInt(parts[SPOTTER].trim());
String animal = parts[ANIMAL].trim();
int count = Integer.parseInt(parts[COUNT].trim());
int area = Integer.parseInt(parts[AREA].trim());
int period = Integer.parseInt(parts[PERIOD].trim());
return new Sighting(animal, spotter, count, area, period);
}
catch(NumberFormatException e) {
System.out.println("Sighting record has a malformed integer: " + record);
return null;
}
}
else {
System.out.println("Sighting record has the wrong number of fields: " + record);
return null;
}
};
ArrayList<Sighting> sightings;
try {
sightings = Files.lines(Paths.get(filename))
.filter(record -> record.length() > 0 && record.charAt(0) != '#')
.map(createSighting)
.filter(sighting -> sighting != null)
.collect(Collectors.toCollection(ArrayList::new));
}
catch(IOException e) {
System.out.println("Unable to open " + filename);
sightings = new ArrayList<>();
}
return sightings;
}
}

View File

@@ -0,0 +1,64 @@
#BlueJ package file
dependency1.from=AnimalMonitor
dependency1.to=SightingReader
dependency1.type=UsesDependency
dependency2.from=AnimalMonitor
dependency2.to=Sighting
dependency2.type=UsesDependency
dependency3.from=SightingReader
dependency3.to=Sighting
dependency3.type=UsesDependency
objectbench.height=76
objectbench.width=747
package.editor.height=421
package.editor.width=639
package.editor.x=70
package.editor.y=80
package.numDependencies=3
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=514
readme.editor.width=661
readme.editor.x=64
readme.editor.y=23
target1.editor.height=700
target1.editor.width=998
target1.editor.x=64
target1.editor.y=23
target1.height=50
target1.name=SightingReader
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=120
target1.x=330
target1.y=120
target2.editor.height=1035
target2.editor.width=900
target2.editor.x=64
target2.editor.y=23
target2.height=50
target2.name=Sighting
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=220
target2.y=220
target3.editor.height=865
target3.editor.width=900
target3.editor.x=112
target3.editor.y=156
target3.height=50
target3.name=AnimalMonitor
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=120
target3.x=80
target3.y=70

View File

@@ -0,0 +1,21 @@
# Fields: spotter,animal,count,area,period
0,Mountain Gorilla,3,1,0
0,Buffalo,10,1,0,
0,Elephant,0,1,0
1,Mountain Gorilla,1,2,0
2,Mountain Gorilla,3,3,0
3,Mountain Gorilla,0,2,0
3,Buffalo,2,1,0
3,Topi,25,1,0
0,Mountain Gorilla,4,1,1
0,Buffalo,16,1,1,
1,Topi,20,1,1,
3,Buffalo,0,2,1
3,Topi,30,2,1
0,Mountain Gorilla,1,1,2
1,Mountain Gorilla,2,2,2
2,Mountain Gorilla,0,3,2
3,Topi,30,2,2
3,Elephant,24,2,2
1 # Fields: spotter,animal,count,area,period
2 0,Mountain Gorilla,3,1,0
3 0,Buffalo,10,1,0,
4 0,Elephant,0,1,0
5 1,Mountain Gorilla,1,2,0
6 2,Mountain Gorilla,3,3,0
7 3,Mountain Gorilla,0,2,0
8 3,Buffalo,2,1,0
9 3,Topi,25,1,0
10 0,Mountain Gorilla,4,1,1
11 0,Buffalo,16,1,1,
12 1,Topi,20,1,1,
13 3,Buffalo,0,2,1
14 3,Topi,30,2,1
15 0,Mountain Gorilla,1,1,2
16 1,Mountain Gorilla,2,2,2
17 2,Mountain Gorilla,0,3,2
18 3,Topi,30,2,2
19 3,Elephant,24,2,2

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=
comment0.target=AnimalMonitor()
comment0.text=\n\ Create\ an\ AnimalMonitor.\n
comment1.params=filename
comment1.target=void\ addSightings(java.lang.String)
comment1.text=\n\ Add\ the\ sightings\ recorded\ in\ the\ given\ filename\ to\ the\ current\ list.\n\ @param\ filename\ A\ CSV\ file\ of\ Sighting\ records.\n
comment2.params=
comment2.target=void\ printList()
comment2.text=\n\ Print\ details\ of\ all\ the\ sightings.\n
comment3.params=animal
comment3.target=void\ printSightingsOf(java.lang.String)
comment3.text=\n\ Print\ details\ of\ all\ the\ sightings\ of\ the\ given\ animal.\n\ @param\ animal\ The\ type\ of\ animal.\n
comment4.params=spotter
comment4.target=void\ printSightingsBy(int)
comment4.text=\n\ Print\ all\ the\ sightings\ by\ the\ given\ spotter.\n\ @param\ spotter\ The\ ID\ of\ the\ spotter.\n
comment5.params=animal
comment5.target=int\ getCount(java.lang.String)
comment5.text=\n\ Return\ a\ count\ of\ the\ number\ of\ sightings\ of\ the\ given\ animal.\n\ @param\ animal\ The\ type\ of\ animal.\n\ @return\ The\ count\ of\ sightings\ of\ the\ given\ animal.\n
numComments=6

View File

@@ -0,0 +1,77 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Monitor counts of different types of animal.
* Sightings are recorded by spotters.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.03.01 (functional)
*/
public class AnimalMonitor
{
private ArrayList<Sighting> sightings;
/**
* Create an AnimalMonitor.
*/
public AnimalMonitor()
{
this.sightings = new ArrayList<>();
}
/**
* Add the sightings recorded in the given filename to the current list.
* @param filename A CSV file of Sighting records.
*/
public void addSightings(String filename)
{
SightingReader reader = new SightingReader();
sightings.addAll(reader.getSightings(filename));
}
/**
* Print details of all the sightings.
*/
public void printList()
{
sightings.forEach(sighting -> System.out.println(sighting.getDetails()));
}
/**
* Print details of all the sightings of the given animal.
* @param animal The type of animal.
*/
public void printSightingsOf(String animal)
{
sightings.stream()
.filter(sighting -> animal.equals(sighting.getAnimal()))
.forEach(sighting -> System.out.println(sighting.getDetails()));
}
/**
* Print all the sightings by the given spotter.
* @param spotter The ID of the spotter.
*/
public void printSightingsBy(int spotter)
{
sightings.stream()
.filter(sighting -> sighting.getSpotter() == spotter)
.map(sighting -> sighting.getDetails())
.forEach(details -> System.out.println(details));
}
/**
* Return a count of the number of sightings of the given animal.
* @param animal The type of animal.
* @return The count of sightings of the given animal.
*/
public int getCount(String animal)
{
return sightings.stream()
.filter(sighting -> animal.equals(sighting.getAnimal()))
.map(sighting -> sighting.getCount())
.reduce(0, (runningSum, count) -> runningSum + count);
}
}

View File

@@ -0,0 +1,12 @@
Project: animal-monitoring-v2
Authors: David J. Barnes and Michael Kölling
This project is part of the material for chapter 5 of the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project demonstrates how to process collections with lambdas
and streams.

View File

@@ -0,0 +1,23 @@
#BlueJ class context
comment0.params=animal\ spotter\ count\ area\ period
comment0.target=Sighting(java.lang.String,\ int,\ int,\ int,\ int)
comment0.text=\n\ Create\ a\ record\ of\ a\ sighting\ of\ a\ particular\ type\ of\ animal.\n\ @param\ animal\ The\ animal\ spotted.\n\ @param\ spotter\ The\ ID\ of\ the\ spotter.\n\ @param\ count\ How\ many\ were\ seen\ (>\=\ 0).\n\ @param\ area\ The\ ID\ of\ the\ area\ in\ which\ they\ were\ seen.\n\ @param\ period\ The\ reporting\ period.\n
comment1.params=
comment1.target=java.lang.String\ getAnimal()
comment1.text=\n\ Return\ the\ type\ of\ animal\ spotted.\n\ @return\ The\ animal\ type.\n
comment2.params=
comment2.target=int\ getSpotter()
comment2.text=\n\ Return\ the\ ID\ of\ the\ spotter.\n\ @return\ The\ spotter's\ ID.\n
comment3.params=
comment3.target=int\ getCount()
comment3.text=\n\ Return\ how\ many\ were\ spotted.\n\ @return\ The\ number\ seen.\n
comment4.params=
comment4.target=int\ getArea()
comment4.text=\n\ Return\ the\ ID\ of\ the\ area\ in\ which\ they\ were\ seen.\n\ @return\ Where\ they\ were\ seen.\n
comment5.params=
comment5.target=int\ getPeriod()
comment5.text=\n\ Return\ the\ period\ in\ which\ they\ were\ seen.\n\ @return\ When\ they\ were\ seen.\n
comment6.params=
comment6.target=java.lang.String\ getDetails()
comment6.text=\n\ Return\ a\ string\ containing\ details\ of\ the\ animal,\ the\ number\ seen,\n\ where\ they\ were\ seen,\ who\ spotted\ them\ and\ when.\n\ @return\ A\ string\ giving\ details\ of\ the\ sighting.\n
numComments=7

View File

@@ -0,0 +1,98 @@
/**
* Details of a sighting of a type of animal by an individual spotter.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class Sighting
{
// The animal spotted.
private final String animal;
// The ID of the spotter.
private final int spotter;
// How many were seen.
private final int count;
// The ID of the area in which they were seen.
private final int area;
// The reporting period.
private final int period;
/**
* Create a record of a sighting of a particular type of animal.
* @param animal The animal spotted.
* @param spotter The ID of the spotter.
* @param count How many were seen (>= 0).
* @param area The ID of the area in which they were seen.
* @param period The reporting period.
*/
public Sighting(String animal, int spotter, int count, int area, int period)
{
this.animal = animal;
this.spotter = spotter;
this.count = count;
this.area = area;
this.period = period;
}
/**
* Return the type of animal spotted.
* @return The animal type.
*/
public String getAnimal()
{
return animal;
}
/**
* Return the ID of the spotter.
* @return The spotter's ID.
*/
public int getSpotter()
{
return spotter;
}
/**
* Return how many were spotted.
* @return The number seen.
*/
public int getCount()
{
return count;
}
/**
* Return the ID of the area in which they were seen.
* @return Where they were seen.
*/
public int getArea()
{
return area;
}
/**
* Return the period in which they were seen.
* @return When they were seen.
*/
public int getPeriod()
{
return period;
}
/**
* Return a string containing details of the animal, the number seen,
* where they were seen, who spotted them and when.
* @return A string giving details of the sighting.
*/
public String getDetails()
{
return animal +
", count = " + count +
", area = " + area +
", spotter = " + spotter +
", period = " + period;
}
}

View File

@@ -0,0 +1,8 @@
#BlueJ class context
comment0.params=
comment0.target=SightingReader()
comment0.text=\n\ Create\ a\ SightingReader.\n
comment1.params=filename
comment1.target=java.util.ArrayList\ getSightings(java.lang.String)
comment1.text=\n\ Read\ sightings\ in\ CSV\ format\ from\ the\ given\ file.\n\ Return\ an\ ArrayList\ of\ Sighting\ objects\ created\ from\n\ the\ information\ in\ the\ file.\n\ \n\ @param\ filename\ The\ file\ to\ be\ read\ -\ should\ be\ in\ CSV\ format.\n\ @return\ A\ list\ of\ Sightings.\n
numComments=2

View File

@@ -0,0 +1,81 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* A class to read CSV-style records of animal sighting reports.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class SightingReader
{
// How many fields are expected.
private static final int NUMBER_OF_FIELDS = 5;
// Index values for the fields in each record.
private static final int SPOTTER = 0,
ANIMAL = 1,
COUNT = 2,
AREA = 3,
PERIOD = 4;
/**
* Create a SightingReader.
*/
public SightingReader()
{
}
/**
* Read sightings in CSV format from the given file.
* Return an ArrayList of Sighting objects created from
* the information in the file.
*
* @param filename The file to be read - should be in CSV format.
* @return A list of Sightings.
*/
public ArrayList<Sighting> getSightings(String filename)
{
// Create a Sighting from a CSV input line.
Function<String, Sighting> createSighting =
record -> {
String[] parts = record.split(",");
if(parts.length == NUMBER_OF_FIELDS) {
try {
int spotter = Integer.parseInt(parts[SPOTTER].trim());
String animal = parts[ANIMAL].trim();
int count = Integer.parseInt(parts[COUNT].trim());
int area = Integer.parseInt(parts[AREA].trim());
int period = Integer.parseInt(parts[PERIOD].trim());
return new Sighting(animal, spotter, count, area, period);
}
catch(NumberFormatException e) {
System.out.println("Sighting record has a malformed integer: " + record);
return null;
}
}
else {
System.out.println("Sighting record has the wrong number of fields: " + record);
return null;
}
};
ArrayList<Sighting> sightings;
try {
sightings = Files.lines(Paths.get(filename))
.filter(record -> record.length() > 0 && record.charAt(0) != '#')
.map(createSighting)
.filter(sighting -> sighting != null)
.collect(Collectors.toCollection(ArrayList::new));
}
catch(IOException e) {
System.out.println("Unable to open " + filename);
sightings = new ArrayList<>();
}
return sightings;
}
}

View File

@@ -0,0 +1,64 @@
#BlueJ package file
dependency1.from=AnimalMonitor
dependency1.to=SightingReader
dependency1.type=UsesDependency
dependency2.from=SightingReader
dependency2.to=Sighting
dependency2.type=UsesDependency
dependency3.from=AnimalMonitor
dependency3.to=Sighting
dependency3.type=UsesDependency
objectbench.height=76
objectbench.width=747
package.editor.height=425
package.editor.width=639
package.editor.x=70
package.editor.y=80
package.numDependencies=3
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=570
readme.editor.width=690
readme.editor.x=544
readme.editor.y=121
target1.editor.height=700
target1.editor.width=900
target1.editor.x=378
target1.editor.y=23
target1.height=50
target1.name=SightingReader
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=120
target1.x=350
target1.y=140
target2.editor.height=700
target2.editor.width=900
target2.editor.x=941
target2.editor.y=99
target2.height=50
target2.name=Sighting
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=210
target2.y=250
target3.editor.height=865
target3.editor.width=845
target3.editor.x=839
target3.editor.y=108
target3.height=50
target3.name=AnimalMonitor
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=120
target3.x=90
target3.y=60

View File

@@ -0,0 +1,21 @@
# Fields: spotter,animal,count,area,period
0,Mountain Gorilla,3,1,0
0,Buffalo,10,1,0,
0,Elephant,0,1,0
1,Mountain Gorilla,1,2,0
2,Mountain Gorilla,3,3,0
3,Mountain Gorilla,0,2,0
3,Buffalo,2,1,0
3,Topi,25,1,0
0,Mountain Gorilla,4,1,1
0,Buffalo,16,1,1,
1,Topi,20,1,1,
3,Buffalo,0,2,1
3,Topi,30,2,1
0,Mountain Gorilla,1,1,2
1,Mountain Gorilla,2,2,2
2,Mountain Gorilla,0,3,2
3,Topi,30,2,2
3,Elephant,24,2,2
1 # Fields: spotter,animal,count,area,period
2 0,Mountain Gorilla,3,1,0
3 0,Buffalo,10,1,0,
4 0,Elephant,0,1,0
5 1,Mountain Gorilla,1,2,0
6 2,Mountain Gorilla,3,3,0
7 3,Mountain Gorilla,0,2,0
8 3,Buffalo,2,1,0
9 3,Topi,25,1,0
10 0,Mountain Gorilla,4,1,1
11 0,Buffalo,16,1,1,
12 1,Topi,20,1,1,
13 3,Buffalo,0,2,1
14 3,Topi,30,2,1
15 0,Mountain Gorilla,1,1,2
16 1,Mountain Gorilla,2,2,2
17 2,Mountain Gorilla,0,3,2
18 3,Topi,30,2,2
19 3,Elephant,24,2,2