Guest User

Untitled

a guest
Jul 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.51 KB | None | 0 0
  1. package main;
  2.  
  3. // Interfaces
  4. import interfaces.Global;
  5. import interfaces.Lang;
  6.  
  7. // Custom Classes
  8. import io.In;
  9. import io.Out;
  10.  
  11. /**
  12.  *
  13.  * <p>
  14.  * User getta le basi per la creazione di un utente, sfruttando i costruttori
  15.  * per inizializzare l'oggetto che vogliamo creare a seconda delle esigenze.
  16.  * </p>
  17.  *
  18.  * <p>
  19.  * Questa classe non e' stata designata per essere istanziata direttamente,
  20.  * ma solo per definire il concetto di 'Utente' e di quali campi  esso e'
  21.  * composto.
  22.  * </p>
  23.  *
  24.  * <p>
  25.  * Le due sottoclassi piu' importanti sono BuildCompany() e BuildPerson()
  26.  * per creare l'utente desiderato con le dovute caratteristiche. Se avete
  27.  * l'esigenza di altri tipi di utente, create una classe estendendo questa.
  28.  * </p>
  29.  *
  30.  * @version 2012.0403
  31.  * @since 2012.0403
  32.  */
  33. public class User implements Global, Lang {
  34.  
  35.     protected String name;
  36.     protected String surname;
  37.     protected String address;
  38.     protected String city;
  39.     protected String country;
  40.     protected String zipcode;
  41.    
  42.     protected long UID;
  43.    
  44.     protected boolean error;
  45.    
  46.     protected User() {
  47.         name = getName();
  48.         surname = getSurname();
  49.         address = getAddress();
  50.         city = getCity();
  51.         zipcode = getZipcode();
  52.         country = getCountry();
  53.     }
  54.    
  55.     protected User(User object) {
  56.         name = object.getName();
  57.         surname = object.getSurname();
  58.         address = object.getAddress();
  59.         city = object.getCity();
  60.         zipcode = object.getZipcode();
  61.         country = object.getCountry();
  62.     }
  63.    
  64.     /** Il metodo setName() ha il compito di recuperare un valore di tipo nome
  65.      * sullo standard input da tastiera.
  66.      *
  67.      * @see io.In#readString()
  68.      * @see java.lang.String#length()
  69.      * @see java.lang.String#matches(String)
  70.      */
  71.     private void setName() {
  72.        
  73.         do {
  74.             error = false;
  75.            
  76.             Out.println(name_insert);
  77.             name = In.readString();
  78.            
  79.             if(name.matches("^[a-zA-Z]{2,15}")) {
  80.                 Out.println(name_accepted);
  81.             } else {
  82.                 Out.println(err_name);
  83.                 error = true;
  84.             }
  85.         } while(error);
  86.     }
  87.    
  88.     /** Il metodo setSurname() ha il compito di recuperare un valore di tipo
  89.      * surname sullo standard input da tastiera.
  90.      *
  91.      * @see io.In#readString()
  92.      * @see java.lang.String#length()
  93.      * @see java.lang.String#matches(String)
  94.      */
  95.     private void setSurname() {
  96.         do {
  97.             error = false;
  98.            
  99.             Out.println(surname_insert);
  100.             surname = In.readString();
  101.            
  102.             if(!surname.equals(name)){
  103.                 if(surname.matches("^[a-zA-Z]{2,20}")) {
  104.                     Out.println(surname_accepted);
  105.                 } else { Out.println(err_surname); error = true; }
  106.             } else { Out.println(err_surname_equals); error = true; }
  107.         } while(error);
  108.     }
  109.    
  110.     /** Il metodo setAddress() ha il compito di recuperare un valore di tipo
  111.      * surname sullo standard input da tastiera.
  112.      *
  113.      * @see io.In#readString()
  114.      * @see java.lang.String#length()
  115.      * @see java.lang.String#matches(String)
  116.      */
  117.     private void setAddress() {
  118.         do {
  119.             error = false;
  120.            
  121.             Out.println(address_insert);
  122.             address = In.readString();
  123.            
  124.             if(address.matches("^[a-zA-Z ]{2,36} [0-9]{1,4}$")) {
  125.                 Out.println(address_accepted);
  126.             } else {
  127.                 Out.println(err_address);
  128.                 error = true;
  129.             }
  130.         } while(error);
  131.     }
  132.    
  133.     /** Il metodo setCity() ha il compito di recuperare un valore di tipo city
  134.      * sullo standard input da tastiera.
  135.      *
  136.      * @see io.In#readString()
  137.      * @see java.lang.String#length()
  138.      * @see java.lang.String#matches(String)
  139.      */
  140.     private void setCity() {
  141.         do {
  142.             error = false;
  143.            
  144.             Out.println(city_insert);
  145.             city = In.readString();
  146.            
  147.             if(city.matches("^[a-zA-Z ]{2,15}")) {
  148.                 Out.println(city_accepted);
  149.             } else {
  150.                 Out.println(err_city);
  151.                 error = true;
  152.             }
  153.         } while(error);
  154.     }
  155.    
  156.     /** Il metodo setCountry() ha il compito di recuperare un valore di tipo
  157.      * country sullo standard input da tastiera.
  158.      *
  159.      * @see io.In#readString()
  160.      * @see java.lang.String#length()
  161.      * @see java.lang.String#matches(String)
  162.      */
  163.     private void setCountry() {
  164.         do {
  165.             error = false;
  166.            
  167.             Out.println(country_insert);
  168.             country = In.readString();
  169.            
  170.             if(country.matches("^[a-zA-Z ]{2,15}")) {
  171.                 Out.println(country_accepted);
  172.             } else {
  173.                 Out.println(err_country);
  174.                 error = true;
  175.             }
  176.         } while(error);
  177.     }
  178.    
  179.     /** Il metodo setZipcode() ha il compito di recuperare un valore di tipo
  180.      * zipcode sullo standard input da tastiera.
  181.      *
  182.      * @see io.In#readString()
  183.      * @see java.lang.String#length()
  184.      * @see java.lang.String#matches(String)
  185.      */
  186.     private void setZipcode() {
  187.         do {
  188.             error = false;
  189.            
  190.             Out.println(zipcode_insert);
  191.             zipcode = In.readString();
  192.            
  193.             if(zipcode.matches("^[0-9]{5}")) {
  194.                 Out.println(zipcode_accepted);
  195.             } else {
  196.                 Out.println(err_zipcode);
  197.                 error = true;
  198.             }
  199.         } while(error);
  200.     }
  201.    
  202.     /** <p>
  203.      * Il metodo setUID ha il compito di generare l'UID (User IDentificator),
  204.      * in modo da riconosce ogni utente in modo univoco. Certo generando
  205.      * il numero in modo casuale vi e' sempre una probabilita' che due cliente
  206.      * ottengano lo stesso UID, anche se, bisogna tenere conto che il numero
  207.      * generato e' compreso:
  208.      * </p>
  209.      *     
  210.      *  <pre>
  211.      *      tra: '1.000.000.000.000.000.000'
  212.      *        e: '9.223.372.036.854.775.807'
  213.      *  </pre>
  214.      *  
  215.      * <p>
  216.      * Come potete vedere le probabilita' che due clienti abbiano lo stesso UID
  217.      * e' veramente minima. L'altra soluzione sarebbe quella di memorizzare
  218.      * l'UID in un file a parte, leggerlo, ed assegnare all'utente successivo
  219.      * l'UID successivo, in modo progressivo. Sicuramente l'ultimo metodo
  220.      * risulta piu' elegante e professionale di quello utilizzato in questa sede,
  221.      * ma visto che rimaniamo in ambito universitario e puramente didattico,
  222.      * la soluzione adottata andra' sicuramente bene.
  223.      * </p>
  224.      *
  225.      * @see io.In#readString()
  226.      * @see java.lang.Math#random()
  227.      */
  228.     private void setUID() {
  229.         do {
  230.             error = false;
  231.            
  232.             try {
  233.                 UID = (long)(Math.random() * (MIN + MAX));
  234.             } catch (ArithmeticException e) {
  235.                 // nel caso ci sia qualche problema
  236.                 // genero un'altro numero.
  237.                 continue;
  238.             }
  239.            
  240.         } while(error);
  241.     }
  242.    
  243.     /** Il metodo ritorna il nome
  244.      * @return String name.  */
  245.     public String getName() { setName(); return name; }
  246.    
  247.     /** Il metodo ritorna il cognome
  248.      * @return String surname. */
  249.     public String getSurname() { setSurname(); return surname; }
  250.    
  251.     /** Il metodo ritorna l'indirizzo
  252.      * @return String address. */
  253.     public String getAddress() { setAddress(); return address; }
  254.    
  255.     /** Il metodo ritorna il citta'
  256.      * @return String city. */
  257.     public String getCity() { setCity(); return city; }
  258.    
  259.     /** Il metodo ritorna il paese
  260.      * @return String country. */
  261.     public String getCountry() { setCountry(); return country; }
  262.    
  263.     /** Il metodo ritorna il codice postale
  264.      * @return String zipcode. */
  265.     public String getZipcode() { setZipcode(); return zipcode; }
  266.    
  267.     /** Il metodo ritorna l' UID (User IDentificator)
  268.      * @return long UID. */
  269.     public long getUID() { setUID(); return UID; }
  270.  
  271. }
  272.  
  273. /**
  274.  *
  275.  * <p>
  276.  * Gli utenti che questo software trattera' saranno principalmente di due tipi:
  277.  * </p>
  278.  *
  279.  * <ol>
  280.  *      <li>Le aziende (Company)</li>
  281.  *      <li>Le persone (Person)</li>
  282.  * </ol>
  283.  *
  284.  * <p>
  285.  * Questa classe recupera le informazioni dalla superclasse 'User',
  286.  * che accomuna tutti gli utenti e ne estende le funzionalita', aggiungendo
  287.  * l'UID (User IDentificator) e l'IVA. Successivamente viene definito l'utente
  288.  * 'Azienda', che per noi puo' essere un'azienda cliente (Customer) oppure
  289.  * un'azienda fornitore (Supplier).
  290.  * </p>
  291.  *
  292.  * <p>
  293.  * La classe contiene i metodi per creare l'utente desiderato 'Azienda'
  294.  * tramite i metodi setCustomer() e setSupplier() e tramite i metodi
  295.  * getCustomer() e getSupplier() ritorna il loro valore.
  296.  * </p>
  297.  *
  298.  * <pre>
  299.  * Non estendere questa classe per alcun motivo, se si ha l'esigenza
  300.  * di avere un utente di tipo diverso o simile, creare una nuova classe che
  301.  * estende la classe 'User'
  302.  * </pre>
  303.  *
  304.  * @version 2012.0403
  305.  * @since 2012.0403
  306.  */
  307. final class BuildCompany extends User {
  308.    
  309.     private String vat;
  310.    
  311.     BuildCompany Company;
  312.    
  313.     @SuppressWarnings("unused")
  314.     private int CID;
  315.    
  316.     private BuildCompany() {
  317.         super();
  318.         vat = getVat();
  319.         UID = getUID();
  320.     }
  321.    
  322.     // Se avete l'esigenza di creare una copia dell'oggetto...
  323.     private BuildCompany(BuildCompany object) {
  324.         super(object);
  325.         vat = object.getVat();
  326.         UID = object.getUID();
  327.     }
  328.    
  329.     /** Il metodo setVat() ha il compito di recuperare un valore di tipo vat
  330.      * sullo standard input da tastiera.
  331.      *
  332.      * @see io.In#readString()
  333.      * @see java.lang.String#length()
  334.      * @see java.lang.String#matches(String)
  335.      */
  336.     private void setVat() {
  337.         do {
  338.             error = false;
  339.        
  340.             Out.println(vat_insert);
  341.             vat = In.readString();
  342.        
  343.             if(vat.matches("^[0-9]{11}")) {
  344.                 Out.println(vat_accepted);
  345.             } else {
  346.                 Out.println(err_vat);
  347.                 error = true;
  348.             }
  349.         } while(error);
  350.     }
  351.    
  352.     /**
  353.      * <p>
  354.      * Questo metodo si occupa di creare l'oggetto Company
  355.      * In particolare avviene che: Allocchiamo un oggetto reale (BuildCompany)
  356.      * ed assegnamo a Company il suo riferimento. Quando l'oggetto viene
  357.      * creato, automaticamente viene invocato il costruttore di default di
  358.      * questa classe BuildCompany() che tramite la parola chiave 'super()' e
  359.      * l'invocazione dei giusti metodi "getVat() e getUID()" andra' a
  360.      * inizializzare l'utente Customer.
  361.      * </p>
  362.      */
  363.     private final void setCompany() {
  364.        
  365.         Company = new BuildCompany();
  366.        
  367.         // Company ID = Customer = 1
  368.         Company.CID = 1;
  369.     }
  370.  
  371.     /** Il metodo ritorna l'iva
  372.      * @return String vat. */
  373.     public final String getVat() { setVat(); return vat; }
  374.    
  375.     /** Il metodo ritorna il riferimento all'oggetto BuildCompany
  376.      * @return BuildCompany Company */
  377.     public final BuildCompany getSupplier() { setCompany(); return Company; }
  378. }
  379.  
  380.  
  381. /**
  382.  *
  383.  * </p>
  384.  * Lo stesso discorso effettuato per la classe BuildCompany vale per questa
  385.  * classe, solo che qui estendiamo le funzionalita' del comune utente per
  386.  * andare a definire una persona (non un'azienda in BuildCompany).
  387.  * </p>
  388.  *
  389.  * <p>
  390.  * Questa classe recupera le informazioni dalla superclasse 'User',
  391.  * che accomuna tutti gli utenti e ne estende le funzionalita', aggiungendo
  392.  * l'UID (User IDentificator) e il Codice Fiscale.
  393.  * </p>
  394.  *
  395.  * <pre>
  396.  * Non estendere questa classe per alcun motivo, se si ha l'esigenza
  397.  * di avere un utente di tipo diverso o simile, creare una nuova classe che
  398.  * estende la classe 'User'
  399.  * </pre>
  400.  *
  401.  * @version 2012.0403
  402.  * @since 2012.0403
  403.  *
  404.  * @see BuildCompany
  405.  */
  406. class BuildPerson extends User {
  407.    
  408.     private String taxcode;
  409.    
  410.     static BuildPerson Person;
  411.    
  412.     @SuppressWarnings("unused")
  413.     private int PID;
  414.    
  415.     private BuildPerson() {
  416.         super();
  417.         taxcode = getTaxcode();
  418.         UID = getUID();
  419.        
  420.     }
  421.    
  422.     // Se avete l'esigenza di creare una copia dell'oggetto
  423.     private BuildPerson(BuildPerson object) {
  424.         super(object);
  425.         taxcode = object.getTaxcode();
  426.         UID = object.getUID();
  427.     }
  428.    
  429.     /** Il metodo setTaxcode() ha il compito di recuperare un valore di tipo
  430.      * taxcode sullo standard input da tastiera.
  431.      *
  432.      * @param Nessun parametro in ingresso.
  433.      * @return Il metodo non ritorna alcun valore
  434.      * @see io.In#readString()
  435.      * @see java.lang.String#length()
  436.      * @see java.lang.String#matches(String)
  437.      */
  438.     private void setTaxcode() {
  439.         do {
  440.             error = false;
  441.            
  442.             Out.println(taxcode_insert);
  443.             taxcode = In.readString();
  444.            
  445.             if(taxcode.matches("^\\w{16}")) {
  446.                 Out.println(taxcode_accepted);
  447.             } else {
  448.                 Out.println(err_taxcode);
  449.                 error = true;
  450.             }
  451.         } while(error);
  452.     }
  453.    
  454.     /**
  455.      * <p>
  456.      * Questo metodo si occupa di creare l'oggetto BuildPerson di tipo Person.
  457.      * In particolare avviene che: Allocchiamo un oggetto reale (BuildPerson)
  458.      * ed assegnamo a Person il suo riferimento. Quando l'oggetto viene
  459.      * creato, automaticamente viene invocato il costruttore di default di
  460.      * questa classe BuildPerson() che tramite la parola chiave 'super()' e
  461.      * l'invocazione dei giusti metodi "getTaxcode() e getUID()" andra' a
  462.      * inizializzare l'utente Person.
  463.      * </p>
  464.      */
  465.     private final void setPerson() {
  466.        
  467.         Person = new BuildPerson();
  468.        
  469.         // Person ID = Customer = 1
  470.         Person.PID = 1;
  471.     }
  472.    
  473.     /** Il metodo ritorna il codice fiscale
  474.      * @return String taxcode. */
  475.     public final String getTaxcode() { setTaxcode(); return taxcode; }
  476.    
  477.     /** Il metodo ritorna il riferimento all'oggetto Supplier
  478.      * @return BuildPerson Supplier */
  479.     public final BuildPerson getPerson() { setPerson(); return Person; }
  480. }
Add Comment
Please, Sign In to add comment