• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Basic guidance on how to implement Java class function effectively  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to java programming and I am currently implementing a project. within the project, there should be a booking  Gym class that allows users to select a lesson on a particular day. and change the lesson. I have the code on how to implement this but I just need some guidance on how to implement the function effectively as I receive errors and spend hours trying to solve the errors. As I am a new programmer I find it difficult to solve flaws within the code on the spot

My code is below

The array of the list of gym classes that a customer will choose








I have created a String to string method that returns the booking confirmation




Is there a more effective way to implement a booking class for the Gym lesson functionality. also, I would like a user to book a lesson on a certain day such as any day in the week and be able to change the lesson using a switch loop.   I would just like some basic guidance on making the functionality above more effective and implementing a function for booking lesson on a certain day and changing lesson using switch loops, please?
 
Bartender
Posts: 5307
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you "received errors" we need to know *exactly* what those errors are. Please post the *entire* error message.

We will also need to see the complete code so that we can match the line numbers in the error message to the lines of code.
 
Marshal
Posts: 5634
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Zara bailey!

While I agree with Carey that you should post entire classes, here are some things I've noticed:
There should never be a semicolon (;) in the condition part of an if statement.  You also should use the conditional OR (||) instead of the bitwise OR (|).
Unless you need the variable zumba elsewhere, I would just write...
I would use String.format(...) in the toString() method.

 
Zara bailey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for the quick responses, apologies for not posting full code as I did not want to clog the post with code. After I have implemented some changes I will attach files. Thanks
 
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Zara bailey wrote: . . .. . .

As well as the compiler error Knute pointed out, there is an out‑by‑one error in that line; I challenge you to find it.

Please find out about array initialisers: I think this is a quicker and less error‑prone way to create an array if you know its contents in advance:-Why have you got spaces at the beginnings of those String literals?
 
Marshal
Posts: 6363
437
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Zara bailey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am still not 100% confident at grasping java yet but things are starting to piece together slowly.  However, I am taking forever to implement functionality to a booking that needs to be done and I would a appreciate a little guidance in how to link together the functionality that I am struggling with. The structure I have used for this code is the main functionality in the controller and basic setters and getters in other classes..  Ok for the implementation of the booking system I have a rough idea but just need some guidance on how to combine all this code in the controller.   please see code and pointers below




this is the controller that I will have to add functionality on from below,   I just need a basic direction on how as I have a rough idea but then again I do not have any idea how to for other functionalities. please see pointers at the end of the code



public class BookingController {
[/code]



Ideas and questions for functionality


Adding a lesson status method.
1. after a user selects a lesson a message should show the status of attendance so I am thinking to implement a status method with an If statement with a decision variable that determines if the array list was selected. Then system.out.print ( you are attending class )  but I am not too sure how to show if the user has returned from the lesson?

2. The customer can change the selected fitness class
do I implement this by using a switch statement in a loop? and allow the user to switch and insert new input

3. the user can select specific data and time for a lesson
I have no clue how to start with this functionality any suggestions on how I can tackle this?

4. if a class has 20 customers the book will be unsuccessful

My first thought on how to approach this is to implement a counter variable that will count the instances of the lesson list Array? them implement if statement with expression > 20  ...system.out print( "this lesson is fully booked ")   but I do not think this is the way? any suggestions, please?


Staff note (Knute Snortum):

THe dos and don'ts about starting new threads: DON'T start a new thread if you're posting about the same subject. DO start a new thread if your post has nothing to do with the subject.

 
Carey Brown
Bartender
Posts: 5307
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will cause some trouble. Can you see why, if y > lessonList.length, you'll end up in an infinite loop.

Other problems with this:
  • Use conditional OR "||" not bit wise OR "|".
  • If lessonList.length is '4' and you enter '4' you will get an out of bounds exception because the last element in the array has an index of '3' because arrays are zero based.
  •  
    Master Rancher
    Posts: 920
    17
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First a friendly tip, in a couple of places in your code you use == to compare a string or a char, == dosn't compare the value, but the object itself, so it's safer to use the .equal method.
    A couple of things you asked are not quite clear to me what you mean, but I do have a couple of pointers for you.


