Advertisement
Julia_S

Untitled

Mar 14th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.37 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.util.Date;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Scanner;
  6.  
  7. class Zero extends task{
  8. public static void zero() {
  9. System.exit(0);
  10. }
  11. }
  12.  
  13. class One extends task{
  14. public static void one() {
  15. System.out.println("Hello World ");
  16. }
  17. }
  18.  
  19. class Two extends task{
  20. public static void read_variable(double variable, Scanner sc){
  21. while (!sc.hasNextDouble()) {
  22. System.out.println("Ошибка. Введено не число. Введите заново.");
  23. sc.next();
  24. }
  25. }
  26.  
  27. public static double less0(double variable, Scanner sc) {
  28. read_variable(variable, sc);
  29. variable = sc.nextDouble();
  30. while (variable < 0) {
  31. System.out.println("Число должно быть >=0. Введите заново");
  32. read_variable(variable, sc);
  33. variable = sc.nextDouble();
  34. }
  35. return variable;
  36. }
  37.  
  38. public static double equally0(double variable, Scanner sc) {
  39. read_variable(variable, sc);
  40. variable = sc.nextDouble();
  41. while(variable==0){
  42. System.out.println("Число не должно равнятся 0. Введите заново");
  43. read_variable(variable, sc);
  44. variable = sc.nextDouble();
  45. }
  46. return variable;
  47. }
  48.  
  49. public static void two() {
  50. double dbl_x=1, dbl_y=0, dbl_z=1;
  51. Scanner sc = new Scanner(System.in);
  52. System.out.println("введите x ");
  53. dbl_x= less0(dbl_x, sc);
  54. System.out.println("введите z ");
  55. dbl_z=equally0(dbl_z, sc);
  56. dbl_y=Math.sqrt(dbl_x)-6/(dbl_z);
  57. System.out.printf("y=%.3f\n", dbl_y);
  58. }
  59. }
  60.  
  61. class Three extends task{
  62. public static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
  63. private static boolean isValid(String date)
  64. {
  65. try {
  66. return DATE_FORMAT.format(DATE_FORMAT.parse(date)).equals(date);
  67. } catch (ParseException ex){
  68. return false;
  69. }
  70. }
  71.  
  72. public static Date get_date(Scanner sc) {
  73. String s_original = sc.nextLine();
  74. s_original=s_original.trim();
  75. while(!isValid(s_original))
  76. {
  77. System.out.println("Неправильный ввод даты,введите дату формата ДД.ММ.ГГГГ");
  78. s_original = sc.nextLine();
  79. s_original=s_original.trim();
  80. }
  81. try {
  82. return DATE_FORMAT.parse(s_original);
  83. } catch (ParseException ex) {
  84. ex.printStackTrace();
  85. return new Date();
  86. }
  87. }
  88.  
  89. public static void ShowNums(int N, int depth) {
  90. if (depth==0) {
  91. System.out.print(N);
  92. return;
  93. }
  94. int i_count=N-depth;
  95. depth=depth-1;
  96. System.out.print(i_count+" ");
  97. ShowNums(N, depth);
  98. }
  99.  
  100. public static void three() {
  101. Scanner sc = new Scanner(System.in);
  102. System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
  103. Date t_date1 = get_date(sc);
  104. System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
  105. Date t_date2 = get_date(sc);
  106. while(t_date1.getTime() > t_date2.getTime())
  107. {
  108. System.out.println("Первая дата больше второй,введите даты повторно");
  109. System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
  110. t_date1 = get_date(sc);
  111. System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
  112. t_date2 = get_date(sc);
  113. }
  114. while(((t_date2.getTime() - t_date1.getTime())/86400000)>2500)
  115. {
  116. System.out.println("Слишком большая разница в днях. Введите повторно.");
  117. System.out.println("Введите первую дату формата ДД.ММ.ГГГГ");
  118. t_date1 = get_date(sc);
  119. System.out.println("Введите вторую дату формата ДД.ММ.ГГГГ");
  120. t_date2 = get_date(sc);
  121. }
  122. System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
  123. Date t_date3 = get_date(sc);
  124. System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
  125. Date t_date4 = get_date(sc);
  126. while(t_date3.getTime() > t_date4.getTime())
  127. {
  128. System.out.println("Первая дата больше второй,введите даты повторно");
  129. System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
  130. t_date3 = get_date(sc);
  131. System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
  132. t_date4 = get_date(sc);
  133. }
  134. while(((t_date4.getTime() - t_date3.getTime())/86400000)>2500)
  135. {
  136. System.out.println("Слишком большая разница в днях. Введите повторно.");
  137. System.out.println("Введите третью дату формата ДД.ММ.ГГГГ");
  138. t_date3 = get_date(sc);
  139. System.out.println("Введите четвертую дату формата ДД.ММ.ГГГГ");
  140. t_date2 = get_date(sc);
  141. }
  142.  
  143. long l_1, l_2, l_3, l_4;
  144. if (t_date3.getTime()<t_date1.getTime()){
  145. l_3=t_date1.getTime();
  146. l_1=t_date3.getTime();
  147. l_2=t_date4.getTime();
  148. l_4=t_date2.getTime();
  149. }
  150. else {
  151. l_1 = t_date1.getTime();
  152. l_3 = t_date3.getTime();
  153. l_4 = t_date4.getTime();
  154. l_2 = t_date2.getTime();
  155. }
  156. if(l_3>l_2){
  157. System.out.println("Даты не пересекаются");
  158. }
  159. long d_length;
  160. long l_Start = Math.max(l_1, l_3);
  161. long l_End = Math.min(l_2, l_4);
  162. d_length=l_End-l_Start;
  163. int d_days = (int) (d_length / (86400000))+1;
  164. System.out.println("Пересечение дат в днях: " + d_days);
  165. System.out.print("все числа от 0 до " + d_days + ": ");
  166. ShowNums(d_days, d_days);
  167. }
  168. }
  169.  
  170. class Four extends task {
  171. public static void check(String str) {
  172. check_email(str);
  173. check_IP(str);
  174. check_number(str);
  175. }
  176.  
  177. public static void check_number(String str) {
  178. if (str.matches("^[\\+]?[(]?[0-9]{3}[)]?[-\\s\\.]?[0-9]{3}[-\\s\\.]?[0-9]{4,6}$")) {
  179. System.out.printf("\nявляется номером телефона");
  180. }
  181. else System.out.printf("\nне является номером телефона");
  182. }
  183.  
  184. public static void check_email(String str) {
  185. if (str.matches("[^@ \\t\\r\\n]+@[^@ \\t\\r\\n]+\\.[^@ \\t\\r\\n]+")) {
  186. System.out.printf("\nявляется email'ом");
  187. }
  188. else System.out.printf("\nне является email'ом");
  189. }
  190.  
  191. public static void check_IP(String str) {
  192. if (str.matches("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}")) {
  193. System.out.printf("\nявляется IP-адресом");
  194. }
  195. else System.out.printf("\nне является IP-адресом");
  196. }
  197.  
  198. public static String get_string(Scanner in){
  199. String str = in.nextLine();
  200. return str;
  201. }
  202.  
  203. public static void string_coincide(String one, String two){
  204. if (one.equals(two)) {
  205. System.out.printf("Исходные строки совпадают");
  206. }
  207. else System.out.printf("Исходные строки разные");
  208. }
  209.  
  210. public static void string_abs_coincide(String one, String two){
  211. if (one.equals(two)) {
  212. System.out.printf("\nСтроки совпадают (в одном регистре, без повторяющихся пробелов)");
  213. }
  214. else System.out.printf("\nСтроки разные (в одном регистре, без повторяющихся пробелов)");
  215. }
  216.  
  217. public static void string_shifter(String one, String two){
  218. if (one.equals(two)) {
  219. System.out.printf("\nОдна строка является перевертышем другой");}
  220. else System.out.printf("\nОдна строка не является перевертышем другой");
  221. }
  222.  
  223. public static void four() {
  224. Scanner in = new Scanner(System.in);
  225. System.out.print("Введите первую строку: ");
  226. String one_s = get_string(in);
  227. System.out.print("Введите вторую строку: ");
  228. String two_s = get_string(in);
  229. string_coincide(one_s, two_s);
  230. String one_s1= ((one_s.trim()).toUpperCase()).replaceAll(" +", " ");
  231. String two_s1=((two_s.trim()).toUpperCase()).replaceAll(" +", " ");
  232. string_abs_coincide(one_s1, two_s1);
  233. String one_s2 = new StringBuffer(one_s1).reverse().toString();
  234. string_shifter(one_s2, two_s1);
  235. System.out.printf("\nСтрока 1:");
  236. check(one_s1);
  237. System.out.printf("\nСтрока 2:");
  238. check(two_s1);
  239. }
  240. }
  241.  
  242. public class task {
  243. public static void MENU() {
  244. System.out.println("\n0. Exit ");
  245. System.out.println("1. Hello World! ");
  246. System.out.println("2. Calc: math.sqrt(x)-6/(z) ");
  247. System.out.println("3. Recursion date ");
  248. System.out.println("4. Strings ");
  249. System.out.println("Выберите один из вышеперечисленных пунктов: ");
  250. }
  251. public static int read_choice(int choice, Scanner sc){
  252. while (!sc.hasNextInt()) {
  253. System.out.println("Выберите один из вышеперечисленных пунктов: ");
  254. sc.next();
  255. }
  256. choice = sc.nextInt();
  257. return choice;
  258. }
  259. public static void main(String[] args) throws Exception{
  260. boolean isExit=false;
  261. Scanner sc = new Scanner(System.in);
  262. int i_choice = 0;
  263. System.out.println("Выберите один из вышеперечисленных пунктов: ");
  264. while (!isExit){
  265. MENU();
  266. i_choice=read_choice(i_choice, sc);
  267. switch (i_choice){
  268. case 0:
  269. Zero zero_o = new Zero();
  270. zero_o.zero();
  271. break;
  272. case 1:
  273. One one_o = new One();
  274. one_o.one();
  275. break;
  276. case 2:
  277. Two two_o = new Two();
  278. two_o. two();
  279. break;
  280. case 3:
  281. Three three_o = new Three();
  282. three_o. three();
  283. break;
  284. case 4:
  285. Four four_o = new Four();
  286. four_o. four();
  287. break;
  288. }
  289. }
  290. }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement