package Interfaces.InternetShop; import java.io.IOException; import java.util.Objects; import java.util.Scanner; import java.util.concurrent.TimeUnit; public class HelloKitty { public static class colors { public static final String reset = "\u001B[0m"; public static final String black = "\u001B[30m"; public static final String red = "\u001B[31m"; public static final String green = "\u001B[32m"; public static final String yellow = "\u001B[33m"; public static final String blue = "\u001B[34m"; public static final String purple = "\u001B[35m"; public static final String cyan = "\u001B[36m"; public static final String white = "\u001B[37m"; } public static class User { private final String login; private final String password; private User(String user, String password) { this.login = user; this.password = password; } public Basket purchased; private String getLogin() { return login; } private String getPassword() { return password; } } public static class ProductHelloKitty { String modelName; double price; public ProductHelloKitty(String modelName, double price) { this.modelName = modelName; this.price = price; } } public static class CategoryHelloKitty { String nameCategory; ProductHelloKitty[] productHelloKitties; public CategoryHelloKitty(String nameCategory, ProductHelloKitty[] productHelloKitties) { this.nameCategory = nameCategory; this.productHelloKitties = productHelloKitties; } } public static class Basket { ProductHelloKitty[] purchased; public Basket(ProductHelloKitty[] purchased) { this.purchased = purchased; } } public static void Buy() { Basket cards = new Basket(card); client.purchased = cards; System.out.println("Товарный чек"); for(int i = 0; i < client.purchased.purchased.length; ++i) { if(client.purchased.purchased[i] == null) break; System.out.println((i+1) + ". " + client.purchased.purchased[i].modelName + " ------- " + client.purchased.purchased[i].price); } double gc = getCash(client.purchased.purchased); System.out.print("Общая сумма покупок составляет -------" + gc); } public static double getCash(ProductHelloKitty[] p) { double cash = 0.0; for(int i = 0; i < p.length; ++i) { if(p[i] == null) break; cash+=p[i].price; } return cash; } public static void getBasket(ProductHelloKitty[] product) throws InterruptedException { for (int i = 0; i < product.length; i++) { if (product[i] == null) break; System.out.println((i+1) + ". " + product[i].modelName + " " + product[i].price + "UAH"); } System.out.println("0. Назад"); Scanner scanner = new Scanner(System.in); System.out.println(""); int number = scanner.nextInt(); System.out.println(""); if (number == 0) getMenu(); } public static void seeProduct(CategoryHelloKitty hk) throws InterruptedException { for (int i = 0; i < hk.productHelloKitties.length; i++) { System.out.println((i+1) + ". " + hk.productHelloKitties[i].modelName + " " + hk.productHelloKitties[i].price + "UAH"); } System.out.println("0. Назад"); Scanner scanner = new Scanner(System.in); System.out.println(""); int number = scanner.nextInt(); System.out.println(""); if (number == 0) getMenu(); else buyProduct(hk.productHelloKitties[number -1]); } public static void buyProduct(ProductHelloKitty product) throws InterruptedException { System.out.println(""); System.out.println(colors.red + "Продукт " + product.modelName + " " + product.price + "UAH был добавлен в корзину" + colors.reset); System.out.println(""); for (int i = 0; i < card.length; i++) { if (card[i] == null) { card[i] = product; break; } else { if (i+1 == card.length && card[i] == null) { System.out.println("Ваша корзина заполнена"); break; } } } getCategory(all); } public static void getCategory(CategoryHelloKitty[] categoryHelloKitties) throws InterruptedException { for (int i = 0; i < categoryHelloKitties.length; i++) { System.out.println("#" + (i+1) + " " + categoryHelloKitties[i].nameCategory); } System.out.println("0. Назад"); Scanner scanner = new Scanner(System.in); System.out.println(""); int number = scanner.nextInt(); System.out.println(""); if (number == 0) getMenu(); else seeProduct(all[number - 1]); } public static void Sleep() throws InterruptedException { TimeUnit.SECONDS.sleep(2); } public static void Redirect() { for (int i = 0; i < 200; i++) { System.out.println(); } } private static void isAuth() throws InterruptedException, IOException { User user = new User("1", "2"); Scanner scanner = new Scanner(System.in); System.out.println("Введите логин и пароль.\n"); String userLogin = user.getLogin(); String userPassword = user.getPassword(); System.out.print("Логин: "); String userInputLogin = scanner.nextLine(); System.out.print("Пароль: "); String userInputPassword = scanner.nextLine(); if (Objects.equals(userLogin, userInputLogin) && Objects.equals(userPassword, userInputPassword)) { System.out.println("\nУспешно!"); Sleep(); System.out.println("\nПеренаправляем на сайт..."); Sleep(); Redirect(); } else { System.out.println("Логин или пароль введены не верно."); System.exit(0); } } public static void getMenu() throws InterruptedException { System.out.println("Добро пожаловать в Hello Kitty/Melody магазин!\n"); System.out.println("1.Каталог"); System.out.println("2.Корзина"); System.out.println("3.Оплатить"); System.out.println("0.Выйти"); Scanner scanner = new Scanner(System.in); System.out.println(""); int number = scanner.nextInt(); System.out.println(""); switch (number) { case 1: getCategory(all); break; case 2: getBasket(card); break; case 3: { Buy(); break; } case 0: Redirect(); System.out.println("Будем рады если Вы зайдете к нам позже! <3"); Sleep(); System.exit(number); } } static ProductHelloKitty helloKitty1 = new ProductHelloKitty("Hello Kitty повар", 100); static ProductHelloKitty helloKitty2 = new ProductHelloKitty("Hello Kitty уборощица", 200); static ProductHelloKitty helloKitty3 = new ProductHelloKitty("Мелоди1", 300); static ProductHelloKitty helloKitty4 = new ProductHelloKitty("Мелоди2", 400); static ProductHelloKitty[] Type1 = {helloKitty1, helloKitty2}; static ProductHelloKitty[] Type2 = {helloKitty3, helloKitty4}; static CategoryHelloKitty type1 = new CategoryHelloKitty("Hello Kitty ассортимент", Type1); static CategoryHelloKitty type2 = new CategoryHelloKitty("Melody ассортимент", Type2); static CategoryHelloKitty[] all = {type1, type2}; static ProductHelloKitty[] card; static User client; public static void main(String[] args) throws InterruptedException, IOException { card = new ProductHelloKitty[20]; isAuth(); getMenu(); } }
Compile
write 1
then write 2
WAIT 4 SECONDS
then write 3
Why when I write 3, my code not working and show me error NullPointerException.