Created
February 22, 2024 21:52
-
-
Save cmoz/27a68132322e4ec5ab67f25d1e2c8c05 to your computer and use it in GitHub Desktop.
First example with linkedList and a Product object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
// our product class that we created to create a product | |
class Product { | |
private String name; | |
private double price; | |
private String description; | |
// Getters and setters for product properties | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public String getDescription() { | |
return description; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
LinkedList<Product> products = new LinkedList<>(); | |
// Create some product objects | |
Product product1 = new Product(); | |
product1.setName("Product A"); | |
product1.setPrice(10.99); | |
product1.setDescription("A great product"); | |
Product product2 = new Product(); | |
product2.setName("Product B"); | |
product2.setPrice(15.50); | |
product2.setDescription("An even better product"); | |
// Add products to the list | |
products.add(product1); | |
products.add(product2); | |
// Iterate through the list and print product details | |
for (Product product : products) { | |
System.out.println("Name: " + product.getName()); | |
System.out.println("Price: $" + product.getPrice()); | |
System.out.println("Description: " + product.getDescription()); | |
System.out.println("---"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment