Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Getters and Setters to store user input

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody. I have this school project where I need to create this simple inventory system and I need to use getters and setters. I created mine fine. I just need some help regarding how to use them when the user is asked to enter the description or the product weight for example. Find code below :

And here is part of my main menu where the user is asked the questions :
 
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for attempting to use code tags but you didn't get it quite right. The code needs to come between the tags. Please re-read UseCodeTags (<-link).
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use "Double", use  "double". Consider using BigDecimal for currency variables.
Make your field variables "private".
Decide on a case-by-case basis which fields really need a setter. For example, once an ID is established it should never be changed so: it should be set in the constructor, and it should  NOT have a setter.
Consider renaming "itemID" to just "id". Almost always the usage will tell you what kind of "id" is being referred to, e.g. "product.getId()". Ditto for "itemDescription". Just use "description".
In main() some times you refer to "item" and sometimes "product", be consistent.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Unique" and "random" are inconsistent. Best to have a variable holding a lastUsedId that you increment as you need the next one.

This could be a private static variable of Product. Then you'd need a method: public static int getNextId().
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply and helpful comments. Below find one of my menus. I just need some guidance how to use the getters and setters to the user inputs. Thnanks
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It looks like you are trying to create a space delimited line of entries. This will fail because a description can contain spaces.

As a general design point, I would take all your inputs and construct a Product object and then write that object out to a file using Product's toString() method. I'd suggest using a bar (|) character as the delimiter as that shouldn't happen. When you want to read the file, take each line and pass it to a constructor Product( String line ) that can parse the line and construct a Product object.

When doing anything with files put the code into separate methods, one for reading and one for writing. Always use try-with-resources when opening files.

When doing a text based user interface most seasoned developers use a Console or Keyboard utility class, it makes the code MUCH easier to read and provides its own error handling. There's a link to the source code at the end of my signature below. It should have plenty to get you started but it is designed to be added to or subtracted from to suit a project's needs. For example:
could be reduced to:
Note: I don't think promptDoublePositive() is currently in the utility but there is a promptDoubleRange() which could be copied and modified. You'll also see in the utility code that handles improper input and will re-prompt the user until it is correct, and only then return.
 
 
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good grief! What a long method.
Look at this FAQ, which tells you the ideal length for a main() method is one statement. You can probably divide that code into at least six other methods.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help ☺️ Regarding the getter and setter can anyone put me in the right direction how can they be used in the main() please? Thanks
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far I don't see the need to use it in main(), but you should still keep them in the Product class, except for id, which should have a getter but no setter.
In main() take all the values you get from prompting the user and put them in local variables first. Then construct a Product like
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you can have a method
That will append product to the file using Product's toString() method which you've written to output a bar (|) delimited record.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit lost regarding the to string() method. I searched and saw that I can use serialisation too. Is that right? If you can show me a bit of an example it would be really helpful so I can understand better and implement it to the rest of my project. Thanks
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case serialization is way overkill. A simple CSV (comma separated variable) filie (or in your case, a bar (|) separated variable file) would keep it simple and clean.
In your toString() method just concatenate all the fields into a long string separated by bars (|). Then append that string to a file
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization is something you start to consider when objects contain other objects.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. can you show me just one example to understand please. As you have seen from my coding I'm storing the user input in variables and then in an Array List and then saving it to a .txt file on my hard drive. Sorry for asking but I understand better with a shown example as I'm relatively new to Java. As said my objective is to ask the user, store his inputs (using the getters and setters ), and then saving to a file which is used to read if wanted. Thanks
 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Warren Griscti wrote:I'm a bit lost regarding the to string() method . . .

Hasn't somebody taught you about toString()? See Joshua Bloch Effective Java 2nd edition=page 51, 3rd edition=page 55.
Write something that will return a human‑intelligible representation of the object.

I searched

Careful: you never know whether you will find something really good or something unhelpful.

. . . I can use serialisation too. . . .

Why? Unless you already know where you are going to serialise an object, I think that would probably cause confusion at this stage.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A row in your text file would look like
Your toString() method must take these fields and build that String.
   int itemID;
   String itemDescription;
   Double price;
   Double sellingPrice;
   int stockLevel;
Then your write method will take one (or more?) Product objects, call its toString() method, and write the string to a file.

When it comes time to read the file you'll read each line and split it into fields by using String.split("|") with a sprinkle of type conversion.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your toString() method would start out like
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that this doesn't handle your use of "Null" to indicate a field that is not valid. You have two choices there. You can put in extra logic into toString() to detect invalid values and output "Null" or you can designate a specific value, such as 0.00, to indicate null. I would choose the second approach if it were me.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using the null value when the user chooses not to input for example the selling price and if he chooses to enter it later he searched by ID and updates it
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, fine. But what will you write to the file?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the user chose to not enter a value I was writing "Null" and then I had a method to search for the Null at the specific index and replace it with the value the user wished to enter
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you don't intend on writing "Null" to the file then why accept it as input?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was writing "Null" to the file as text so there won't be an empty space in the file. I also would like the file to look in tabular form with the headers itemID, Description and so on so it is readable not just a bunch of data and you don't know which is which but I don't know how to print them in tabular form too
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Data files are not designed to be tabular. For a tabular display add a report/display/print method to your project that will do that. How are you going to deal with Null in your Product object?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was doing this to understand me better.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mentioned before that in your case splitting on a space won't work.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using startsWith() to find the ID is an accident waiting to happen. What if you searched on "1"? It would also find "12345". ID should be an int, not a String and you should use an int comparison.

 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Warren Griscti wrote:I was doing this to understand me better.
<code>


A problem with this code is that you will have to write out ALL the lines, not just the line that matches.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AM I ON THE right track ?

It's telling me to initialise consumables
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should do away with consumables and use Product instead. Make local variables for: id, description, price, etc.. Fill them in with user responses. Then use them to construct a Product object. You can make a List of these objects to write or write one at a time.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to have two product types which are consumbles or hardware and they have different input options such as one has an expiry date whereas the other has to have dimensions and weight.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Warren Griscti wrote:I need to have two product types which are consumbles or hardware and they have different input options such as one has an expiry date whereas the other has to have dimensions and weight.

That's a new requirement to me. Can you explain further?
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I see the references in your original post. Why aren't these fields in your Product class?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok right so I'll explain exactly what is expected of me. I need to do a basic inventory system where the user enters the product has a unique id, description, purchase date, stock level, cost and selling price... the user has to choose to enter either a consumable or hardware product where as said before they have some different input options. the program needs to save to file and read from file. Also the user has to have the option to enter stock level, cost price and selling at later stage
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are supposed to use inheritance so I had to create two other classes for consumbles and hardware which extend Product
 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't sound too difficult; what do those classes look like?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find parts of my program below:
Consumbles :

Hardware:

Updating stock:


Any help appreciated or guidance with examples with my code. Thanks
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Using startsWith() to find the ID is an accident waiting to happen. What if you searched on "1"? It would also find "12345". ID should be an int, not a String and you should use an int comparison.

 
Campbell Ritchie
Marshal
Posts: 72479
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . String.split("|") . . .

Isn't | a meta‑character which needs escaping?
 
no wonder he is so sad, he hasn't seen this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic