Skip to content
Open
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,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
71 changes: 71 additions & 0 deletions emailslicer-dkasargod-java/mvnw

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions emailslicer-dkasargod-java/mvnw.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions emailslicer-dkasargod-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.emailslicer</groupId>
<artifactId>email-slicer</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Email Slicer</name>
<description>A CLI tool that parses an email address into its username and domain parts</description>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.2</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>email-slicer</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.emailslicer.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.emailslicer;

/**
* Immutable value object holding the parsed username and domain parts of an email address.
*
* @param username the local part of the email address (before the {@code @} symbol)
* @param domain the domain part of the email address (after the {@code @} symbol)
*/
public record EmailParts(String username, String domain) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.emailslicer;

/**
* Parses an email address string into its username and domain components.
*
* <p>This class contains the core parsing logic, separated from I/O concerns
* to allow direct unit testing without mocking {@code System.in}/{@code System.out}.
*/
public final class EmailSlicer {

private EmailSlicer() {
// Utility class — not instantiable
}

/**
* Parses the given email string into an {@link EmailParts} record.
*
* <p>The input is trimmed before validation. Validation requires:
* <ul>
* <li>Input is not null or blank</li>
* <li>Input contains exactly one {@code @} character</li>
* <li>Both the username (before {@code @}) and domain (after {@code @}) are non-empty</li>
* </ul>
*
* @param email the raw email address string
* @return an {@link EmailParts} instance containing the username and domain
* @throws IllegalArgumentException if the input fails validation
*/
public static EmailParts parse(String email) {
if (email == null || email.isBlank()) {
throw new IllegalArgumentException(Messages.INVALID_EMAIL_MESSAGE);
}

String trimmed = email.strip();

int atIndex = trimmed.indexOf('@');
if (atIndex == -1) {
throw new IllegalArgumentException(Messages.INVALID_EMAIL_MESSAGE);
}

// Reject multiple '@' characters
if (trimmed.indexOf('@', atIndex + 1) != -1) {
throw new IllegalArgumentException(Messages.INVALID_EMAIL_MESSAGE);
}

String username = trimmed.substring(0, atIndex);
String domain = trimmed.substring(atIndex + 1);

if (username.isEmpty() || domain.isEmpty()) {
throw new IllegalArgumentException(Messages.INVALID_EMAIL_MESSAGE);
}

return new EmailParts(username, domain);
}
}
34 changes: 34 additions & 0 deletions emailslicer-dkasargod-java/src/main/java/com/emailslicer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.emailslicer;

import java.util.Scanner;

/**
* CLI entry point for the Email Slicer application.
*
* <p>Prompts the user for an email address, delegates parsing to
* {@link EmailSlicer#parse(String)}, and prints the result. Exits with
* code 1 when the input is invalid.
*/
public final class Main {

private Main() {
// Entry-point class — not instantiable
}

public static void main(String[] args) {
System.out.println(Messages.PROMPT_MESSAGE);

try (Scanner scanner = new Scanner(System.in)) {
String email = scanner.nextLine().strip();

try {
EmailParts parts = EmailSlicer.parse(email);
System.out.println(Messages.USERNAME_PREFIX + parts.username());
System.out.println(Messages.DOMAIN_PREFIX + parts.domain());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.emailslicer;

/**
* Centralised store for user-facing messages.
*/
public final class Messages {

public static final String PROMPT_MESSAGE = "Please enter your Email Id:";
public static final String INVALID_EMAIL_MESSAGE = "Please enter a valid Email Id.";
public static final String USERNAME_PREFIX = "Your username is: ";
public static final String DOMAIN_PREFIX = "Your domain is: ";

private Messages() {
// Utility class — not instantiable
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.emailslicer;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

/**
* Unit tests for {@link EmailSlicer#parse(String)}.
*/
class EmailSlicerTest {

@Test
@DisplayName("Valid email is split into username and domain")
void validEmail() {
EmailParts result = EmailSlicer.parse("avimax37@gmail.com");
assertEquals(new EmailParts("avimax37", "gmail.com"), result);
}

@Test
@DisplayName("Leading and trailing whitespace is trimmed before parsing")
void leadingTrailingWhitespace() {
EmailParts result = EmailSlicer.parse(" user@example.org ");
assertEquals(new EmailParts("user", "example.org"), result);
}

@Test
@DisplayName("Missing '@' throws IllegalArgumentException")
void missingAtSign() {
IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> EmailSlicer.parse("invalidemail")
);
assertEquals(Messages.INVALID_EMAIL_MESSAGE, ex.getMessage());
}

@Test
@DisplayName("Empty string throws IllegalArgumentException")
void emptyString() {
assertThrows(IllegalArgumentException.class, () -> EmailSlicer.parse(""));
}

@Test
@DisplayName("Null input throws IllegalArgumentException")
void nullInput() {
assertThrows(IllegalArgumentException.class, () -> EmailSlicer.parse(null));
}

@Test
@DisplayName("Multiple '@' signs throws IllegalArgumentException")
void multipleAtSigns() {
assertThrows(IllegalArgumentException.class, () -> EmailSlicer.parse("a@b@c.com"));
}

@Test
@DisplayName("Empty username (@domain.com) throws IllegalArgumentException")
void emptyUsername() {
assertThrows(IllegalArgumentException.class, () -> EmailSlicer.parse("@domain.com"));
}

@Test
@DisplayName("Empty domain (user@) throws IllegalArgumentException")
void emptyDomain() {
assertThrows(IllegalArgumentException.class, () -> EmailSlicer.parse("user@"));
}
}