Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Amie Mac

Ranch Hand
+ Follow
since Jul 18, 2015
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
15
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Amie Mac

Just googled it... found out it's Lamda
4 years ago
Yes, the details of the DVDs are in the different text fields.
DVDs is what the instructor used in his inventory program, whereas I used books.

We used an array because that was the progression of the assignments. Since it's a beginning Java course. I also think the List would have been better, but didn't take the time to change the code to it from the Array. I am sure there are way better ways (as you stated) to do this entire program. I may look into re-doing it with the better code that you mentioned.

By the way, what is λs? I don't recognize that symbol.
4 years ago

Campbell Ritchie wrote:

Amie Mac wrote: . . .
1. When clicking on the Add button, the button text should change to Click to Add! so the user should click it to add the item to an array

Why an array?
You realise the change in button text may cause the whole display to change strangely

2. When the Add button is clicked it would make the appropriate JTextFields editable . . .

You can create listeners which call certain methodsBut if you are setting them editable when you push the button, how is the user supposed to enter anything beforehand? And how are you setting them uneditable?

Go through the JTextComponent class and its subclasses. You will probably find you can add action listeners to them, and they can be activated by pushing the enter key while the component has focus.
For things more complicated you can consider key bindings and document listeners. Also find somebody who knows more about them than I do!

Note I am not creating a Listener there but using a λ. It only works in Java8.



That's why I am so confused. I asked my instructor for help... he stated that each time the button is clicked, the code has to handle the event of hitting the button with the actionListener.
These are the examples he sent to me for the Add button:

Instructors Note: I assume that you can display the data. So when you click on Add, use the actionlistener to perfrom the task of adding. Here is a sample code:


Instructors Note: Each time when you have a button, you need to handle the event of hitting the button with the actionlisterner. So in the actionlistener when the source of the button is add, then you call the add function.


I was able to get the Search method to partially work through the JTextField, but just by pressing Enter and not by clicking on the button a second time, but not the Add method...
Taking an online class through UoP for Java Programming was not the greatest way to learn Java Programming.>
4 years ago
My assignment is already handed in and I ended up using JOptionPanes. But for my own learning I would like to know how to use the JTextFields for the input and modifications for the top row of JButtons that I have in the program (Add, Modify, Delete)

For example, the requirements for the Add button and method are:
1. When clicking on the Add button, the button text should change to Click to Add! so the user should click it to add the item to an array
2. When the Add button is clicked it would make the appropriate JTextFields editable so that text can be entered (Item Name, Item Number, Author, Qty in Stock, and Price)
Note: I am able to make the button text change with a setText on the button, and I am able to get the fields to become editable, but I am not able to take what is typed in the fields and enter them into an Array with the Enter key or with the "Go" button.
4 years ago
I apologize for the short answer... I've been frustrated with this program all week.

I have put a couple of methods in my InventoryLauncher file to show that I've tried to code something that works according to the requirements. I felt that I explained that in the OP.
4 years ago

Paul Clapham wrote:Well, yes, there is something wrong with your question. Sorry, but it's far too long to be read and understood due to the large volume of code which is not relevant to the problem. I looked at it twice and gave up on the idea of trying to answer because of that.

So have a look at the SSCCE (Short, Self Contained, Correct (Compilable), Example) page which describes how you can produce a much better code example for your question.



It would be nice to know if I would need to modify my question to get some help instead of just not answering at all.
4 years ago
I am surprised I haven't seen a reply on my post. I am wondering if there is something wrong with my original post?
4 years ago
Does my question make sense?
4 years ago
I am working on the final part of my Inventory Program.

I am working on the addButton and searchButton pieces.

For the addButton, I have it working with a JOptionPane asking for additional items. However, the assignment calls for them to be added through the JTextField instead. Also, the button needs to change the text to Click to Add and then be able to be clicked to Add the item. I can get the text to change on the button, but unable to get the button to add the item to the array. I have a an addItem() method and an addItem2() method written up in InventoryLauncher, where the addItem2() is the one where I was trying to use the JTextField to enter the text.

For the searchButton, I have it working to find the item in the array, but I am unable to get the button to work to search for an item. It is only working when I hit the enter key.

Can someone point me in the right direction for using the button a second time to add and to search?

Thanks!


Here is the code

Inventory.java


Books.java
4 years ago

Knute Snortum wrote:If you want a restocking fee in your inventory value, then you can't have a method that both sets and returns the fee. When you "next" thru the books, you're adding a fee. This changes the total inventory value.



Thanks for spotting this! I changed it to:

and added the restockingFee() method in the inventoryValue() method.

Works great!

Thanks again!
4 years ago

Knute Snortum wrote:You have two inventoryValue() methods -- one on Books and one on Inventory. The Books values includes the restocking fee. Books start with no restocking fee. Calling restockingFee() both sets and returns the fee. This is bad. What if you want to just get the fee without setting it?



Yes, books extends inventory and the inventoryValue() method in books overrides the one in inventory. Do I have that wrong?
4 years ago

Knute Snortum wrote:It looks like a restocking fee is getting added to Book until all Book instances have a restocking fee. Totals change as more Books get their restocking fee added.

BTW, you have about six lines of code that get copied four times -- great place for a method.



So, if I have a method and then have totalInventoryValue listed once instead of on each button, would that help?
4 years ago
Good Morning...
I am working on Part 5 of my Inventory Program. I have everything working except that the Total Inventory Value changes. It will show one value when I click on First, then it will show another value when I click on Last, then it will go up and finally settle on a different value when I iterate through the array with the first and next buttons. I am hoping someone can tell me why this is happening... Thanks

The buttons are placed in the InventoryLauncher.java file
Calling the totalInventoryValue from the Inventory.java file


Here's the code:

InventoryLauncher.java


Inventory.java


Books.java
4 years ago

Campbell Ritchie wrote:You are not returning the sum, but the array of inventory. You have shown the printout wrongly; there should be a [ at its beginning. You are supposed to return the BigDecimal sum and if you print it out you get a number. What you are doing is printing the array and arrays don't have an overridden toString method. So you get the same as with Object#toString.
I trust your values from the inventory are not in double format, because otherwise using BigDecimal you will simply immortalise the imprecision. Either change the inventory to use a precise form of number or round the values.
sum = sum.add(new BigDecimal(theInventory[i].inventoryValue()).round(...));
Why are you using a for loop rather than a for‑each loop?



So I got it working using double instead of Inventory []. I also got it working using BigDecimal but then the number was very long. At this point, for my assignment, I am not worried about the precision, but it will be something to look into because it will be important to be precise when I write code about money.

I didn't think I could use a for each loop because I need to add together just the inventoryValue() of each entry in the array. How would a for each loop work for this?
4 years ago