Skip to content

Commit ca0b09d

Browse files
authored
Merge branch 'code-differently:main' into lesson_16
2 parents d70085b + 5f02591 commit ca0b09d

File tree

23 files changed

+962
-1
lines changed

23 files changed

+962
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.footballteamobject;
2+
3+
public class DuplicatePlayerException extends Exception {
4+
public DuplicatePlayerException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.footballteamobject;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class FootballTeam {
7+
8+
public static class DuplicatePlayerException extends Exception {
9+
public DuplicatePlayerException(String message) {
10+
super(message);
11+
}
12+
}
13+
14+
private int playerCount;
15+
private int jerseyNumber50AndOver;
16+
private int jerseyNumberUnder50;
17+
private Map<Integer, String> players = new HashMap<>();
18+
private final String teamName;
19+
private final String location;
20+
private final Conference conference;
21+
22+
public FootballTeam(
23+
String teamName, String location, Conference conference, Map<Integer, String> players) {
24+
this.teamName = teamName;
25+
this.location = location;
26+
this.conference = conference;
27+
this.players = players;
28+
}
29+
30+
public enum Conference {
31+
// Power Five Conferences
32+
ACC,
33+
BIG_12,
34+
BIG_10,
35+
PAC_12,
36+
SEC,
37+
// Group of Five Conferences
38+
AMERICAN,
39+
CONFERENCE_USA,
40+
MAC,
41+
MOUNTAIN_WEST,
42+
SUN_BELT
43+
}
44+
45+
public boolean isPowerFive() {
46+
if (conference == Conference.ACC
47+
|| conference == Conference.BIG_12
48+
|| conference == Conference.BIG_10
49+
|| conference == Conference.PAC_12
50+
|| conference == Conference.SEC) {
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
public String getTeamName() {
57+
return teamName;
58+
}
59+
60+
public String getLocation() {
61+
return location;
62+
}
63+
64+
public Conference getConference() {
65+
return conference;
66+
}
67+
68+
public int getPlayerCount() {
69+
return playerCount;
70+
}
71+
72+
public Map<Integer, String> getPlayers() {
73+
return players;
74+
}
75+
76+
public void addPlayer(int number, String name) throws DuplicatePlayerException {
77+
if (players.containsKey(number)) {
78+
throw new DuplicatePlayerException("A player with that number already exists.");
79+
}
80+
players.put(number, name);
81+
playerCount++;
82+
}
83+
84+
public void removePlayer(int number) {
85+
players.remove(number);
86+
playerCount--;
87+
}
88+
89+
public void tallyJerseyNumbers() {
90+
for (Map.Entry<Integer, String> entry : players.entrySet()) {
91+
if (entry.getKey() < 50) {
92+
jerseyNumberUnder50++;
93+
} else {
94+
jerseyNumber50AndOver++;
95+
}
96+
}
97+
}
98+
99+
public int getJerseyNumber50AndOver() {
100+
return jerseyNumber50AndOver;
101+
}
102+
103+
public int getJerseyNumberUnder50() {
104+
return jerseyNumberUnder50;
105+
}
106+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
public enum Colors {
4+
GREEN,
5+
RED,
6+
PINK
7+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
import java.util.HashMap;
4+
import java.util.Random;
5+
6+
public class GumBallMachine {
7+
private Colors colors; // Current color of the gumball
8+
private int gumBallCount; // Count of gumballs in the machine
9+
private boolean isBroken; // Status of the machine
10+
private HashMap<Colors, Integer> gumball; // Collection of gumballs
11+
private Random rand; // Random object for color selection
12+
13+
// Constructor
14+
public GumBallMachine(int gumBallCount, boolean isBroken) {
15+
this.colors = Colors.GREEN; // Default color
16+
this.gumBallCount = gumBallCount;
17+
this.isBroken = isBroken;
18+
this.gumball = new HashMap<>();
19+
this.rand = new Random();
20+
}
21+
22+
public Colors getCurrentColor() {
23+
return this.colors;
24+
}
25+
26+
public void setRandomColors() {
27+
Colors[] colorArray = Colors.values();
28+
int randIndex = rand.nextInt(colorArray.length);
29+
this.colors = colorArray[randIndex]; // Set a random color
30+
}
31+
32+
public int getGumBallCount() {
33+
return this.gumBallCount;
34+
}
35+
36+
public void dispenseGumBall(double quarter) throws invalidCoinInsertedException {
37+
if (quarter != 0.25) {
38+
throw new invalidCoinInsertedException("You need a quarter!");
39+
}
40+
if (gumBallCount > 0 && !isBroken) {
41+
gumBallCount--; // Dispense a gumball
42+
} else if (gumBallCount == 0) {
43+
throw new invalidCoinInsertedException("No more gumballs!");
44+
} else {
45+
throw new invalidCoinInsertedException("Machine is broken!");
46+
}
47+
}
48+
49+
public boolean isBroken() {
50+
return isBroken;
51+
}
52+
53+
public void breakMachine() {
54+
isBroken = true;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
public class invalidCoinInsertedException extends Exception {
4+
public invalidCoinInsertedException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codedifferently.lesson16.hummadtanweer;
2+
3+
import java.util.ArrayList;
4+
5+
public class Person {
6+
private final String name;
7+
private final int age;
8+
private final Gender gender;
9+
private final ArrayList<String> hobbies;
10+
private final String email;
11+
private final int MAX_HOBBIES = 2;
12+
13+
public class HobbyLimitExceededException extends Exception {
14+
public HobbyLimitExceededException(String message) {
15+
super(message);
16+
}
17+
}
18+
19+
enum Gender {
20+
MALE,
21+
FEMALE,
22+
OTHER
23+
}
24+
25+
public Person(String name, int age, Gender gender, String email) {
26+
this.name = name;
27+
this.age = age;
28+
this.gender = gender;
29+
this.email = email;
30+
this.hobbies = new ArrayList<>();
31+
}
32+
33+
public void addHobby(String hobby) throws HobbyLimitExceededException {
34+
if (hobbies.size() >= MAX_HOBBIES) {
35+
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
36+
}
37+
hobbies.add(hobby);
38+
}
39+
40+
public ArrayList<String> getHobbies() {
41+
return hobbies;
42+
}
43+
44+
public boolean isAdult() {
45+
return age >= 18;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getEmail() {
53+
return email;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.lunch_app;
2+
3+
public class InvalidCalorieException extends Exception {
4+
public InvalidCalorieException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.codedifferently.lesson16.lunch_app;
2+
3+
import java.util.ArrayList;
4+
5+
public class Lunch {
6+
7+
public enum LunchType {
8+
VEGETARIAN,
9+
NON_VEGETARIAN,
10+
VEGAN,
11+
GLUTEN_FREE
12+
}
13+
14+
// Member variables
15+
private final String mainDish; // String type
16+
private final String sideDish; // String type
17+
private final int calories; // Integer type
18+
private final LunchType lunchType; // Enum type
19+
private final ArrayList<String> drinks; // Collection type (ArrayList)
20+
21+
// Constructor
22+
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType)
23+
throws InvalidCalorieException {
24+
if (calories <= 0) {
25+
throw new InvalidCalorieException("Calories cannot be zero or negative.");
26+
}
27+
this.mainDish = mainDish;
28+
this.sideDish = sideDish;
29+
this.calories = calories;
30+
this.lunchType = lunchType;
31+
this.drinks = new ArrayList<>();
32+
}
33+
34+
// Member function to add a drink
35+
public void addDrink(String drink) {
36+
drinks.add(drink);
37+
}
38+
39+
// Member function to get a healthy message based on calories
40+
public String getHealthMessage() {
41+
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories.";
42+
}
43+
44+
// Member function to display all drinks
45+
public void displayDrinks() {
46+
47+
System.out.println("Available drinks:");
48+
for (String drink : drinks) {
49+
System.out.println("- " + drink);
50+
}
51+
}
52+
53+
// Getters for member variables (optional)
54+
public String getMainDish() {
55+
return mainDish;
56+
}
57+
58+
public String getSideDish() {
59+
return sideDish;
60+
}
61+
62+
public int getCalories() {
63+
return calories;
64+
}
65+
66+
public LunchType getLunchType() {
67+
return lunchType;
68+
}
69+
70+
public ArrayList<String> getDrinks() {
71+
return drinks;
72+
}
73+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
public enum BuyType {
4+
BONUS_BUY,
5+
DOUBLE_CHANCE,
6+
NORMAL_BUY,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
public class InvalidPayAmountException extends Exception {
4+
5+
public InvalidPayAmountException(String message) {
6+
super(message);
7+
}
8+
}

0 commit comments

Comments
 (0)