Skip to content

Commit 8078fd9

Browse files
committed
Upload Part08.PrintMeAnotherHashMap
1 parent 4f1ea68 commit 8078fd9

File tree

9 files changed

+259
-0
lines changed

9 files changed

+259
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part08_09.PrintMeAnotherHashmap</artifactId>
6+
<name>Part08_09.PrintMeAnotherHashmap</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
public class Book {
3+
4+
private String name;
5+
private String contents;
6+
private int publicationYear;
7+
8+
public Book(String name, int publicationYear, String contents) {
9+
this.name = name;
10+
this.publicationYear = publicationYear;
11+
this.contents = contents;
12+
}
13+
14+
public String getName() {
15+
return this.name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public int getPublicationYear() {
23+
return this.publicationYear;
24+
}
25+
26+
public void setPublicationYear(int publicationYear) {
27+
this.publicationYear = publicationYear;
28+
}
29+
30+
public String getContents() {
31+
return this.contents;
32+
}
33+
34+
public void setContents(String contents) {
35+
this.contents = contents;
36+
}
37+
38+
public String toString() {
39+
return "Name: " + this.name + " (" + this.publicationYear + ")\n"
40+
+ "Contents: " + this.contents;
41+
}
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import java.util.HashMap;
3+
4+
public class Program {
5+
6+
public static void main(String[] args) {
7+
8+
// The exercise template contains the already familiar classes Book and Program.
9+
// In the class Program implement the following class methods:
10+
// public static void printValues(HashMap<String,Book> hashmap),
11+
// which prints all the values in the hashmap given as a parameter using the toString method of the Book objects.
12+
// public static void printValueIfNameContains(HashMap<String,Book> hashmap, String text),
13+
// which prints only the Books in the given hashmap which name contains the given string.
14+
HashMap<String, Book> hashmap = new HashMap<>();
15+
hashmap.put("sense", new Book("Sense and Sensibility", 1811, "..."));
16+
hashmap.put("prejudice", new Book("Pride and prejudice", 1813, "...."));
17+
18+
printValues(hashmap);
19+
System.out.println("---");
20+
printValueIfNameContains(hashmap, "prejud");
21+
}
22+
23+
public static String cleanString(String text) {
24+
if (text == null) {
25+
return "";
26+
}
27+
28+
text = text.toLowerCase();
29+
return text.trim();
30+
}
31+
32+
public static void printValues(HashMap<String, Book> hashmap) {
33+
for (Book book : hashmap.values()) {
34+
System.out.println(book);
35+
}
36+
}
37+
38+
public static void printValueIfNameContains(HashMap<String, Book> hashmap, String text) {
39+
String name = cleanString(text);
40+
41+
for (Book book : hashmap.values()) {
42+
if (book.getName().contains(name)) {
43+
System.out.println(book);
44+
}
45+
}
46+
47+
}
48+
49+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.Reflex;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
12+
@Points("08-09")
13+
public class PrintMeAnotherHashmapTest {
14+
15+
@Rule
16+
public MockStdio io = new MockStdio();
17+
18+
@Test
19+
public void printValuesTest() throws Throwable {
20+
Reflex.reflect(Program.class).staticMethod("printValues").returningVoid().taking(HashMap.class).requirePublic();
21+
HashMap<String, Book> testHashmap = makeAHashMap();
22+
Reflex.reflect(Program.class).staticMethod("printValues").returningVoid().taking(HashMap.class).invoke(testHashmap);
23+
24+
String koodi = "HashMap<String, Book> hashmap = new HashMap<>();\n"
25+
+ "hashmap.put(\"sense\", new Book(\"Sensibility\", 1811, \"...\"));\n"
26+
+ "hashmap.put(\"prejudice\", new Book(\"pride\", 1813, \"....\"));\n"
27+
+ "hashmap.put(\"happy\", new Book(\"Don't let the pigeon drive the bus\", 2003, \"....\"));\n"
28+
+ "printValues(hashmap);";
29+
30+
for (String s : Arrays.asList("Sensibility", "pride", "Don't let the pigeon drive the bus", "1811", "1813", "2003")) {
31+
assertTrue("Output not correct. Try:\n" + koodi, io.getSysOut().contains(s));
32+
}
33+
34+
for (String s : Arrays.asList("sense", "prejudice", "happy")) {
35+
assertFalse("Output not correct. Try:\n" + koodi, io.getSysOut().contains(s));
36+
}
37+
}
38+
39+
@Test
40+
public void printValueIfNameContainsTest2() throws Throwable {
41+
Reflex.reflect(Program.class).staticMethod("printValueIfNameContains").returningVoid().taking(HashMap.class, String.class).requirePublic();
42+
HashMap<String, Book> testHashmap = makeAHashMap();
43+
Reflex.reflect(Program.class).staticMethod("printValueIfNameContains").returningVoid().taking(HashMap.class, String.class).invoke(testHashmap, "ide");
44+
String codeToTest = "HashMap<String, Book> hashmap = new HashMap<>();\n"
45+
+ "hashmap.put(\"sense\", new Book(\"Sensibility\", 1811, \"...\"));\n"
46+
+ "hashmap.put(\"prejudice\", new Book(\"pride\", 1813, \"....\"));\n"
47+
+ "hashmap.put(\"happy\", new Book(\"Don't let the pigeon drive the bus\", 2003, \"....\"));\n"
48+
+ "printValueIfNameContains(hashmap, \"ide\");";
49+
50+
for (String s : Arrays.asList("pride", "1813")) {
51+
assertTrue("Output not correct. Try:\n" + codeToTest, io.getSysOut().contains(s));
52+
}
53+
54+
for (String s : Arrays.asList("sense", "prejudice", "happy", "ensibility", "Don't let the pigeon drive the bus", "1811", "2003")) {
55+
assertFalse("Output not correct. Try:\n" + codeToTest, io.getSysOut().contains(s));
56+
}
57+
}
58+
59+
@Test
60+
public void printValueIfNameContains2() throws Throwable {
61+
Reflex.reflect(Program.class).staticMethod("printValueIfNameContains").returningVoid().taking(HashMap.class, String.class).requirePublic();
62+
HashMap<String, Book> testHashmap = makeAHashMap();
63+
Reflex.reflect(Program.class).staticMethod("printValueIfNameContains").returningVoid().taking(HashMap.class, String.class).invoke(testHashmap, "p");
64+
65+
String codeToTest = "HashMap<String, Book> hashmap = new HashMap<>();\n"
66+
+ "hashmap.put(\"sense\", new Book(\"Sensibility\", 1811, \"...\"));\n"
67+
+ "hashmap.put(\"prejudice\", new Book(\"pride\", 1813, \"....\"));\n"
68+
+ "hashmap.put(\"happy\", new Book(\"Don't let the pigeon drive the bus\", 2003, \"....\"));\n"
69+
+ "printValueIfNameContains(hashmap, \"p\");";
70+
71+
for (String s : Arrays.asList("pride", "Don't let the pigeon drive the bus", "1813", "2003")) {
72+
assertTrue("Output not correct. Try:\n" + codeToTest, io.getSysOut().contains(s));
73+
}
74+
75+
for (String s : Arrays.asList("Sensibility", "1811", "sense", "prejudice", "happy")) {
76+
assertFalse("Output not correct. Try:\n" + codeToTest, io.getSysOut().contains(s));
77+
}
78+
}
79+
80+
private static HashMap<String, Book> makeAHashMap() {
81+
HashMap<String, Book> hashmap = new HashMap<>();
82+
hashmap.put("sense", new Book("Sensibility", 1811, "..."));
83+
hashmap.put("prejudice", new Book("pride", 1813, "...."));
84+
hashmap.put("happy", new Book("Don't let the pigeon drive the bus", 2003, "...."));
85+
86+
return hashmap;
87+
}
88+
89+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Program.class
2+
Book.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
C:\Users\llasantinelli\Documents\NetBeansProjects\mooc-java-programming-ii\part08-Part08_09.PrintMeAnotherHashmap\src\main\java\Program.java
2+
C:\Users\llasantinelli\Documents\NetBeansProjects\mooc-java-programming-ii\part08-Part08_09.PrintMeAnotherHashmap\src\main\java\Book.java

0 commit comments

Comments
 (0)