Advertisement
lnovokhatko

Task3

Mar 7th, 2025
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. /* pom.xml */
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <project xmlns="http://maven.apache.org/POM/4.0.0"
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6.     <modelVersion>4.0.0</modelVersion>
  7.  
  8.     <groupId>com.epam.automation</groupId>
  9.     <artifactId>Task3</artifactId>
  10.     <version>1.0-SNAPSHOT</version>
  11.  
  12.     <properties>
  13.         <maven.compiler.source>17</maven.compiler.source>
  14.         <maven.compiler.target>17</maven.compiler.target>
  15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16.         <junit5.version>5.6.2</junit5.version>
  17.     </properties>
  18.  
  19.     <dependencies>
  20.         <dependency>
  21.             <groupId>org.junit.jupiter</groupId>
  22.             <artifactId>junit-jupiter-engine</artifactId>
  23.             <version>${junit5.version}</version>
  24.             <scope>test</scope>
  25.         </dependency>
  26.         <dependency>
  27.             <groupId>org.junit.jupiter</groupId>
  28.             <artifactId>junit-jupiter-api</artifactId>
  29.             <version>${junit5.version}</version>
  30.             <scope>test</scope>
  31.         </dependency>
  32.         <dependency>
  33.             <groupId>org.junit.jupiter</groupId>
  34.             <artifactId>junit-jupiter-params</artifactId>
  35.             <version>${junit5.version}</version>
  36.             <scope>test</scope>
  37.         </dependency>
  38.  
  39.     </dependencies>
  40.  
  41. </project>
  42.  
  43.  
  44. /* HandyClass */
  45.  
  46. package com.epam.automation;
  47.  
  48. public class HandyClass {
  49.  
  50.     public static int countMaxUnequalConsecutive(String input){
  51.         int max = 0;
  52.         int current = 1;
  53.         for (int i = 0; i < input.length() - 1; i++) {
  54.             if (input.charAt(i) != input.charAt(i+1)){
  55.                 current++;
  56.             } else {
  57.                 current = 1;
  58.             }
  59.             if (max < current){
  60.                 max = current;
  61.             }
  62.         }
  63.         return max;
  64.     }
  65.  
  66.     public static int countMaxEqualLatinLettersConsecutive(String input){
  67.         int max = 0;
  68.         int current = 1;
  69.         if (input.matches("[A-Za-z]+")) {
  70.             for (int i = 0; i < input.length() - 1; i++) {
  71.                 if (input.charAt(i) == input.charAt(i + 1)) {
  72.                     current++;
  73.                 } else {
  74.                     current = 1;
  75.                 }
  76.                 if (max < current) {
  77.                     max = current;
  78.                 }
  79.             }
  80.         } else {
  81.             throw new IllegalArgumentException("Please enter latin letters");
  82.         }
  83.         return max;
  84.     }
  85.  
  86.     public static int countMaxIdenticalDigitsConsecutive(String input){
  87.         int max = 0;
  88.         int current = 1;
  89.         if(input.matches("[0-9]+")){
  90.             for (int i = 0; i < input.length() - 1; i++) {
  91.                 if (input.charAt(i) == input.charAt(i + 1)) {
  92.                     current++;
  93.                 } else {
  94.                     current = 1;
  95.                 }
  96.                 if (max < current) {
  97.                     max = current;
  98.                 }
  99.             }
  100.         } else {
  101.             throw new IllegalArgumentException("Please enter digits");
  102.         }
  103.         return max;
  104.     }
  105. }
  106.  
  107. /* Task3Tests */
  108.  
  109. package com.epam.automation;
  110.  
  111. import org.junit.jupiter.api.Test;
  112.  
  113. import static org.junit.jupiter.api.Assertions.*;
  114.  
  115. public class Task3Tests {
  116.  
  117.     @Test
  118.     public void testCountMaxUnequalConsecutive(){
  119.         assertEquals(0, HandyClass.countMaxUnequalConsecutive("a"));
  120.         assertEquals(1, HandyClass.countMaxUnequalConsecutive("aa"));
  121.         assertEquals(6, HandyClass.countMaxUnequalConsecutive("asdfgggsdjflllll"));
  122.         assertEquals(15, HandyClass.countMaxUnequalConsecutive("abcdefbdjuhgdyk"));
  123.         assertEquals(0, HandyClass.countMaxUnequalConsecutive(""));
  124.     }
  125.  
  126.     @Test
  127.     public void testCountMaxEqualLatinLettersConsecutive(){
  128.         assertEquals(0, HandyClass.countMaxEqualLatinLettersConsecutive("a"));
  129.         assertEquals(2, HandyClass.countMaxEqualLatinLettersConsecutive("aa"));
  130.         assertEquals(5, HandyClass.countMaxEqualLatinLettersConsecutive("asdfgggsdjflllll"));
  131.         assertEquals(1, HandyClass.countMaxEqualLatinLettersConsecutive("abcdefbdjuhgdyk"));
  132.     }
  133.  
  134.     @Test
  135.     public void testCountMaxEqualLatinLettersConsecutiveShouldThrow(){
  136.         assertThrows(IllegalArgumentException.class,
  137.                 () -> HandyClass.countMaxEqualLatinLettersConsecutive("1223"));
  138.         assertThrows(IllegalArgumentException.class,
  139.                 () -> HandyClass.countMaxEqualLatinLettersConsecutive("абв"));
  140.         assertThrows(IllegalArgumentException.class,
  141.                 () -> HandyClass.countMaxEqualLatinLettersConsecutive("*;?)"));
  142.         assertThrows(IllegalArgumentException.class,
  143.                 () -> HandyClass.countMaxIdenticalDigitsConsecutive(""));
  144.     }
  145.  
  146.     @Test
  147.     public void testCountMaxIdenticalDigitsConsecutive(){
  148.         assertEquals(0, HandyClass.countMaxIdenticalDigitsConsecutive("1"));
  149.         assertEquals(2, HandyClass.countMaxIdenticalDigitsConsecutive("55"));
  150.         assertEquals(4, HandyClass.countMaxIdenticalDigitsConsecutive("13666638"));
  151.         assertEquals(4, HandyClass.countMaxIdenticalDigitsConsecutive("123444567888868686"));
  152.         assertEquals(1, HandyClass.countMaxIdenticalDigitsConsecutive("1234567890"));
  153.     }
  154.  
  155.     @Test
  156.     public void testCountMaxIdenticalDigitsConsecutiveShouldThrow(){
  157.         assertThrows(IllegalArgumentException.class,
  158.                 () -> HandyClass.countMaxIdenticalDigitsConsecutive("abc"));
  159.         assertThrows(IllegalArgumentException.class,
  160.                 () -> HandyClass.countMaxIdenticalDigitsConsecutive("абв"));
  161.         assertThrows(IllegalArgumentException.class,
  162.                 () -> HandyClass.countMaxIdenticalDigitsConsecutive("*;?)"));
  163.         assertThrows(IllegalArgumentException.class,
  164.                 () -> HandyClass.countMaxIdenticalDigitsConsecutive(""));
  165.     }
  166. }
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement