A
A
Amir Kenesbay2021-06-28 12:23:16
Java
Amir Kenesbay, 2021-06-28 12:23:16

Check if a string occurs more than once?

This is an educational problem, there is a two-dimensional array, and when booking a ticket for the second time, it should show that this seat is already taken

Enter the number of rows:
> 6
Enter the number of seats in each row:
> 6

1. Show the seats
2. Buy a ticket
3 Statistics
0. Exit

> 3

Number of purchased tickets: 0
Percentage: 0.00%
Current income: $0
Total income: $360


1. Show the seats
2. Buy a ticket
3. Statistics
0. Exit

> 2

Enter a row number:
> 1
Enter a seat number in that row:
> 1

Ticket price: $10

1. Show the seats
2. Buy a ticket
3. Statistics
0. Exit

> 3

Number of purchased tickets: 1
Percentage: 2.78%
Current income: $10
Total income: $360


1. Show the seats
2. Buy a ticket
3. Statistics
0. Exit
> 2

Enter a row number:
> 1
Enter a seat number in that row:
> 1

That ticket has already been purchased!

Below is the code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int rows = scanner.nextInt();
        System.out.println("Enter the number of seats in each row: ");
        int seats = scanner.nextInt();

        System.out.println();

        String[][] arr = createCinema(rows, seats);
        while (true) {
            System.out.println("1. Show the seats\n" +
                    "2. Buy a ticket\n" +
                    "3. Statistics\n" +
                    "0. Exit");
            int choice = scanner.nextInt();
            if (choice == 1) {
                printCinema(arr);
            } else if (choice == 2) {
                System.out.println("Enter a row number: ");
                int checkRow = scanner.nextInt();
                System.out.println("Enter a seat number in that row: ");
                int checkSeat = scanner.nextInt();
                ticketPrice(rows, seats, checkRow);
                arr[checkRow][checkSeat] = "B";
            } else if (choice == 3) {
                int countOfTickets = 0;
                int count = 0;
                double percentage;
                for (int i = 0; i < arr.length; i++) {
                    for (int j = 0; j < arr[i].length; j++) {
                        if (arr[i][j].equals("B")) {
                            countOfTickets++;
                            count += currentIncome(rows, seats, i);
                        }
                    }
                }
                percentage = (double) Math.round(count * 100) / totalIncome(rows, seats);

                System.out.println("Number of purchased tickets: " + countOfTickets);
                System.out.print("Percentage: ");
                System.out.format("%.2f", percentage);
                System.out.println("%");
                System.out.println("Current income: $" + count);
                System.out.println("Total income: $" + totalIncome(rows, seats));
            } else if (choice == 0) {
                break;
            }
        }
    }

    public static String[][] createCinema(int rows, int seats) {
        String[][] arr = new String[rows + 1][seats + 1];
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (i == 0 && j == 0) {
                    arr[i][j] = " ";
                } else {
                    arr[i][j] = String.valueOf(count);
                    count++;
                }
                if (i > 0 && j == 0) {
                    arr[i][j] = String.valueOf(i);
                } else if (i > 0 && j > 0) {
                    arr[i][j] = "S";
                }
            }
        }
        return arr;
    }

    public static void printCinema(String[][] cinema) {
        System.out.println();
        System.out.println("Cinema: ");
        for (int i = 0; i < cinema.length; i++) {
            for (int j = 0; j < cinema[i].length; j++) {
                System.out.print(cinema[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void ticketPrice(int rows, int seats, int checkRow) {
        System.out.println();
        if (rows * seats <= 60) {
            System.out.println("Ticket price: $10");
        } else {
            if (rows % 2 == 0) {
                if (checkRow <= rows / 2) {
                    System.out.println("Ticket price: $10");
                } else {
                    System.out.println("Ticket price: $8");
                }
            } else {
                if (checkRow <= rows / 2) {
                    System.out.println("Ticket price: $10");
                } else {
                    System.out.println("Ticket price: $8");
                }
            }
        }
    }

    public static int currentIncome(int rows, int seats, int checkRow) {
        if (rows * seats <= 60) {
            return 10;
        } else {
            if (rows % 2 == 0) {
                if (checkRow <= rows / 2) {
                    return 10;
                } else {
                    return 8;
                }
            } else {
                if (checkRow <= rows / 2) {
                    return 10;
                } else {
                    return 8;
                }
            }
        }

    }

    public static int totalIncome(int rows, int seats) {
        return rows * seats * 10;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-06-28
Hasanly @azerphoenix

Good afternoon. A long time ago I wrote a similar tutorial project about buying a movie ticket.
True, I was already using Hibernate + MySQL then.
Here, here you can look at the source - https://github.com/hasanli-orkhan/learn-java/tree/...
I will say that your code is implemented incorrectly.
Firstly, Java is an OOP language and therefore you need to use the full power of OOP. For example, create a pojo Ticket , each ticket has its own cost, as well as the isPurchased boolean, and so on. In general, look at the source of my project, it will become clear.
This option is bad because, firstly, it is more of a procedural approach than OOP. Secondly, imagine that your ticket prices will change tomorrow and you need to edit the code in several places. And tomorrow the customer will say that the currency is not in dollars, but in rubles, and you will have to edit the code again, etc.

it should not be
public static void ticketPrice(int rows, int seats, int checkRow) {
        System.out.println();
        if (rows * seats <= 60) {
            System.out.println("Ticket price: $10");
        } else {
            if (rows % 2 == 0) {
                if (checkRow <= rows / 2) {
                    System.out.println("Ticket price: $10");
                } else {
                    System.out.println("Ticket price: $8");
                }
            } else {
                if (checkRow <= rows / 2) {
                    System.out.println("Ticket price: $10");
                } else {
                    System.out.println("Ticket price: $8");
                }
            }
        }
    }

Further, if you operate with currency, it is better to use BigDecimal instead of double.
Divide your code into objects. For example, Cinema, Ticket, Session, etc. Next, create service classes that will contain methods for adding a movie, purchasing tickets, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question