I have a method that updates portfolio of client by his id. But when I use it twice or more, even for other clients, occurs aliasing and all clients get "new" same portfolio.
ArrayList<String> namesOfStocksFromInput=new ArrayList<>();
ArrayList<Integer> amountFromInput=new ArrayList<>();
int clientId;
int selection=0;
Scanner input = new Scanner(System.in);
do
{
try{
selection=input.nextInt();}
switch(selection) {
case 1:
System.out.println("Please enter clients id.");
try{
clientId=input.nextInt();}
catch(Exception e){System.out.println("Wrong input"); break;}
/* Some tests if client exists, if exists- continue to updating his portfolio*/
System.out.println("Please enter new stocks for clients portfolio"
+ " and beside respective amount.");
/*Method, that separates names and integers from input.
namesOfStocks get names, amount list get integers*/
scanf(namesOfStocksFromInput,amountFromInput);
/*some tests */
/*And finally updating the clients portfolio.*/
updateClientPortfolio(clientId,
new StockPortfolio(namesOfStocksFromInput,amountFromInput));
break;
}
}while(selection!=100);
Declaration of updateClientPortfolio:
public void updateClientPortfolio(int id,StockPortfolio portf)
I tried to use copy constructor- didn't help. So what is the best way to avoid this aliasing?