Last active
May 18, 2023 08:59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public class TestClass { | |
public void ProcessInfo(List<InfoBase> infoList) { | |
StringBuilder result = new StringBuilder(); | |
for (int i = 0; i < infoList.size(); i++) { | |
InfoBase info = infoList.get(i); | |
if (this.Validate(info)) { | |
StringBuilder infoData = this.ExtractData(info); | |
result.append(infoData); | |
} else { | |
infoList.remove(info); | |
} | |
} | |
System.out.println(result.toString()); | |
} | |
private <T extends InfoBase> boolean Validate(T infoBase) { | |
boolean isValid = false; | |
switch (infoBase.GetInfoType()) { | |
case Sport: { | |
if (infoBase instanceof InfoSport) { | |
if (((InfoSport) infoBase).title.startsWith("Sport news: ")) { | |
isValid = true; | |
} | |
} | |
} | |
break; | |
case Music: { | |
if (infoBase instanceof InfoMusic) { | |
if (((InfoMusic) infoBase).title.startsWith("Music charts: ")) { | |
isValid = true; | |
} | |
} | |
} | |
break; | |
} | |
return infoBase != null && isValid; | |
} | |
private StringBuilder ExtractData(InfoBase info) { | |
StringBuilder result = new StringBuilder(); | |
String title = info.GetInfoType().getTitle(); | |
result.append(title).append("\r\n"); | |
result.append(info.getData()).append("\r\n\r\n"); | |
return result; | |
} | |
private abstract class InfoBase { | |
protected InfoType infoType; | |
public InfoType GetInfoType() { | |
return this.infoType; | |
} | |
public abstract String getData(); | |
} | |
public class InfoMusic extends InfoBase { | |
public String title; | |
public String author; | |
public InfoMusic(String title, String author) { | |
this.infoType = InfoType.Music; | |
this.title = title; | |
this.author = author; | |
} | |
public String getData() { | |
return String.format("%s\r\nAuthor:%s", this.title, this.author); | |
} | |
} | |
public class InfoSport extends InfoBase { | |
public String title; | |
public String data; | |
public InfoSport(String title, String data) { | |
this.infoType = InfoType.Sport; | |
this.title = title; | |
this.data = data; | |
} | |
public String getData() { | |
return String.format("%s\r\n%s", this.title, this.data); | |
} | |
} | |
private enum InfoType { | |
Music, | |
Sport, | |
; | |
private String getTitle() { | |
switch (this) { | |
case Sport: | |
return "<b>Breaking sport news!</b>"; | |
case Music: | |
return "<b>New single!</b>"; | |
} | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment