Skip to content

Commit 6431dc6

Browse files
committed
Completed Initial Draft
1 parent 36a797d commit 6431dc6

10 files changed

+355
-7
lines changed

Diff for: .idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/workspace.xml

+54-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: README.md

+87-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
# software-dev-course-java-basics-3
1+
# Java Basics - Loops and Conditionals
2+
3+
## Introduction
4+
In this exercise, you will use loops and conditionals to solve a series of problems.
5+
You will use provided unit tests to verify that your solutions are correct.
6+
7+
There are two sets of required exercises in this project: conditional exercises and loop exercises.
8+
You will find the exercises in the `src/main/java/org/example` folder.
9+
10+
There is also an optional set of challenges that involve extending a simple program called `JavaCafe`.
11+
You will find the JavaCafe class in the `src/main/java/org/example` folder.
12+
13+
## Conditional Exercises (Required)
14+
You will find the conditional exercises in the `src/main/java/org/example/ConditionalExercises.java` file.
15+
16+
Each method in this class is a conditional exercise. You will need to implement the method to solve the problem described in the
17+
comments within the method.
18+
19+
1. `lessThanFive(int number)` - Given a number, return true if the number is less than 5, false otherwise.
20+
2. `getAgeGroup(int age)` - Given an age, return the age group based on the following criteria:
21+
- 0-12: Child
22+
- 13-19: Teen
23+
- 20-64: Adult
24+
3. `isValidPassword(String password)` - Given a password, return true if the password is valid, false otherwise. A valid password must:
25+
- Be at least 8 characters long
26+
27+
Once you have completed the exercises, run the tests in the `src/test/java/org/example/ConditionalExercisesTests.java` file to verify that your solutions are correct.
28+
29+
To run these tests, right-click on the `ConditionalExercisesTests` class and select `Run 'ConditionalExercisesTests'`.
30+
31+
You will see the results of the tests in the `Run` tab at the bottom of the IDE. Yellow means the test failed, green means the test passed.
32+
You should see all green checkmarks if your solutions are correct.
33+
34+
![Unit Tests](doc-resources/unit-tests.png)
35+
36+
## Loop Exercises (Required)
37+
38+
You will find the loop exercises in the `src/main/java/org/example/LoopExercises.java` file.
39+
40+
Each method in this class is a loop exercise. You will need to implement the method to solve the problem described in the
41+
comments within the method.
42+
43+
1. `sum(int n)` - Given a number n, return the sum of all numbers from 1 to n.
44+
2. `sumUntilEven(int n)` - Given a number n, return the sum of all numbers from 1 to n, stopping when an even number is reached.
45+
46+
Once you have completed the exercises, run the tests in the `src/test/java/org/example/LoopExercisesTests.java` file to verify that your solutions are correct.
47+
48+
You can run these tests in the same way as the conditional exercises described above.
49+
50+
You can also run ALL tests in this project by right-clicking on the `java` folder and selecting `Run 'All Tests'`.
51+
52+
![Java](doc-resources/java.jpg)
53+
## Java Cafe Challenges (Optional)
54+
55+
A class named `JavaCafe` is included in this project to show a larger example of loops and conditionals
56+
to create a simple nested menu system for a restaurant. If you right-click on the `Main` class and select `Run 'Main'`,
57+
you can see the program in action.
58+
59+
You are encouraged to explore and review the JavaCafe class to see how loops and conditionals can be used together to create a more complex program.
60+
Notice the use of loops to create a menu that repeats until the user chooses to exit, and the use of conditionals to handle different user choices.
61+
62+
Listed below are some optional challenges that you can take on to extend the JavaCafe program. These challenges are optional and are meant to be more challenging than the exercises above.
63+
These challenges are meant to stretch your understanding and application of the concepts so far. Do not be discouraged if you find them difficult or fail
64+
to complete them. The important thing is to try and learn from the experience.
65+
66+
### Challenge #1
67+
68+
Add a new menu item to the JavaCafe class that allows the user to browse and add desserts to their order.
69+
You will need to add a new method to the JavaCafe class to handle this new menu item.
70+
71+
> [!NOTE] Imposter Syndrome
72+
> As you take on this challenge, you will likely end up solving it in a way that involves
73+
> copying or referencing code that is already present. You may even find that some of the
74+
> syntax or logic that you are copying is not code that you fully understand. This is normal!
75+
> An important part of learning to code is to sometimes work with unfamiliar code and concepts
76+
> and to learn by doing.
77+
78+
### Challenge #2
79+
80+
Add a variable to the JavaCafe class that keeps track of the total cost of the user's order.
81+
Update the program to display a cost on each item, and track the total cost as the user
82+
adds items to their order.
83+
84+
### Challenge #3
85+
86+
Add a new menu item to the JavaCafe class that allows the user to remove items from their order.
87+
Don't forget to update the total cost when an item is removed.

Diff for: pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,19 @@
1313
<maven.compiler.target>23</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>RELEASE</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.junit.jupiter</groupId>
25+
<artifactId>junit-jupiter</artifactId>
26+
<version>RELEASE</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
1630

1731
</project>

Diff for: src/main/java/org/example/ConditionalExercises.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.example;
2+
3+
public class ConditionalExercises {
4+
public boolean lessThanFive(int x) {
5+
// Replace the line below with code that returns true if x is less than 5, and false otherwise
6+
// (use an if statement with an else block.)
7+
return false;
8+
}
9+
10+
public String getAgeGroup(int age) {
11+
// Replace the line below with code that returns "child" if age is less than 13,
12+
// "teen" if age is less than 20, and "adult" otherwise
13+
// (use if, else if, and else)
14+
return "";
15+
}
16+
17+
public boolean isValidPassword(String password) {
18+
int passwordLength = password.length();
19+
20+
// Replace the line below with code that returns true if password is at least 8 characters long
21+
// and false otherwise
22+
// (use an if statement with an else block.)
23+
return false;
24+
}
25+
}

Diff for: src/main/java/org/example/JavaCafe.java

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.example;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
/**
8+
* Represents the Java Cafe program.
9+
*/
10+
public class JavaCafe {
11+
private Scanner scanner = new Scanner(System.in);
12+
public List<String> order = new ArrayList<String>();
13+
14+
/**
15+
* Runs the Java Cafe program. (Mauin menu)
16+
*/
17+
public void run() {
18+
while(true) {
19+
System.out.println("Welcome to Java Cafe!");
20+
System.out.println("1. View Food Menu");
21+
System.out.println("2. View Drink Menu");
22+
System.out.println("3. View Current Order");
23+
System.out.println("4. Exit");
24+
25+
String choice = scanner.nextLine();
26+
if (choice.equals("1")) {
27+
viewFoodMenu();
28+
} else if (choice.equals("2")) {
29+
viewDrinkMenu();
30+
} else if (choice.equals("3")) {
31+
viewCurrentOrder();
32+
} else if (choice.equals("4")) {
33+
break;
34+
} else {
35+
System.out.println("Invalid choice. Please try again.");
36+
}
37+
}
38+
}
39+
40+
/**
41+
* Displays the food menu and allows the user to add items to the order.
42+
*/
43+
public void viewFoodMenu() {
44+
while(true) {
45+
System.out.println("Food Menu");
46+
System.out.println("1. Sandwich");
47+
System.out.println("2. Salad");
48+
System.out.println("3. Soup");
49+
System.out.println("4. Return to Main Menu");
50+
51+
String choice = scanner.nextLine();
52+
if (choice.equals("1")) {
53+
order.add("Sandwich");
54+
} else if (choice.equals("2")) {
55+
order.add("Salad");
56+
} else if (choice.equals("3")) {
57+
order.add("Soup");
58+
} else if (choice.equals("4")) {
59+
break;
60+
} else {
61+
System.out.println("Invalid choice. Please try again.");
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Displays the drink menu and allows the user to add items to the order.
68+
*/
69+
public void viewDrinkMenu() {
70+
while(true) {
71+
System.out.println("Drink Menu");
72+
System.out.println("1. Coffee");
73+
System.out.println("2. Tea");
74+
System.out.println("3. Juice");
75+
System.out.println("4. Return to Main Menu");
76+
77+
String choice = scanner.nextLine();
78+
if (choice.equals("1")) {
79+
order.add("Coffee");
80+
} else if (choice.equals("2")) {
81+
order.add("Tea");
82+
} else if (choice.equals("3")) {
83+
order.add("Juice");
84+
} else if (choice.equals("4")) {
85+
break;
86+
} else {
87+
System.out.println("Invalid choice. Please try again.");
88+
}
89+
}
90+
}
91+
92+
/**
93+
* Displays the current order.
94+
*/
95+
public void viewCurrentOrder() {
96+
System.out.println("Current Order:");
97+
for (String item : order) {
98+
System.out.println(item);
99+
}
100+
}
101+
}

Diff for: src/main/java/org/example/LoopExercises.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example;
2+
3+
public class LoopExercises {
4+
public int sum(int n) {
5+
// Replace the line below with code that returns the sum of the numbers from 1 to n
6+
// (use a for loop)
7+
return 0;
8+
}
9+
10+
public int sumUntilEven(int n) {
11+
// Replace the line below with code that returns the sum of the numbers from 1 to n
12+
// but stops adding when the sum is even
13+
// (use a while loop with a sum variable and a counter variable)
14+
return 0;
15+
}
16+
}

Diff for: src/main/java/org/example/Main.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public class Main {
44
public static void main(String[] args) {
5-
System.out.println("Hello world!");
5+
JavaCafe javaCafe = new JavaCafe();
6+
javaCafe.run();
67
}
78
}

Diff for: src/test/java/ConditionalExercisesTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.example.ConditionalExercises;
2+
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import static org.junit.Assert.*;
5+
6+
public class ConditionalExercisesTests {
7+
@Test
8+
public void testLessThanFive() {
9+
ConditionalExercises exercises = new ConditionalExercises();
10+
assertTrue(exercises.lessThanFive(4));
11+
assertFalse(exercises.lessThanFive(5));
12+
assertFalse(exercises.lessThanFive(6));
13+
}
14+
15+
@Test
16+
public void testGetAgeGroup() {
17+
ConditionalExercises exercises = new ConditionalExercises();
18+
assertEquals("child", exercises.getAgeGroup(12));
19+
assertEquals("teen", exercises.getAgeGroup(15));
20+
assertEquals("adult", exercises.getAgeGroup(20));
21+
}
22+
23+
@Test
24+
public void testIsValidPassword() {
25+
ConditionalExercises exercises = new ConditionalExercises();
26+
assertFalse(exercises.isValidPassword("1234567"));
27+
assertTrue(exercises.isValidPassword("12345678"));
28+
assertTrue(exercises.isValidPassword("123456789"));
29+
}
30+
}

0 commit comments

Comments
 (0)