[JAVA] Falle ersetzen

ich wurde reingelegt

Versuchen Sie es per E-Mail

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;
	}
}

Wenn Sie einen Text erstellen, sieht er so aus

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]Zu[place]Es ist ein großes Geschäft.\r\n Wenn nicht kommen[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("die andere 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]", "Flasche");
		return text;
	}

}

Und das Ergebnis

Okunushi
Okurisaki
die andere Person
Kenmei
[time]Zu[place]Es ist ein großes Geschäft.
Wenn du nicht kommst[kill]。

ersetzen

Sie können es nicht wie Getter Setter tun

Korrekt

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]Zu[place]Es ist ein großes Geschäft.\r\n Wenn nicht kommen[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("die andere 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]", "Flasche");
		return text;
	}

}

Ergebnis

Okunushi
Okurisaki
die andere Person
Kenmei
2019/12/Es ist eine Stadt am Tokyo Station am 25 ..
Wenn es nicht kam, war es eine Flasche.

Wenn Sie es nicht fest überschreiben, ändert sich nichts, aber es scheint scharf mit einem blinden Fleck zu sein

Recommended Posts

Falle ersetzen
Docker-Compose ~ Volume Trap ~