pretty new stuff , struggling problem... supposed create variable has many instances. example in case need ask user city of departure , city of arrival.
system.out.println("vnesite ime mesta kamor potujete: "); // here ask first city string mesto = scan.next(); system.out.println("vnesite ime države kamor potujete: "); string drzava = scan.next(); system.out.println("vnesite ime mesta iz katerega potujete: "); //here ask second city string mesto2 = scan.next(); system.out.println("vnesite ime države iz katere potujete: "); string drzava2 = scan.next(); kraj kraj = new kraj(mesto, drzava);
and here instances of variable should stored.
public class kraj { private string kraj; // variable supposed have many instances private string drzava; public kraj(string kraj, string drzava) { this.kraj = kraj; this.drzava = drzava; } public string getkraj ( ) { return kraj; } public void setkraj (string kraj) { this.kraj = kraj; } public string getdrzava ( ) { return drzava; } public void setdrzava (string drzava) { this.drzava = drzava; }
i've tried creating second variable, doesn't solve (what if traveler wants make stop @ destination in middle , proceed final destination). im looking explanation if possible not solution :)
thanks in advance guys/girls!
you can have a list of string
store multiple destinations shown below:
public class kraj { private list<string> kraj;//now stores multiple destinations public list<string> getkraj() { return kraj; } public void setkraj(list<string> kraj) { this.kraj = kraj; } //add drzava , it's setters , getters }
so, in main code, need collect them array , pass shown below:
list<string> destinations = new arraylist<>(); system.out.println("vnesite ime mesta kamor potujete: "); string mesto = scan.next(); system.out.println("vnesite ime države kamor potujete: "); string drzava = scan.next(); destinations.add(drzava); system.out.println("vnesite ime mesta iz katerega potujete: "); string mesto2 = scan.next(); system.out.println("vnesite ime države iz katere potujete: "); string drzava2 = scan.next(); destinations.add(drzava2); kraj kraj = new kraj(mesto, destinations);
Comments
Post a Comment