简体   繁体   中英

How do I get my program to do random methods?

I'm working on a project for the google science fair. I'm making an application which takes two numbers (Barcode and Serial Number) and changes them into two different numbers using various algorithms( ie, Like Fisher-Yates shuffle). My code looks something like this :

enter code here public static void main(String[] args) {
    reverseNum b = new reverseNum();
    addOne   c= new addOne();
    plusThree f= new plusThree();
    plusNine  i = new plusNine();
    plusTwo l = new plusTwo();
    minusTwo  p = new minusTwo();
    plusOne  r= new plusOne();
    plusFour u= new plusFour();
    threeTwo x= new threeTwo();
    intoTwo  ab= new intoTwo();
    plusFive bc = new plusFive();
    intoSix cd= new intoSix();
     twoOne de = new twoOne() ;
    plusSeven ef= new plusSeven();
    plusEight fg= new plusEight();
    minOne gh = new minOne();
    intoSeven hi = new intoSeven();
    intoEight ij = new intoEight();
    intoNine jk = new intoNine();
    intOne kl = new intOne();
    intoFour lm = new intoFour();
    // TODO code application logic here
    Scanner user_input = new Scanner( System.in );
    String a ;
      System.out.println("Enter the Barcode:");
 a = user_input.next();
         System.out.println("Encrypted Barcode:"+ blah.randomMethod(a));
         //Random method from the above given methods should modify this.
         String z;
         System.out.println("Enter the Serial Number:");
         z= user_input.next();
         System.out.println("Encrypted Serial Number:" + blah.randomMethod(z) );
         //Random method from the above given methods should modify this
}

}

My main objective is to modify the two given numbers with the above mentioned methods(algorithms).I want a random method from the given list to modify the given numbers each time the user runs the program. I also need to put this all into a GUI. Please help.

Use the strategy pattern. Have each algorithm implement a version of that strategy. Then use a factory to get a random strategy implementation using any mechanism you want. No need to combine the inputs into a single object.

I agree with jgitter about the Strategy and Factory pattern.

I'm posting an example.

public interface Algorithm {
     public String execute(String input);
}

public class SomeAlgorithm implements Algorithm {

     public String execute(String input) {
         ///... Algorithm here
     }
}

public class AnotherAlgorithm implements Algorithm {

     public String execute(String input) {
         ///... Algorithm here
     }
}

public abstract class AlgorithmFactory {
     public static Algorithm createRandomAlgorithm() {

         //Create a new Random object
         Random randomEngine = new Random();

         //Retrieve a number from 0 (inclusive) to 2 (exclusive)
         int randomNumber = randomEngine.nextInt(2);

         //Select which implementation to use according to the random number
         switch(randomNumber) {
             case 0: return new SomeAlgorithm();
             case 1: return new AnotherAlgorithm();
         }

         //This will most likely never get called, but a return value is mandatory
         return null;
     }
}

Then, you would retrieve and execute the algorithm like this:

String encryption = AlgorithmFactory.createRandomAlgorithm().execute(input);

Please note that the factory is abstract and has a static method, which is not how I would approach this, but it will help you out in your problem nice and easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM