mardi 31 janvier 2017

Java, Should I use switch-case or if-else within a switch-case?

This program copies a string (a password) on the clipboard, and I want to add the option to copy a username as well. So if a user forgets his/her username on an online account (or is just lazy), it is possible to get that as well.

First the user chooses the game/whatever, then the user gets to choose what gets copied to the clipboard: the username or the password. So do I add another switch-case under all those options, or do I go with an if-statement? Should I put a function call under each of those?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helloworldapp;

/**
 *
 * @author Au-Thor
 */
import java.util.Scanner; 
import java.awt.datatransfer.*;
import java.awt.Toolkit;

public class HelloWorldApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int n = 1;
       String addedString = "replace"; //This is for later usage
       String titleNames[] = {"Planetside 2","Nasty Website","Useless Social Media Account","Someother"};
       Scanner userInput1 = new Scanner(System.in);
       String thePassword = "Nothing";

       System.out.println("Enter a key: "); //This is for later usage
       addedString=(userInput1.nextLine()); //This is for later usage

    while(n!=0){ 
        Scanner userChoice = new Scanner(System.in);  // Reading from System.in

        for(int i = 0; i < titleNames.length; i++){  //Menu print-out
            int h=i+1;
            System.out.println( "["+h+".] " + titleNames[i]); 
        }

        System.out.println( "\n[0.] Quit\n");         
        System.out.println("\nEnter a number: ");
        n = userChoice.nextInt(); // Scans the next token of the input as an int.

        switch (n) {
            case 1:  //Ask if the user wants the username or the password
                     thePassword = "MAD PASSWORD FOR MY ACCOUNT" ;
                     break;
            case 2:  thePassword = "Replace";
                     break;
            case 3:  thePassword = "Replace";
                     break;
            case 4:  thePassword = "Replace";
                     break;
            case 5:  thePassword = "Replace";
                     break;
            case 6:  thePassword = "Replace";
                     break;
            case 7:  thePassword = "Replace";
                     break;
            case 8:  thePassword = "Replace";
                     break;
            case 9:  thePassword = "Replace";
                     break;
            case 10: thePassword = "Replace";
                     break;
            case 11: thePassword = "Replace";
                     break;
            case 12: thePassword = "Replace";
                     break;
            case 0:
                break;
            default: System.out.println("\nOption does not exist");;
                     break;
        }
        System.out.println("Current: " +thePassword+"\n"); //Change this to the Page or Game the credentials are for

        String myString = thePassword;
        StringSelection stringSelection = new StringSelection(myString);
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        clpbrd.setContents(stringSelection, null);
        }

    System.out.println("Quitting..");
    }    
}

Extra stuff: Is there something that could be done more efficiently (besides all of it :D)? Should I use more functions? Can I use a function to produce a switch-case structure that scales to the parameters given to it, to create all the switch cases with the same function, is that even possible?

Aucun commentaire:

Enregistrer un commentaire