deyanmalinov

09. Furniture - While - scan

Mar 26th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Main {
  8.         public static void main(String[] args) {
  9.             Scanner scan = new Scanner(System.in);
  10.            String regex = "\\>\\>(?<one>[A-Za-z]+)\\<\\<(?<two>[\\d\\.]+)!(?<three>[\\d]+)";
  11.             List<String> mebel = new ArrayList<>();
  12. ///////////////////////////////////
  13.             String string = scan.nextLine();
  14.             double sum =0;
  15.  
  16.             while (!"Purchase".equals(string)){
  17.                 Pattern pattern = Pattern.compile(regex);
  18.                 Matcher matcher = pattern.matcher(string);
  19. //////////////////////////////////////
  20.             while (matcher.find()) {
  21.                 mebel.add(matcher.group("one"));
  22.                 double cena = Double.parseDouble(matcher.group("two"));
  23.                 int broi = Integer.parseInt(matcher.group("three"));
  24.  
  25.                 sum += cena*broi;
  26.  
  27.             }
  28.                 string = scan.nextLine();
  29.            }
  30.             System.out.println("Bought furniture:");
  31.             for (String s : mebel) {
  32.                 System.out.println(s);
  33.             }
  34.  
  35.             System.out.printf("Total money spend: %.2f", sum);
  36.  
  37.         }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment