0% found this document useful (0 votes)
14 views2 pages

Eclipse Practice

The document contains a Java program that defines a 'Product' class with attributes like item number, name, quantity, and price, along with constructors, getters, setters, and a toString method. A 'ProductTester' class is also included, which creates instances of 'Product' using both default and overloaded constructors, adds them to a list, and displays their details. The program demonstrates basic object-oriented programming principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Eclipse Practice

The document contains a Java program that defines a 'Product' class with attributes like item number, name, quantity, and price, along with constructors, getters, setters, and a toString method. A 'ProductTester' class is also included, which creates instances of 'Product' using both default and overloaded constructors, adds them to a list, and displays their details. The program demonstrates basic object-oriented programming principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.List;

class Product {
private int itemNumber;
private String name;
private int quantity;
private double price;

// Instance field declarations

public Product() {
// Default constructor
this.itemNumber = 0;
this.name = null;
this.quantity = 0;
this.price = 0.0;
}

public Product(int itemNumber, String name, int quantity, double price) {


this.itemNumber = itemNumber;
this.name = name;
this.quantity = quantity;
this.price = price;
}

// Getter methods
public int getItemNumber() {
return itemNumber;
}

public String getName() {


return name;
}

public int getQuantity() {


return quantity;
}

public double getPrice() {


return price;
}

// Setter methods
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}

public void setName(String name) {


this.name = name;
}

public void setQuantity(int quantity) {


this.quantity = quantity;
}

public void setPrice(double price) {


this.price = price;
}
@Override
public String toString() {
return "Item Number: " + itemNumber + "\n" +
"Name: " + name + "\n" +
"Quantity in stock: " + quantity + "\n" +
"Price: $" + price;
}
}

public class ProductTester {


public static void main(String[] args) {
// Two products using default constructor
Product product1 = new Product();
Product product2 = new Product();

// Four products using overloaded constructor


Product product3 = new Product(1002, "Pen", 25, 2.50);
Product product4 = new Product(1003, "Printer Cartridge", 10, 35.99);
Product product5 = new Product(1004, "Classic Rock CD", 50, 14.99);
Product product6 = new Product(1005, "Action Movie DVD", 30, 19.99);

// Create a list of products


List<Product> products = new ArrayList<>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
products.add(product5);
products.add(product6);

// Display product details


for (Product product : products) {
System.out.println(product);
System.out.println();
}
}
}

You might also like