GioZio GioZio - 3 months ago 12
Java Question

How to save the results of a called method of all the objects in an array?

I am creating a program that handles a car dealership. The user has the opportunity to add a car in the store by creating a random 3 digit number.

Now the question is how I can search/delete cars depending on the 3 digit code?

I'm thinking that I need every code that the cars have to save it on an array so I can search and delete afterwards.

I have created a class and certain methods on it, I have also created 5 objects and I'm trying to see if it works on these 5.

Here is the method of the random number:

I use the metritis variable because I can't achieve to place correctly the values on the array so I have to give parameter of 1,2,3,4,5 so I can place them correctly to the array.

package antiprosopeia;
import java.util.Random;

public class Antiprosopeia {

private String company,colour;
private int model,horsePower,speed,price,specialCode,metritis;
private int[] codes = new int[]{0,0,0,0,0};

public Antiprosopeia(String company, String colour, int model, int horsePower, int speed, int price){ = company;
this.colour = colour;
this.model = model;
this.horsePower = horsePower;
this.speed = speed;
this.price = price; }

public Antiprosopeia(){ company = ""; colour = ""; model = 0; horsePower = 0; speed = 0; price = 0; }

public void setRandomNumber(int metritis){
Random rand = new Random();
int randNum2 = rand.nextInt(900) + 100;
specialCode = randNum2;

codes[metritis] = specialCode;

public void printarray(){
for(int i=0; i<codes.length; i++){
System.out.println(" " + codes[i]);}

public void Info(){ System.out.println("Company : " + company + "\nColour : " + colour + "\nModel : " + model + "\nHorse Power : " + horsePower +
"\nSpeed : " + speed + "\nPrice : " + price ); }

public static void main(String[] args) {

Antiprosopeia car1 = new Antiprosopeia("Toyota","red",333,100,2223,8000);

Antiprosopeia car2 = new Antiprosopeia("Mercedes","yellow",233,100,2990,9000);

Antiprosopeia car3 = new Antiprosopeia("Volkswagen","green",153,100,2780,6000);

Antiprosopeia car4 = new Antiprosopeia("Mitsubisi","white",678,140,2600,7000);

Antiprosopeia car5 = new Antiprosopeia("Porsche","black",390,1000,2000,30000);



[EDIT] Now when i call the printarray() method it seems that at my array only one value is hold and all the others are zer0s as i defined the array at start of my program


If I were doing this, I would use a HashMap. This way you know that you have a unique 3 digit number, and if you wanted to, you could also store more data. You could do something like:

HashMap<Integer, Car> cars = new HashMap<Integer, Car>();

This example would allow you to add a car object to the map. You don't have to that, but it's an option. If you didn't want to do that, you could do:

HashMap<Integer, String> cars = new HashMap<Integer, String>();

and then do:

cars.put(123, "Description of car"); 

Using a HashMap would give you more options when storing the data. This would also prevent you from creating an array with 1000 elements, all of which are 0 until you have a value for them. You could easily print out all your numbers by doing:

for(int number : cars.entrySet()){
    System.out.println("My car number: " + number);

Searching for keys would extremely easy, as you could do:

String description = cars.getKey(123);

If description was null, you would know that there is no key for it.