Poprzez zalecenia osób z forum przestałem pisać nic nie wnoszące w naukę programy z "programowaniem obiektowym" i skupiłem się na napisaniu programu po prostu na pętlach metodach itd. Program ma symulować grę lotto. Jak oceniacie ten kod? Co można poprawić, aby był bardziej czytelny? Czy jest zgodny ze standardami języka na etapie mojej nauki?
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Witaj w symulatorze gry lotto!");
displayMenu();
}
static int[] userTicket = new int[6];
static int[] lotteryTicket = new int[6];
static int[] correctNumbers = new int[6];
static long deposit = 20;
static long betAmount;
private static void displayMenu() {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println(STR."\nStan konta: \{deposit}zł");
System.out.println("Co chcesz teraz zrobić?");
System.out.println("1. Stawianie kuponu");
System.out.println("2. Sprawdzanie kuponu");
System.out.println("3. Koniec gry");
System.out.print("Twój wybór: ");
choice = input.nextInt();
switch (choice) {
case 1:
createUserTicket();
System.out.print("Postawiony kupon: ");
displayTicket(userTicket);
break;
case 2:
generateLotteryTicket();
System.out.print("Wylosowany kupon: ");
displayTicket(lotteryTicket);
checkLotteryTicket();
displayContinueMenu();
break;
case 3:
System.out.println("Dziękuję za granie w lotto!");
System.exit(0);
break;
default:
System.out.println("Wprowadzono zły znak!");
System.exit(0);
break;
}
} while (true);
}
private static void createUserTicket() {
Scanner input = new Scanner(System.in);
do {
System.out.print("Za jaką stawkę chcesz zagrać? ");
betAmount = input.nextLong();
if (betAmount > deposit) {
System.out.println("Zakład nie może być większy niż stan konta!");
}
} while (betAmount > deposit);
deposit -= betAmount;
for (int i = 0; i < userTicket.length; i++) {
int singleNumber;
System.out.print(STR."Podaj \{i + 1}. liczbę do kuponu (1 - 49): ");
singleNumber = input.nextInt();
if (validateTicket(userTicket, singleNumber)) {
userTicket[i] = singleNumber;
} else {
System.out.println("Twoje liczby nie mogą sie powtarzać i być spoza zakresu!");
i--;
}
}
}
private static boolean validateTicket(int[] tickets, int singleNumber) {
if (singleNumber > 0 && singleNumber < 50) {
for(int ticket: tickets) {
if (singleNumber == ticket) {
return false;
} else {
return true;
}
}
} else {
return false;
}
return true;
}
private static void displayTicket(int[] tickets) {
for (int i = 0; i < tickets.length; i++) {
if (tickets[i] != 0) {
if (i == tickets.length - 1) {
System.out.print(STR."\{tickets[i]}.");
} else {
System.out.print(STR."\{tickets[i]}, ");
}
}
}
System.out.println();
}
private static void generateLotteryTicket() {
Random random = new Random();
for (int i = 0; i < lotteryTicket.length; i++) {
int singleNumber = random.nextInt(49) + 1;
if (validateTicket(lotteryTicket, singleNumber)) {
lotteryTicket[i] = singleNumber;
} else {
i--;
}
}
}
private static void checkLotteryTicket() {
int counter = 0;
for (int i = 0; i < userTicket.length; i++) {
for (int k : lotteryTicket) {
if (userTicket[i] == k) {
correctNumbers[i] = userTicket[i];
counter++;
}
}
}
if (counter == 0 || counter == 1 || counter == 2) {
System.out.println("Niestety nic nie wygrałeś");
} else if (counter == 3) {
deposit += betAmount * 2;
System.out.println(STR."Gratulacje! Na twoje konto trafi \{betAmount * 2}zł");
} else if (counter == 4) {
deposit += betAmount * 10;
System.out.println(STR."Gratulacje! Na twoje konto trafi \{betAmount * 10}zł");
} else if (counter == 5) {
deposit += betAmount * 100;
System.out.println(STR."Gratulacje! Na twoje konto trafi \{betAmount * 100}zł");
} else {
deposit += betAmount * 100000;
System.out.println(STR."Gratulacje! Na twoje konto trafi \{betAmount * 100000}zł");
}
System.out.print(STR."Liczba trafień: \{counter}\nTrafione liczby: ");
displayTicket(correctNumbers);
}
private static void displayContinueMenu() {
Scanner input = new Scanner(System.in);
int choice;
if (deposit > 0) {
System.out.println("Czy chcesz grać dalej?");
System.out.println("1. Tak");
System.out.println("2. Nie");
System.out.print("Twój wybór: ");
choice = input.nextInt();
switch (choice) {
case 1:
userTicket = new int[userTicket.length];
lotteryTicket = new int[lotteryTicket.length];
displayMenu();
break;
case 2:
System.out.println("Dziękuję za granie w lotto!");
System.exit(0);
break;
default:
System.out.println("Wprowadzono zły znak!");
System.exit(0);
break;
}
} else {
System.out.println("Nie masz już pieniędzy na koncie!");
System.exit(0);
}
}
}