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

Java Solution 1 #6

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0261a81
Sample code lesson 1
wakaleo Apr 17, 2020
75c7ea4
Starting point for lesson 3 exercises
wakaleo Apr 20, 2020
73f19db
Starting point
wakaleo Apr 20, 2020
0998f43
starting point for the exercises
wakaleo Apr 20, 2020
e4cb378
Sample fields and objects code
wakaleo Apr 22, 2020
28ffb99
Lesson 6 starting point
wakaleo Apr 22, 2020
f907617
Cats and dogs
wakaleo Apr 22, 2020
06878ee
Sample solution
wakaleo Apr 22, 2020
504d383
Sample code
wakaleo Apr 24, 2020
c83f3dc
Sample solution
wakaleo Apr 27, 2020
8eb82f4
Sample code for conditionals
wakaleo Apr 30, 2020
0eb1fcb
Sample solution
wakaleo Apr 30, 2020
1b42e34
Exercise starting point
wakaleo Apr 30, 2020
de3b82d
Sample solution
wakaleo Apr 30, 2020
03d821d
Exerise starting point
wakaleo May 23, 2020
b529aa8
sample maps
wakaleo Jun 1, 2020
1e581eb
WIP
wakaleo Jun 2, 2020
b2abf00
Sample code
wakaleo Jun 2, 2020
163374e
Sample code
wakaleo Jun 2, 2020
66e373e
Sample code
wakaleo Jun 16, 2020
a9baf35
Sample code
wakaleo Jun 30, 2020
6dbc2ef
Sample solutions
wakaleo Nov 22, 2020
9dffd06
Starting point for the exercises
wakaleo Nov 22, 2020
28f8b3d
Sample solutions
wakaleo Nov 29, 2020
7b37dfd
Starting point
wakaleo Nov 29, 2020
8c6d939
Sample solution
wakaleo Nov 29, 2020
521b301
Merge branch 'exception-handling' of https://github.com/Ninitha701/ja…
Ninitha701 Mar 16, 2021
be06feb
Merge branch 'java-challenge-start' of https://github.com/Ninitha701/…
Ninitha701 Mar 17, 2021
1e64643
Merge branch 'java-challenge-solution' of https://github.com/Ninitha7…
Ninitha701 Mar 21, 2021
3940aab
Java Challenge 1
Ninitha701 Mar 27, 2021
f4812fb
Java Challenge 1
Ninitha701 Mar 27, 2021
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
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -70,5 +85,16 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
<compilerArgs>--enable-preview</compilerArgs>
</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();
}
43 changes: 43 additions & 0 deletions src/main/java/com/serenitydojo/exceptions/FileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.serenitydojo.exceptions;

import org.apache.commons.io.filefilter.IOFileFilter;

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

public class FileLoader {
public String readHelloWorld() throws IOException {
return Files.readString(Paths.get("src/main/resources/hello.txt"));
}

public Boolean fileContainsText(String filename, String expectedText) {
try
{
String path = "src/main/resources/" + filename;
return Files.readString(Paths.get(path)).contains(expectedText);
}
catch(NoSuchFileException fileDoesNotExist)
{
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}

}

public Boolean fileHasText(String filename, String expectedText) throws IOException,MissingWelcomeFileException {
try
{
String path = "src/main/resources/" + filename;
return Files.readString(Paths.get(path)).contains(expectedText);
}
catch (NoSuchFileException missingFile)
{
throw new MissingWelcomeFileException("Unable to find the file " + filename,missingFile);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serenitydojo.exceptions;

public class MissingWelcomeFileException extends RuntimeException {
public MissingWelcomeFileException(String message, Throwable e) {
super(message, e);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/serenitydojo/exceptions/StringProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.serenitydojo.exceptions;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class StringProcessor {

public String showLengthOf(String input) {
int length = (input == null) ? 0 : input.length();
return input + " has a length of " + length;
}


public int getPortOf(String urlAsAString) {
try {
URL url = new URL(urlAsAString);
return url.getDefaultPort();
} catch(MalformedURLException badlyWrittenException) {
throw new TestEnvironmentUnavailableException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.exceptions;

public class TestEnvironmentUnavailableException extends RuntimeException {

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

import java.util.*;

public class Catalog {

private Map<Fruit, Double> pricePerKilo = new HashMap<>();

public PriceSetter setPriceOf(Fruit fruit) {
return new PriceSetter(this, fruit);
}

public Double getPriceOf(Fruit fruit) throws FruitUnavailableException {
try
{
return pricePerKilo.get(fruit);
}
catch (NullPointerException fruitNotFound)
{
throw new FruitUnavailableException("Unable to find the fruit ",fruitNotFound);
}
}

public static class PriceSetter {
private final Catalog catalog;
private final Fruit fruit;

public PriceSetter(Catalog catalog, Fruit fruit) {
this.catalog = catalog;
this.fruit = fruit;
}

public Catalog to(Double price) {
catalog.pricePerKilo.put(fruit, price);
return catalog;
}
}
public List<String> returnCurrentlyAvailableFruit()
{
List <String> availableFruits = new ArrayList<>();
for (Map.Entry <Fruit, Double> entry:pricePerKilo.entrySet())
{
availableFruits.add(entry.getKey().toString());
}
Collections.sort(availableFruits);
return availableFruits;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/fruitmarket/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.fruitmarket;

public enum Fruit {
Apple, Orange, Banana, Pear, Peach, Strawberries, Mulberries
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serenitydojo.fruitmarket;

public class FruitUnavailableException extends RuntimeException {
public FruitUnavailableException(String message, Throwable e) {
super(message, e);
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/serenitydojo/fruitmarket/ShoppingCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.serenitydojo.fruitmarket;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
private final Catalog catalog;
private final List<ShoppingCartItem> items;

public ShoppingCart(Catalog catalog) {
this.catalog = catalog;
this.items = new ArrayList<>();
}

public void setItems(ShoppingCartItem item)
{
this.items.add(item);
}

public List<ShoppingCartItem> getItems()
{
return this.items;
}


public double calculateTotalCost()
{
double totalCost=0.0;
for (ShoppingCartItem item : this.items) {
totalCost = totalCost + item.getTotalCost();
}

return totalCost;
}
}
Loading