Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bf6dc2

Browse files
Add Part06
Merge pull request #4 from SeaHuyty/branch2203
2 parents a33ccd0 + 2842147 commit 0bf6dc2

File tree

111 files changed

+5310
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+5310
-0
lines changed

‎part06-Part06_01.Menu/.tmcproject.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
force_new_sandbox: true

‎part06-Part06_01.Menu/pom.xml

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>Part06_01.Menu</artifactId>
6+
<name>Part06_01.Menu</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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
public class Main {
3+
public static void main(String[] args) {
4+
Menu exactum = new Menu();
5+
6+
// When you have completed the method addMeal(String meal)
7+
// You can delete the comments below
8+
9+
10+
// exactum.addMeal("Smoked salmon, white wine and butter sauce with basil");
11+
// exactum.addMeal("Seasonal green salad with apple-honey vinegarette");
12+
// exactum.addMeal("Roasted lamb in a red wine sauce");
13+
14+
// When you have completed the method printMeals()
15+
// you can remove the comment below
16+
17+
// exactum.printMeals();
18+
19+
// When you have completed the method clearMenu()
20+
// you can remove the comments below
21+
// exactum.clearMenu();
22+
// exactum.printMeals();
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import java.util.ArrayList;
3+
4+
public class Menu {
5+
6+
private ArrayList<String> meals;
7+
8+
public Menu() {
9+
this.meals = new ArrayList<>();
10+
}
11+
12+
// implement the required methods here
13+
public void addMeal(String meal) {
14+
if (!this.meals.contains(meal)) {
15+
this.meals.add(meal);
16+
}
17+
}
18+
19+
public void printMeals() {
20+
for (String meal: this.meals) {
21+
System.out.println(meal);
22+
}
23+
}
24+
25+
public void clearMenu() {
26+
this.meals.clear();
27+
}
28+
29+
}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
5+
import fi.helsinki.cs.tmc.edutestutils.Reflex;
6+
import java.lang.reflect.Field;
7+
import java.lang.reflect.Method;
8+
import java.util.ArrayList;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
11+
import org.junit.*;
12+
import static org.junit.Assert.*;
13+
14+
public class MenuTest {
15+
16+
@Rule
17+
public MockStdio stdio = new MockStdio();
18+
19+
@Test
20+
@Points("06-01.1")
21+
public void methodAddMealExists() throws Throwable {
22+
String klassName = "Menu";
23+
24+
String method = "addMeal";
25+
26+
Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName);
27+
Object object = productClass.constructor().takingNoParams().invoke();
28+
29+
assertTrue("implement a method public void " + method + "(String meal) for the class " + klassName, productClass.method(object, method)
30+
.returningVoid().taking(String.class).isPublic());
31+
32+
String v = "\nThe code that caused the error: rl = new Menu(); rl.addMeal(\"Bratwurst\");";
33+
34+
productClass.method(object, method)
35+
.returningVoid().taking(String.class).withNiceError(v).invoke("Bratwurst");
36+
}
37+
38+
@Test
39+
@Points("06-01.1")
40+
public void addMealAddsANewMeal() {
41+
Field mealsField = null;
42+
try {
43+
mealsField = Menu.class.getDeclaredField("meals");
44+
} catch (NoSuchFieldException ex) {
45+
fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;");
46+
}
47+
48+
Menu menu = new Menu();
49+
mealsField.setAccessible(true);
50+
51+
Method m = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class);
52+
53+
try {
54+
ReflectionUtils.invokeMethod(void.class, m, menu, "first");
55+
} catch (Throwable ex) {
56+
fail("Make sure that the methof addMeal is of type void, i.e. doesn't return a value.");
57+
}
58+
try {
59+
ArrayList<String> meals = (ArrayList<String>) mealsField.get(menu);
60+
if (meals.size() != 1) {
61+
fail("Calling the addMeal method of Menu should add a meal to the meals list.");
62+
}
63+
try {
64+
ReflectionUtils.invokeMethod(void.class, m, menu, "second");
65+
} catch (Throwable ex) {
66+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
67+
68+
}
69+
if (meals.size() != 2) {
70+
fail("When two meals with different names are added, there should be two meals on the list.");
71+
}
72+
} catch (IllegalArgumentException ex) {
73+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
74+
} catch (IllegalAccessException ex) {
75+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
76+
}
77+
}
78+
79+
@Test
80+
@Points("06-01.1")
81+
public void mealsWithSameNameAreOnlyAddedOnce() {
82+
Field mealsField = null;
83+
try {
84+
mealsField = Menu.class.getDeclaredField("meals");
85+
} catch (NoSuchFieldException ex) {
86+
fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;");
87+
}
88+
89+
mealsField.setAccessible(true);
90+
91+
Menu menu = new Menu();
92+
Method m = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class);
93+
try {
94+
ReflectionUtils.invokeMethod(void.class, m, menu, "first");
95+
ReflectionUtils.invokeMethod(void.class, m, menu, "first");
96+
} catch (Throwable ex) {
97+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
98+
}
99+
100+
ArrayList<String> meals;
101+
try {
102+
meals = (ArrayList<String>) mealsField.get(menu);
103+
if (meals.size() != 1) {
104+
fail("A particular food must only appear once on the menu.");
105+
}
106+
} catch (IllegalArgumentException ex) {
107+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
108+
} catch (IllegalAccessException ex) {
109+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
110+
}
111+
112+
}
113+
114+
@Test
115+
@Points("06-01.2")
116+
public void methodPrintMealsExists() throws Throwable {
117+
String klassName = "Menu";
118+
119+
String method = "printMeals";
120+
121+
Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName);
122+
Object object = productClass.constructor().takingNoParams().invoke();
123+
124+
assertTrue("implement a method public void " + method + "() for the class " + klassName, productClass.method(object, method)
125+
.returningVoid().takingNoParams().isPublic());
126+
127+
String v = "\nThe code that caused the error: rl = new Menu(); rl.printMeals();";
128+
129+
productClass.method(object, method)
130+
.returningVoid().takingNoParams().withNiceError(v).invoke();
131+
}
132+
133+
@Test
134+
@Points("06-01.2")
135+
public void printMealsPrintsTheMenu() {
136+
String carrotSoup = "Le carrot soup";
137+
String porkStew = "Pork stew";
138+
139+
Menu menu = new Menu();
140+
141+
Method addMeal = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class);
142+
143+
try {
144+
ReflectionUtils.invokeMethod(void.class, addMeal, menu, carrotSoup);
145+
ReflectionUtils.invokeMethod(void.class, addMeal, menu, porkStew);
146+
} catch (Throwable ex) {
147+
fail("Make sure that adding a meal works for a new menu.");
148+
}
149+
150+
Method m = ReflectionUtils.requireMethod(Menu.class, "printMeals");
151+
try {
152+
ReflectionUtils.invokeMethod(void.class, m, menu);
153+
} catch (Throwable ex) {
154+
fail("Make sure that printing the meals works when there are more than one meal.");
155+
}
156+
157+
String out = stdio.getSysOut();
158+
assertTrue("Printing the meals should print all the added meals.", out.contains(carrotSoup) && out.contains(porkStew));
159+
assertTrue("Each meal should be printed on its own line. Currently the output is:" + out, out.split("\n").length > 1);
160+
161+
}
162+
163+
@Test
164+
@Points("06-01.2")
165+
public void methodClearMenuExists() throws Throwable {
166+
String klassName = "Menu";
167+
168+
String method = "clearMenu";
169+
170+
Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName);
171+
Object object = productClass.constructor().takingNoParams().invoke();
172+
173+
assertTrue("implement a method public void " + method + "() for the class " + klassName, productClass.method(object, method)
174+
.returningVoid().takingNoParams().isPublic());
175+
176+
String v = "\nThe code that caused the error: rl = new Menu(); rl.clearMenu();";
177+
178+
productClass.method(object, method)
179+
.returningVoid().takingNoParams().withNiceError(v).invoke();
180+
}
181+
182+
@Test
183+
@Points("06-01.3")
184+
public void clearingTheMenuClearsTheMenu() {
185+
Field mealsField = null;
186+
try {
187+
mealsField = Menu.class.getDeclaredField("meals");
188+
} catch (NoSuchFieldException ex) {
189+
fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;");
190+
}
191+
192+
mealsField.setAccessible(true);
193+
194+
Menu menu = new Menu();
195+
Method addMeal = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class);
196+
197+
try {
198+
ReflectionUtils.invokeMethod(void.class, addMeal, menu, "first");
199+
ReflectionUtils.invokeMethod(void.class, addMeal, menu, "second");
200+
} catch (Throwable ex) {
201+
fail("Make sure that the method addMeal is of type void, i.e. doesn't return a value.");
202+
}
203+
204+
Method clear = ReflectionUtils.requireMethod(Menu.class, "clearMenu");
205+
try {
206+
ReflectionUtils.invokeMethod(void.class, clear, menu);
207+
} catch (Throwable ex) {
208+
fail("Make sure the the method clearMenu is of type void, i.e. doesn't return a value. Also, make sure it works as intended.");
209+
}
210+
211+
try {
212+
ArrayList<String> meals = (ArrayList<String>) mealsField.get(menu);
213+
if (meals == null) {
214+
fail("Do not empty the menu by setting the value of the meals variable to null.");
215+
}
216+
217+
if (!meals.isEmpty()) {
218+
fail("The menu should be empty after calling the method clearMenu.");
219+
}
220+
} catch (IllegalArgumentException ex) {
221+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
222+
} catch (IllegalAccessException ex) {
223+
Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex);
224+
}
225+
226+
Method m = ReflectionUtils.requireMethod(Menu.class, "printMeals");
227+
try {
228+
ReflectionUtils.invokeMethod(void.class, m, menu);
229+
} catch (Throwable ex) {
230+
fail("Make sure that printing the meals works when there are more than one meal.");
231+
}
232+
233+
String out = stdio.getSysOut();
234+
out = out.trim();
235+
if (!out.isEmpty()) {
236+
fail("There should be no output when printing an empty menu.");
237+
}
238+
}
239+
}
411 Bytes
Binary file not shown.
1.13 KB
Binary file not shown.
Binary file not shown.

‎part06-Part06_02.Stack/.tmcproject.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
force_new_sandbox: true

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /