方法,类和使用数组的问题构成了它们

时间:2020-11-30 16:22:10

So basically, I have 2 classes (so far) "Seat" and "Theatre" and my main method.

基本上,我有两个班级(到目前为止)“座位”和“剧院”以及我的主要方法。

I am to make a cinema booking system, and to do so I need to create a 'cinema' by using an array. However I want to be able to make as many of these cinemas as I want, and as I may add other variables such as (Time, movies, etc later) I thought I would make a "Theatre" class which contains an Array of Seats in its constructor, and then have a method which displays the array, however I'm not sure how to execute this.

我要制作电影预约系统,为此我需要使用阵列创建一个“电影院”。但是我想能够制作尽可能多的这些电影院,因为我可能会添加其他变量,例如(时间,电影等),我想我会创建一个包含一系列座位的“剧院”类在它的构造函数中,然后有一个显示数组的方法,但是我不知道如何执行它。

Here is what I have so far"

这是我到目前为止所拥有的“

Main Class:

public class CinemaSystem {


    public static void main(String[] args) {


        // Creating a cinema.

        Theatre cinema1 = new Theatre();


        Scanner scan = new Scanner(System.in);
        Scanner scan1 = new Scanner(System.in);
        System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
                + "\nWould you like to purchase tickets or list available seats?"
                + "(/Purchase/List/Help)");
        String answer, answer2;

        answer = scan.nextLine();
        int count = 0;


        if (answer.equalsIgnoreCase("purchase")) {
            cinema1.DisplayTheatre(Seat 
            y[][]
          );


            } else if (answer.equalsIgnoreCase("list")) {
        } else if (answer.equalsIgnoreCase("help")) {
            // Code for help
        } else if (answer.equalsIgnoreCase("quit")) {
            System.exit(-1);
        } else {
            do {
                System.out.print("Sorry, incorrect input please enter"
                        + " a valid input (Purchase/List/Help or QUIT to exit");
                answer = scan1.nextLine();
                count++;


                if (answer.equalsIgnoreCase("purchase")) {
                    // Code for purchase  
                } else if (answer.equalsIgnoreCase("list")) {
                    // Code for list   
                } else if (answer.equalsIgnoreCase("help")) {
                    // Code for help
                } else if (answer.equalsIgnoreCase("quit")) {
                    System.exit(-1);
                } else if (count == 3) {
                    System.out.print("Are you stupid? Purchase/List/Help or Quit are your options. ... Terminating...");
                    System.out.println();
                    System.exit(1);
                }
            } while (!answer.equalsIgnoreCase("purchase")
                    || !answer.equalsIgnoreCase("list")
                    || !answer.equalsIgnoreCase("help")
                    || !answer.equalsIgnoreCase("quit"));




        }
    }
}

Seat Class:

public class Seat {


    public Seat() {
    }

    private String type;
    private boolean status;


    public String GetType() {
        return type;
    }

    public void SetType(String x) {

        type = x;
    }

    public boolean SetStatus() {
        return status;
    }

    public void GetStatus(boolean x) {
        status = x;
    }
}

Theatre Class:

public class Theatre{

    public Theatre() {
        Seat B = new Seat();
        Seat S = new Seat();
        Seat G = new Seat();



        Seat y[][] = {
            {B, B, B, B, B, B, B, B},
            {B, B, B, B, B, B, B, B},
            {B, B, B, B, B, B, B, B},
            {B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B},
            {B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B},
            {B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B},
            {B, B, B, B, S, S, S, S, S, S, S, S, S, S, B, B, B, B},
            {B, B, B, B, S, S, S, S, S, G, G, G, G, G, S, S, S, S, S, B, B, B, B},
            {B, B, B, B, S, S, S, S, S, G, G, G, G, G, S, S, S, S, S, B, B, B, B},
            {S, S, S, S, S, S, S, S, S, G, G, G, G, G, S, S, S, S, S, S, S, S, S},
            {S, S, S, S, S, S, S, G, G, G, G, G, G, G, G, G, S, S, S, S, S, S, S},
            {S, S, S, S, S, S, S, G, G, G, G, G, G, G, G, G, S, S, S, S, S, S, S},
            {S, S, S, S, S, S, S, G, G, G, G, G, G, G, G, G, S, S, S, S, S, S, S}
        };
    }

