I have a query is that I have a string that I get through a param, the issue is that the split I realize you do not know if it's the best, but the idea was to separate these value chains that interest me, and then compare them.
public class TestSplit {
public static final String CODIGO_BUSCAR = "001";
public static final String CODIGO_NUEVO = "002";
public static final String CODIGO_MODIFICAR = "003";
public static final String CODIGO_ELIMINAR = "004";
public static final String CODIGO_CONSULTAR = "006";
public static void main(String[] args) throws Exception {
boolean mostrarBotonNuevo = false;
boolean mostrarBotonModificar = false;
boolean mostrarBotonConsultar = false;
boolean mostrarBotonEliminar = false;
boolean mostrarBotonBuscar = false;
String key = "{javax.faces.partial.ajax=true, javax.faces.source=j_idt88:linkAjax, javax.faces.partial.execute=#all, javax.faces.partial.render=mensajesGrowl, j_idt88:linkAjax=j_idt88:linkAjax, parametroAccion={javax.faces.partial.ajax=true, javax.faces.source=j_idt80:j_idt82:linkAjax, javax.faces.partial.execute=#all, javax.faces.partial.render=mensajesGrowl, j_idt80:j_idt82:linkAjax=j_idt80:j_idt82:linkAjax, parametroAccion={menuForm=menuForm, j_idt46_activeIndex=0, javax.faces.ViewState=KAITjDUzvPckml3R4yYr9j3J2z/iwqr8HrkQyCyPl4QUNAom0iY7kGQqomJaUN4enfhiwIGBXb1c9bgohVXD3P0emxCpykIM9fGP2hBtKq2fPssmHkbbYDNa5DX2mxTvTssCeRZ/Y1dTSim2x/J/lJQFpa8e/GJh8KkK7AaVCb1X3kk5J5nZHIlhDpfnRwGe7VfHxkQNKsdiDhf+lCwp+rDb0eGsOyGnRSaIB83TVbCBqN6E8IARA6i5FiQe6qW6hxAA0tpCoMGQO69sKau3TCZEAbDCyLnaGa471QEl4974sSziZVQBRywJJsdBM+seSrgnO04idUdCpY7n/qJLyB8L5yNn5beTki/azTOMBPKQgK2NC/ACILU/E8QeQ6zq0vJ43/I16s+t1IgB3ajnOAMsvkMyU9LLDjlbCrMnlyoUVlPIwVK6Qg+dBTkyNOXXTuYLm9hXdaIcjbXXqLbQj7igfFWA9dAmCn5AAHA8ttIQ6kEBuTc+Ldd5Ux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menuItemCotizaciones006, j_idt46:j_idt55_menuid=2_9, menuItemCotizaciones003=menuItemCotizaciones003, menuItemCotizaciones004=menuItemCotizaciones004, menuItemCotizaciones001=menuItemCotizaciones001, menuItemCotizaciones002=menuItemCotizaciones002}, principalForm=principalForm, idSeleccionoRegistro=false, j_idt120:input=, j_idt124:select_focus=, j_idt124:select_input=, j_idt129:select_focus=, j_idt129:select_input=, j_idt134:calendar_input=, j_idt137:calendar_input=, javax.faces.ViewState=YmaLdynq93csVHnfBbfsdEoqLX1VsE34AnGcIl1K609wg0pfECjwZI0e3Y/hu6fvEhAElNo9Theyrh8/cEDWpeiQRJ/is89OXCI3dpp1Gk8oEERlP3xS4ivexx3OzET5k2UqbGDOYvv9yejBe7jwCJqA4Uw/yXtXHc959XW7sAjV9Rs8d5JqWgchaayW0UaWdPb0piAs+e5WETWxYZShLXGF3kQ+ikf4NG1xUEiOnXvUHyn+Th7tbmRqSEa/idPOKzoirxMunWtGXWg9/Uu3cl2OqBwDJEMT99ryys5THtds8fgrixOvNksVsuk8HkeR/QrwFq9LM7CqWu1ho9PyAraySSg5WRN9IZkcFV7MGw5TzJG9pVkGZs21xeZozB28Fk5tiG9c3G9qa9IXYL53RLOZb27/wLZlWPedGkiDoEzXXdnSP315uRshDASLCMzvqJFy4/Y4IX2Sdk5Ks4px9aqGMoXFUd2vJpOQniKrx+tkzUsX5NNheLQZCHGchSDivhjwxmbxk/hy0KnKAsitC/XM2cb47Jvd/Y/zqDmFMu05Jya36rOC+bT7hElV9u/NZgf0ZscFuZ4x6LIWKyjkhAmRanSOBDlFs24fu3NMv+FE2Q5OpGzgHyDn+gq1fo4tcNk9szeSuF06FMtIA5vnd1GCojssv5c0cRv656ndBLx9hHmIULjg9avxu0fpKHTLQultYTXTp9m7OCkESSTKV/qLymItsGgwLK3NvDEQ0a3ovweO6GKEi8MTnOxn0aFw06F7YmsObd4ITKIR8qwHTjORGcPDskBL0mS3+r7idBG8JEgTyjPM4l9D9lYreeeYeX4kUt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Yu6DzMdW8egfjGydZuKx/zfDsycvYUJ1HKD8W588F5ZkDTxSH3C/CPf50FpTEEBX+RXe7+6WPs71rUTepkqAjOYMgw4gVpD+nNx5C...";
String keyFormat [] = key.split("=");
for(String value : keyFormat) {
System.out.println("--> "+value);
if(value.endsWith(CODIGO_BUSCAR)) {
mostrarBotonBuscar = true;
}
if(value.endsWith(CODIGO_NUEVO)) {
mostrarBotonNuevo = true;
}
if(value.endsWith(CODIGO_MODIFICAR)) {
mostrarBotonModificar = true;
}
if(value.endsWith(CODIGO_ELIMINAR)) {
mostrarBotonEliminar = true;
}
if(value.endsWith(CODIGO_CONSULTAR)) {
mostrarBotonConsultar = true;
}
}
System.out.println("--> mostrarBotonBuscar: "+mostrarBotonBuscar+" mostrarBotonNuevo: "+mostrarBotonNuevo+
" mostrarBotonModificar: "+mostrarBotonModificar+" mostrarBotonEliminar: "+mostrarBotonEliminar+
" mostrarBotonConsultar: "+mostrarBotonConsultar);
}
}
Basically what I do is use the slipt to separate strings and compare whether the value ends in "001", "002", "003", "004" and "006", what other details do not matter, and then they meet is activate the corresponding boolean state.
I was thinking to use a switch or while operator to optimize my code, if anyone can give me a guidance to improve my code, because as noticing the loop is comparing each chain, the idea is to compare each and finish the loop.
I don't think while even fits here. switch will not do any good either (rather leave it as it is, or better, change ifs to else-ifs.. But I can give a better idea, that involves none of them. Just make use of || operator. That will require changing your for loop to something like:
for(String value : keyFormat) {
System.out.println("--> "+value);
mostrarBotonBuscar = mostrarBotonBuscar || value.endsWith(CODIGO_BUSCAR);
mostrarBotonNuevo = mostrarBotonNuevo || value.endsWith(CODIGO_NUEVO);
mostrarBotonModificar = mostrarBotonModificar || value.endsWith(CODIGO_MODIFICAR);
mostrarBotonEliminar = mostrarBotonEliminar || value.endsWith(CODIGO_ELIMINAR);
mostrarBotonConsultar = mostrarBotonConsultar || value.endsWith(CODIGO_CONSULTAR);
}
That's it. Nothing else you need. Moreover it will also stop evaluating the corresponding endsWith() methods once the flag is set to true.
Thus concern with doing subtring and switch ....
public class TestSplit {
public static final String CODIGO_BUSCAR = "001";
public static final String CODIGO_NUEVO = "002";
public static final String CODIGO_MODIFICAR = "003";
public static final String CODIGO_ELIMINAR = "004";
public static final String CODIGO_CONSULTAR = "006";
public static void main(String[] args) throws Exception {
boolean mostrarBotonNuevo = false;
boolean mostrarBotonModificar = false;
boolean mostrarBotonConsultar = false;
boolean mostrarBotonEliminar = false;
boolean mostrarBotonBuscar = false;
String key = "{javax.faces.partial.ajax=true, javax.faces.source=j_idt88:linkAjax, javax.faces.partial.execute=#all, javax.faces.partial.render=mensajesGrowl, j_idt88:linkAjax=j_idt88:linkAjax, parametroAccion={javax.faces.partial.ajax=true, javax.faces.source=j_idt80:j_idt82:linkAjax, javax.faces.partial.execute=#all, javax.faces.partial.render=mensajesGrowl, j_idt80:j_idt82:linkAjax=j_idt80:j_idt82:linkAjax, parametroAccion={menuForm=menuForm, j_idt46_activeIndex=0, javax.faces.ViewState=KAITjDUzvPckml3R4yYr9j3J2z/iwqr8HrkQyCyPl4QUNAom0iY7kGQqomJaUN4enfhiwIGBXb1c9bgohVXD3P0emxCpykIM9fGP2hBtKq2fPssmHkbbYDNa5DX2mxTvTssCeRZ/Y1dTSim2x/J/lJQFpa8e/GJh8KkK7AaVCb1X3kk5J5nZHIlhDpfnRwGe7VfHxkQNKsdiDhf+lCwp+rDb0eGsOyGnRSaIB83TVbCBqN6E8IARA6i5FiQe6qW6hxAA0tpCoMGQO69sKau3TCZEAbDCyLnaGa471QEl4974sSziZVQBRywJJsdBM+seSrgnO04idUdCpY7n/qJLyB8L5yNn5beTki/azTOMBPKQgK2NC/ACILU/E8QeQ6zq0vJ43/I16s+t1IgB3ajnOAMsvkMyU9LLDjlbCrMnlyoUVlPIwVK6Qg+dBTkyNOXXTuYLm9hXdaIcjbXXqLbQj7igfFWA9dAmCn5AAHA8ttIQ6kEBuTc+Ldd5UxNhxn1lVrcafItdp+4kkv1BhMJVsVUJRDwRwnfeTbf6XwlaICH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TOReNWImmQ6RxluGBto0wugeHdHKWaZjuRZ37rRjCc2WYy/OSMDdbxgnK6ehX7X2Wo00JJAE9gTr+0r1Z3kDwoeHGIwhzohFYdOeRvpzmnMHoTN4vDMcTDKWDXSO/QKUhlDOQq3paABugq995/+gUrr53JfeVuNEC9AWUaiZ6K/bcJgQ7X4mR4v/Ns93Uee+9oSTTIQbzz5zD/GGCke0sA1vuAdgA5f/1LdSf7rkWWVP1oj9GJHgF1iVol+V+8VaLjA6iOUgndR30bNjL1le0vBC/4/lPmWGfJbEqHadRnkiijNzxK5ul2YqXJByPKBvVWF+Kee6xpKPG6Py8HDO/IAfi9Cq3Apbt6R7ZAKi8sB7ltAuCY6h60qDT3m46OO8nvTJQvVESBrPxKiuHEEx4g6gLjI362tb+tPOQ0iQ3I0VZgF6UvtHK8D0hw+b7tGsnILcQYabjzciN0g9cf+9w6Kr6FpfT7gKPP1ODMoBKcZYC/HgbIfx2f/+/Qsszc2njFiZlPmtqZBR7T+lHdK7g6sUQm0DsGO1jDHBw3zNc1clPPcFm6oxSE3fkNaIPzwZZDkwpv7UkJGAlQ9NVGayHEWZBk+aMKjRLDNOordde5byeuYoopwG9Ex6UtwPMlDiAd3ofcvrGXJoE9J5zWRijmFlbD524VG703Dx8c5x+3ymKPg9snfLMSPM4c5tYhT9ileJ1CwwBGPRhByu9QbIGvm9BO04V3G7h+XMSoCsIzFDmzQ6GbktnTFVEyaHcM9XJ2DNSfFzOYbrUiWM4hNFQTUWSPPJ8VdiurJnbN1Dl+mSub+bQgurmnkO7Iq9foE7ILlXiQv1qI3fvHIdpB4kCNCYSTeHXtgwCxQzvLBD1CGdStzgAkLxZa2m9tTy5Cc8dVh/spKlv0Ia+AHWFDZAziZD8gbPkos5N8=, vdesOpcForm=menuItemCotizaciones, j_idt46:j_idt55=j_idt46:j_idt55, menuItemCotizaciones005=menuItemCotizaciones005, menuItemCotizaciones006=menuItemCotizaciones006, j_idt46:j_idt55_menuid=2_9, menuItemCotizaciones003=menuItemCotizaciones003, menuItemCotizaciones004=menuItemCotizaciones004, menuItemCotizaciones001=menuItemCotizaciones001, menuItemCotizaciones002=menuItemCotizaciones002}, principalForm=principalForm, idSeleccionoRegistro=false, j_idt120:input=, j_idt124:select_focus=, j_idt124:select_input=, j_idt129:select_focus=, j_idt129:select_input=, j_idt134:calendar_input=, j_idt137:calendar_input=, javax.faces.ViewState=YmaLdynq93csVHnfBbfsdEoqLX1VsE34AnGcIl1K609wg0pfECjwZI0e3Y/hu6fvEhAElNo9Theyrh8/cEDWpeiQRJ/is89OXCI3dpp1Gk8oEERlP3xS4ivexx3OzET5k2UqbGDOYvv9yejBe7jwCJqA4Uw/yXtXHc959XW7sAjV9Rs8d5JqWgchaayW0UaWdPb0piAs+e5WETWxYZShLXGF3kQ+ikf4NG1xUEiOnXvUHyn+Th7tbmRqSEa/idPOKzoirxMunWtGXWg9/Uu3cl2OqBwDJEMT99ryys5THtds8fgrixOvNksVsuk8HkeR/QrwFq9LM7CqWu1ho9PyAraySSg5WRN9IZkcFV7MGw5TzJG9pVkGZs21xeZozB28Fk5tiG9c3G9qa9IXYL53RLOZb27/wLZlWPedGkiDoEzXXdnSP315uRshDASLCMzvqJFy4/Y4IX2Sdk5Ks4px9aqGMoXFUd2vJpOQniKrx+tkzUsX5NNheLQZCHGchSDivhjwxmbxk/hy0KnKAsitC/XM2cb47Jvd/Y/zqDmFMu05Jya36rOC+bT7hElV9u/NZgf0ZscFuZ4x6LIWKyjkhAmRanSOBDlFs24fu3NMv+FE2Q5OpGzgHyDn+gq1fo4tcNk9szeSuF06FMtIA5vnd1GCojssv5c0cRv656ndBLx9hHmIULjg9avxu0fpKHTLQultYTXTp9m7OCkESSTKV/qLymItsGgwLK3NvDEQ0a3ovweO6GKEi8MTnOxn0aFw06F7YmsObd4ITKIR8qwHTjORGcPDskBL0mS3+r7idBG8JEgTyjPM4l9D9lYreeeYeX4kUtLcbYBfgf6uMgN/h2Q0RSGMLqmmpmp1jfp9ngVI8kVKB4o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3C/CPf50FpTEEBX+RXe7+6WPs71rUTepkqAjOYMgw4gVpD+nNx5C...";
String keyFormat [] = key.split("=");
for(String value : keyFormat) {
int size = value.length();
if(size > 3) {
String vcodIdeObj = value.substring(size-3, size);
switch (vcodIdeObj) {
case CODIGO_BUSCAR:
mostrarBotonBuscar = true;
break;
case CODIGO_NUEVO:
mostrarBotonNuevo = true;
break;
case CODIGO_MODIFICAR:
mostrarBotonModificar = true;
break;
case CODIGO_ELIMINAR:
mostrarBotonEliminar = true;
break;
case CODIGO_CONSULTAR:
mostrarBotonConsultar = true;
break;
}
}
}
}
}
Please validate my code, thanks.
Related
I need to find the first true value in arraylist and then the next true I need to turn into false and I don't know how to do so. I tried to use isCorrect but it doesn't do what I want it to.
Here's what I wrote so far:
private boolean singleOption;
private ArrayList<Option> allOptions;
public ClosedQuestion(int id, String title, boolean mandatory,boolean singleOption)
{
super(id,title,mandatory);
this.singleOption=singleOption;
allOptions = new ArrayList<Option>();
}
public boolean isSingleOption() {
return singleOption;
}
public void setSingleOption(boolean singleOption)
{
boolean flag = true;
for (int i = 0; i < allOptions.size();i++)
{
if(allOptions.get(i).isCorrect()&&flag)
{
flag = false;
}
if (!flag)
{
for(int j=1; j< allOptions.size();j++)
{
allOptions.get(j).setCorrect(false);
}
}
}
}
public ClosedQuestion(ClosedQuestion other)
{
super(other);
this.singleOption=other.singleOption;
allOptions = new ArrayList<Option>();
for (Option o: other.allOptions)
{
allOptions.add(new Option(o));
}
}
}
Code: Search word in Trie
Implement the function SearchWord for the Trie class.
For a Trie, write the function for searching a word. Return true if the word is found successfully, otherwise return false.
Note: main function is given for your reference which we are using internally to test the code.
class TrieNode{
char data;
boolean isTerminating;
TrieNode children[];
int childCount;
public TrieNode(char data) {
this.data = data;
isTerminating = false;
children = new TrieNode[26];
childCount = 0;
}
}
public class Trie {
private TrieNode root;
public int count;
public Trie() {
root = new TrieNode('\0');
count = 0;
}
private boolean add(TrieNode root, String word){
if(word.length() == 0){
if (!root.isTerminating) {
root.isTerminating = true;
return true;
} else {
return false;
}
}
int childIndex = word.charAt(0) - 'a';
TrieNode child = root.children[childIndex];
if(child == null){
child = new TrieNode(word.charAt(0));
root.children[childIndex] = child;
root.childCount++;
}
return add(child, word.substring(1));
}
public void add(String word){
if (add(root, word)) {
this.count++;
}
}
public boolean search(String word){
// add your code here
return search(root,word);
}
private boolean search(TrieNode root, String word){
if(word.length()==0){
return true;
}
int childIndex = word.charAt(0) -'a';
TrieNode child = root.children[childIndex];
if(child==null){
return false;
}
return search(child, word.substring(1));
}
}
//Main Function
code
import java.io.*;
public class Runner {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException{
Trie t = new Trie();
String[] string = br.readLine().split("\\s");
int choice = Integer.parseInt(string[0]);
String word = "Null";
if (string.length!=1)
{
word = string[1];
}
while(choice != -1) {
switch(choice) {
case 1 : // insert
t.add(word);
break;
case 2 : // search
System.out.println(t.search(word));
break;
default :
return;
}
string = br.readLine().split("\\s");
choice = Integer.parseInt(string[0]);
if (string.length!=1)
{
word = string[1];
}
}
}
}
You need to make use of the isTerminating information. In search, change:
if(word.length()==0){
return true;
}
To:
if(word.length()==0){
return root.isTerminating;
}
Im fairly new to java and ive been doing som searching for an answer to my problem but i just cant seem to get the output from the arraylist.
I get a red mark under Ordtildikt String arrayoutput = kontrollObjekt.Ordtildikt;saying it cannot be resolved or is not a field. The program is supposed to get userinput and create an arraylist from the input
Interface class
import javax.swing.JOptionPane;
public class Grensesnitt {
public static void main(String[] args) {
Grensesnitt Grensesnitt = new Grensesnitt();
Grensesnitt.meny();
}
Kontroll kontrollObjekt = new Kontroll();
private final String[] ALTERNATIVER = {"Registrere ord","Skriv dikt","Avslutt"};
private final int REG = 0;
private final int SKRIV = 1;
public void meny() {
boolean fortsett = true;
while(fortsett) {
int valg = JOptionPane.showOptionDialog(
null,
"Gjør et valg:",
"Prosjektmeny",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
ALTERNATIVER,
ALTERNATIVER[0]);
switch(valg) {
case REG: regNy();
break;
case SKRIV: regDikt();
break;
default: fortsett = false;
}
}
}
int i = 0;
public void regNy() {
while(i<=16)
{
String Ord = JOptionPane.showInputDialog("Skriv or til diktet: ");
kontrollObjekt.regNy(Ord);
//String Diktord = JOptionPane.showInputDialog("Skriv ord til diktet: ");
//kontrollObjekt.regNy(Diktord);
i = i + 1;
}
}
public void regDikt() {
String arrayoutput = kontrollObjekt.Ordtildikt;
JOptionPane.showMessageDialog(null, arrayoutput);
}
//JOptionPane.showMessageDialog(null, kontrollObjekt.Diktord);
}
Controll Class
import java.util.ArrayList;
public class Kontroll {
public ArrayList<String> Diktord = new ArrayList<String>();
public void regNy(String Ord) {
Diktord.add(Ord);
Diktord.add("\n");
}
public String Ordtildikt(String Ord) {
return Ord=Diktord.toString();
}
}
This is a method, not a variable.
kontrollObjekt.Ordtildikt;
You are trying to call this?
public String Ordtildikt(String Ord) {
return Ord=Diktord.toString();
}
1) Make it return Diktord.toString();
2) Get rid of String Ord unless you are going to use that parameter
3) Actually call the method, e.g. Put some parenthesis.
String arrayoutput = kontrollObjekt.Ordtildikt();
Also, I think this should be the correct regNy method unless you want to falsely report that the list is twice its size.
public void regNy(String Ord) {
Diktord.add(Ord + "\n");
}
I am trying to test a controller of mine which returns me a List of Objects on the get method to populate a dropdown on my page.
I am trying to write a JUnit test using MockMvc and Hamcrest to test the same.
I want to compare the List of objects and test if it fails or not.
I have created a static List of objects in my Test.java and I am getting a List of objects from the model.attribute method.
To Test: if both the List of Objects are equal and don't contain any other objects.
My object is called Option which has 3 properties. Key, Value and Selected. I have to check if the all the keys exists in the List or not.
I am unable to create a matcher to do the same. I am trying to create a matcher to compare my List.
So far I have done the following:
#Before
public void setup() throws Exception {
// This would build a MockMvc with only the following controller
this.mockMvc = MockMvcBuilders.standaloneSetup(openAccountController)
.build();
}
#Test
public void testOpenAccount() {
try {
setAllLegislations();
this.mockMvc
.perform(get("/open_account.htm"))
// This method is used to print out the actual httprequest
// and httpresponse on the console.
.andDo(print())
// Checking if status is 200
.andExpect(status().isOk())
.andExpect(
model().attributeExists("appFormAccountPlans",
"appFormLiraLegislations",
"appFormLrspLegislations",
"appFormRlspLegislations"))
.andExpect(
model().attribute("appFormAccountPlans", hasSize(5)))
.andExpect(
model().attribute("appFormLiraLegislations",
hasSize(8)))
.andExpect(
model().attribute("appFormLrspLegislations",
hasSize(2)))
.andExpect(
model().attribute("appFormRlspLegislations",
hasSize(1)))
.andExpect(
model().attribute(
"appFormLiraLegislations",
hasKeyFeatureMatcher(getLiraLegislations(allLegislations))));
private Matcher<List<Option>> hasKeyFeatureMatcher(
final List<Option> expectedOptions) {
return new FeatureMatcher<List<Option>, List<Option>>(
equalTo(expectedOptions), "Options are", "was") {
#Override
protected List<Option> featureValueOf(List<Option> actualOptions) {
boolean flag = false;
if (actualOptions.size() == expectedOptions.size()) {
for (Option expectedOption : expectedOptions) {
for (Option actualOption : actualOptions) {
if (expectedOption.getKey().equals(
actualOption.getKey())) {
flag = true;
} else {
flag = false;
break;
}
}
}
}
if (flag)
return actualOptions;
else
return null;
}
};
}
private List<Option> getLiraLegislations(List<Option> legislation) {
List<Option> liraLegislations = new ArrayList<Option>();
Iterator<Option> iterator = legislation.iterator();
while (iterator.hasNext()) {
Option option = iterator.next();
if (LIRA_LEGISLATIONS.contains(option.getKey())) {
liraLegislations.add(option);
}
}
return liraLegislations;
}
private List<Option> allLegislations;
public List<Option> getAllLegislations() {
return allLegislations;
}
public void setAllLegislations() {
allLegislations = new ArrayList<Option>();
for (String key : ALL_LEGISLATIONS) {
Option option = new Option();
option.setKey(key);
allLegislations.add(option);
}
}
private static final Set<String> ALL_LEGISLATIONS = new HashSet<String>(
Arrays.asList(AccountLegislationEnum.AB.toString(),
AccountLegislationEnum.MB.toString(),
AccountLegislationEnum.NB.toString(),
AccountLegislationEnum.NL.toString(),
AccountLegislationEnum.NS.toString(),
AccountLegislationEnum.ON.toString(),
AccountLegislationEnum.QC.toString(),
AccountLegislationEnum.SK.toString(),
AccountLegislationEnum.BC.toString(),
AccountLegislationEnum.FE.toString(),
AccountLegislationEnum.NT.toString(),
AccountLegislationEnum.PE.toString(),
AccountLegislationEnum.YT.toString(),
AccountLegislationEnum.NU.toString(),
AccountLegislationEnum.UNKNOWN.toString()));
This is how I am getting my model attribute as:
Attribute = appFormLiraLegislations
value = [com.abc.arch.core.gui.eform.gui.Option#199d1739, com.abc.arch.core.gui.eform.gui.Option#185fac52, com.abc.arch.core.gui.eform.gui.Option#312a47fe, com.abc.arch.core.gui.eform.gui.Option#4edc8de9, com.abc.arch.core.gui.eform.gui.Option#71e8e471, com.abc.arch.core.gui.eform.gui.Option#70edf123, com.abc.arch.core.gui.eform.gui.Option#15726ac1, com.abc.arch.core.gui.eform.gui.Option#abeafe7]
Thanks in advance.
You can make your life definitely easier when you correctly implement Option object hashCode() and equals() methods using key attribute; then you can simply write:
model().attribute("appFormLiraLegislations",getLiraLegislations(allLegislations)))
and rely on list1.equals(list2) method to do the work for you.
Option hashCode and equals implementation:
public class Option {
private String key;
private String label;
...
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Option other = (Option) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
}
Methods above are generated by my IDE. I also don't know exactly what is structure of your Option class, so I add label property for example in addition to key property.
I created a custom Hamcrest matcher to compare the List of Option by checking the size and the keys.
private Matcher<List<Option>> hasOptionsFeatureMatcher(
final List<Option> expectedOptions) {
return new FeatureMatcher<List<Option>, List<Option>>(
equalTo(expectedOptions), "Options are", "Options were") {
#Override
protected List<Option> featureValueOf(List<Option> actualOptions) {
boolean flag = false;
if (expectedOptions.size() == actualOptions.size()) {
for (Option expected : expectedOptions) {
for (Option actual : actualOptions) {
if (expected.getKey().equals(actual.getKey())) {
flag = true;
break;
} else {
flag = false;
}
}
}
} else
flag = false;
if (flag)
return expectedOptions;
else
return null;
}
};
Implementation would be as follows:
private static final ImmutableBiMap<String, String> LIRA = new ImmutableBiMap.Builder<String, String>()
.put(AccountLegislationEnum.AB.toString(), "ALBERTA")
.put(AccountLegislationEnum.MB.toString(), "MANITTOBA")
.put(AccountLegislationEnum.NB.toString(), "NEW BRUNSWICK")
.put(AccountLegislationEnum.NL.toString(), "NEWFOUNDLAND")
.put(AccountLegislationEnum.NS.toString(), "NOVA SCOTIA")
.put(AccountLegislationEnum.ON.toString(), "ONTARIO")
.put(AccountLegislationEnum.QC.toString(), "QUEBEC")
.put(AccountLegislationEnum.SK.toString(), "SASKATCHEWAN")
.put(AccountLegislationEnum.UNKNOWN.toString(), "UNKNOWN").build();
private List<Option> prepareOptions(ImmutableBiMap<String, String> map) {
List<Option> legislations = new ArrayList<Option>();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Option option = new Option();
option.setKey(key);
option.setValue(value);
legislations.add(option);
}
return legislations;
}
.andExpect(model().attribute("appFormLiraLegislations",hasOptionsFeatureMatcher(prepareOptions(LIRA))))
This is a java assignment containing 3 class files. The problem is when I call the "doOperations" method in main. The if statements read the file correctly, but the object methods dont work(e.g. tv.on, tv.volumeUp tv.setChannel(scan.nextInt(), etc.)).
Can anyone see where I am going wrong? Thanks.
TV class file.
import java.io.*;
import java.util.Scanner;
public class TV {
boolean on;
boolean subscribe;
int currentchannel;
int indexChan;
int volume;
File chanList;
Scanner chanScan;
TVChannel[] channels;
final int MaxChannel = 100;
public TV(){
on = false;
currentchannel = 1;
volume = 1;
channels = new TVChannel[MaxChannel]; //Create object of an array, default value = null.
subscribe = false;
}
public void turnOn(){
on = true;
}
public void turnOff(){
on = false;
}
public void channelUp(){
currentchannel++;
}
public void channelDown(){
currentchannel--;
}
public void volumeUp(){
volume++;
}
public void volumeDown(){
volume--;
}
public TVChannel getChannel(){
return channels[currentchannel];
}
public void setChannel(int c){
if(channels[c]!= null)
currentchannel = c;
}
public boolean subscribe(String provider) throws IOException{
chanList = new File(provider);
chanScan = new Scanner(chanList);
if(chanList.exists()){
while(chanScan.hasNext()){
indexChan = chanScan.nextInt();
channels[indexChan] = new TVChannel(indexChan, chanScan.nextLine());
}
chanScan.close();
return true;
}
else return false;
}
public void printAll(){
for(int i = 0; i < MaxChannel; i++){
if(channels[i]!= null)
System.out.println(channels[i].channelNumber+"\t"+channels[i].channelName);
}
}
public void displayChannel(int c){
if(channels[c]!= null)
System.out.println(channels[c].getChannel()+"\t"+channels[c].getName());
}
public void displayCurrentChannel(){
if(channels[currentchannel] != null)
System.out.println(channels[currentchannel].getChannel() +" "+ channels[currentchannel].getName());
}
}
Main function
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class TestTV {
public static void main(String[] args) throws IOException{
TV tv = new TV();
tv.subscribe("chan.txt");
if(tv.subscribe = true){
tv.printAll();
doOperations("operations.txt", tv);
}
}
//Static means only one instance of object.
public static void doOperations(String opFileName, TV tv) throws IOException{
File op = new File(opFileName);
Scanner scan = new Scanner(op);
String opa = scan.next();
while(scan.hasNext()){
System.out.println(scan.next());
if(opa.equals("on")) tv.turnOn();
else if(opa.equals("off")) tv.turnOff();
else if(opa.equals("channelUp")) tv.channelUp();
else if(opa.equals("channelDown")) tv.channelDown();
else if(opa.equals("volumeUp")) tv.volumeUp();
else if(opa.equals("volumeDown")) tv.volumeDown();
else if(opa.equals("showChannel")) tv.displayCurrentChannel();
else if(opa.equals("setChannel"))tv.setChannel(scan.nextInt()); //Error
}
scan.close();
}
}
You forgot to put this
String opa = scan.next(); inside of while loop. You need to store the result of course, on each token.
while(scan.hasNext()){
opa = scan.next();
System.out.println(opa); //don't just call scan.next()
//and discard the result. Unless that's really what you want?
.....
}
Nothing wrong with your code.
make sure your operation.txt looks like this
showChannel
setChannel 2
Note:since you are scan.next(); before the while loop and again in the loop as System.out.println(scan.next());
you are going to ignore the first line of the operation.txt and you would see it set the channel correctly.
Suggestion:
Change it as
String opa= "";// = scan.next();
while(scan.hasNext()){
System.out.println(scan.next());