loops - Java reverse encoding -
so i'm making program can either encode or decode message using keyword. have encoding part down i'm not sure how decode message. new java appreciated!
here's code far:
import java.util.scanner; class coder { public static void main(string[] arg) { boolean encode = false; boolean decode = false; int multi = 1; scanner inputdata=new scanner(system.in); system.out.print("type e encode or d decode:\n"); string opt=inputdata.nextline(); if (opt.equals("e")||opt.equals("e")) { system.out.print("type keyword use encoding:\n"); encode = true; } else if (opt.equals("d")||opt.equals("d")) { system.out.print("type keyword decoding:\n"); decode = true; } else { system.out.print("error:this not option"); } if (encode==true) { string keyword=inputdata.nextline(); int[] key = new int[1000]; (int k = 0; k < key.length; ++k) { char c = keyword.charat(k % keyword.length()); if (c >= 'a' && c <= 'z') { key[k] = c-'a'; } } system.out.print("type encode message:\n"); string message=inputdata.nextline(); (int = 0; < message.length(); ++i) { if (message.charat(i) >= 'a' && message.charat(i) <= 'z') { system.out.println((int)message.charat(i) - (int)'a' + key[i]); } else if (message.charat(i) >= 'a' && message.charat(i) <= 'z') { system.out.println((int)message.charat(i) - (int)'a' + key[i]); } else if (message.charat(i) == ' ') { system.out.println(" "); } else { system.out.println(message.charat(i)); } } } if (decode==true) { } } }
alright i'm unsure kind encryption/decryption algorithm trying write looks it's kind of rot shuffle. best guess along lines of lee's answer honest didn't digest code. it's pretty hard read inside main function , if conditionals.
i'm guessing new java instead here helpful practices , code design tips make life easier.
junit friend:
it's bad idea run , debug code inside main method. every time want examine code you're force manually run through each scenario , inspect current state of program. can write test programs work you, plus makes easier jump debugger when need inspect condition.
junit built java, there other libraries such hamcrest , mockito make easier work with.
for example need basic test:
public class encryptiontest { @test // test annotation marks method should evaluated public testencoding() { final string orginalstring = "foobar"; final string expectedencryptionresults = "barfoo"; // should point. final string encryptedstring = encoder.encode(oringalstring) // or want structure encoder object. asserttrue( encryptedstring.equals( expectedencryptionresults ) ); // if false, test fail. } }
design patterns
a rule of thumb keep in mind wikipedia: law of demeter. put, rule states method or objects should know more necessary.
utils
an easy design pattern use utility pattern.
public class encoder { private encoder() {} // private constructor public static encode( final string message ) { string encodedmessage = null // complex encryption magic goes here return encodedmessage; } }
since pattern has private constructor, can't instantiate encoder object , able use static methods defined within it. it's way reuse common logic or checks, apache utitls classes great example utility pattern.
so in junit demo, used encoder class.
final string encryptedstring = encoder.encode(oringalstring)
factories
factories common , easy use design pattern. factories simple objects 1 goal in life, make other objects.
public class encryptionfactory { public string build( string message ) { // complex magic goes here. return encryptedstring; } } public class encryptionfactorytest { final encryptionfactory factory = new encryptionfactory(); @test public void testencryption() { final string = originalmessage = "foobar"; final string encryptedmessage = factory.build(originalmessage); asserttrue( encryptedmessage.equals("barfoo") ); } }
the main benefit of using design patterns pushes implementation logic such encryption algorithm smaller segment of code, making easier re-use , spot errors. plus cuts down on development , maintenance time ^_^
Comments
Post a Comment