简体   繁体   中英

the ticket class not being recognized?

I am a beginner and I have to make a program which allows the user to select multiple types of tickets and quantities then prints them a total. I tried using classes but it won't recognize the instance of my class. If you also have any suggestions on how I can make a better ticketing program please let me know as I am also having a hard time understanding what the best way to approach this is.

import hsa.Console;
import java.awt.*;
import javax.imageio.*;
import java.io.*;

public class raps
{
  static Console c;
  static Image image;
  public static void main(String[] args)
  { 

    c = new Console();
    image = null;

    //Copy and paste this below every time you want to put in a new picture

    {

      ticket first = new ticket();
      ticket.selection = 'A';
      ticket.ticketType = "Courtside Prime";
      ticket.price = 360.00;

      ticket second = new ticket();
      ticket.selection = 'B';
      ticket.ticketType = "Baseline Prime";
      ticket.price = 106.50;

      ticket third = new ticket();
      ticket.selection = 'C';
      ticket.ticketType = "Box Seat";
      ticket.price = 97.00;

      ticket fourth = new ticket();
      ticket.selection = 'D';
      ticket.ticketType = "Lower Bowl";
      ticket.price = 94.50;

      ticket fifth = new ticket();
      ticket.selection = 'E';
      ticket.ticketType = "Upper Bowl";
      ticket.price = 91.00;

      ticket fifth = new ticket();
      ticket.selection = 'F';
      ticket.ticketType = "Drake Zone";
      ticket.price = 50.00;



      /*c.println(" A | Courtside Prime | $360.00 ");
      c.println(" B | Baseline Prime  | $106.50 ");
      c.println(" C |     Box Seat    | $97.00 ");
      c.println(" D |    Lower Bowl   | $94.50 ");
      c.println(" E |    Upper Bowl   | $91.00 ");
      c.println(" F |    Drake Zone   | $50.00 ");
      c.println("Enter the letter of the seat section you would like to select");
      */

      class ticket{
        char selection;          //The Letter of Selection The User will choose ex. A, B, C etc.
        String ticketType;      //The type of ticket the user will choose ex. Courtside Prime, Baseline Prime etc.
        double price;          //The price of each ticket

      }




    }





  }
}

Java

I think there are two basic issues with how you have it set up. Moving the Ticket class out of the method is mostly my preference.

public class Raps {
  static Console c;
  static Image image;

  static class Ticket {
    char selection;          //The Letter of Selection The User will choose ex. A, B, C etc.
    String ticketType;      //The type of ticket the user will choose ex. Courtside Prime, Baseline Prime etc.
    double price;          //The price of each ticket

  }


  public static void main(String[] args) {
  ...

Your code is using the class name rather than the variable.

        Ticket first = new Ticket();
        first.selection = 'A';
        first.ticketType = "Courtside Prime";
        first.price = 360.00;

And a best-practice: Name classes starting with an upper-case letter. This would have eliminated some of the confusion between classes and variables.

Avoid putting too much code in static methods. Later on when you start using inheritance (extends) you won't be able to override static methods and you will most likely have to do some refactoring.

Change main to be something like

private Console c;
private Image image;

public static void main (String[] args) {
    Raps raps = new Raps();
    raps.createTickets();
}

private void createTickets() {
    // all of your code here
}

Read it as procedural code since it is inside a static method. ticket first = new ticket(); doesn't know about what ticket is until reaching this line class ticket{ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM