0

I have a question to make a program for Car Hiring Company. I am stuck in few questions and not sure about some of my answers, so I would appreciate it if anyone can help me.

At the beginning it required me to implement the status of a hire car using an enum type, with this information (current status (viz. on hire, available for hire, or being serviced). so I wrote:

private enum carStatus {On_Hire, Available_For_Hire, Being_Serviced};

Is this correct?

If so, we come to the question that I didn't know how to solve:

Write three public boolean methods isAvailable, isOnHire and isBeingServiced to determine information about a hire car’s status.

Then it asks to write methods hireOut, returnFromHire and returnFromService.

I don't know what it wants from this question? anyone can give me a tip ?

Can anyone help me how to solve this one please.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
ASD123
  • 3
  • 5
  • 2
    By convention, constant names should be in all-uppercase: `ON_HIRE`, `AVAILABLE_FOR_HIRE` etc. The enum name itself should start with an uppercase, as it is a type name (like interfaces and classes). Also, the semicolon (`;`) at the end is unnecessary. – RealSkeptic Apr 07 '15 at 20:33

1 Answers1

0

Yes, this is correct - the variable namings could be improved though (see: Coding Conventions - Naming Enums)

Since you have three enum-values and need a public boolean method, you can write them as follows:

public boolean isAvailable() {
   return carStatus.Available_For_Hire.equals(myCarStatus);
}

This will return true, if the current car status field myCarStatus equals the enumeration value Available_For_Hire. To make this work, you'll need to add the field myCarStatus of type carStatus (which is your enum) to your class:

private carStatus myCarStatus;
Community
  • 1
  • 1
flogy
  • 906
  • 4
  • 16
  • 2
    I'd prefer `==` over `equals` for enums. – RealSkeptic Apr 07 '15 at 20:39
  • Thank you very much, I am not sure if I get the the second part of ur answer,, do I add the code ( private carStatus myCarStatus; ) below my enum code to make it work ? – ASD123 Apr 07 '15 at 20:41
  • @ASD123 You theoretically can add it where you want, but usually, fields are placed before methods in the class. – flogy Apr 07 '15 at 20:46
  • Thank you very much, I will add another question related to this I hope you can help me with it. – ASD123 Apr 07 '15 at 20:49
  • @flogy look at the edit and if you know what to do please help me – ASD123 Apr 07 '15 at 20:52
  • @ASD123 Please add more specific requirements to your question. Like that one can only guess what you are trying to do... – flogy Apr 07 '15 at 20:54
  • @flogy The question is just like that,, write methods hireOut, returnFromHire and returnFromService.,, is there anyway I can contact you and show you the full question as there are some questions before they might be usefull but I don't know if they are related or no – ASD123 Apr 07 '15 at 20:56