Desenvolva um programa que exiba uma mensagem diferente para cada dia da semana, usando o padrão Strategy.
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Strategy
*/
public interface Weekdays
{
/**
* Retornar uma mensagem
*
* @return mensagem
*/
public String message();
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Sunday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Sunday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Monday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Monday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Tuesday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Tuesday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Wednesday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Wednesday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Thursday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Thursday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Friday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Friday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Concrete Strategy
*/
public class Saturday implements Weekdays
{
/* (non-Javadoc)
* @see com.ybadoo.Week#message()
*/
public String message()
{
return "Today is Saturday!";
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Context
*/
public class Week
{
/**
* Retornar o dia da semana
*
* @param weekdays dia da semana
* @return instancia do dia da semana
*/
public Weekdays getWeekdays(int weekdays)
{
// Verificar o dia da semana desejado
switch(weekdays)
{
case 1: return new Sunday();
case 2: return new Monday();
case 3: return new Tuesday();
case 4: return new Wednesday();
case 5: return new Thursday();
case 6: return new Friday();
case 7: return new Saturday();
}
// Dia da semana invalido
throw new IllegalArgumentException("'" + weekdays +
"' is an illegal argument for the parameter weekdays!");
}
/**
* Retornar o dia da semana, via reflexao
*
* @param weekdays dia da semana
* @return instancia do dia da semana
*/
public Weekdays getWeekdays(String weekdays)
{
try
{
// Obter a instancia da classe desejada
return (Weekdays) Class.forName(weekdays).newInstance();
}
catch(Exception exception)
{
// Dia da semana invalido
throw new IllegalArgumentException("'" + weekdays +
"' is an illegal argument for the parameter weekdays!");
}
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
import java.util.Calendar;
import java.util.Locale;
/**
* Classe de teste da aplicacao
*/
public class Application
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Week week = new Week();
Calendar calendar = Calendar.getInstance();
System.out.println(week.getWeekdays(calendar.get(Calendar.DAY_OF_WEEK)).message());
System.out.println(week.getWeekdays(calendar.getDisplayName(Calendar.DAY_OF_WEEK,
Calendar.LONG, Locale.ENGLISH)).message());
}
}