• 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
  • 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

 
Saloon Keeper
Posts: 7999
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

Campbell Ritchie wrote:

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

Isn't | a meta‑character which needs escaping?


Not a meta-character so no escaping.
 
Carey Brown
Saloon Keeper
Posts: 7999
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:

Campbell Ritchie wrote:

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

Isn't | a meta‑character which needs escaping?


Not a meta-character so no escaping.


Ah. My bad. Yes it is, and yes it does.
 
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 everyone 😊 the bit I’m still struggling a bit is regarding the getters and setters as I have two types of products
 
Carey Brown
Saloon Keeper
Posts: 7999
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
Can you be a bit more specific with your question?

So, presumably you'll end up with a List<Product>, so now you have polymorphism. If you try to access a Product field from a Product reference, no problem. On the other hand, it's not so simple to get to a Hardware field given a Product reference. You'd need something like:
 
Marshal
Posts: 72407
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always worry when I see that sort of cast. I don't think the cast is usually the problem, but the design of the inheritance tree. It is all right to refine or enhance methods, but I get suspicious when I see new methods and new fields. Can you really say that a Hardware IS‑A Product. Can you move the fields into the Product, even if it means the getLentgh() returning null, or (probably better) Optional.empty().
 
Carey Brown
Saloon Keeper
Posts: 7999
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 don't like either solution (though I've used both). One corrupts the super class with the needs of the subclass, the other spreads that corruption all over the application. If you can localize the need to access a subclass's fields, then the first solution isn't so bad. If it starts popping up everywhere, then that's not good.
 
Campbell Ritchie
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a word for that (well, a phrase)

Favour composition over inheritance.

 
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 still in the learning stage so thanks for your input. With my code how can I improve the part where the user chooses to not enter the cost price and when he wants to add it finds the id and enters it. I know mine is working maybe it’s not the best thing to code it that way. Also like you told me there seems to be no need to use the getters and setters right?
 
Campbell Ritchie
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Entering a description or ID without a price looks peculiar. It means yuo cannot sell your product because it has no price. Are you using bidding or auction‑style prices as eBay did when it started? If not, you might have to decide that a price is an essential part of the Product object and without a price the object is in an inconsistent state, which means you must enter a positive price.
 
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 know it sounds funny but that is what’s required of us from the project.
 
Campbell Ritchie
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is more than funny, I am afraid. It looks to me like making you write low quality code.
 
Rancher
Posts: 184
7
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is more than funny, I am afraid. It looks to me like making you write low quality code.



I would not worry about the quality of the code at this point, I guess that this exercise is about understanding the very basics of inheritance. Later he will learn why instaceof and downcasting is a code smell and why it's better to use composition but for now, I would just focus on completing the assignment and getting feedback.

Warren Griscti wrote: Also like you told me there seems to be no need to use the getters and setters right?



Depends on what you want to achieve and what the requirements are. If your teacher expects you to write getters and setters for every field then just do it, you will later learn how to do it in a better way.

You might want to google stuff like OOP SOLID principles, immutability, polymorphism, composition vs inheritance. Also, somebody mentioned Effective Java (Joshua Bloch) book, it's great.





 
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
Hi thanks everyone. I sent my code to the tutor amd I was suggested to have only a read and write file method in the main method. Anyone can help me a bit with that part please? Thanks
 
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
This is my main method


Should I put the file in and out at the top and the following in each respective switch case?
 
Carey Brown
Saloon Keeper
Posts: 7999
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
What purpose does passing "args" to each run() serve? What input are you expecting from the command line that the runs could use?
 
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 just used this https://coderanch.com/wiki/660020/Main-Pain that was suggested here to only have one main method
 
Carey Brown
Saloon Keeper
Posts: 7999
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 find this all confusing.
Here I would have assumed that it's class meant a Stock class, which would mean you'd call Stock.update(), or, assuming you have an instance of Stock, then stock.update(). Using run() everywhere destroys any readability.
 
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 I fixed it and remove all run (args) and just used public void () and I'm just calling them in my main method. Regarding the read and write methods in the main class do you have any suggestions how can I implement it. The idea is that as I'm using the same files over and over again I was suggested that there's no need to split it in different classes and can implement it in the main method. Th problem is li said above that I use the files for different executions.
 
Carey Brown
Saloon Keeper
Posts: 7999
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

Warren Griscti wrote:I just used this https://coderanch.com/wiki/660020/Main-Pain that was suggested here to only have one main method

If you had followed this then your main() method would have had only ONE line in it. It was not telling you to plaster run() everywhere.
 
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 again
 
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
Can someone give me a direction regarding the read/write file in main class instead in each of the separate classes please? Thanks
 
Carey Brown
Saloon Keeper
Posts: 7999
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
Post the part of your requirements that specifies what operations the user needs to be able to perform from the user interface.
 
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 using this

to read and write to a file.
And then using it to different outcomes like this to delete an entry.

My tutor suggested me to put my read and writing methods in the main method as right now I'm using the part of the file in in several different classes and obviously using it for different outcomes not only the delete record as pasted above. Ho can it be achieved please? I was also suggested to Put the read and write methods in the class where the main method has been declared and the read file method should return the data read from the file and assign it to a vector.
 
Carey Brown
Saloon Keeper
Posts: 7999
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, you can supposedly add a new Product to Stock, delete a Product from Stock, or update a Product in Stock. You need to be able to persist Stock to/from a file. So here's a question, you could write after every single operation, or, you could operate on a Collection of Products in Stock and then do a single write, either on request or when you're done. How do you want to do it?
 
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
No I want to write to file after every operation so as at each operation the updated file is read.
 
Carey Brown
Saloon Keeper
Posts: 7999
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. So when your program starts up you can READ all the Products into a List or a Map, and then from there perform all your operations on the List followed by a WRITE. In this case the WRITE would use the List as its source, so, no input file. So you'll need a method to load() the Products into the List, and a method to save() the List to a 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
Or in my case a vector right?
 
Carey Brown
Saloon Keeper
Posts: 7999
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
Vectors are (almost) never used any more. They were replaced by Lists like ArrayList.
 
Carey Brown
Saloon Keeper
Posts: 7999
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
An ArrayList is like a Vector with all of the multi-thread code ripped out. Runs much faster than Vectors.
 
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
But that’s what my tutor recommended but I’ll keep it in mind. Can you show me an example how it looks and I’ll implement it to the other situations. Thanks
 
Carey Brown
Saloon Keeper
Posts: 7999
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
ArrayList dates back to Java version 1.2. Very, very, very, old.
 
Carey Brown
Saloon Keeper
Posts: 7999
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
Possible methods 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
This is my main method

I need to do it here
 
Carey Brown
Saloon Keeper
Posts: 7999
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, do it there, but first fix your main() as suggested.
 
Carey Brown
Saloon Keeper
Posts: 7999
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
Where were you told that you need to be able to update a Product? I don't see it in your requirements unless I've missed something.
 
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
To clarify. I have to read a file (Inventory.txt) to a vector in the main method. And above each switch case has it's own output. Ex in case 2 I read the file and update some data and save to same file.
 
this is supposed to be a surprise, but it smells like a 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