    public void DisplayTheatre(Seat x[][]) {
        for (int row = 0; row < x.length; row++) {
            for (int col = 0; col < x[row].length; col++) {
                System.out.print(x[row][col]);
            }
            System.out.println();
        }

    }
}

So since this doesn't work, I know I am going on about it the wrong way, how would I be able to print out the array that is in the Theatre classes' constructor?

因此,由于这不起作用,我知道我正在以错误的方式进行,我将如何打印出Theater类构造函数中的数组?

Any help will be greatly appreciated, thank you in advance. ^^

非常感谢任何帮助,谢谢你提前。 ^^

1 个解决方案

#1


0  

You are doing three things wrong.

你做错了三件事。

  1. You are doing nothing to differentiate the objects, you call new Seat() for all three but you don't ever call setType() to change their values, so they would all print the same way. You might want to consider having a constructor Seat(String type) and load the value there.
  2. 你没有做任何事情来区分对象,你为所有三个调用新的Seat(),但你不会调用setType()来改变它们的值,所以它们都会以相同的方式打印。您可能需要考虑使用构造函数Seat(String类型)并在其中加载值。

  3. Java naturally has the ability to turn all Objects into Strings by the native method Object.toString() If you want to have your System.out.print(x[row][col]); print something other than the default Object output (this is almost always the case), you either need to explicitly call a method that returns the string you want (for example, Seat.GetType(), or create a method public String toString() in your seat class.
  4. Java自然能够通过本机方法将所有对象转换为字符串Object.toString()如果你想拥有你的System.out.print(x [row] [col]);打印除默认对象输出以外的东西(这几乎总是如此),您需要显式调用返回所需字符串的方法(例如,Seat.GetType(),或创建方法public String toString()在座位上。

  5. The variables in your constructor are all in local scope to the theater class. So after the Constructor finishes, they all disappear. Also, since in theater you seem to want to define the array as part of the theater object, you probably shouldn't be passing it as an argument, you should be just working with the field in the class. (Summary version: move the variable declarations outside the constructor)
  6. 构造函数中的变量都在剧院类的本地范围内。所以在构造函数完成后,它们都会消失。此外,因为在剧院你似乎想要将数组定义为剧院对象的一部分,你可能不应该将它作为一个参数传递,你应该只是在课堂上使用该字段。 (摘要版本:在构造函数外部移动变量声明)

#1


0  

You are doing three things wrong.

你做错了三件事。

  1. You are doing nothing to differentiate the objects, you call new Seat() for all three but you don't ever call setType() to change their values, so they would all print the same way. You might want to consider having a constructor Seat(String type) and load the value there.
  2. 你没有做任何事情来区分对象,你为所有三个调用新的Seat(),但你不会调用setType()来改变它们的值,所以它们都会以相同的方式打印。您可能需要考虑使用构造函数Seat(String类型)并在其中加载值。

  3. Java naturally has the ability to turn all Objects into Strings by the native method Object.toString() If you want to have your System.out.print(x[row][col]); print something other than the default Object output (this is almost always the case), you either need to explicitly call a method that returns the string you want (for example, Seat.GetType(), or create a method public String toString() in your seat class.
  4. Java自然能够通过本机方法将所有对象转换为字符串Object.toString()如果你想拥有你的System.out.print(x [row] [col]);打印除默认对象输出以外的东西(这几乎总是如此),您需要显式调用返回所需字符串的方法(例如,Seat.GetType(),或创建方法public String toString()在座位上。

  5. The variables in your constructor are all in local scope to the theater class. So after the Constructor finishes, they all disappear. Also, since in theater you seem to want to define the array as part of the theater object, you probably shouldn't be passing it as an argument, you should be just working with the field in the class. (Summary version: move the variable declarations outside the constructor)
  6. 构造函数中的变量都在剧院类的本地范围内。所以在构造函数完成后,它们都会消失。此外,因为在剧院你似乎想要将数组定义为剧院对象的一部分,你可能不应该将它作为一个参数传递,你应该只是在课堂上使用该字段。 (摘要版本:在构造函数外部移动变量声明)