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 aLinkedListwith three elements. ** The first element of the
* * @param cm1 aLinkedListis the object *s1, the second element of theLinkedListis * the objects2, and the third element of the *LinkedListis the objects3*CoffeeMakerobject * @param cm2 aCoffeeMakerobject * @param cm3 aCoffeeMakerobject * @return aLinkedListwith 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 aLinkedListfrom an array of *CoffeeMaker** The elements in the
* * @param anArray an array ofLinkedListhave the same order that in * the array *CoffeeMaker* @return aLinkedListwith 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 aLinkedListobject 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 aStringwith the delimiter used in the stream * referenced bybr* @return aLinkedListwith theCoffeeMakerdata * 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 emptyCoffeeMakerin a *LinkedList. * * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @returntrueiflistcontains a empty *CoffeeMakerobject; * 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 emptyCoffeeMakerobjects stored in a *LinkedList. * * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @return the number of emptyCoffeeMakerobjects 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 eachCoffeeMakerobjects stored in a *LinkedList* * @param list aLinkedListthat 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 aLinkedListthat contains objects of * the classCoffeeMaker* @return aintwith 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 eachCoffeeMakerobject stored in a *LinkedList* * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @param qty aintwith 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 specificCoffeeMakerobject * stored in aLinkedList* * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @param index aintthat specify the location of the *CoffeeMakerobject * @param qty aintwith 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 eachCoffeeMakerstored in a *LinkedListthat have a specific model * * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @param model aStringthat specify the model of the *CoffeeMakerobject * @param qty aintwith 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 aLinkedListthe emptyCoffeeMaker* objects * * @param list aLinkedListthat 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.TextAreacomponent the information of * theCoffeeMakerobjects 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 aCoffeeMakeris displayed in a * diferent line. No extra lines and white space is displayed *LinkedListthat contains objects of * the classCoffeeMaker* @param ta ajava.awt.TextAreacomponent 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.Listcomponent the information of the *CoffeeMakerobjects contained in aLinkedList. ** The component
*Listis cleared at the begining, so the * componentlsthas the same size that thelist. ** This method calls the method {@link CoffeeMaker#toString()} to display * the information of each
* * @param linkedList aCoffeeMaker. *LinkedListthat contains objects of * the classCoffeeMaker* @param lst ajava.awt.Listcomponent where the elements * oflistwill 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 fieldmodelof the *CoffeeMakerobjects contained in the specified *LinkedList. ** This method calls the method {@link Arrays#sort(String[])} to sort the * array. *
* * @param list aLinkedListthat contains objects of * the classCoffeeMaker* @return the sorted array with theCoffeeMakerobjects * 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 |