Java examples for Object Oriented Design:Class
Define custom class to hold another class
import java.util.*; class Storefront { private LinkedList catalog = new LinkedList(); public void addItem(String id, String name, String price, String quant) { Item it = new Item(id, name, price, quant); catalog.add(it);/*w w w .ja v a2s .c om*/ } public Item getItem(int i) { return (Item) catalog.get(i); } public int getSize() { return catalog.size(); } public void sort() { Collections.sort(catalog); } } class Item{ private String id; private String name; private String price; private String quantity; public Item(String id, String name, String price, String quant){ this.id = id; this.name = name; this.price = price; this.quantity = quantity; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getQuantity() { return quantity; } public void setQuantity(String quantity) { this.quantity = quantity; } } public class Main { public static void main(String[] arguments) { Storefront store = new Storefront(); store.addItem("C01", "MUG", "9.99", "150"); store.addItem("C02", "LG MUG", "12.99", "82"); store.addItem("C03", "MOUSEPAD", "10.49", "800"); store.addItem("D01", "T SHIRT", "16.99", "90"); store.sort(); for (int i = 0; i < store.getSize(); i++) { Item show = (Item) store.getItem(i); System.out.println("\nItem ID: " + show.getId() + "\nName: " + show.getName() + "\nPrice: $" + show.getPrice() + "\nQuantity: " + show.getQuantity()); } } }