Mary_99

task5 java

Apr 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintWriter;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9.  
  10. class DifferentVectorsLengthException extends Exception {
  11.     //private String msg = "Vector are not equals";
  12.     private static final long serialVersionUID = 1L;
  13.  
  14.     public DifferentVectorsLengthException(String msg) {
  15.         super(msg);
  16.     }
  17.  
  18. }
  19.  
  20.  
  21.  class Program {
  22.  
  23.     public static void main(String[] args)  {
  24.         Application app = new Application();
  25.         app.runApplication();
  26.     }
  27.  
  28. }
  29.  
  30.  
  31.  
  32.   class MyVector{
  33.   private List<Integer> components;
  34.   public MyVector(List<Integer> com)
  35.   {
  36.     components = new ArrayList<Integer>(com);
  37.   }
  38.   public List<Integer> getComponents()
  39.   {
  40.     return Collections.unmodifiableList(components);
  41.   }
  42. }
  43.  
  44. class VectorUtils {
  45.     public static boolean isEquals(MyVector vector1, MyVector vector2) throws DifferentVectorsLengthException {
  46.         if (vector1.getComponents().size() == vector2.getComponents().size())
  47.             return true;
  48.         else
  49.             throw new DifferentVectorsLengthException("Vector are not equals");
  50.     }
  51. }
  52.  
  53. class Application {
  54.     private String v1Str;
  55.     private String v2Str;
  56.  
  57.  
  58.     public void runApplication() {
  59.         boolean isEquals = false;
  60.         while (!isEquals) {
  61.             getDataUser();
  62.             MyVector vector1 = new MyVector(convertStringToList(v1Str));
  63.             MyVector vector2 = new MyVector(convertStringToList(v2Str));
  64.             try {
  65.                 isEquals = VectorUtils.isEquals(vector1, vector2);
  66.             } catch (DifferentVectorsLengthException e) {
  67.                 System.out.println(e);
  68.             }
  69.         }
  70.         saveSum();
  71.     }
  72.  
  73.     public void saveSum()
  74.     {
  75.         List<Integer> v1Numbers= convertStringToList(v1Str);
  76.         List<Integer> v2Numbers= convertStringToList(v2Str);
  77.         int sum = 0;
  78.         for (int i = 0; i < v1Numbers.size(); i++)
  79.         {
  80.             sum += (v1Numbers.get(i) + v2Numbers.get(i));
  81.         }
  82.         try (PrintWriter out = new PrintWriter("Sum.txt")) {
  83.             out.println(sum);
  84.         } catch (FileNotFoundException e) {
  85.             e.printStackTrace();
  86.         }
  87.     }
  88.  
  89.     public void getDataUser() {
  90.  
  91.         Scanner reader = new Scanner(System.in);
  92.         System.out.println("Enter vector one: ");
  93.         v1Str = reader.nextLine();
  94.         System.out.println("Enter vector two: ");
  95.         v2Str = reader.nextLine();
  96.  
  97.     }
  98.  
  99.     private List<Integer> convertStringToList(String v) {
  100.         String parts[] = v.split(" ");
  101.         List<Integer> vInt = new ArrayList<Integer>();
  102.         for (String stringValue : parts) {
  103.             try {
  104.  
  105.                 vInt.add(Integer.parseInt(stringValue));
  106.             } catch (NumberFormatException e) {
  107.  
  108.             }
  109.         }
  110.         return vInt;
  111.     }
  112. }
Add Comment
Please, Sign In to add comment