Newsgroup: it.comp.java
Titolo: Tutorial


"Vi or emacs? .....OpenOffice! =)"
 

stavo cercando un buon tutorial non necessariamente in italiano su Java.
[CUT]
Cosa altro se non
http://java.sun.com/docs/books/tutorial/
?
-- 
Ciao
	Fabrizio C.

> Vi prego non mi dite il Thinking in Java perchè è troppo logorroico.
Nessuno ti costringe a leggerlo tutto (anche se non sarebbe una pessima
idea).
Fortuna che Eckel non legge questo ng.



Non vorrei sembrare una voce contro il coro, quindi lascio volentieri parlare il codice
(di Burce Eckel) che ho estratto da "Thinking in Patterns", pensate un po', un libro che
vi _spiega_ come fare buon design.
Non lo commento, lascio a voi questo compito. Vi dico solo che, personalmente, per
riuscire a leggere il libro dopo avere incontrato queste due pezzi di codice ho dovuto
mettermi d'impegno. E il libro non e' poi troppo male, poi lui lo sta scrivendo, ecc.ecc.
...
//: com:bruceeckel:test:RunUnitTests.java
// Discovering the unit test
// class and running each test.
package com.bruceeckel.test;
import java.lang.reflect.*;
import java.util.Iterator;
public class RunUnitTests {
  public static void 
  require(boolean requirement, String errmsg) {
    if(!requirement) {
      System.err.println(errmsg);
      System.exit(1);
    }
  }
  public static void main(String[] args) {
    require(args.length == 1,
      "Usage: RunUnitTests qualified-class");
    try {
      Class c = Class.forName(args[0]);
      // Only finds the inner classes 
      // declared in the current class:
      Class[] classes = c.getDeclaredClasses();
      Class ut = null;
      for(int j = 0; j < classes.length; j++) {
        // Skip inner classes that are 
        // not derived from UnitTest:
        if(!UnitTest.class.
            isAssignableFrom(classes[j]))
              continue;
        ut = classes[j];
        break; // Finds the first test class only
      }
      // If it found an inner class, 
      // that class must be static:
      if(ut != null)
        require(
          Modifier.isStatic(ut.getModifiers()),
          "inner UnitTest class must be static");
      // If it couldn't find the inner class, 
      // maybe it's a regular class (for black-
      // box testing:
      if(ut == null)
        if(UnitTest.class.isAssignableFrom(c))
          ut = c;
      require(ut != null, 
        "No UnitTest class found");
      require(
        Modifier.isPublic(ut.getModifiers()),
        "UnitTest class must be public");
      Method[] methods = ut.getDeclaredMethods();
      for(int k = 0; k < methods.length; k++) {
        Method m = methods[k];
        // Ignore overridden UnitTest methods:
        if(m.getName().equals("cleanup")) 
          continue;
        // Only public methods with no 
        // arguments and void return 
        // types will be used as test code:
        if(m.getParameterTypes().length == 0 &&
           m.getReturnType() == void.class &&
           Modifier.isPublic(m.getModifiers())) {
             // The name of the test is 
             // used in error messages:
             UnitTest.testID = m.getName();
             // A new instance of the 
             // test object is created and 
             // cleaned up for each test:
             Object test = ut.newInstance();
             m.invoke(test, new Object[0]);
             ((UnitTest)test).cleanup();
        }
      }
    } catch(Exception e) {
      e.printStackTrace(System.err);
      // Any exception will return a nonzero 
      // value to the console, so that 
      // 'make' will abort:
      System.err.println("Aborting make");
      System.exit(1);
    }
    // After all tests in this class are run,
    // display any results. If there were errors,
    // abort 'make' by returning a nonzero value.
    if(UnitTest.errors.size() != 0) {
      Iterator it = UnitTest.errors.iterator();
      while(it.hasNext())
        System.err.println(it.next());
      System.exit(1);
    }
  }
} ///:~
public VendingMachine() {
    super(VM.quiescent); // Initial state
    for(int i = 0; i < items.length; i++)
      for(int j = 0; j < items[i].length; j++)
        items[i][j] = new ItemSlot((j+1)*25, 5);
    items[3][0] = new ItemSlot(25, 0);
    buildTable(new Object[][][]{
      { {VM.quiescent}, // Current state
        // Input, test, transition, next state:
        {Money.class, null, 
         showTotal, VM.collecting}},
      { {VM.collecting}, // Current state
        // Input, test, transition, next state:
        {Quit.quit, null, 
         returnChange, VM.quiescent},
        {Money.class, null, 
         showTotal, VM.collecting},
        {FirstDigit.class, null, 
         showDigit, VM.selecting}},
      { {VM.selecting}, // Current state
        // Input, test, transition, next state:
        {Quit.quit, null, 
         returnChange, VM.quiescent},
        {SecondDigit.class, notEnough, 
         clearSelection, VM.collecting},
        {SecondDigit.class, itemNotAvailable, 
         clearSelection, VM.unavailable},
        {SecondDigit.class, itemAvailable, 
         dispense, VM.wantMore}},
      { {VM.unavailable}, // Current state
        // Input, test, transition, next state:
        {Quit.quit, null, 
         returnChange, VM.quiescent},
        {FirstDigit.class, null, 
         showDigit, VM.selecting}},
      { {VM.wantMore}, // Current state
        // Input, test, transition, next state:
        {Quit.quit, null, 
         returnChange, VM.quiescent},
        {FirstDigit.class, null, 
         showDigit, VM.selecting}},
    });
    ...
}
Ciao,
    Bruno.
---
| Bruno Bossola
| A Java Software Engineer :-)
| 
http://digilander.iol.it/bbossola

Oh mamma! :-/