-1

I am supposed to have different classes for each product in a store, and if a costumer chooses laptop then it will show the laptop available. How do you create an array of 5 laptops and show the different laptops for each of them? This is my tester

if (choice == "Laptop"){

    Laptop[] lap = new Laptop[5];

    for (int i = 0; i < lap.length; i++) {
        lap[i] = new Laptop();
    }
    lap[1].msg();

Sample output:

What do you want to buy?  Laptop

We have 5 different types of laptops:
1.  Manufacturer: HP, Price: $400, Hard drive: 100GB, RAM: 6 GB
2.  Manufacturer: HP, Price: $800, Hard drive: 200GB, RAM: 8 GB
3.  Manufacturer: Dell, Price: $300, Hard drive: 80GB, RAM: 6 GB
4.  Manufacturer: Dell, Price: $680, Hard drive: 150GB, RAM: 8 GB
5.  Manufacturer: Toshiba, Price: $350, Hard drive: 60GB, RAM: 4 GB
azro
  • 53,056
  • 7
  • 34
  • 70
  • 6
    `new Laptop()` is always going to create the same thing (unless you use statics, but... don't). You need to parameterise the constructor i.e. `new Laptop(Manufacturer.HP, 400, 100, 6)`. See [Providing Constructors for Your Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html). You won't be able to create these in a loop – Michael Nov 02 '18 at 15:18
  • 1
    @Makoto While the string comparison **is** incorrect, that's not what he's asking. – Michael Nov 02 '18 at 15:19
  • 4
    `if (choice == "Laptop")` should be `if (choice.equals("Laptop"))` see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Michael Nov 02 '18 at 15:20
  • Show us the code for the `Laptop` class. – Joakim Danielson Nov 02 '18 at 15:21

1 Answers1

1

Since you are creating a new Laptop on each pass of your loop, you're getting identical objects each time. Unless you set the fields in your Laptop class, you won't see different data between them.

Instead, your Laptop class needs to have it's fields set, either in the constructor (when creating each Laptop), or via setters.

Here's a quick and simple demo application you can try out and see it in action:

class LaptopDemo {
    public static void main(String[] args) {

        // First, create some sample laptops and add them to an Array
        Laptop[] laptops = new Laptop[5];

        // You need to create a new laptop and set it's field values
        laptops[0] = new Laptop("HP", 400.00, 100, 6);
        laptops[1] = new Laptop("HP", 800.00, 80, 8);
        laptops[2] = new Laptop("Dell", 300.00, 150, 6);
        laptops[3] = new Laptop("Dell", 680.00, 100, 8);
        laptops[4] = new Laptop("Toshiba", 350.00, 60, 4);

        // Now you can loop through the array and print the details
        for (int i = 0; i < laptops.length; i++) {

            // Build the String to be printed
            StringBuilder sb = new StringBuilder();
            sb.append("Manufacturer: ").append(laptops[i].getManufacturer()).append(", ")
                    .append("Price: $").append(laptops[i].getPrice()).append(", ")
                    .append("Hard Drive: ").append(laptops[i].getHdCapacity()).append("GB, ")
                    .append("RAM: ").append(laptops[i].getRamCapacity()).append("GB");

            System.out.println(sb.toString());
        }
    }
}

class Laptop {

    private final String manufacturer;
    private final double price;
    private final int hdCapacity;
    private final int ramCapacity;

    public Laptop(String manufacturer, double price, int hdCapacity, int ramCapacity) {
        this.manufacturer = manufacturer;
        this.price = price;
        this.hdCapacity = hdCapacity;
        this.ramCapacity = ramCapacity;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public double getPrice() {
        return price;
    }

    public int getHdCapacity() {
        return hdCapacity;
    }

    public int getRamCapacity() {
        return ramCapacity;
    }
}

Results:

Manufacturer: HP, Price: $400.0, Hard Drive: 100GB, RAM: 6GB
Manufacturer: HP, Price: $800.0, Hard Drive: 80GB, RAM: 8GB
Manufacturer: Dell, Price: $300.0, Hard Drive: 150GB, RAM: 6GB
Manufacturer: Dell, Price: $680.0, Hard Drive: 100GB, RAM: 8GB
Manufacturer: Toshiba, Price: $350.0, Hard Drive: 60GB, RAM: 4GB
Zephyr
  • 9,885
  • 4
  • 28
  • 63