• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Button problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have to write the following, program that creates an application
the application has buttons that allow the user to enter data for the year and month alone and if required they can click a button that enables the vat and goods txt fields
the information that is required is as follows:
● Year – integer value – greater than 2020 and less than 2050.
● Month – integer value – range 1 to 12.
● VAT Reference – string – minimum characters 10.
● Goods amount – float – should allow negative values.

The program should save to the class and safe to a txt file (complete)
My problem

I have so far created the method PVA with constructors getters and conditions
I have created the main method PVA3 that has the GUI elements.

My question is why are my conditions not working?
When I go to test there are no exceptions thrown, ie if I enter 12 for a year I don't get any errors.
I should be getting notified that it is out of the bounds of the conditions but they are being accepted by the main and passed to the constructors.
I have wrote all of this myself so far , it is all my own work , this is my 7th week learning Java so I apologise in advance for sloppiness




This is my method




here is my main method

 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget your GUI for the time being. Write a class to test your PVA class (not method) and test all its methods.
What does the name PVA mean? It means polyvinyl acetate to me, and it's a kind of glue. Please give all your code names that people can understand as soon as they see them, like vatreference, though I would spell that vatReference myself.
I am afraid we won't tolerate sloppiness, because somebody has to read your code, and the person who will have the most difficulty with it if it is hard to read is yourself. Your code is a lot better than a lot of what we see here, but the entire contents of your first class ought to be indented one more level to the right. Your second class is better for indentation.
The most serious error I can see is that all your class' members are marked static. With few exceptions, you should remove the keyword static from your vocabulary.
Be aggressive with errors. You can't cope with a negative amount, so don't print an error message. Throw an exception.Similarly if your VAT reference is null, throw a null pointer exception. Your input will have to handle those exceptions. Don't catch the exception in the same place as it is thrown (lines 59 and 78).
Look very closely at line 50, corret the indentation, and work out when line 51 might be reached.
Never write == false or == true, which are both poor style and error‑prone (if you mistakenly write =).
Never if (b == true) ... please.
Always if (b) ... please.
Never if (b == false) ... please.
Always if (!b) ... please.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just started to look at your code and here are some initial observations.
  • Your fields, lines 4-7, should not be static.
  • "goodsAmount" should be a double, not a float (check with instructor). Floats in general are no longer used except to connect to legacy code that requires it.
  • Once your fields are no longer static, change your use of "PVA." to "this.".
  • Line 30: a  setter should not be asking for input. Validating values here is good but it should throw an exception if something is wrong.
  • Your constructors are not doing any validation.
  • Line 32: Your boolean logic is incorrect.
  • Line 33: Throw an exception.
  • Line 12: Your class  is only inheriting from Object so calling super() is not necessary.
  •  
    Dave Bourke
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have taken on board both of your comments.
    Thank you for directing me to test the CLASS <- apologies
    I am having success and cleaning up my indentations
    I really appreciate you sharing your experience with me




    Thank you so much, BTW I can only apologize for my sloppy code I really am trying to improve.
    I really appreciate your tolerance of my inexperience,
    regards
    D
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's a good start
    Test your code bit by bit. If your previous amount was 100, try this sort of thing:-Note I have got the catch all wrong; you should normally never write an empty catch. In this case however, you expect an illegal argument exception, so the try‑catch written “backwards” will give you what you want: no output at all if the porgram works, and a load of error messages if anything goes wrong.
     
    Dave Bourke
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All my tests seemed to have passed

    here is the new PVA <(Price Vat Analysis) Class
    The instructions were to use float thanks for the feedback
    some of the loops were unnecessary

    my problem now is that i cant save the data to text it seems that nothing is being passed or the values are all 0 null??


    But now my GUIs dont work at all
    It passes my test Class




    I feel like i could be close but want to give up !
     
    Dave Bourke
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OMG it works!!!

    thank you so much if this was stack overflow i would have given up , you dont know how much a bit of positive encouragement means to a NOOB
    Thanks guys you rock
     
    Dave Bourke
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [quote=
    Never write == false or == true, which are both poor style and error‑prone (if you mistakenly write =).
    Never if (b == true) ... please.
    Always if (b) ... please.
    Never if (b == false) ... please.
    Always if (!b) ... please.



    This  is great advice thank you
     
    Carey Brown
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Variable and method names  MUST begin with a lower case letter, your Year() and Month() need to be fixed. Additionally, you separated the setting of the year from the validation of the year. Probably not a  good idea. Go back to validation in the setter but throw exceptions.

    In your constructors you can utilize the setters which will then provide validation to the constructors.
     
    Carey Brown
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are you sure you really want/need the no-argument constructor? If so, then all your business logic must be able to deal with its default values. Note that the default value for year will be zero, which is illegal.

    You're doing well.  Keep up the good work!
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Are you sure you really want/need the no-argument constructor? . . .

    Or the two arguments constructor? Both will allow the VAT reference to be null. Beware of nulls; they can cause you no end of trouble. But in the case of a VAT reference, there are lots of people who are not registered and haven't got a VAT reference. That may require special logic.
    Don't use floats, but notice that your amount() method doesn't return a float in the first place. Don't make setXXX() methods return anything.
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see you have been told to use floats
    Your interpretation fo between 2020 and 2050 is different from Carey's. I would think Carey's version is correct.
     
    Dave Bourke
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I see you have been told to use floats
    Your interpretation fo between 2020 and 2050 is different from Carey's. I would think Carey's version is correct.



    Yes we have been specifically told to use it, I think I might use it then after I do my tests as an improvement i will suggest double,
    The constructors are gone (Yes I was having the Null issues).
    I have changed the year entry and tidied up my code.

    Thank you this was a great learning experience( i think you learn more from your mistakes )
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Bourke wrote:. . . after I do my tests as an improvement i will suggest double, . . .

    That's a good idea. Another good idea is to learn about the two ways of doing precise arithmetic:-
  • 1: Integer arithmetic. The only wrong result you ever get is the effects of overflow.
  • 2: BigDecimal. If you search my posts for BigDecimal, you will probably find some examples.
  • If you use integer arithmetic for money, you would denominate the money in cents/pence/etc.

    Thank you this was a great learning experience( i think you learn more from your mistakes )

    That's a pleasure I agree; you learn more from mistakes and correcting them than from getting everything right first time.
    reply
      Bookmark Topic Watch Topic
    • New Topic