Skip to main content
added 373 characters in body
Source Link
Little
  • 3.5k
  • 12
  • 53
  • 77

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> detailsList;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

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> detailsList; public add(Article a){ detailsList.add(a); } } 

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 Article a1=new Article(....) Article a2=new Article(....) Invoice inv; inv.add(a1) inv.add(a2) //for example I want the first element to get a code of 10, the next as 20 and so on 

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

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

Source Link
Little
  • 3.5k
  • 12
  • 53
  • 77

automatic generation of a variable value inside a class

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> detailsList; public add(Article a){ detailsList.add(a); } } 

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 Article a1=new Article(....) Article a2=new Article(....) Invoice inv; inv.add(a1) inv.add(a2) //for example I want the first element to get a code of 10, the next as 20 and so on 

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