    1. after a user selects a lesson a message should show the status of attendance so I am thinking to implement a status method with an If statement with a decision variable that determines if the array list was selected. Then system.out.print ( you are attending class )  but I am not too sure how to show if the user has returned from the lesson?

    I'm not sure what you mean by attent, do you mean sign up or actually being there, if it's signing up I will explain in the last question.

    2. The customer can change the selected fitness class
    do I implement this by using a switch statement in a loop? and allow the user to switch and insert new input


    At what point can the user changes classes? You could create a menu that provided the users with the functionality and then create a switch based on that.
    For example:

    3. the user can select specific data and time for a lesson
    I have no clue how to start with this functionality any suggestions on how I can tackle this?


    these are some code examples on how to make an appointement calender in java

    4. if a class has 20 customers the book will be unsuccessfull


    You have a customerClass too most likely? In production you would have a db and join the two tables, for now I would keep a list of customers in your lessonClass, before you sign up a new customer you check the lenght of that list to see if it's still smaller then 2O. With that list it's also verry easy to remove or add a customer, if that is what you ment with question one, you can use the remove(Customer)
     
    Carey Brown
    Bartender
    Posts: 5307
    55
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you tried to compile and test what you've written so far? Remember to test with both proper answers and also unexpected answers, such as out of range values or entering a letter when an int is expected.
     
    Carey Brown
    Bartender
    Posts: 5307
    55
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:First a friendly tip, in a couple of places in your code you use == to compare a string or a char, == dosn't compare the value, but the object itself, so it's safer to use the .equal method.

    I don't see where the OP is comparing a String using ==. There are a couple of places where a char is compared using == but that is actually correct.
     
    Campbell Ritchie
    Marshal
    Posts: 62231
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why have you got such a long main() method?
    Why are you marking your other method static?
    What have you been taught about loops? As well as the infinite loop problem Knute pointed out, the loop in lines 21‑32 might work, but that isn't the right way to write the loop, nor do I think the logic for finding whether the user says they are over 18 is correct. You don't want to repeat the test until they say they are over 18.
     
    Zara bailey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi , I have taken all your suggestions on board and have been reimplementing the code in readable suitable chucks. before starting the project I have read up on the basic of java, but I did not realize how much gaps of knowledge there is during implementing an actual project.  I would like the system to run smoothly on the terminal for the user, however, the code stops after loops because I am not too sure how to invoke method arrays? is there another technique I tried the basics of calling array like details.arrayList()



    but I have never invoked a method that has an array within the method ... I have tried searching online but example got confusing as I can understand once I have done it and modeled around my code.

    I have tried to set a parameter to the array method such as " (int x []) "   and calling that but did not work as i am not too sure what to do, can someone please clarify so I know what to do for future reference.

    the code I am working on is below

     
    Campbell Ritchie
    Marshal
    Posts: 62231
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Zara bailey wrote:. . . I have tried to set a parameter to the array method such as " (int x []) " . . .

    Line 16? I thought that bit looked peculiar.
  • 1: The [] belong before the name of the array. The type is int[], so you should write int[] numbers
  • 2: I am sure you could find a better name for an array than x.
  • 3: You declare a parameter when you write the method. That parameter has both a type and a name.
  • 4: You pass an argument when you call the method. The argument has only a name, though sometimes it is possible to pass a value directly, e.g. myObject.myMethod(123);
  •  
    Rancher
    Posts: 1920
    15
    Android Eclipse IDE Java Java ME
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all I don't think customerDetails method needs to have a int return type because customerAge is an instance level variable.  So the method may be somewhat like this.
     
    Campbell Ritchie
    Marshal
    Posts: 62231
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Swastik Dey wrote:. . . I don't think customerDetails method . . .

    Looking at what you supplied made me think, is that supposed to be a constructor? Or should it be replaced by a factory method? You could write this sort of thing:-I personally would have a utility class to wrap the Scanner for keyboard input, so I could write KeyboardInputs.nextInt("Please enter mileage: "); or similar.
     
    He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!