import java.util.ArrayList; import java.util.Iterator; /** * The database class provides a facility to store CD and video * objects. A list of all CDs and videos can be printed to the * terminal. * * This version does not save the data to disk, and it does not * provide any search functions. * * @author Michael Kolling and David J. Barnes * @version 2002-05-04 */ public class Database { private ArrayList items; /** * Construct an empty Database. */ public Database() { items = new ArrayList(); } /** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); } /** * Print a list of all currently stored CDs and videos to the * text terminal. */ public void list() { for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); } } public void selSortTimes() { int aSize = items.size(); int j; for(int i = 0; i < aSize - 1; i++) { j = findSmallestTime(i,aSize - 1); swap(i,j); } } private void swap(int i, int j) { if (!(i == j)) { Item temp = (Item)items.get(i); items.set(i, items.get(j)); items.set(j, temp); } } private int findSmallestTime(int lower, int upper) { int smallPos = lower; Item smallItem = (Item)items.get(smallPos); int smallTime = smallItem.getPlayTime(); Item theNewItem; int theNewTime; for(int i = lower + 1; i <= upper; i++) { theNewItem = (Item)items.get(i); theNewTime = theNewItem.getPlayTime(); if(smallTime > theNewTime) { smallPos = i; smallTime = theNewTime; } } return smallPos; } }