Para exercitar a utilização do padrão de projeto Factory Method, desenvolva um conversor de números arábicos, no intervalo de 1 a 1000, para números romanos, números arábicos por extenso e números ordinais por extenso.
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Classificacao dos numerais
*/
public enum NumeralSystem
{
/**
* Numeral cardinal
*/
CARDINAL,
/**
* Numeral ordinal
*/
ORDINAL,
/**
* Numeral romano
*/
ROMAN
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numero no sistema de numeracao decimal
*
* Product define a interface de objetos que o metodo factory cria
* (Gamma, 2000)
*/
public abstract class Number
{
/**
* Primeira ordem - classe das unidades - ordem das unidades
*/
private int firstOrder;
/**
* Segunda ordem - classe das unidades - ordem das dezenas
*/
private int secondOrder;
/**
* Terceira ordem - classe das unidades - ordem das centenas
*/
private int thirdOrder;
/**
* Quarta ordem - classe dos milhares - ordem das unidades de milhar
*/
private int fourthOrder;
/**
* Validar e converter o numero no sistema de numeracao decimal no
* numeral desejado
*
* @param number numero no sistema de numeracao decimal
* @return numero desejado
*/
public String converter(int number)
{
if((number > 0) && (number < 10000))
{
return convert(number);
}
throw new IllegalArgumentException("Number out of range: " + number);
}
/**
* Converter o numero no sistema de numeracao decimal no numero desejado
*
* @param number numero no sistema de numeracao decimal
* @return numero desejado
*/
protected abstract String convert(int number);
/**
* Classificar os componentes do numero no sistema de numeracao decimal
*
* @param number numero no sistema de numeracao decimal
*/
protected void categorize(int number)
{
setFirstOrder(number);
setSecondOrder(number);
setThirdOrder(number);
setFourthOrder(number);
}
/**
* Retornar a primeira ordem - classe das unidades - ordem das unidades
*
* @return primeira ordem - classe das unidades - ordem das unidades
*/
protected int getFirstOrder()
{
return firstOrder;
}
/**
* Retornar a segunda ordem - classe das unidades - ordem das dezenas
*
* @return segunda ordem - classe das unidades - ordem das dezenas
*/
protected int getSecondOrder()
{
return secondOrder;
}
/**
* Configurar a terceira ordem - classe das unidades - ordem das centenas
*
* @return terceira ordem - classe das unidades - ordem das centenas
*/
protected int getThirdOrder()
{
return thirdOrder;
}
/**
* Retornar a quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*
* @return quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*/
protected int getFourthOrder()
{
return fourthOrder;
}
/**
* Configurar a primeira ordem - classe das unidades - ordem das unidades
*
* @param number numero no sistema de numeracao decimal
*/
private void setFirstOrder(int number)
{
this.firstOrder = number % 10;
}
/**
* Configurar a segunda ordem - classe das unidades - ordem das dezenas
*
* @param number numero no sistema de numeracao decimal
*/
private void setSecondOrder(int number)
{
this.secondOrder = (number % 100) / 10;
}
/**
* Configurar a terceira ordem - classe das unidades - ordem das centenas
*
* @param number numero no sistema de numeracao decimal
*/
private void setThirdOrder(int number)
{
this.thirdOrder = (number % 1000) / 100;
}
/**
* Configurar a quarta ordem - classe dos milhares - ordem das unidades
* de milhar
*
* @param number numero no sistema de numeracao decimal
*/
private void setFourthOrder(int number)
{
this.fourthOrder = (number % 10000) / 1000;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numero cardinal
*
* ConcreteProduct implementa a interface de Product (Gamma, 2000)
*/
public class NumberCardinal extends Number
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Decimal#convert(int)
*/
@Override
protected String convert(int number)
{
categorize(number);
String result = mount(null, fourthOrder());
result = mount(result, thirdOrder());
result = mount(result, secondOrder());
result = mount(result, firstOrder());
return result;
}
/**
* Montagem do numero cardinal
*
* @param base numero cardinal montado ate o momento
* @param order ordem a ser inserida no numero cardinal
* @return numero cardinal
*/
private String mount(String base, String order)
{
if(order != null)
{
if(base != null)
{
return base + " e " + order;
}
return order;
}
return base;
}
/**
* Retornar a primeira ordem - classe das unidades - ordem das unidades
*
* @return primeira ordem - classe das unidades - ordem das unidades
*/
private String firstOrder()
{
if(getSecondOrder() != 1)
{
switch(getFirstOrder())
{
case 1 : return "um";
case 2 : return "dois";
case 3 : return "três";
case 4 : return "quatro";
case 5 : return "cinco";
case 6 : return "seis";
case 7 : return "sete";
case 8 : return "oito";
case 9 : return "nove";
}
}
return null;
}
/**
* Retornar a segunda ordem - classe das unidades - ordem das dezenas
*
* @return segunda ordem - classe das unidades - ordem das dezenas
*/
private String secondOrder()
{
switch(getSecondOrder())
{
case 1 : switch(getFirstOrder())
{
case 1 : return "onze";
case 2 : return "doze";
case 3 : return "treze";
case 4 : return "catorze";
case 5 : return "quinze";
case 6 : return "dezesseis";
case 7 : return "dezessete";
case 8 : return "dezoito";
case 9 : return "dezenove";
default: return "dez";
}
case 2 : return "vinte";
case 3 : return "trinta";
case 4 : return "quarenta";
case 5 : return "cinquenta";
case 6 : return "sessenta";
case 7 : return "setenta";
case 8 : return "oitenta";
case 9 : return "noventa";
default: return null;
}
}
/**
* Configurar a terceira ordem - classe das unidades - ordem das centenas
*
* @return terceira ordem - classe das unidades - ordem das centenas
*/
private String thirdOrder()
{
switch(getThirdOrder())
{
case 1 : return "cem";
case 2 : return "duzentos";
case 3 : return "trezentos";
case 4 : return "quatrocentos";
case 5 : return "quinhentos";
case 6 : return "seiscentos";
case 7 : return "setecentos";
case 8 : return "oitocentos";
case 9 : return "novecentos";
default: return null;
}
}
/**
* Retornar a quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*
* @return quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*/
private String fourthOrder()
{
switch(getFourthOrder())
{
case 1 : return "mil";
case 2 : return "dois mil";
case 3 : return "três mil";
case 4 : return "quatro mil";
case 5 : return "cinco mil";
case 6 : return "seis mil";
case 7 : return "sete mil";
case 8 : return "oito mil";
case 9 : return "nove mil";
default: return null;
}
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numero ordinal
*
* ConcreteProduct implementa a interface de Product (Gamma, 2000)
*/
public class NumberOrdinal extends Number
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Decimal#convert(int)
*/
@Override
protected String convert(int number)
{
categorize(number);
String result = mount(null, fourthOrder());
result = mount(result, thirdOrder());
result = mount(result, secondOrder());
result = mount(result, firstOrder());
return result;
}
/**
* Montagem do numero ordinal
*
* @param base numero ordinal montado ate o momento
* @param order ordem a ser inserida no numero ordinal
* @return numero ordinal
*/
private String mount(String base, String order)
{
if(order != null)
{
if(base != null)
{
return base + " " + order;
}
return order;
}
return base;
}
/**
* Retornar a primeira ordem - classe das unidades - ordem das unidades
*
* @return primeira ordem - classe das unidades - ordem das unidades
*/
private String firstOrder()
{
switch(getFirstOrder())
{
case 1 : return "primeiro";
case 2 : return "segundo";
case 3 : return "terceiro";
case 4 : return "quarto";
case 5 : return "quinto";
case 6 : return "sexto";
case 7 : return "sétimo";
case 8 : return "oitavo";
case 9 : return "nono";
default: return null;
}
}
/**
* Retornar a segunda ordem - classe das unidades - ordem das dezenas
*
* @return segunda ordem - classe das unidades - ordem das dezenas
*/
private String secondOrder()
{
switch(getSecondOrder())
{
case 1 : return "décimo";
case 2 : return "vigésimo";
case 3 : return "trigésimo";
case 4 : return "quadragésimo";
case 5 : return "quinquagésimo";
case 6 : return "sexagésimo";
case 7 : return "septuagésimo";
case 8 : return "octogésimo";
case 9 : return "nonagésimo";
default: return null;
}
}
/**
* Configurar a terceira ordem - classe das unidades - ordem das centenas
*
* @return terceira ordem - classe das unidades - ordem das centenas
*/
private String thirdOrder()
{
switch(getThirdOrder())
{
case 1 : return "centésimo";
case 2 : return "ducentésimo";
case 3 : return "trecentésimo";
case 4 : return "quadringentésimo";
case 5 : return "quingentésimo";
case 6 : return "sexcentésimo";
case 7 : return "septingentésimo";
case 8 : return "octingentésimo";
case 9 : return "noningentésimo";
default: return null;
}
}
/**
* Retornar a quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*
* @return quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*/
private String fourthOrder()
{
switch(getFourthOrder())
{
case 1 : return "milésimo";
case 2 : return "dois milésimo";
case 3 : return "três milésimo";
case 4 : return "quatro milésimo";
case 5 : return "cinco milésimo";
case 6 : return "seis milésimo";
case 7 : return "sete milésimo";
case 8 : return "oito milésimo";
case 9 : return "nove milésimo";
default: return null;
}
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numero romano
*
* ConcreteProduct implementa a interface de Product (Gamma, 2000)
*/
public class NumberRoman extends Number
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Decimal#convert(int)
*/
@Override
protected String convert(int number)
{
categorize(number);
return fourthOrder() + thirdOrder() + secondOrder() + firstOrder();
}
/**
* Retornar a primeira ordem - classe das unidades - ordem das unidades
*
* @return primeira ordem - classe das unidades - ordem das unidades
*/
private String firstOrder()
{
switch(getFirstOrder())
{
case 1 : return "I";
case 2 : return "II";
case 3 : return "III";
case 4 : return "IV";
case 5 : return "V";
case 6 : return "VI";
case 7 : return "VII";
case 8 : return "VIII";
case 9 : return "IX";
default: return "";
}
}
/**
* Retornar a segunda ordem - classe das unidades - ordem das dezenas
*
* @return segunda ordem - classe das unidades - ordem das dezenas
*/
private String secondOrder()
{
switch(getSecondOrder())
{
case 1 : return "X";
case 2 : return "XX";
case 3 : return "XXX";
case 4 : return "XL";
case 5 : return "L";
case 6 : return "LX";
case 7 : return "LXX";
case 8 : return "LXXX";
case 9 : return "XC";
default: return "";
}
}
/**
* Configurar a terceira ordem - classe das unidades - ordem das centenas
*
* @return terceira ordem - classe das unidades - ordem das centenas
*/
private String thirdOrder()
{
switch(getThirdOrder())
{
case 1 : return "C";
case 2 : return "CC";
case 3 : return "CCC";
case 4 : return "CD";
case 5 : return "D";
case 6 : return "DC";
case 7 : return "DCC";
case 8 : return "DCCC";
case 9 : return "CM";
default: return "";
}
}
/**
* Retornar a quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*
* @return quarta ordem - classe dos milhares - ordem das unidades de
* milhar
*/
private String fourthOrder()
{
switch(getFourthOrder())
{
case 1 : return "M";
case 2 : return "MM";
case 3 : return "MMM";
case 4 : return "IV";
case 5 : return "V";
case 6 : return "VI";
case 7 : return "VII";
case 8 : return "VIII";
case 9 : return "IX";
default: return "";
}
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numeral
*
* Creator declara o metodo factory o qual retorna um objeto do tipo
* Product. Creator pode tambem definir uma implementacao por
* omissao do metodo factory que retorna por omissao um objeto
* ConcreteProduct. Pode chamar o metodo factory para criar um
* objeto Product (Gamma, 2000)
*/
public abstract class Numeral
{
/**
* Retornar o numero desejado
*
* @param system sistema numerico desejado
* @return numero desejado
*/
public static Number getNumeral(NumeralSystem system)
{
Numeral numeral = null;
if(NumeralSystem.CARDINAL == system)
{
numeral = new NumeralCardinal();
}
else if(NumeralSystem.ORDINAL == system)
{
numeral = new NumeralOrdinal();
}
else if(NumeralSystem.ROMAN == system)
{
numeral = new NumeralRoman();
}
else
{
throw new IllegalArgumentException("Numeral system unknown: " +
system);
}
return numeral.getNumber();
}
/**
* Retornar o numero desejado
*
* @return numero desejado
*/
public abstract Number getNumber();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numeral cardinal
*
* ConcreteCreator redefine (overrides) o metodo factory para retornar
* uma instancia de um ConcreteProduct (Gamma, 2000)
*/
public class NumeralCardinal extends Numeral
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Numeral#getNumber()
*/
@Override
public Number getNumber()
{
return new NumberCardinal();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numeral ordinal
*
* ConcreteCreator redefine (overrides) o metodo factory para retornar
* uma instancia de um ConcreteProduct (Gamma, 2000)
*/
public class NumeralOrdinal extends Numeral
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Numeral#getNumber()
*/
@Override
public Number getNumber()
{
return new NumberOrdinal();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
/**
* Especificacao do numeral romano
*
* ConcreteCreator redefine (overrides) o metodo factory para retornar
* uma instancia de um ConcreteProduct (Gamma, 2000)
*/
public class NumeralRoman extends Numeral
{
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial08.exercicio16.Numeral#getNumber()
*/
@Override
public Number getNumber()
{
return new NumberRoman();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial08.exercicio16;
import java.util.Scanner;
/**
* Classe responsavel pela execucao do padrao Factory Method
*/
public class Application
{
/**
* Construtor para inicializar a execucao do padrao Factory Method
*/
private Application()
{
}
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Forneça o número desejado: ");
int value = scanner.nextInt();
scanner.close();
Number number = Numeral.getNumeral(NumeralSystem.CARDINAL);
System.out.println("Cardinal: " + number.converter(value));
number = Numeral.getNumeral(NumeralSystem.ORDINAL);
System.out.println("Ordinal: " + number.converter(value));
number = Numeral.getNumeral(NumeralSystem.ROMAN);
System.out.println("Romano: " + number.converter(value));
}
}