• Post Reply Bookmark Topic Watch Topic
  • New Topic

How am I doing! Suggestions?  RSS feed

 
Luzie Blankenship
Greenhorn
Posts: 7
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my first post as I'm starting to get outrageously addicted too this, I never thought I would find an interest strong enough to pull me away from a life of video games! So, like anything I have an interest in, I decided to go ahead and get involved in a like-minded community. Anyways, over the last few days I've been losing sleep over the amount of learning I've been doing and with no formal training in the art of the code it was a rough start but nonetheless fun and enjoyable.

Below, is what I've managed to spew out of my ever confusing mind over these last few days of learning. The program is a "Budget Assistant" of my own design and effort that has also doubled as a learning tool for me thus, as I learn it changes and improves. I do use this too manage my actual money so I've definitely had fun working on something of actual use and have taken what most here will see an awful lot of time working on it (About 10 days nonstop, it's currently 2:02AM). As you see in the To-Do List, I have a lot to do but I am also dying to hear what more veteran coders have to say and suggest. Thank you in advance!

*Take note, As this does involve my real budget, I have omitted some data from the code.

 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First welcome to the ranch and I'm glad that you decided to join the rest of us.

To me, it looks like you are well under way.
I do have a few suggestions as noted below which may help out:
  • Instead of using an IF construct you may be able to use the SWITCH/CASE construct.
    I think that many people would agree that switch constructs are easier to follow then if constructs especially when you get into many choices.
  • You may want to pull the individual statements out of the IF/ELSE IF blocks and make them methods. So


  • Becomes this

    with this calling it later on:

    That way you can concentrate on one method at a time and you can get into code reuse in the future.

    Currently, as many beginners do, you have placed everything in the main method.
    While this does work, it is not really suggested when you get into programs that are more then let's say 50 - 100 lines.
    Depending on who you ask and what you are trying to do these values can vary.
    Java is very much based on object oriented programming. So you may want to research how that affects your programming.
    Object oriented programming can be a rather large topic and there are many books on this in general, so for now I would suggest that just keep it simple.
    Object oriented programming can take months if not years to master.

    With regards to using text files:
    There is absolutely nothing wrong with doing that when programming, especially when starting out. That being said, many of the larger/more advanced programs do use a database of some sort, and you can use a text file as a database source.
    There are many volumes on databases in general and there volumes on how to use databases with Java, that is what the JDBC/SQL stuff is all about.
    When just starting out I would suggest that you use text files as database programming can take months if not years to master.

    Keep up the good work and remember that it is easy to start programming, but it can take years to master programming. So don't be discouraged when you are learning  programming.
     
    Pete Letkeman
    Ranch Foreman
    Posts: 917
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One last thing. It has been recommended other Java masters, such as Jeanne Boyarsky, that if you are just starting out and you want a book to help you along then try the Head First Java Book by Bert Bates and Kathy Sierra.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7993
    143
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to CodeRanch Luzie!

    I'm happy you're having fun with coding. There are some major improvements you can make to the code. The first problem that stands out the most is that you're not using Java's most important feature: classes. Java is an object oriented language, and it is expected that you divide up your program into parts that each take on a separate responsibility. Right now you have one huge main() method that does absolutely everything. While impressive, it's also hard to read, understand and maintain. Methods should rarely be longer than 10 to 15 lines. Yours is almost 200. you should create a model of your problem before you worry about reading input or processing commands. You could start with a Budget class that allows the client to add income and deduct expenses of a certain category. Here's what such a class could look like:

    What are BudgetEntry, IncomeCategory and ExpenseCategory? These are also types that you create. By separating code over these types, everything becomes more easy to understand.

    Some other issues you should work on:

  • Classes should be final, unless you have a good reason to extend them.
  • Don't use the File class. Use java.nio.file.Path for file paths, and use Files.newBufferedReader() and Files.newBufferedWriter() to open files for reading and writing.
  • When using resources such as readers and writers, use a try-with-resources statement to automatically close them.
  • Close resources as soon as you're done with them. Don't keep a file open for the entire duration of the program. Open, read, close and *then* process.
  • Don't hard code file paths. Your program doesn't work on any other machine than yours.
  • Don't require files to exist. I need to manually create those files before the program will run.
  • Write out identifiers in full. FC doesn't mean anything. costOfFood on the other hand does. Characters don't cost anything. Use them.
  • Don't use double to represent money, or other exact amounts. Instead, use BigInteger and BigDecimal.
  • Why are you writing all those "OMITTED" lines? Even if it's intended, why don't you use a loop to do it?
  •  
    Luzie Blankenship
    Greenhorn
    Posts: 7
    C++ Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems I have a lot to learn! Thank you for the info as you all made valid and helpful points.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7993
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Code should almost never contain hard coded sensitive information. Sensitive information should be entered by a user, or retrieved from an encrypted file or a secure connection.

    What kind of sensitive information are we talking about here?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!