Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- // Interfaces
- import interfaces.Global;
- import interfaces.Lang;
- // Custom Classes
- import io.In;
- import io.Out;
- /**
- *
- * <p>
- * User getta le basi per la creazione di un utente, sfruttando i costruttori
- * per inizializzare l'oggetto che vogliamo creare a seconda delle esigenze.
- * </p>
- *
- * <p>
- * Questa classe non e' stata designata per essere istanziata direttamente,
- * ma solo per definire il concetto di 'Utente' e di quali campi esso e'
- * composto.
- * </p>
- *
- * <p>
- * Le due sottoclassi piu' importanti sono BuildCompany() e BuildPerson()
- * per creare l'utente desiderato con le dovute caratteristiche. Se avete
- * l'esigenza di altri tipi di utente, create una classe estendendo questa.
- * </p>
- *
- * @version 2012.0403
- * @since 2012.0403
- */
- public class User implements Global, Lang {
- protected String name;
- protected String surname;
- protected String address;
- protected String city;
- protected String country;
- protected String zipcode;
- protected long UID;
- protected boolean error;
- protected User() {
- name = getName();
- surname = getSurname();
- address = getAddress();
- city = getCity();
- zipcode = getZipcode();
- country = getCountry();
- }
- protected User(User object) {
- name = object.getName();
- surname = object.getSurname();
- address = object.getAddress();
- city = object.getCity();
- zipcode = object.getZipcode();
- country = object.getCountry();
- }
- /** Il metodo setName() ha il compito di recuperare un valore di tipo nome
- * sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setName() {
- do {
- error = false;
- Out.println(name_insert);
- name = In.readString();
- if(name.matches("^[a-zA-Z]{2,15}")) {
- Out.println(name_accepted);
- } else {
- Out.println(err_name);
- error = true;
- }
- } while(error);
- }
- /** Il metodo setSurname() ha il compito di recuperare un valore di tipo
- * surname sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setSurname() {
- do {
- error = false;
- Out.println(surname_insert);
- surname = In.readString();
- if(!surname.equals(name)){
- if(surname.matches("^[a-zA-Z]{2,20}")) {
- Out.println(surname_accepted);
- } else { Out.println(err_surname); error = true; }
- } else { Out.println(err_surname_equals); error = true; }
- } while(error);
- }
- /** Il metodo setAddress() ha il compito di recuperare un valore di tipo
- * surname sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setAddress() {
- do {
- error = false;
- Out.println(address_insert);
- address = In.readString();
- if(address.matches("^[a-zA-Z ]{2,36} [0-9]{1,4}$")) {
- Out.println(address_accepted);
- } else {
- Out.println(err_address);
- error = true;
- }
- } while(error);
- }
- /** Il metodo setCity() ha il compito di recuperare un valore di tipo city
- * sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setCity() {
- do {
- error = false;
- Out.println(city_insert);
- city = In.readString();
- if(city.matches("^[a-zA-Z ]{2,15}")) {
- Out.println(city_accepted);
- } else {
- Out.println(err_city);
- error = true;
- }
- } while(error);
- }
- /** Il metodo setCountry() ha il compito di recuperare un valore di tipo
- * country sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setCountry() {
- do {
- error = false;
- Out.println(country_insert);
- country = In.readString();
- if(country.matches("^[a-zA-Z ]{2,15}")) {
- Out.println(country_accepted);
- } else {
- Out.println(err_country);
- error = true;
- }
- } while(error);
- }
- /** Il metodo setZipcode() ha il compito di recuperare un valore di tipo
- * zipcode sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setZipcode() {
- do {
- error = false;
- Out.println(zipcode_insert);
- zipcode = In.readString();
- if(zipcode.matches("^[0-9]{5}")) {
- Out.println(zipcode_accepted);
- } else {
- Out.println(err_zipcode);
- error = true;
- }
- } while(error);
- }
- /** <p>
- * Il metodo setUID ha il compito di generare l'UID (User IDentificator),
- * in modo da riconosce ogni utente in modo univoco. Certo generando
- * il numero in modo casuale vi e' sempre una probabilita' che due cliente
- * ottengano lo stesso UID, anche se, bisogna tenere conto che il numero
- * generato e' compreso:
- * </p>
- *
- * <pre>
- * tra: '1.000.000.000.000.000.000'
- * e: '9.223.372.036.854.775.807'
- * </pre>
- *
- * <p>
- * Come potete vedere le probabilita' che due clienti abbiano lo stesso UID
- * e' veramente minima. L'altra soluzione sarebbe quella di memorizzare
- * l'UID in un file a parte, leggerlo, ed assegnare all'utente successivo
- * l'UID successivo, in modo progressivo. Sicuramente l'ultimo metodo
- * risulta piu' elegante e professionale di quello utilizzato in questa sede,
- * ma visto che rimaniamo in ambito universitario e puramente didattico,
- * la soluzione adottata andra' sicuramente bene.
- * </p>
- *
- * @see io.In#readString()
- * @see java.lang.Math#random()
- */
- private void setUID() {
- do {
- error = false;
- try {
- UID = (long)(Math.random() * (MIN + MAX));
- } catch (ArithmeticException e) {
- // nel caso ci sia qualche problema
- // genero un'altro numero.
- continue;
- }
- } while(error);
- }
- /** Il metodo ritorna il nome
- * @return String name. */
- public String getName() { setName(); return name; }
- /** Il metodo ritorna il cognome
- * @return String surname. */
- public String getSurname() { setSurname(); return surname; }
- /** Il metodo ritorna l'indirizzo
- * @return String address. */
- public String getAddress() { setAddress(); return address; }
- /** Il metodo ritorna il citta'
- * @return String city. */
- public String getCity() { setCity(); return city; }
- /** Il metodo ritorna il paese
- * @return String country. */
- public String getCountry() { setCountry(); return country; }
- /** Il metodo ritorna il codice postale
- * @return String zipcode. */
- public String getZipcode() { setZipcode(); return zipcode; }
- /** Il metodo ritorna l' UID (User IDentificator)
- * @return long UID. */
- public long getUID() { setUID(); return UID; }
- }
- /**
- *
- * <p>
- * Gli utenti che questo software trattera' saranno principalmente di due tipi:
- * </p>
- *
- * <ol>
- * <li>Le aziende (Company)</li>
- * <li>Le persone (Person)</li>
- * </ol>
- *
- * <p>
- * Questa classe recupera le informazioni dalla superclasse 'User',
- * che accomuna tutti gli utenti e ne estende le funzionalita', aggiungendo
- * l'UID (User IDentificator) e l'IVA. Successivamente viene definito l'utente
- * 'Azienda', che per noi puo' essere un'azienda cliente (Customer) oppure
- * un'azienda fornitore (Supplier).
- * </p>
- *
- * <p>
- * La classe contiene i metodi per creare l'utente desiderato 'Azienda'
- * tramite i metodi setCustomer() e setSupplier() e tramite i metodi
- * getCustomer() e getSupplier() ritorna il loro valore.
- * </p>
- *
- * <pre>
- * Non estendere questa classe per alcun motivo, se si ha l'esigenza
- * di avere un utente di tipo diverso o simile, creare una nuova classe che
- * estende la classe 'User'
- * </pre>
- *
- * @version 2012.0403
- * @since 2012.0403
- */
- final class BuildCompany extends User {
- private String vat;
- BuildCompany Company;
- @SuppressWarnings("unused")
- private int CID;
- private BuildCompany() {
- super();
- vat = getVat();
- UID = getUID();
- }
- // Se avete l'esigenza di creare una copia dell'oggetto...
- private BuildCompany(BuildCompany object) {
- super(object);
- vat = object.getVat();
- UID = object.getUID();
- }
- /** Il metodo setVat() ha il compito di recuperare un valore di tipo vat
- * sullo standard input da tastiera.
- *
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setVat() {
- do {
- error = false;
- Out.println(vat_insert);
- vat = In.readString();
- if(vat.matches("^[0-9]{11}")) {
- Out.println(vat_accepted);
- } else {
- Out.println(err_vat);
- error = true;
- }
- } while(error);
- }
- /**
- * <p>
- * Questo metodo si occupa di creare l'oggetto Company
- * In particolare avviene che: Allocchiamo un oggetto reale (BuildCompany)
- * ed assegnamo a Company il suo riferimento. Quando l'oggetto viene
- * creato, automaticamente viene invocato il costruttore di default di
- * questa classe BuildCompany() che tramite la parola chiave 'super()' e
- * l'invocazione dei giusti metodi "getVat() e getUID()" andra' a
- * inizializzare l'utente Customer.
- * </p>
- */
- private final void setCompany() {
- Company = new BuildCompany();
- // Company ID = Customer = 1
- Company.CID = 1;
- }
- /** Il metodo ritorna l'iva
- * @return String vat. */
- public final String getVat() { setVat(); return vat; }
- /** Il metodo ritorna il riferimento all'oggetto BuildCompany
- * @return BuildCompany Company */
- public final BuildCompany getSupplier() { setCompany(); return Company; }
- }
- /**
- *
- * </p>
- * Lo stesso discorso effettuato per la classe BuildCompany vale per questa
- * classe, solo che qui estendiamo le funzionalita' del comune utente per
- * andare a definire una persona (non un'azienda in BuildCompany).
- * </p>
- *
- * <p>
- * Questa classe recupera le informazioni dalla superclasse 'User',
- * che accomuna tutti gli utenti e ne estende le funzionalita', aggiungendo
- * l'UID (User IDentificator) e il Codice Fiscale.
- * </p>
- *
- * <pre>
- * Non estendere questa classe per alcun motivo, se si ha l'esigenza
- * di avere un utente di tipo diverso o simile, creare una nuova classe che
- * estende la classe 'User'
- * </pre>
- *
- * @version 2012.0403
- * @since 2012.0403
- *
- * @see BuildCompany
- */
- class BuildPerson extends User {
- private String taxcode;
- static BuildPerson Person;
- @SuppressWarnings("unused")
- private int PID;
- private BuildPerson() {
- super();
- taxcode = getTaxcode();
- UID = getUID();
- }
- // Se avete l'esigenza di creare una copia dell'oggetto
- private BuildPerson(BuildPerson object) {
- super(object);
- taxcode = object.getTaxcode();
- UID = object.getUID();
- }
- /** Il metodo setTaxcode() ha il compito di recuperare un valore di tipo
- * taxcode sullo standard input da tastiera.
- *
- * @param Nessun parametro in ingresso.
- * @return Il metodo non ritorna alcun valore
- * @see io.In#readString()
- * @see java.lang.String#length()
- * @see java.lang.String#matches(String)
- */
- private void setTaxcode() {
- do {
- error = false;
- Out.println(taxcode_insert);
- taxcode = In.readString();
- if(taxcode.matches("^\\w{16}")) {
- Out.println(taxcode_accepted);
- } else {
- Out.println(err_taxcode);
- error = true;
- }
- } while(error);
- }
- /**
- * <p>
- * Questo metodo si occupa di creare l'oggetto BuildPerson di tipo Person.
- * In particolare avviene che: Allocchiamo un oggetto reale (BuildPerson)
- * ed assegnamo a Person il suo riferimento. Quando l'oggetto viene
- * creato, automaticamente viene invocato il costruttore di default di
- * questa classe BuildPerson() che tramite la parola chiave 'super()' e
- * l'invocazione dei giusti metodi "getTaxcode() e getUID()" andra' a
- * inizializzare l'utente Person.
- * </p>
- */
- private final void setPerson() {
- Person = new BuildPerson();
- // Person ID = Customer = 1
- Person.PID = 1;
- }
- /** Il metodo ritorna il codice fiscale
- * @return String taxcode. */
- public final String getTaxcode() { setTaxcode(); return taxcode; }
- /** Il metodo ritorna il riferimento all'oggetto Supplier
- * @return BuildPerson Supplier */
- public final BuildPerson getPerson() { setPerson(); return Person; }
- }
Add Comment
Please, Sign In to add comment