[JAVA] replace trap

I was fooled

Try by email

MailTemplate.java


package mail;

public class MailTemplate {

	/** from */
	private String from;
	/** to */
	private String to;
	/** cc */
	private String cc;
	/** title */
	private String title;
	/** body */
	private String body;

	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
}

If you create a text, it looks like this

Test1.java


package test;

import mail.MailTemplate;

/**
 * test
 *
 * @author me
 *
 */
public class Test1 {

	/** mail template */
	private static MailTemplate mail = new MailTemplate();

	/** body template */
	private static final String BODY = "[time]To[place]It's a machiawase.\r\n If not come[kill]。";

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(join(createMail()));
	}

	private static String join(MailTemplate mail) {
		String crlf = "\r\n";
		return mail.getFrom() + crlf + mail.getTo() + crlf + mail.getCc() + crlf + mail.getTitle() + crlf + mail.getBody();
	}

	/**
	 * create
	 * @return
	 */
	private static MailTemplate createMail() {

		// from
		mail.setFrom("Okunushi");
		// to
		mail.setTo("Okurisaki");
		// cc
		mail.setCc("the other person");
		// title
		mail.setTitle("Kenmei");
		// body
		mail.setBody(replaceBody(BODY));

		return mail;
	}

	/**
	 * replace
	 * @param text
	 * @return
	 */
	private static String replaceBody(String text) {
		text.replace("[time]", "2019/12/25");
		text.replace("[place]", "Tokyo Station");
		text.replace("[kill]", "Bottle");
		return text;
	}

}

And the result

Okunushi
Okurisaki
the other person
Kenmei
[time]To[place]It's a machiawase.
If you don't come[kill]。

replace

You can't do it like Getter Setter

Correctly

Test2.java


package test;

import mail.MailTemplate;

/**
 * test
 *
 * @author me
 *
 */
public class Test2 {

	/** mail template */
	private static MailTemplate mail = new MailTemplate();

	/** body template */
	private static final String BODY = "[time]To[place]It's a machiawase.\r\n If not come[kill]。";

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(join(createMail()));
	}

	private static String join(MailTemplate mail) {
		String crlf = "\r\n";
		return mail.getFrom() + crlf + mail.getTo() + crlf + mail.getCc() + crlf + mail.getTitle() + crlf + mail.getBody();
	}

	/**
	 * create
	 * @return
	 */
	private static MailTemplate createMail() {

		// from
		mail.setFrom("Okunushi");
		// to
		mail.setTo("Okurisaki");
		// cc
		mail.setCc("the other person");
		// title
		mail.setTitle("Kenmei");
		// body
		mail.setBody(replaceBody(BODY));

		return mail;
	}

	/**
	 * replace
	 * @param text
	 * @return
	 */
	private static String replaceBody(String text) {
		text = text.replace("[time]", "2019/12/25")
				.replace("[place]", "Tokyo Station")
				.replace("[kill]", "Bottle");
		return text;
	}

}

result

Okunushi
Okurisaki
the other person
Kenmei
2019/12/Machiawase at Tokyo Station on 25th.
If it didn't come, it was a bottle.

If you do not overwrite it firmly, nothing will change, but it seems to be sharp with blind spots

Recommended Posts

replace trap
Docker-compose ~ volume trap ~