gadzik gadzik - 4 months ago 21
Java Question

why it is not agree to clean code principles (can you point out?)

Hello i failed interview exercise for a certain company and i would like to know where is my fault , what i did in wrong way , can you describe it ? i received message that i didnt use clean code approach . It was imitation of command in java .

public class Klasa {
public static void main(String[] args){
String inputcommand = new String("");
StringBuilder outputcommand = new StringBuilder("[MyShell] $>");
while(true){
System.out.print(outputcommand);
Scanner scan = new Scanner(System.in);
inputcommand = scan.nextLine();
boolean boo=isWhite(inputcommand);
check(inputcommand,outputcommand,boo);
}
}
public static void check(String nowcheckit , StringBuilder changeit,boolean trueOrFalse){
if(trueOrFalse){
if(nowcheckit.substring(0, nowcheckit.indexOf(" ")).equalsIgnoreCase("prompt")){
if(nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("reset")){
changeit.setLength(0);
changeit.append("[MyShell] $>");
}
else if(nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("$cwd")){
changeit.setLength(0);
changeit.append("[MyShell]" +" " + System.getProperty("user.dir") + ">");
}
else if(!nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("reset")&&
!nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("$cwd"))
{
changeit.setLength(0);
changeit.append("[MyShell]" +" " + nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()) + ">");
}

}

if(nowcheckit.substring(0, 2).equalsIgnoreCase("cd")){
if(nowcheckit.substring(3, 5).equalsIgnoreCase("..")&&(nowcheckit.length()==5)){
changeit.setLength(0);
changeit.append(System.getProperty("user.dir"));
while(true){
changeit.deleteCharAt(changeit.length()-1);
if(changeit.charAt(changeit.length()-1)=='\\'){
changeit.deleteCharAt(changeit.length()-1);
break;
}

}
System.setProperty("user.dir", changeit.toString() );
changeit.setLength(0);
changeit.append("[MyShell] "+System.getProperty("user.dir")+">");


}
else if(!nowcheckit.substring(3, 5).equalsIgnoreCase("..")){
System.setProperty("user.dir", System.getProperty("user.dir") + "\\"+
nowcheckit.substring(nowcheckit.indexOf(' ')+1, nowcheckit.length()));
changeit.setLength(0);
changeit.append("[MyShell]" +" " + System.getProperty("user.dir") + ">");

}
}
else if(!nowcheckit.substring(0, 2).equalsIgnoreCase("cd")&&
!nowcheckit.substring(0, nowcheckit.indexOf(" ")).equals("prompt")&&
!nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("reset")&&
!nowcheckit.substring(nowcheckit.indexOf(" ")+1, nowcheckit.length()).equalsIgnoreCase("$cwd")){
System.out.println(nowcheckit.substring(0, nowcheckit.indexOf(" "))+ " : unknow command");

}
}

else if(!trueOrFalse){
if(nowcheckit.equalsIgnoreCase("dir")){
File folder = new File(System.getProperty("user.dir"));
File[] listaa = folder.listFiles();
System.out.println("CONTENT OF " + System.getProperty("user.dir"));

for (int i = 0; i < listaa.length; i++) {
if (listaa[i].isFile()) {
System.out.println("FILE " + listaa[i].getName());
} else if (listaa[i].isDirectory()) {
System.out.println("DIR " + listaa[i].getName());
}
}
}
else if(nowcheckit.equalsIgnoreCase("tree")){
File inputFolder = new File(System.getProperty("user.dir"));
doTree(inputFolder, "");
}
else if(nowcheckit.equalsIgnoreCase("exit")){
System.out.println("bye");

}
else if(!nowcheckit.equalsIgnoreCase("exit")&&!nowcheckit.equalsIgnoreCase("dir")){
System.out.println(nowcheckit+" : unknow command");
}




}
}




public static boolean isWhite(String word){
if(word != null){
for(int i = 0; i < word.length(); i++){
if(Character.isWhitespace(word.charAt(i))){
return true;
}
}
}
return false;
}
public static void doTree(File ffile, String prefix) {
if (ffile.isDirectory()) {
System.out.println(prefix + ffile.getName());
prefix += "-";
File subdirectories[] = ffile.listFiles();
for (File subdir : subdirectories) {
doTree(subdir, prefix);
}
}
}


}

Answer

Well here are the things you are doing wrong just by looking at your code:

  • For starters you didn't properly indent your code(in eclipse do ctrl + I for indentation), it will make it much more readable.
  • You're repeating the same stuff when you should write specific methods for them, You should check out the DRY principle (don't repeat yourself).
  • You're not using case instead of multiple If else statements.
  • You're not properly naming your variables ffile is a terrible name for a file variable.