Advertisement
tuturox

Untitled

Dec 5th, 2022
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3.  
  4. public class Files {
  5.     public static void main(String[] args) {
  6.         String data = "Program title: Balance\nAuthor: Tolkien\nCorporation: DMK\nPhone: +1-503-555-0091"
  7.                 + "\nDate Tues April 9, 2005\nVersion: 13.2\nLevel: Alpha";
  8.         String program = "Wood";
  9.         String version = "1.9";
  10.         System.out.println(modifyData(data, program, version));
  11.     }
  12.  
  13.     public static String modifyData(String data, String program, String version) {
  14.         List<String> splitedRows = Arrays.stream(data.split("\\n")).toList();
  15.         String correctProgram = parseProgram(splitedRows.get(0));
  16.         String Author = "Author: g964";
  17.         String phoneResult = parsePhone(splitedRows.get(3));
  18.         String versionResult = parseVersion(splitedRows.get(5), version);
  19.         if(phoneResult == null || versionResult == null) {
  20.             return "ERROR: VERSION or PHONE";
  21.         }
  22.         return correctProgram + "\n" + Author + "\n" + "Phone: " + phoneResult + "\n" + "Date: 2019-01-01" + "\n" + versionResult;
  23.     }
  24.  
  25.     private static String parseVersion(String stringVerion, String replacedVersion) {
  26.         String[] splittedVersion = stringVerion.split(" ");
  27.         String version = splittedVersion[1];
  28.         String startString = "Version: ";
  29.         if(version.matches("\\d+\\.\\d+")) {
  30.             Double versionD = Double.valueOf(version);
  31.             if(versionD != 2.0D) {
  32.                 return startString + replacedVersion;
  33.             } else {
  34.                return startString + "2.0";
  35.             }
  36.         } else {
  37.             return null;
  38.         }
  39.     }
  40.  
  41.     private static String parsePhone(String phone) {
  42.         String[] splitedPhoneLine = phone.split(" ");
  43.         String splittedPhone = splitedPhoneLine[1];
  44.  
  45.         if(splittedPhone.matches("\\+1-(\\d{3})-(\\d{3})-(\\d{4})")) {
  46.             return splittedPhone;
  47.         }
  48.         return null;
  49.     }
  50.  
  51.     private static String parseProgram(String program) {
  52.         String[] splittedProgram = program.split("( title: )");
  53.         return splittedProgram[0] + ": " + splittedProgram[1];
  54.  
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement