0

I want to program a class so that each time a new object is created, that object has a new generated code, but the trick is that I do not want to pass that value as an argument to the constructor. Roughly I have the following:

public class Article{ private int cod; private String name; public Article(String name){ this.name=name: } } 

then I have a class called invoice in which I can call this Article class:

public class Invoice{ ArrayList<Article> detailList; public add(Article a){ detailsList.add(a); } public ArrayList<Article> getArticleList(){ return detailList; } } 

so I want that each time I make some articles in the main class and add those in the Invoie class to have the code generated automatically:

main class ArrayList<Article> temp; Article a1=new Article(....) Article a2=new Article(....) Article a3=new Article(....) Invoice inv; inv.add(a1) inv.add(a2) inv.add(a3) //for example I want the first element to get a code of 10, the next as 20 and so on temp=inv.getArticleList(); for (int i=0;i<temp.size();i++){ System.out.println(temp.get(i).getCod()); } 

I have tried using:

private static int cod in the Article class

and then adding +10 each time I call to the add method, but when I print the results from the list in the main class, it only prints me the last generated code; how can I fix that?

thanks

2
  • You should read what static variables are. You could use it almost like you do now but should assign the static variable to the instance variable from within a synchronized block or using an AtomicInteger. Commented Nov 4, 2013 at 20:31
  • The code doesn't calculate, unclear where do you want to add a code or subtract it. Commented Nov 4, 2013 at 20:37

2 Answers 2

3

You need two attributes, one static and one at the instance level:

public class Article { private int cod; private String name; private static int counter = 10; public Article(String name) { this.name = name; this.cod = counter; counter += 10; } } 

Using the above, each article will have a different code, starting at 10 and incrementing ten units each time. I tested it using this Invoice class, which fixes some errors in the posted code:

public class Invoice { ArrayList<Article> detailList = new ArrayList<Article>(); public void add(Article a) { detailList.add(a); } public ArrayList<Article> getArticleList(){ return detailList; } } 

Now this works as expected:

Invoice inv = new Invoice(); inv.add(a1); inv.add(a2); inv.add(a3); ArrayList<Article> temp = inv.getArticleList(); for (int i=0;i<temp.size();i++){ System.out.println(temp.get(i).getCod()); } 

It prints on the console:

10 20 30 
Sign up to request clarification or add additional context in comments.

5 Comments

Unless you are in a multithreaded environment: then using the static counter like that won't ensure that the generated "cod" is unique across instances.
thansk @oscar, but when I print the results from the list it does not show the results correctly; it only gets two codes and the third one gets duplicated
@Giodude OP is just learning how to program, your concern is valid (and I think you meant "single-threaded" environment), but put it in perspective: multithreading issues are the least of OP's concerns right now.
@Little post the code you're using to create and add the articles, there must be an error in it
@Little it works for me, but also be aware that the Invoice code you posted has errors, I updated my answer with the corrected code
1

Here's a thread safe version of Oscar's code:

import java.util.concurrent.atomic.AtomicInteger; public class Article { private int cod; private String name; private final static AtomicInteger counter = new AtomicInteger(10); public Article(String name) { this.name = name; this.cod = counter.getAndAdd(10); } } 

Using an AtomicInteger will allow you to create instances of Article from many threads concurrently. Using the regular int will likely cause "dirty" reads of the counter and thus different instances of Article will get different values of the counter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.