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

Simplify code for Records, only convert Bagel class #145

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,6 @@ public void testBagelGetters()
assertEquals(bagel1.spreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
public void testCookieRecord()
{
Cookie cookie2 = new Cookie(CHOCOLATE_CHIP, true);
assertTrue(Cookie.class.isRecord());
assertEquals(cookie1, cookie2);
assertEquals("Cookie[cookieType=CHOCOLATE_CHIP, warmed=true]", cookie1.toString());
}

@Test
public void testCookieGetters()
{
assertTrue(cookie1.warmed());
assertEquals(cookie1.cookieType(), CHOCOLATE_CHIP);
}

@Test
public void testDonutRecord()
{
Donut donut2 = new Donut(GLAZED);
assertTrue(Donut.class.isRecord());
assertEquals(donut1.donutType(), GLAZED);
assertEquals(donut1, donut2);
assertEquals("Donut[donutType=GLAZED]", donut1.toString());
}

@Test
public void testDonutGetters()
{
assertEquals(donut1.donutType(), GLAZED);
}

@Test
public void testSealedClasses()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,10 @@

package bnymellon.codekatas.coffeeshopkata.food;

import java.util.Objects;

/**
* Replace the entire class with a Record.
* This replaces the need for a constructor, getters, toString(),
* equals() and hashcode() method
* NOTE: This example highlights the usage of a record to replace the
* boilerplate of a plain Java class.
*
* @see <a href="https://openjdk.org/jeps/395">...</a>
*/
// TODO: convert class to record
public class Cookie implements BakeryItem
{
private final CookieType cookieType;
private final boolean warmed;

public Cookie(CookieType cookieType, boolean warmed)
{
this.cookieType = cookieType;
this.warmed = warmed;
}
public record Cookie(CookieType cookieType, boolean warmed) implements BakeryItem {

@Override
public double getPrice()
{
public double getPrice() {
return 1.25;
}

public boolean isWarmed()
{
return warmed;
}

public CookieType getCookieType()
{
return cookieType;
}

@Override
public boolean equals(Object obj)
{
if (obj instanceof Cookie) {
Cookie cookie = (Cookie) obj;
return this.isWarmed() == cookie.isWarmed() && this.getCookieType() == cookie.getCookieType();
}
return false;
}

@Override
public String toString()
{
return String.format("Cookie[cookieType=%s, warmed=%s]",
this.getCookieType(), this.isWarmed());
}

@Override
public int hashCode()
{
return Objects.hash(cookieType, warmed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,10 @@

package bnymellon.codekatas.coffeeshopkata.food;

import java.util.Objects;

/**
* Replace the entire class with a Record.
* This replaces the need for a constructor, getters, toString(),
* equals() and hashcode() method
* NOTE: This example highlights the usage of a record to replace the
* boilerplate of a plain Java class.
*
* @see <a href="https://openjdk.org/jeps/395">...</a>
*/
// TODO: convert class to record
public class Donut implements BakeryItem
{
private final DonutType donutType;

public Donut(DonutType donutType)
{
this.donutType = donutType;
}
public record Donut(DonutType donutType) implements BakeryItem {

@Override
public double getPrice()
{
public double getPrice() {
return 1.75;
}

public DonutType getDonutType()
{
return this.donutType;
}

@Override
public boolean equals(Object obj)
{
if (obj instanceof Donut) {
Donut donut = (Donut) obj;
return this.getDonutType() == donut.getDonutType();
}
return false;
}

@Override
public String toString()
{
return String.format("Donut[donutType=%s]",
this.getDonutType());
}

@Override
public int hashCode()
{
return Objects.hash(donutType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,41 +100,6 @@ public void testBagelGetters()
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
public void testCookieRecord()
{
// TODO: Convert Cookie to record
Cookie cookie2 = new Cookie(CHOCOLATE_CHIP, true);
assertTrue(Cookie.class.isRecord());
assertEquals(cookie1, cookie2);
assertEquals("Cookie[cookieType=CHOCOLATE_CHIP, warmed=true]", cookie1.toString());
}

@Test
public void testCookieGetters()
{
// TODO: Convert assertions to use Record getters
assertTrue(cookie1.isWarmed());
assertEquals(cookie1.getCookieType(), CHOCOLATE_CHIP);
}

@Test
public void testDonutRecord()
{
// TODO: Convert Donut to record
Donut donut2 = new Donut(GLAZED);
assertTrue(Donut.class.isRecord());
assertEquals(donut1, donut2);
assertEquals("Donut[donutType=GLAZED]", donut1.toString());
}

@Test
public void testDonutGetters()
{
// TODO: Convert assertions to use Record getters
assertEquals(donut1.getDonutType(), GLAZED);
}

@Test
public void testSealedClasses()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,6 @@ public void testBagelGetters()
assertEquals(bagel1.getSpreadType(), HERB_GARLIC_CREAM_CHEESE);
}

@Test
public void testCookieClass()
{
Cookie cookie2 = new Cookie(CHOCOLATE_CHIP, true);
assertEquals(cookie1, cookie2);
assertEquals("Cookie[cookieType=CHOCOLATE_CHIP, warmed=true]", cookie1.toString());
}

@Test
public void testCookieGetters()
{
assertTrue(cookie1.isWarmed());
assertEquals(cookie1.getCookieType(), CHOCOLATE_CHIP);
}

@Test
public void testDonutClass()
{
Donut donut2 = new Donut(GLAZED);
assertEquals(donut1, donut2);
assertEquals("Donut[donutType=GLAZED]", donut1.toString());
}

@Test
public void testDonutGetters()
{
assertEquals(donut1.getDonutType(), GLAZED);
}

@Test
public void getDrinkItems()
{
Expand Down