SHARE
TWEET

Untitled

a guest Mar 24th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package TechModule.TextProcessingAndRegularExpressionsExercise;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Furniture {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String regex = ">>(?<item>[A-Za-z]+)<<(?<price>\\d+(.?\\d+)?)!(?<quantity>\\d+)";
  12.  
  13.         double sum = 0;
  14.  
  15.         boolean flag = false;
  16.  
  17.         while (true) {
  18.             String input = scanner.nextLine();
  19.  
  20.             if (input.equalsIgnoreCase("Purchase")) {
  21.                 break;
  22.             }
  23.  
  24.             Pattern pattern = Pattern.compile(regex);
  25.             Matcher matcher = pattern.matcher(input);
  26.  
  27.             if (matcher.find()) {
  28.                 String product = matcher.group(1);
  29.                 double price = Double.parseDouble(matcher.group(2));
  30.                 int quantity = Integer.parseInt(matcher.group(4));
  31.  
  32.                 sum += price * quantity;
  33.  
  34.                 if (!flag) {
  35.                     System.out.println("Bought furniture:");
  36.                     flag = true;
  37.                 }
  38.                 System.out.printf("%s%n", product);
  39.             }
  40.         }
  41.         System.out.printf("Total money spend: %.2f", sum);
  42.     }
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top