Desenvolva um programa que coloque o comentário principal no início do arquivo especificado pelo usuário, conforme a linguagem de programação contida no arquivo. Por exemplo, caso a linguagem de programação seja Java (.java), o texto a ser inserido no início do arquivo deve ser:
/** *
* Copyright (C) YEAR - YOUR NAME *
* *
* 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". *
* */
Caso a linguagem de programação seja SQL (.sql), o texto a ser inserido no início do arquivo deve ser:
---------------------------------------------------------------------------
-- Copyright (C) YEAR - YOUR NAME --
-- --
-- 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". --
---------------------------------------------------------------------------
Caso a linguagem de programação seja Pascal (.pas), o texto a ser inserido no início do arquivo deve ser:
// Copyright (C) YEAR - YOUR NAME
//
// 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".
Utilize o padrão de projeto Template Method na implementação do programa.
/*************************************************************************
* 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.tutorial10.exercicio34;
/**
* Inserção do comentário no cabeçalho do arquivo
*
* AbstractClass - define operações primitivas abstratas que as subclasses
* concretas definem para implementar passos de um
* algoritmo, implementa um método template que define o
* esqueleto de um algoritmo. O método template invoca
* operações primitivas, bem como operações definidas em
* AbstractClass ou ainda outros objetos (Gamma, 2000).
*/
public abstract class CommentsFile
{
/**
* Conteúdo do comentário
*/
private String[] lines;
/**
* Número de caracteres do conteúdo do comentário
*/
private int size;
/**
* Inicializar o comentário do cabeçalho do arquivo
*
* @param lines conteúdo do comentário
*/
public CommentsFile(String[] lines)
{
int size = 0;
for(String line: lines)
{
if(line.length() > size)
{
size = line.length();
}
}
this.lines = lines;
this.size = size;
}
/**
* Retornar o comentário do cabeçalho do arquivo
*
* @return comentário do cabeçalho do arquivo
*/
public String comments()
{
StringBuilder comment = new StringBuilder();
comment.append(firstLine());
for(String line: lines)
{
comment.append(contentLine(line)).append('\n');
}
comment.append(lastLine()).append('\n');
return comment.toString();
}
/**
* Retornar a linha formatada com o conteúdo
*
* @param content conteúdo a ser inserido no comentário
* @return linha formatada com o conteúdo
*/
protected abstract String contentLine(String content);
/**
* Retornar a primeira linha do comentário
*
* @return primeira linha do comentário
*/
protected abstract String firstLine();
/**
* Retornar o número de caracteres do conteúdo do comentário
*
* @return número de caracteres do conteúdo do comentário
*/
public int getSize()
{
return size;
}
/**
* Retornar a última linha do comentário
*
* @return última linha do comentário
*/
protected abstract String lastLine();
}
/*************************************************************************
* 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.tutorial10.exercicio34;
/**
* Inserção do comentário no cabeçalho do arquivo .java
*
* ConcreteClass - implementa as operações primitivas para executarem os
* passos específicos do algoritmo da subclasse (Gamma, 2000).
*/
public class CommentsFileJava extends CommentsFile
{
/**
* Inicializar o comentário do cabeçalho do arquivo .java
*
* @param lines linhas do comentário
*/
public CommentsFileJava(String[] lines)
{
super(lines);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#contentLine(java.lang.String)
*/
@Override
public String contentLine(String content)
{
StringBuilder comment = new StringBuilder();
comment.append(' ').append('*').append(' ');
comment.append(content);
for(int i = content.length(); i < getSize(); i++)
{
comment.append(' ');
}
comment.append(' ').append('*');
return comment.toString();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#firstLine()
*/
@Override
public String firstLine()
{
StringBuilder comment = new StringBuilder();
comment.append('/').append('*').append('*');
for(int i = 0; i < getSize(); i++)
{
comment.append(' ');
}
comment.append(' ').append('*').append('\n');
return comment.toString();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#lastLine()
*/
@Override
public String lastLine()
{
StringBuilder comment = new StringBuilder();
comment.append(' ').append('*').append(' ');
for(int i = 0; i < getSize(); i++)
{
comment.append(' ');
}
comment.append(' ').append('*').append('/').append('\n');
return comment.toString();
}
}
/*************************************************************************
* 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.tutorial10.exercicio34;
/**
* Inserção do comentário no cabeçalho do arquivo .pas
*
* ConcreteClass - implementa as operações primitivas para executarem os
* passos específicos do algoritmo da subclasse (Gamma, 2000).
*/
public class CommentsFilePas extends CommentsFile
{
/**
* Inicializar o comentário do cabeçalho do arquivo .pas
*
* @param lines linhas do comentário
*/
public CommentsFilePas(String[] lines)
{
super(lines);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#contentLine(java.lang.String)
*/
@Override
public String contentLine(String content)
{
StringBuilder comment = new StringBuilder();
comment.append('/').append('/').append(' ');
comment.append(content);
return comment.toString();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#firstLine()
*/
@Override
public String firstLine()
{
return "";
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#lastLine()
*/
@Override
public String lastLine()
{
return firstLine();
}
}
/*************************************************************************
* 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.tutorial10.exercicio34;
/**
* Inserção do comentário no cabeçalho do arquivo .sql
*
* ConcreteClass - implementa as operações primitivas para executarem os
* passos específicos do algoritmo da subclasse (Gamma, 2000).
*/
public class CommentsFileSql extends CommentsFile
{
/**
* Inicializar o comentário do cabeçalho do arquivo .sql
*
* @param lines linhas do comentário
*/
public CommentsFileSql(String[] lines)
{
super(lines);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#contentLine(java.lang.String)
*/
@Override
public String contentLine(String content)
{
StringBuilder comment = new StringBuilder();
comment.append('-').append('-').append(' ');
comment.append(content);
for(int i = content.length(); i < getSize(); i++)
{
comment.append(' ');
}
comment.append(' ').append('-').append('-');
return comment.toString();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#firstLine()
*/
@Override
public String firstLine()
{
StringBuilder comment = new StringBuilder();
for(int i = 0; i < getSize() + 6; i++)
{
comment.append('-');
}
comment.append('\n');
return comment.toString();
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio34.CommentsFile#lastLine()
*/
@Override
public String lastLine()
{
return firstLine();
}
}
/*************************************************************************
* 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.tutorial10.exercicio34;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;
/**
* Classe responsável pela execução do padrão Template Method
*/
public class Application
{
/**
* Construtor para inicializar a execução do padrão Template Method
*/
private Application()
{
}
/**
* Método principal da linguagem de programação Java
*
* @param args argumentos da linha de comando (não utilizado)
*/
public static void main(String[] args)
{
String[] text = {"Copyright (C) 2009/2016 - Cristiano Lehrer",
" Ybadoo - Soluções 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\"."};
Scanner scanner = new Scanner(System.in);
System.out.print("Forneça o arquivo: ");
File file = new File(scanner.nextLine());
String extension = null;
int i = file.getAbsolutePath().lastIndexOf('.');
if (i > 0)
{
extension = file.getAbsolutePath().substring(i+1);
}
CommentsFile comments = null;
if("java".equals(extension))
{
comments = new CommentsFileJava(text);
}
else if("sql".equals(extension))
{
comments = new CommentsFileSql(text);
}
else if("pas".equals(extension))
{
comments = new CommentsFilePas(text);
}
else
{
System.exit(0);
}
try
{
String dados = comments.comments() + new String(Files.readAllBytes(file.toPath()));
Files.write(file.toPath(), dados.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
}
catch (IOException exception)
{
exception.printStackTrace();
}
scanner.close();
}
}