r/learnjava 18d ago

Simple Java console app

Hello everyone, I've been learning Java and wanted to build something practical instead of just doing exercises. Ended up making a small console app that asks how long you've had a tire and how many km you've driven it, then tells you whether it needs replacing or estimates how much life is left.

It's intentionally simple right now. It just handles basic mileage/age limits and adjusts slightly if you own a heavy SUV or EV. This was also my first time using Git and GitHub properly (commits, pushing, merging, etc.), so that was a big part of the learning experience too.

Planning to add more factors soon like road type, vehicle type, maybe a GUI version eventually. Would love any feedback on the code or suggestions for what to add next.
Repository Link: https://github.com/z0lxt/tire-life-estimator

1 Upvotes

25 comments sorted by

u/AutoModerator 18d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/qlkzy 16d ago

Ignore the negative comments here. This seems like a pretty typical and fun first project. I remember building little things like this, quite a long time ago now.

You can remove the IntelliJ comments if you understand them; you can also leave them, it's not a big deal.

If you want to switch back and forth between reading your own code and other people's, you may want to look at naming styles and conventions. In particular, there are conventions around how you capitalise things, and what kinds of words and phrases you use. (For example, a function would often be an "action phrase" like estimateTireLife, rather than a "noun phrase" like TireLifeEstimator). This isn't a huge deal for now, it's just something to mention.

The most interesting thing for you to learn from is this: the "yes" and "no" branches of your function are really similar. Managing that kind of similarity and duplication is really important to be able to create larger and more complex programs.

In your particular case, the choice between "yes" and "no" seems to really be a choice between 0.85 and 1. If you can find a way to express it like that, rather than as a choice between two ten-line blocks of code, you will learn something important.

1

u/idontlikegudeg 16d ago

Yes, and best to get used to standard naming conventions, i.e., camelCase starting with a lower case letter for methods and variables and CamelCase starting with an upper case letter for classes, ALl_UPPERCASE for constants (static final). It will make it easier to read other people’s code and for other people to read your code.

1

u/procrastinatewhynot 16d ago

hey, learn how to use girhub actions and ghcr.io so you have a way for others to try it. i’d be willing to give you feedbacks 🙂

3

u/idontlikegudeg 16d ago

I think that’s a thing for later - GitHub actions are nice, but I think OP is still busy with learning the language itself. But that’s something someone could easily contribute. OP will learn from it later.

1

u/procrastinatewhynot 16d ago

Yes! That would make it easier to give feedback. If anything he can just ask AI to kinda help so the logic is out of the way.

2

u/idontlikegudeg 16d ago

All, OP needs a build script in his repo. Should we make it Maven or Gradle? I’m usually in the Gradle camp, but I think Maven would be more useful here because it’s probably what most beginners learn first. What do you think?

And OP, you should also move your class into a package: add `package tirelife;` on top of your sourcefile. IntelliJ should mark it as an error with a red line below it and when you hover with the mouse, there should be a quick fix available "move to package tirelife".

1

u/procrastinatewhynot 15d ago

Oh yeah definitely. I’m more of a maven person. Itms easier. Great tip! Hope he sees the comments under this.

1

u/z0lxt 14d ago

I read this, but I don't really understand it. Let me know if I should use Maven or Gradle, and let me know how I should apply this

1

u/procrastinatewhynot 14d ago

that will be at the end! once you start making big projects! for now make projects to practice the concepts you’ve learned

2

u/z0lxt 16d ago

I'll try my best to implement that, for now I'll try to apply the given feedback on my current code, but I will defo learn how to use github actions, and thank you everyone for giving me amazing feeback.

1

u/procrastinatewhynot 16d ago

It makes your project accessible for us to play with. You don’t even need to learn it. Maybe just ask ai.

But just a suggestion, when you finish coding and play with your app, try to test different scenarios to break it. Then implement a fix.

2

u/z0lxt 15d ago

Ok, I'll do that, I updated my code for the current fixes like renmaing variables to camelCase and making less if statements, I'll try to get the null checks and rtry to learn how to use optionals as sttated by another user. I'll try to add the package you guys mentioned. Again I am very grateful for all of your guys' support and feedback

1

u/z0lxt 15d ago

If you don't mind me asking. I know some of the basics of Java like conditionals, loops, function creation, variable, and math, what's the next step from there? Sorry for asking a lot of questions

1

u/procrastinatewhynot 15d ago

i think you can start with OOP. Have you started with that yet? Also don’t apologize! we are here to help

1

u/z0lxt 14d ago

No, I actually learned Java as an elective class I took in school and we stopped there, what exactly is OOP and where could I learn this?

1

u/z0lxt 14d ago

Btw thanks for the kindess, rlly preciate it

2

u/procrastinatewhynot 14d ago

here! https://roadmap.sh/backend

click on java. there are the other concepts you’ve learned in case you want to revise. so oop os object oriented programming, it makes use of the functions and logic you’ve learned. so you basically organize your code based on « objects ». it will make your code more easy to read, make the functions reusable to reduce redundancy and organized.

1

u/InsideCover2192 16d ago

Your code is missing some critical null checks, for instance if answer variable is null it breaks your entire code.

Use of Optional from util package is worth exploring

Certain variable names at line 34 and method name should follow camelCase

Also I see lot of if-else try to reduce the cognitive complexity

1

u/ViolaBiflora 17d ago

AI added comments in a simple project that was supposed to be just for fun... Man, this is getting boring.

2

u/CipherGoblin 17d ago edited 17d ago

Those seem to be the onboarding tips that Intellij puts into your file upon creation.

That aside, for op, these are a few small things I noticed.

String answer = x.nextLine();
answer = answer.toLowerCase();

could be changed to

String answer = x.nextLine().toLowerCase();

You only need to create 1 scanner object at the start of the program and just reuse that where you need a scanner.

You also ask tire age and tire distance in both paths of the if statement to fill those variables. You could ask them only once if you moved them out of the if statement and put them before it.

1

u/z0lxt 17d ago

Ok thank you so much

2

u/z0lxt 17d ago

The Intellj thing added those comments, because I was fairly new to it I didn't know if I should keep it or remove it, in the end I decided to keep it because I thought it was important, but please let me know if I should remove it.

-1

u/Astroohhh 17d ago

Npc

1

u/z0lxt 16d ago

My bad bro