Advertisement
darkor

oop_lab_5

Mar 8th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     static String exception = "Неправильна послідовність кроків, дотримуйтеся інструкцї!!!";
  6.     public static boolean step_1 = false, step_2 = false, step_3 = false, step_4 = false, step_5 = false, step_6 = false;
  7.  
  8.     public static void main(String[] args) throws TreeException
  9.     {
  10.         System.out.println("\nВведіть номер кроку щоб виконати його.\n\nІнструкція:\n1) Взяти саджанець, відро з водою і лопату" +
  11.                 "\n2) Викопати яму\n3) Посадити саджанець в яму\n4) Засипати яму\n5) Полити саджанець\n" +
  12.                 "6) Забрати відро і лопату\n0) Щоб закінчити введення даних.");
  13.  
  14.  
  15.         while (true)
  16.         {
  17.             int n = 0;
  18.             Scanner input = new Scanner(System.in);
  19.             System.out.println("Введіть значення:");
  20.                 try
  21.                 {
  22.                     n = input.nextInt();
  23.  
  24.                 }catch (Exception e)
  25.                 {
  26.                     System.out.println("Ви повинні вводити лише цілі числа!");
  27.                     continue;
  28.                 }
  29.  
  30.          try {
  31.  
  32.             switch (n)
  33.             {
  34.                 case (1):
  35.                 {
  36.                     do_step_1();
  37.                     break;
  38.                 }
  39.                 case (2):
  40.                 {
  41.                     do_step_2();
  42.                     break;
  43.                 }
  44.                 case (3):
  45.                 {
  46.                     do_step_3();
  47.                     break;
  48.                 }
  49.                 case (4):
  50.                 {
  51.                     do_step_4();
  52.                     break;
  53.                 }
  54.                 case (5):
  55.                 {
  56.                     do_step_5();
  57.                     break;
  58.                 }
  59.                 case (6):
  60.                 {
  61.                     do_step_6();
  62.                     return;
  63.                 }
  64.                 case (0):
  65.                 {
  66.                     System.out.println("Екстрений вихід");
  67.                     return;
  68.                 }
  69.                 default:
  70.                     System.out.println("Немає такого кроку, слідуйте інструкції!!!");
  71.                     break;
  72.             }
  73.         }catch (Exception e)
  74.          {
  75.              System.out.println(e);
  76.          }
  77.     }
  78.     }
  79.     public static void do_step_1() throws TreeException {
  80.         if (!(step_2 || step_3 || step_4 || step_5 || step_6))
  81.         {
  82.             step_1 = true;
  83.             System.out.println("Ви виконали крок 1 успішно.");
  84.         }
  85.         else
  86.             throw new TreeException(exception);
  87.     }
  88.     public static void do_step_2() throws TreeException {
  89.         if (step_1)
  90.         {
  91.             step_2 = true;
  92.             System.out.println("Ви виконали крок 2 успішно.");
  93.         }
  94.         else
  95.             throw new TreeException(exception);
  96.     }
  97.     public static void do_step_3() throws TreeException
  98.     {
  99.         if (step_1 & step_2)
  100.         {
  101.             step_3 = true;
  102.             System.out.println("Ви виконали крок 3 успішно.");
  103.         }
  104.         else
  105.             throw new TreeException(exception);
  106.     }
  107.     public static void do_step_4() throws TreeException
  108.     {
  109.         if (step_1 & step_2 & step_3)
  110.         {
  111.             step_4 = true;
  112.             System.out.println("Ви виконали крок 4 успішно.");
  113.         }
  114.         else
  115.             throw new TreeException(exception);
  116.     }
  117.     public static void do_step_5() throws TreeException
  118.     {
  119.         if (step_1 & step_2 & step_3 & step_4)
  120.         {
  121.             step_5 = true;
  122.             System.out.println("Ви виконали крок 5 успішно.");
  123.         }
  124.         else
  125.             throw new TreeException(exception);
  126.     }
  127.     public static void do_step_6() throws TreeException
  128.     {
  129.         if (step_1 & step_2 & step_3 & step_4 & step_5)
  130.         {
  131.             step_6 = true;
  132.             System.out.println("Ви виконали крок 6 успішно.\nВітаю! Ви посадили дерево успішно!!!");
  133.         }
  134.         else
  135.             throw new TreeException(exception);
  136.     }
  137. }
  138.  
  139. class TreeException extends Exception
  140. {
  141.     private static String msg;
  142.  
  143.     TreeException()
  144.     {
  145.         msg = null;
  146.     }
  147.  
  148.     TreeException(String s)
  149.     {
  150.         msg = s;
  151.     }
  152.  
  153.     public String toString()
  154.     {
  155.         return "TreeException (" + msg + ")";
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement