Advertisement
desant74268

Shop

Jan 11th, 2022
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package ru.itsjava.shop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BodyCast {
  6.     public static void main(String[] args) {
  7.  
  8.         String[] product = new String[]{"Скульптура", "Слепок", "Мерч"};
  9.         System.out.println("Приветствуем тебя в нашем мире искусств!");
  10.         printMenu();
  11.  
  12.         System.out.println("Введите номер меню: ");
  13.         Scanner console = new Scanner(System.in);
  14.         int menuNum = console.nextInt();
  15.  
  16.         while (true) {
  17.             if (menuNum == 1) {
  18.  
  19.                 printProduct(product);
  20.  
  21.             } else if (menuNum == 2) {
  22.  
  23.                 product = addProduct(console, product);
  24.  
  25.             } else if (menuNum == 3) {
  26.  
  27.                 product = removeProduct(console, product);
  28.  
  29.             } else if (menuNum == 4) {
  30.  
  31.                 bubbleSort(product);
  32.  
  33.             } else if (menuNum == 0) {
  34.                 System.out.println("До скорых встреч!");
  35.                 System.exit(0);
  36.             }
  37.  
  38.             System.out.println("Введите номер меню: ");
  39.             menuNum = console.nextInt();
  40.         }
  41.  
  42.     }
  43.  
  44.     private static String[] removeProduct(Scanner console, String[] product) {
  45.         System.out.println("Удалить услугу или товар: ");
  46.         System.out.println("Введите название товара: ");
  47.         String inputProduct = console.next();
  48.         String[] resArr = new String[product.length - 1];
  49.  
  50.         int removeInx;
  51.         for (removeInx = 0; removeInx < product.length; removeInx++) {
  52.             if (product[removeInx].equals(inputProduct)) break;
  53.             resArr[removeInx] = product[removeInx];
  54.         }
  55.  
  56.         for (int i = removeInx; i < resArr.length; i++) {
  57.             resArr[i] = product[i + 1];
  58.         }
  59.         return resArr;
  60.     }
  61.  
  62.     private static void bubbleSort(String[] product) {
  63.  
  64.         for (int j = 0; j < product.length; j++) {
  65.             for (int i = 0; i < product.length - 1 - j; i++) {
  66.                 if (product[i].charAt(0) > product[i + 1].charAt(0)) {
  67.                     String temp = product[i];
  68.                     product[i] = product[i + 1];
  69.                     product[i + 1] = temp;
  70.                 }
  71.  
  72.             }
  73.         }
  74.  
  75.     }
  76.  
  77.     private static String[] addProduct(Scanner console, String[] product) {
  78.         System.out.println("Добавьте новую услугу или товар: ");
  79.         System.out.println("Введите название товара: ");
  80.         String inputProduct = console.next();
  81.         String[] resArr = new String[product.length + 1];
  82.  
  83.         for (int i = 0; i < product.length; i++) {
  84.             resArr[i] = product[i];
  85.         }
  86.         resArr[product.length] = inputProduct;
  87.         return resArr;
  88.  
  89.     }
  90.  
  91.     private static void printProduct(String[] product) {
  92.         System.out.println("Товары и услуги: \n");
  93.         for (int i = 0; i < product.length; i++) {
  94.             System.out.println(product[i]);
  95.         }
  96.     }
  97.  
  98.     public static void printMenu() {
  99.         System.out.println("\n|          1         |           2           |            3         |       4       |   0   |");
  100.         System.out.println("| Все товары и услуги| Добавить товар/услугу | Удалить товар/услугу | Отсортировать | Выход |");
  101.     }
  102. }
  103.  
  104. //меню
  105. //1 вывести все товары и услуги
  106. //2 добавить товары и услуги
  107. //3 удалить тов и усл
  108. //4 отсортировать
  109. //0 выход
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement