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

Mekhi lesson07 #213

Closed
wants to merge 3 commits into from
Closed
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
@@ -0,0 +1,95 @@
package com.codedifferently.lesson7.MekhiWilliams;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication(scanBasePackages = "com.codedifferently")
public class MekhiWilliams07 {

public static void main(String[] args) {
var application = new SpringApplication(MekhiWilliams07.class);
application.run(args);

// Instantiate and use Sneaker class for demonstration
Sneaker sneaker = new Sneaker("Nike", "Air Max", 10.5, Sneaker.SneakerType.RUNNING, false);
sneaker.displaySneakerDetails();
}
}

// Custom class representing a sneaker
class Sneaker {
// Enum for different sneaker types
public enum SneakerType {
RUNNING,
BASKETBALL,
CASUAL
}

// Member variables
private String brand;
private String model;
private double size;
private SneakerType type;
private boolean isLimitedEdition;

// Constructor
public Sneaker(
String brand, String model, double size, SneakerType type, boolean isLimitedEdition) {
this.brand = brand;
this.model = model;
this.size = size;
this.type = type;
this.isLimitedEdition = isLimitedEdition;
}

// Getter methods
public String getBrand() {
return brand;
}

public String getModel() {
return model;
}

public double getSize() {
return size;
}

public SneakerType getType() {
return type;
}

public boolean isLimitedEdition() {
return isLimitedEdition;
}

// Function using conditional expression
public String getPopularity() {
return isLimitedEdition ? "Limited Edition" : "Regular Edition";
}

// Function using loop
public void displaySneakerDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Size: " + size);
System.out.println("Type: " + type);
System.out.println("Edition: " + getPopularity());
}

// Function throwing custom exception
public void validateSize() throws InvalidSizeException {
if (size <= 0) {
throw new InvalidSizeException("Size should be greater than 0");
}
}

// Custom exception class
public static class InvalidSizeException extends Exception {
public InvalidSizeException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.codedifferently.lesson7.MekhiWilliams;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class MekhiWilliams07Test {

@Test
void testSneakerDetails() {
Sneaker sneaker = new Sneaker("Nike", "Air Max", 10.5, Sneaker.SneakerType.RUNNING, false);
assertEquals("Nike", sneaker.getBrand());
assertEquals("Air Max", sneaker.getModel());
assertEquals(10.5, sneaker.getSize());
assertEquals(Sneaker.SneakerType.RUNNING, sneaker.getType());
assertEquals(false, sneaker.isLimitedEdition());
}

@Test
void testPopularity() {
Sneaker sneaker1 = new Sneaker("Adidas", "Yeezy", 9.5, Sneaker.SneakerType.CASUAL, true);
Sneaker sneaker2 = new Sneaker("Nike", "Air Force 1", 11.0, Sneaker.SneakerType.CASUAL, false);

assertEquals("Limited Edition", sneaker1.getPopularity());
assertEquals("Regular Edition", sneaker2.getPopularity());
}

@Test
void testValidateSize() {
Sneaker sneaker = new Sneaker("Puma", "RS-X", -1, Sneaker.SneakerType.CASUAL, false);

try {
sneaker.validateSize();
} catch (Sneaker.InvalidSizeException e) {
assertEquals("Size should be greater than 0", e.getMessage());
}
}
}

Loading