Reputation: 21
Today I have to find a way, how to manage a Stock. Later you should be able to add items(What already works) and remove items(What doesnt work until now). But now I am having Problems to find a method, which searches within the table and which looks for matchings and removing them. Therefor I use the variables (Productamount(Integer), Productname(String)), those are being searched by an if-operation, to check if they are aviable or not. (And I am asking myself if there is an option to remove tablerows directly by clicking? E.g. by clicking on a cross the whole table is removed. Because this would be an idea, too.) Thats my table with it's textinputs:
<table id="invoverview" border="1">
<caption>Bestand</caption>
<thead>
<tr>
<th>Produktbezeichnung</th>
<th>Anzahl</th>
</tr>
</thead>
<tbody>
<tr th:each="entry : ${products.entries}">
<td th:text="${entry.getProductname()}">H</td>
<td th:text="${entry.getProductamount()}">A</td>
</tr>
</tbody>
</table>
</div>
<div id="right">
<div>
<form name="entry" action="entry" method="POST">
<h3>Neuer Lagereintrag(Stockentry):</h3>
<label>Produktbezeichnung(ItemDescription)
<input id="ProductName" name="ProductName" /></label><br/>
<label>Produktanzahl(ProductAmount)
<input id="ProductAmount" name="ProductAmount" /></label><br/>
<button type="submit" href="/entry">Einfügen(Insert)</button>
</form>
</div>
<div>
<form name="delentry" action="delentry" method="POST">
<h3>Lagereintrag Löschen:</h3>
<label>Produktbezeichnung <input id="ProductName" name="ProductName" /></label><br />
<label>Produktmenge <input id="ProductAmount" name="ProductAmount" /></label><br />
<button type="submit" href="/delentry">Entfernen(Remove)</button>
</form>
StockEntry.java:
public class StockEntry {
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public Integer getProductamount() {
return productamount;
}
public void setProductamount(Integer productamount) {
this.productamount = productamount;
}
public StockEntry(String productname, Integer productamount) {
this.productname = productname;
this.productamount = productamount;
}
private String productname;
private Integer productamount;
}
And an exerpt of StockController.java: (entry should create a Stockentry and delentry should delete/remove an entry)
@RequestMapping(value ="/entry", method = RequestMethod.POST)
public String setNewEntry(Model model, @RequestParam("ProductName")
String productname,
@RequestParam("ProductAmount") Integer productamount) {
products.newEntry(productname,productamount);
models(model);
return "stock";
}
@RequestMapping(value ="/delentry", method = RequestMethod.POST)
public String delEntry(Model model, @RequestParam("ProductName")
String productname,
@RequestParam("ProductAmount") Integer productamount) {
products.delEntry(productname,productamount);
System.out.println(productamount);
System.out.println(productname);
models(model);
return "stock";
}
And thats an exerpt from stock.java( in delEntry I want to check, if LinkedList entries contains the both vars productname and productname):
private LinkedList<StockEntry> entries;
public Stock(){
this.productname = null;
this.productamount = 0;
this.entries = new LinkedList<>();
}
public Iterable<StockEntry> getEntries(){
return this.entries;
}
public void newEntry(String productname, Integer productamount){
StockEntry entry = new StockEntry(productname, productamount);
this.entries.add(entry);
for (StockEntry bla : this.entries){
System.out.println(bla.getProductname());
System.out.println(bla.getProductamount());
} }
public void delEntry(String productname, Integer productamount){
StockEntry entry = new StockEntry(productname, productamount);
if(this.entries.contains(productname)&& this.entries.equals(productamount)){
this.entries.remove();
}
}
}
}
I have to say that adding entries works already.(But how is it possible to make to make the Inputsflieds required? "required" as Html-tag doesn't work. I think somewhere I have to place @NotNull or @NotEmpty) But is the if-Operation correct? Before I already tried "Stock.getProductname().contains(productname)" and "Stock.getProductamount().equals(productamount)", but it didnt work because getProductamount() & getProductname() arent static, if they are static only nonsense will be in the table ( 5 times the same entry..) In the ApplicationInitializer.java this Products will be created:
private void initializeStock(){
products.newEntry("Kastenbier", 50);
products.newEntry("Kastenwasser", 5);
products.newEntry("Kastencola", 500);
products.newEntry("Schnaps", 50000);
products.newEntry("Döner", 9); }
They will be loaded in the table Stock.html. Works already. Only removing doesn't work now. Friends were talking of a enum, that the stock could be made with an enum. But enums are fix and not thought to be dynamic(editable). Thanks for your help!
Upvotes: 0
Views: 56
Reputation: 2100
Try the following:
public void delEntry(String productname, Integer productamount){
Optional<StockEntry> entryToDelete = this.entries.filter(e ->productName.equals(e.getProductName()) && productamount == e.getProductAmount()).findFirst();
if(entryToDelete.isPresent()) {
this.entries.remove(entryToDelete.get());
}
}
This will first find the entry identified by its productname and amount. If a product was find, it will be removed from the list.
The problem with your code is, that you try to find a StockEntry only by its name but you always remove the first item.
Upvotes: 1
Reputation: 2220
I think your problem is in your delEntry method, specifically, this line:
if(this.entries.contains(productname)&& this.entries.get()(productamount)){
Think about what that line is trying to do, first it checks if the entry name, which is a string, is in the list of entries, which is never going to be true, because your items are StockEntry objects, not strings, then you try and check if the list of entries IS the price, which should never be true.
Maybe try changing it to this, and see if it works.
public void delEntry(String productname, Integer productamount){
StockEntry entry = new StockEntry(productname, productamount);
StockEntry to_remove;
for(StockEntry item : this.entries){
if(item.getProductName().equals(productname) && item.getProductamount() == productamount){
to_remove = item;
break; //or return, i forget how to java.
}
}
//I remove it outside the loop to prevent a ConcurrentModificationException
if(to_remove != null){
this.entries.remove(to_remove);
//done!
}
}
Edit: If you can use java 8 methods and stuff, i would recommend @Ria's answer, because it's short and does the same thing as mine
Upvotes: 0