JAVA PROGrAMMING: Using the attached program as a starter, make the following en
ID: 3694738 • Letter: J
Question
JAVA PROGrAMMING:
Using the attached program as a starter, make the following enhancements:
move the user prompt strings to a properties file
create a corresponding properties file for another language/region
query the current system Locale and display it
display the menu from your properties file
change the JVM's Locale
display the menu from your properties file (again)
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
public class MenuFromProperties {
private static final String[] TOP_MENU = { "List Library", "Sort Library By Composer", "Sort Library By Genre",
"Sort Library by Duration", "Play Playlist", "Create Playlist", "Exit" };
public static void main(String[] args) throws InterruptedException, URISyntaxException, FileNotFoundException,
IOException, ClassNotFoundException {
Scanner kybd = new Scanner(System.in);
int userChoice = -1;
userChoice = Utils.userChoose(kybd, TOP_MENU);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Utils {
public static int userChoose(Scanner kybd, String[] choices) {
int choice = -1;
System.out.println("Make a Selection: ");
for (int i = 0; i < choices.length; i++) {
System.out.println(" [" + i + "] " + choices[i]);
}
choice = kybd.nextInt();
return choice;
}
public static int userChoose(Scanner kybd, List<String> choices) {
int choice = -1;
System.out.println("Make a Selection: ");
for (int i = 0; i < choices.size(); i++) {
System.out.println(" [" + i + "] " + choices.get(i));
}
choice = kybd.nextInt();
return choice;
}
}
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
public class MenuFromProperties {
Properties props = new Properties();
InputStream is = null;
try {
File f = new File("menu.properties");
is = new FileInputStream( f );
}
catch ( Exception e ) { is = null; }
try {
if ( is == null ) {
is = getClass().getResourceAsStream("menu.properties");
}
// Try loading properties from the file (if found)
props.load( is );
}
catch ( Exception e ) { }
private static final String[] TOP_MENU = props.getProperty("TOP_MENU"); // We set this priorly
//private static final String[] TOP_MENU = { "List Library", "Sort Library By Composer", "Sort Library By Genre",
"Sort Library by Duration", "Play Playlist", "Create Playlist", "Exit" };
public static void main(String[] args) throws InterruptedException, URISyntaxException, FileNotFoundException,
IOException, ClassNotFoundException {
try {
File f = new File("userChoice.properties");
OutputStream out = new FileOutputStream( f );
}
catch (Exception e ) {
e.printStackTrace();
}
Scanner kybd = new Scanner(System.in);
int userChoice = -1;
userChoice = Utils.userChoose(kybd, TOP_MENU,out,props);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Utils {
public static int userChoose(Scanner kybd, String[] choices,OutputStream out,Properties props) {
int choice = -1;
System.out.println("Make a Selection: ");
for (int i = 0; i < choices.length; i++) {
System.out.println(" [" + i + "] " + choices[i]);
}
choice = kybd.nextInt();
props.store(out, choice);
return choice;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.