简体   繁体   中英

BlueJ Error: Incompatible types: int cannot be converted to java.lang.String (Ticket Machine Simulation)

I'm very new to java and after trying the best I can to create a ticket machine in bluej, I literally have no idea what to do.

 public class CarParkTicketMachine
{
    private double RunningBalance;
    private double CurrentBalance;
    private String CarReg;
    private static double MinTicketCharge = 0.7;

    //Constructor
    public CarParkTicketMachine()
    {
        RunningBalance = 0;
        CurrentBalance = 0;
    }

    //Try and request a ticket
    public void RequestTick()
    {
        if(Validate())
        {
            //Create a ticket for the customer
            CarParkTicket ticket = new CarParkTicket (CarReg, CurrentBalance);
            //keep a running total
            RunningBalance += CurrentBalance;
            //clear the customer's balance
            CurrentBalance = 0;
            //print ticket for the customer
            ticket.PrintTicket();
        }
    }

    //Perform check to ensure we can purchase a ticket
    private boolean Validate()
    {
        boolean valid = false;

        //Check that the user has entered their car registration
        if(!(CarReg == null || CarReg.isEmpty())) //must NOT be null
        {
            //Check that the user has entered enough money
            if(CurrentBalance >= MinTicketCharge)
            {
                return true;
            }
            else
            {
                System.out.println("You have not entered enough money");
            }
        }
        else
        {
            System.out.println("Please enter your car registration");
        }
        return false;
    }
}

public class CarParkTicket
{
    //Customer's Car Registration
    private String CarRegistration; 
   //Cost of the ticket
   private double charge;

   public CarParkTicket (String CarReg, double charge)
   {
       CarRegistration = CarReg;
       double Charge = charge;
    }

    public void PrintTicket()
    {
        System.out.println("*****************");
        System.out.println("Your Ticket");
        System.out.println("Registration:" + CarRegistration);
        System.out.println("Cost: £" + charge );
        System.out.println("*****************");
    }
}

public class Coin
{
    //property to hold the value of the coin
    private double Value;

    //Constructor
    public Coin(double value)
    {
        this.Value = value;
    }

    //accessor
    public double GetValue()
    {
        return Value;
    }
}

My three classes compile without any problems however when I create a new object of the 'CarParkTicketMachine' and try to enter a method call for setting the car registration or inserting a coin I get a 'Error: cannot find symbol - variable.

I feel that the problem lies in this part, but because I'm incredibly new to java/bluej I really have no idea how to solve this issue:

private Boolean Validate()
    {
        boolean valid = false;

        //Check that the user has entered their car registration
        if(CarReg == (" "))
        {
            //Check that the user has entered enough money
            if(CurrentBalance >= MinTicketCharge)
            {
                valid = true;
            }
            else
            {System.out.println("You have not entered enough money");
            }
        }
        else
        {
            System.out.println("Please enter your car registration");
        }
        return valid;
    }

Here is my suggestion for the Validate(): Note that you are trying to check if the user has entered " " and then check for the cash entered. But you need to be sure the user has NOT entered " " (which means he entered a number or a name)

private bool Validate()
{

    //Check that the user has entered their car registration
    if(!(CarReg == null || CarReg.isEmpty())) // must NOT be null
    {
        //Check that the user has entered enough money
        if(CurrentBalance >= MinTicketCharge)
        {
            return true;
        }
        else
        {
          System.out.println("You have not entered enough money");
        }
    }
    else
    {
        System.out.println("Please enter your car registration");
    }
    return false;
}

Edit: I should tell you that after the "return true", the method stops executing. So when you do not return true (which means the if statement for cash was not true), the method goes to the end where it returns false.

You can make your if statement shorter by using a so called "tenary operator".

This should look like so:

return CurrentBalance >= MinTicketCharge ? true : false;

You have your if-statement and your return in one line.

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