import java.util.*; import java.io.*; import coffeemaker.*; /** * ExLinkedListSample ** This class implements some methods to practice the * use of the class {@link LinkedList}. *
* * @author Alfonso Rodruez * @version 1.0.0 * @see CoffeeMaker * @see CoffeeMakerFormatException * @see LinkedList */ public class ExLinkedListSample { /** * Creates aLinkedList
with three elements. ** The first element of the
* * @param cm1 aLinkedList
is the object *s1
, the second element of theLinkedList
is * the objects2
, and the third element of the *LinkedList
is the objects3
*CoffeeMaker
object * @param cm2 aCoffeeMaker
object * @param cm3 aCoffeeMaker
object * @return aLinkedList
with the objectscm1
, *cm2
, andcm3
*/ public static LinkedList makeLinkedList3(CoffeeMaker cm1, CoffeeMaker cm2, CoffeeMaker cm3) { LinkedList v = new LinkedList(); v.add(cm1); v.add(cm2); v.add(cm3); return v; } /** * Creates aLinkedList
from an array of *CoffeeMaker
** The elements in the
* * @param anArray an array ofLinkedList
have the same order that in * the array *CoffeeMaker
* @return aLinkedList
with the objects stored in the array *anArray
*/ public static LinkedList makeLinkedListFromArray(CoffeeMaker anArray[]) { LinkedList v = new LinkedList(); for (int i = 0; i < anArray.length; i++) { v.add(anArray[i]); } return v; } /** * Creates aLinkedList
object from an input stream ** The data of each student is stored in one line in the input stream, * and each field is separated by a the delimiter specified in the parameter *
*DELIM
, for example. ** Super486_1000_ * PentiumIV_1500_ * HotCoffee_900_ *** This method uses {@link StringTokenizer} to obtain each field from a line *
* * @param br a {@link Reader} from whichCoffeeMaker
* data are read * @param DELIM aString
with the delimiter used in the stream * referenced bybr
* @return aLinkedList
with theCoffeeMaker
data * stored in the streambr
* @throws {@link IOException} if cannot read from the input stream * @throws {@link CoffeeMakerFormatException} if receives a badly formed data */ public static LinkedList makeLinkedListFromReader(Reader r, final String DELIM) throws IOException, CoffeeMakerFormatException { BufferedReader br = new BufferedReader(r); LinkedList v = new LinkedList(); StringTokenizer st; String line = ""; String name; int cap; try { line = br.readLine(); while (line != null) { st = new StringTokenizer(line, DELIM); name = st.nextToken(); cap = Integer.parseInt(st.nextToken()); if (st.hasMoreTokens()) { throw new CoffeeMakerFormatException(line); } v.add(new CoffeeMaker(name, cap)); line = br.readLine(); } } catch (NoSuchElementException nsee) { throw new CoffeeMakerFormatException(line); } catch (NumberFormatException nfe) { throw new CoffeeMakerFormatException(line); } return v; } /** * Tests if exists an emptyCoffeeMaker
in a *LinkedList
. * * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @returntrue
iflist
contains a empty *CoffeeMaker
object; * otherwise returnfalse
*/ public static boolean existEmptyCoffeeMaker(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { return true; } } return false; } /** * Obtains the number of emptyCoffeeMaker
objects stored in a *LinkedList
. * * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @return the number of emptyCoffeeMaker
objects with a * grade greater o equal thann
*/ public static int countEmptyCoffeeMakers(LinkedList list) { int count = 0; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { count++; } } return count; } /** * Fills eachCoffeeMaker
objects stored in a *LinkedList
* * @param list aLinkedList
that contains objects of * the classCoffeeMaker
*/ public static void fillCoffeeMakers(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { ((CoffeeMaker) i.next()).fill(); } } /** * Obtains the sum of the quantity of coffee of theCoffeeMaker
* objects stored in aLinkedList
* * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @return aint
with the sum of the quantity of coffee */ public static int sumQuantities(LinkedList list) { int sum = 0; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { sum += ((CoffeeMaker) i.next()).getQuantity(); } return sum; } /** * Serves coffee of eachCoffeeMaker
object stored in a *LinkedList
* * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @param qty aint
with the quantity of coffee to be served * for eachCoffeeMaker
*/ public static void serveCoffee(LinkedList list, int qty) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { ((CoffeeMaker) i.next()).serveACup(qty); } } /** * Serve a cup of coffee of a specificCoffeeMaker
object * stored in aLinkedList
* * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @param index aint
that specify the location of the *CoffeeMaker
object * @param qty aint
with the quantity of coffee to be served * for theCoffeeMaker
*/ public static void serveCoffee(LinkedList list, int index, int qty) { ((CoffeeMaker) list.get(index)).serveACup(qty); } /** * Serve a cup of coffee of eachCoffeeMaker
stored in a *LinkedList
that have a specific model * * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @param model aString
that specify the model of the *CoffeeMaker
object * @param qty aint
with the quantity of coffee to be served * for eachCoffeeMaker
*/ public static void serveCoffee(LinkedList list, String model, int qty) { CoffeeMaker cm; for (ListIterator i = list.listIterator() ; i.hasNext(); ) { cm = (CoffeeMaker) i.next(); if (model.equals(cm.getModel())) { cm.serveACup(qty); } } } /** * Removes from aLinkedList
the emptyCoffeeMaker
* objects * * @param list aLinkedList
that contains objects of * the classCoffeeMaker
*/ public static void removeEmptyCoffeeMakers(LinkedList list) { for (ListIterator i = list.listIterator() ; i.hasNext(); ) { if (((CoffeeMaker) i.next()).isEmpty()) { i.remove(); } } } /** * Display in ajava.awt.TextArea
component the information of * theCoffeeMaker
objects contained in a *LinkedList
** This method calls the method {@link CoffeeMaker#toString()} to display * the information of each
*CoffeeMaker
. ** The information of each
* * @param list aCoffeeMaker
is displayed in a * diferent line. No extra lines and white space is displayed *LinkedList
that contains objects of * the classCoffeeMaker
* @param ta ajava.awt.TextArea
component where the * information will be displayed */ public static void displayInTextArea(LinkedList list, java.awt.TextArea ta) { int size = list.size(); if (size > 0) { ListIterator i = list.listIterator(); ta.setText(i.next().toString()); while(i.hasNext()) { ta.append("\n" + i.next().toString()); } } else { ta.setText(""); } } /** * Display in ajava.awt.List
component the information of the *CoffeeMaker
objects contained in aLinkedList
. ** The component
*List
is cleared at the begining, so the * componentlst
has the same size that thelist
. ** This method calls the method {@link CoffeeMaker#toString()} to display * the information of each
* * @param linkedList aCoffeeMaker
. *LinkedList
that contains objects of * the classCoffeeMaker
* @param lst ajava.awt.List
component where the elements * oflist
will be displayed */ public static void displayInList(LinkedList linkedList, java.awt.List lst) { lst.removeAll(); for (ListIterator i = linkedList.listIterator() ; i.hasNext(); ) { lst.add(i.next().toString()); } } /** * Obtains a sorted array with values of the fieldmodel
of the *CoffeeMaker
objects contained in the specified *LinkedList
. ** This method calls the method {@link Arrays#sort(String[])} to sort the * array. *
* * @param list aLinkedList
that contains objects of * the classCoffeeMaker
* @return the sorted array with theCoffeeMaker
objects * contained inlist
. */ public static String[] getModels(LinkedList list) { String models[] = new String[list.size()]; int i = 0; for (ListIterator it = list.listIterator() ; it.hasNext();) { models[i++] = ((CoffeeMaker) it.next()).getModel(); } Arrays.sort(models); return models; } }
Previou page | Next page |