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

Lesson 11 start #12

Open
wants to merge 22 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
76 changes: 75 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,72 @@
<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.15.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
</dependency>
<dependency>
<groupId>com.serenitydojo</groupId>
<artifactId>java-for-testers</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>orgit g.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
</dependency>
<dependency>
<groupId>com.serenitydojo</groupId>
<artifactId>java-for-testers</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>


<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
Expand Down Expand Up @@ -70,5 +126,23 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
68 changes: 68 additions & 0 deletions src/main/java/com/serenitydojo/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.serenitydojo;

/**
* A feline creature.
*/
public class Cat extends Pet {

private String favoriteFood;
private int age;

// A very useful field
public static final String CAT_NOISE = "Meow";

public static String usualFood() {
return "Tuna";
}

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

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

public void setFavoriteFood(String favoriteFood) {
this.favoriteFood = favoriteFood;
}

public String getFavoriteFood() {
return favoriteFood;
}

public int getAge() {
return age;
}

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

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

public void feed(String food) {
System.out.println(getName() + " eats some " + food);
}

public void groom() {
lickPaws();
cleanFur();
}

private void cleanFur() {
System.out.println(getName() + " cleans his fur");
}

private void lickPaws() {
System.out.println(getName() + " licks his paws");
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/serenitydojo/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.serenitydojo;

public class Dog extends Pet {
private String favoriteToy;
private int age;

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

@Override
public String play() {
return "plays with bone";
}

public String getFavoriteToy() {
return favoriteToy;
}

public int getAge() {
return age;
}

public String makeNoise() {
return "Woof";
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/serenitydojo/Hampster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.serenitydojo;

public class Hampster extends Pet {
private String favoriteGame;
private int age;

public Hampster(String name, String favoriteGame, int age) {
super(name);
this.favoriteGame = favoriteGame;
this.age = age;
}

public String getFavoriteGame() {
return favoriteGame;
}

public int getAge() {
return age;
}

@Override
public String play() {
return "runs in wheel";
}

@Override
public String makeNoise() {
return "Squeak";
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/serenitydojo/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.serenitydojo;

public abstract class Pet {
private String name;
private int age;


public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String goForWalks() { return "walk walk walk"; }
public abstract String makeNoise();
public abstract String play();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.serenitydojo.exceptions;

public class FileHasNoWordException extends RuntimeException{
public FileHasNoWordException(String message){
super(message);
}
public FileHasNoWordException(String message, Throwable cause){
super(message,cause);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.serenitydojo.exceptions;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

public class WhenWorkingWithExceptions {
WordCounter wordCounter = new WordCounter();
@Test
public void shouldCountWordsInAString(){
int numberOfWords = wordCounter.numberOfWordsIn("some string");
Assertions.assertThat(numberOfWords).isEqualTo(2);


}
@Test
public void shouldReturnZeroForANullString(){
Assertions.assertThat(wordCounter.numberOfWordsIn(null)).isEqualTo(0);

}
@Test
public void shouldCountWordsInAFile() throws Exception {
int numberOfWords = wordCounter.numberOfWordsInAfile("src/main/resources/hello.txt");
}
@Test(expected = FileHasNoWordException.class)
public void shoulReportAnErrowIfThatFileDoesNotExist()throws Exception{
int numberOfWords = wordCounter.numberOfWordsInAfile("file-that-does-not-exist.txt");
}
@Test(expected = FileHasNoWordException.class)
public void shouldThrowMeaningfullExceptionIfThereAreNoWordsInAFile()throws Exception{
int numberOfWords = wordCounter.numberOfWordsInAfile("src/main/resources/no_words.txt");
Assertions.assertThat(numberOfWords).isEqualTo(0);


}

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;

public class WordCounter {
public int numberOfWordsIn(String value){
if(value==null){
return 0;
}
return value.split("\\W").length;


}

public int numberOfWordsInAfile(String fileName) throws IOException {
try {
String fileContents = Files.readString(Paths.get(fileName));
int wordCount = numberOfWordsIn(fileContents);
if(wordCount==0){
throw new FileHasNoWordException("no words found in a file " + fileName);
}
return wordCount;
}
catch(NoSuchFileException noSuchFile){
throw new FileHasNoWordException("no words found in a non existing file " + fileName);

}

}
}
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, Hamster, Dog, Cat, 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 FoodType feeds(AnimalType animal, boolean isPremium) {

switch (animal) {
case CAT:
return (isPremium) ? FoodType.SALMON : FoodType.TUNA;
case DOG:
return (isPremium) ? FoodType.DELUXE_DOG_FOOD : FoodType.DOG_FOOD;
case HAMSTER:
return (isPremium) ? FoodType.LETTUCE : FoodType.CABBAGE;
default:
return FoodType.UNKNOWN;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/model/FoodType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.model;

public enum FoodType {
TUNA, CABBAGE, LETTUCE, SALMON, DOG_FOOD, DELUXE_DOG_FOOD, Tuna, Cabbage, Dogs_Food, Salmon, Deluxe_Dog_Food, Lettuce, UNKNOWN
}
1 change: 1 addition & 0 deletions src/main/resources/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
Empty file added src/main/resources/no_words.txt
Empty file.
Loading