Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception-handling #25

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions _bkp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.serenitydojo</groupId>
<artifactId>java-for-testers</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>java-for-testers</name>
<url>https://www.serenity-dojo.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<version>4.13</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -70,5 +77,17 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>21</release>
<compilerArgs>--enable-preview</compilerArgs>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
39 changes: 39 additions & 0 deletions src/main/java/com/serenitydojo/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.serenitydojo;

public class Cat extends Pet {
private String favoriteToy;
public static final String CAT_NOISE = "Meow";

public Cat(String name, String favoriteToy, int age) {
super(name, age);
this.favoriteToy = favoriteToy;
}
public Cat(String name, int age) {
super(name, age);
}

public String getFavoriteToy() {
return favoriteToy;
}

public void setFavoriteToy(String favoriteToy) {
this.favoriteToy = favoriteToy;
}

@Override
public String makeNoise() {
return CAT_NOISE;
}
@Override
public String play() {
return "plays with string";
}

public String getFavoriteFood() {
return "Tuna";
}

public static String usualFood() {
return "Tuna";
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/serenitydojo/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.serenitydojo;

public class Dog extends Pet {
private String favoriteToy;
private boolean isFed = false;

public static final String DOG_NOISE = "Woof";

public Dog(String name, String favoriteToy, int age) {
super(name, age);
this.favoriteToy = favoriteToy;
}

public String getFavoriteToy() {
return favoriteToy;
}

public void setFavoriteToy(String favoriteToy) {
this.favoriteToy = favoriteToy;
}

public boolean isFed() {
return true;
}

// Exercise 4
public String makeNoise() {return DOG_NOISE;}

public void feed() {
this.isFed = true;
}

@Override
public String play() {
return "plays with bone";
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/serenitydojo/Hamster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.serenitydojo;

public class Hamster extends Pet {
private String favoriteGame;
public static final String HAMPSTER_NOISE = "Squeak";
public Hamster(String name, String favoriteGame, int age) {
super(name,age);
this.favoriteGame = favoriteGame;
}

public String getFavoriteGame() {
return favoriteGame;
}

@Override
public String makeNoise() {
return HAMPSTER_NOISE;
}

@Override
public String play() {
return HAMPSTER_NOISE;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/serenitydojo/HelloWorldWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.serenitydojo;

public class HelloWorldWriter {
// Define a method
public void writerHelloWord () {
System.out.println("Hello World");
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/serenitydojo/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.serenitydojo;

public abstract class Pet {

private String name;
private int age;

// Constructor Method
public Pet(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public abstract String makeNoise();

public abstract String play();
}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/model/AnimalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.model;

public enum AnimalType {
CAT, DOG, HAMSTER, FISH, LLAMA
}
17 changes: 17 additions & 0 deletions src/main/java/com/serenitydojo/model/Feeder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.serenitydojo.model;

public class Feeder {
public String feeds(AnimalType animal, boolean isPremium) {

switch (animal) {
case CAT:
return (isPremium) ? "Salmon" : "Tuna";
case DOG:
return (isPremium) ? "Meat" : "Dog Food";
case HAMSTER:
return (isPremium) ? "Letucce" : "Cabbage";
default:
return "Don't known this animal - don't known its food";
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/serenitydojo/people/LikeBlue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.serenitydojo.people;

public class LikeBlue implements PersonChecker {
public boolean test (Person person) {

// System.out.println(person.getFavouriteColor());
return (person.getFavouriteColor().equals("Blue"));
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/serenitydojo/people/PeopleDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.serenitydojo.people;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PeopleDatabase {
List<Person> allThePeople;
public PeopleDatabase(List<Person> allThePeople) {
this.allThePeople = allThePeople;
}

//public List<Person> findAllThePeopleWho(PersonChecker check) {
public List<Person> findAllThePeopleWho(Predicate<Person> check) {
return allThePeople.stream()
.filter(check)
.collect(Collectors.toList());

// List<Person> matchingPeople = new ArrayList<>();
// for (Person person: allThePeople) {
// if (check.test(person)) {
// // System.out.println("check TRUE");
// matchingPeople.add(person);
// }
// }
// return matchingPeople;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/serenitydojo/people/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.serenitydojo.people;

public class Person {
public enum Gender {MALE, FEMALE}

String name;
Gender gender;
int points;

int age;
String favouriteColor;

public Person(String name, Gender gender, int age, String favouriteColor) {
this.name = name;
this.gender = gender;
this.age = age;
this.favouriteColor = favouriteColor;
this.points = 0;
}

public String getName() {
return name;
}

public Gender getGender() {
return gender;
}

public int getAge() {
return age;
}

public String getFavouriteColor() {
return favouriteColor;
}

public int getPoints() {
return points;
}

@Override
public String toString (){
return name;
}

public void earnPoints(int earnedPoints){
System.out.println(getName()+" has just earned "+earnedPoints);
points =+ earnedPoints;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/people/PersonChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.people;

public interface PersonChecker {
boolean test(Person person);
}
1 change: 1 addition & 0 deletions src/main/resource/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Word
Empty file added src/main/resource/no_words.txt
Empty file.
Loading