Guest User

Untitled

a guest
Dec 13th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package com.example.oop.multiplication;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. import static java.util.stream.Collectors.joining;
  7.  
  8. class MultiplicationTable {
  9.  
  10.     private final int size;
  11.  
  12.     public MultiplicationTable(int size) {
  13.         this.size = size;
  14.     }
  15.  
  16.     public int[][] content() {
  17.         if (size <= 0) {
  18.             throw new IllegalArgumentException(String.format("Size must be positive, but is: %d", size));
  19.         }
  20.         int[][] result = new int[size][size];
  21.         for (int row = 0; row < size; ++row) {
  22.             for (int col = 0; col < size; ++col) {
  23.                 result[col][row] = (row + 1) * (col + 1);
  24.             }
  25.         }
  26.         return result;
  27.     }
  28. }
  29.  
  30. class FromString implements PositiveInteger {
  31.  
  32.     private final String string;
  33.  
  34.     public FromString(String string) {
  35.         this.string = string;
  36.     }
  37.  
  38.     @Override
  39.     public int get() {
  40.         Integer parsed = Integer.valueOf(string);
  41.         if (parsed <= 0) {
  42.             throw new IllegalArgumentException("Value is not positive: " + parsed);
  43.         }
  44.         return parsed;
  45.     }
  46. }
  47.  
  48. class IntMatrix {
  49.  
  50.     private final int[][] body;
  51.  
  52.     public IntMatrix(int[][] body) {
  53.         this.body = body;
  54.     }
  55.  
  56.     @Override
  57.     public String toString() {
  58.         return Arrays.stream(body).map(this::printRow).collect(joining("\n"));
  59.     }
  60.  
  61.     private String printRow(int[] row) {
  62.         return Arrays.stream(row).mapToObj(String::valueOf).collect(joining(" "));
  63.     }
  64. }
  65.  
  66. interface PositiveInteger {
  67.     int get();
  68. }
  69.  
  70. class MultiplicationTableApp implements Runnable {
  71.  
  72.     @Override
  73.     public void run() {
  74.         Scanner scanner = new Scanner(System.in);
  75.         boolean success = false;
  76.         do {
  77.             System.out.println("Enter size:");
  78.             String userInput = scanner.nextLine();
  79.             PositiveInteger size;
  80.             try {
  81.                 size = new FromString(userInput);
  82.                 System.out.println(new IntMatrix(new MultiplicationTable(size.get()).content()).toString());
  83.                 success = true;
  84.             } catch (IllegalArgumentException exception) {
  85.                 System.out.println("Invalid input: " + exception.getMessage());
  86.                 System.out.println("Try again");
  87.             }
  88.         } while (!success);
  89.     }
  90. }
  91.  
  92. public class App {
  93.  
  94.     public static void main(String[] args) {
  95.         new MultiplicationTableApp().run();
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment