changed I/O to JOptionPane
This commit is contained in:
158
HockeyStats.java
158
HockeyStats.java
@@ -1,3 +1,4 @@
|
||||
|
||||
/**
|
||||
* HockeyStats.java
|
||||
*
|
||||
@@ -8,6 +9,7 @@
|
||||
*/
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class HockeyStats {
|
||||
static PlayerList players = new PlayerList();
|
||||
@@ -23,7 +25,8 @@ public class HockeyStats {
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
while (true) {
|
||||
if (showMenu()) break;
|
||||
if (showMenu())
|
||||
break;
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
@@ -50,6 +53,7 @@ public class HockeyStats {
|
||||
Integer.parseInt(st.nextToken()), // gamesWon
|
||||
Integer.parseInt(st.nextToken()))); // gamesLost
|
||||
}
|
||||
JOptionPane.showMessageDialog(null, "Data loaded from " + fileName);
|
||||
br.close();
|
||||
}
|
||||
|
||||
@@ -64,7 +68,7 @@ public class HockeyStats {
|
||||
String save = players.createSaveData();
|
||||
fw.write(save);
|
||||
fw.close();
|
||||
System.out.println("Data saved to " + fileName);
|
||||
JOptionPane.showMessageDialog(null, "Data saved to " + fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,8 +78,7 @@ public class HockeyStats {
|
||||
* @throws Exception If input errors occur.
|
||||
*/
|
||||
public static boolean showMenu() throws Exception {
|
||||
System.out.print(
|
||||
"""
|
||||
String menu = """
|
||||
<---------------------------->
|
||||
Hockey Stats Manager Menu
|
||||
|
||||
@@ -88,13 +91,12 @@ public class HockeyStats {
|
||||
(5) Quit
|
||||
|
||||
<---------------------------->
|
||||
""");
|
||||
""";
|
||||
int n;
|
||||
try {
|
||||
n = s.nextInt();
|
||||
s.nextLine();
|
||||
n = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
return false;
|
||||
}
|
||||
boolean close = false;
|
||||
@@ -107,20 +109,22 @@ public class HockeyStats {
|
||||
break;
|
||||
case 3:
|
||||
while (true) {
|
||||
if (showDataMenu()) break;
|
||||
if (showDataMenu())
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
while (true) {
|
||||
if (showStatsMenu()) break;
|
||||
if (showStatsMenu())
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("Thank you!");
|
||||
JOptionPane.showMessageDialog(null, "Thank you!");
|
||||
close = true;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
break;
|
||||
}
|
||||
return close;
|
||||
@@ -133,8 +137,7 @@ public class HockeyStats {
|
||||
* @throws Exception If input errors occur.
|
||||
*/
|
||||
public static boolean showDataMenu() throws Exception {
|
||||
System.out.print(
|
||||
"""
|
||||
String menu = """
|
||||
<---------------------------->
|
||||
Hockey Data Editor Menu
|
||||
|
||||
@@ -146,81 +149,108 @@ public class HockeyStats {
|
||||
(4) Return to main menu
|
||||
|
||||
<---------------------------->
|
||||
""");
|
||||
""";
|
||||
boolean dataExit = false;
|
||||
int k;
|
||||
try {
|
||||
k = s.nextInt();
|
||||
s.nextLine();
|
||||
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
switch (k) {
|
||||
case 1:
|
||||
System.out.println("Enter The Following Stats:");
|
||||
System.out.println("\nPlayer Name:");
|
||||
String newName = s.nextLine();
|
||||
System.out.println("Player Goals Scored:");
|
||||
int newGoals = s.nextInt();
|
||||
System.out.println("Player Assists:");
|
||||
int newAssists = s.nextInt();
|
||||
System.out.println("Games Won:");
|
||||
int newGamesWon = s.nextInt();
|
||||
System.out.println("Games Lost:");
|
||||
int newGamesLost = s.nextInt();
|
||||
s.nextLine();
|
||||
JTextField fieldNewName = new JTextField();
|
||||
JTextField fieldNewGoals = new JTextField();
|
||||
JTextField fieldNewAssists = new JTextField();
|
||||
JTextField fieldNewGamesWon = new JTextField();
|
||||
JTextField fieldNewGamesLost = new JTextField();
|
||||
Object[] newMessage = {
|
||||
"Player Name:",
|
||||
fieldNewName,
|
||||
"Player Goals Scored:",
|
||||
fieldNewGoals,
|
||||
"Player Assists:",
|
||||
fieldNewAssists,
|
||||
"Games Won:",
|
||||
fieldNewGamesWon,
|
||||
"Games Lost:",
|
||||
fieldNewGamesLost
|
||||
};
|
||||
int newOption = JOptionPane.showConfirmDialog(
|
||||
null, newMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION);
|
||||
if (newOption != JOptionPane.OK_OPTION)
|
||||
return false;
|
||||
|
||||
String newName = fieldNewName.getText();
|
||||
int newGoals = Integer.parseInt(fieldNewGoals.getText());
|
||||
int newAssists = Integer.parseInt(fieldNewAssists.getText());
|
||||
int newGamesWon = Integer.parseInt(fieldNewGamesWon.getText());
|
||||
int newGamesLost = Integer.parseInt(fieldNewGamesLost.getText());
|
||||
players.addPlayer(
|
||||
new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost));
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Enter the name of the player you want to remove:");
|
||||
String rmName = s.nextLine();
|
||||
System.out.println(
|
||||
String rmName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:");
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
players.removePlayer(rmName)
|
||||
? rmName + " was successfully removed."
|
||||
: "Player not found.");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("Enter The Following Stats:");
|
||||
System.out.println("\nPlayer Name:");
|
||||
String addName = s.nextLine();
|
||||
System.out.println("Player Goals Scored:");
|
||||
int addGoals = s.nextInt();
|
||||
System.out.println("Player Assists:");
|
||||
int addAssists = s.nextInt();
|
||||
s.nextLine();
|
||||
System.out.println("Games Won? (yes/no)");
|
||||
boolean addGameWon = s.nextLine().equalsIgnoreCase("yes");
|
||||
JTextField fieldAddName = new JTextField();
|
||||
JTextField fieldAddGoals = new JTextField();
|
||||
JTextField fieldAddAssists = new JTextField();
|
||||
JTextField fieldAddGameWon = new JTextField();
|
||||
Object[] addMessage = {
|
||||
"Player Name:",
|
||||
fieldAddName,
|
||||
"Player Goals Scored:",
|
||||
fieldAddGoals,
|
||||
"Player Assists:",
|
||||
fieldAddAssists,
|
||||
"Game Won? (yes/no)",
|
||||
fieldAddGameWon
|
||||
};
|
||||
int addOption = JOptionPane.showConfirmDialog(
|
||||
null, addMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION);
|
||||
if (addOption != JOptionPane.OK_OPTION)
|
||||
return false;
|
||||
String addName = fieldAddName.getText();
|
||||
int addGoals = Integer.parseInt(fieldAddGoals.getText());
|
||||
int addAssists = Integer.parseInt(fieldAddAssists.getText());
|
||||
boolean addGameWon = Boolean.parseBoolean(fieldAddGameWon.getText());
|
||||
HockeyPlayer found = players.findPlayer(addName);
|
||||
if (found == null) {
|
||||
System.out.println("Player not found.");
|
||||
JOptionPane.showMessageDialog(null, "Player not found.");
|
||||
} else {
|
||||
found.addGame(addGoals, addAssists, addGameWon);
|
||||
JOptionPane.showMessageDialog(null, "Game added to " + addName);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
dataExit = true;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Please enter the right kind of data");
|
||||
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
|
||||
}
|
||||
return dataExit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the viewer menu for printing, finding, and sorting player statistics.
|
||||
* Displays the viewer menu for printing, finding, and sorting player
|
||||
* statistics.
|
||||
*
|
||||
* @return true if the user chooses to return to the main menu, false otherwise.
|
||||
*/
|
||||
public static boolean showStatsMenu() {
|
||||
System.out.print(
|
||||
"""
|
||||
String menu = """
|
||||
<---------------------------->
|
||||
Hockey Stats Viewer
|
||||
|
||||
@@ -234,53 +264,47 @@ public class HockeyStats {
|
||||
(6) Return to Main Menu
|
||||
|
||||
<---------------------------->
|
||||
""");
|
||||
""";
|
||||
boolean statsExit = false;
|
||||
int k;
|
||||
try {
|
||||
k = s.nextInt();
|
||||
s.nextLine();
|
||||
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
switch (k) {
|
||||
case 1:
|
||||
System.out.println(players.toString());
|
||||
JOptionPane.showMessageDialog(null, players.toString());
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Enter the name of the player you want to find:");
|
||||
String findName = s.nextLine();
|
||||
String findName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to find:");
|
||||
HockeyPlayer p = players.findPlayer(findName);
|
||||
System.out.println(p == null ? "Player not found." : p.toString());
|
||||
JOptionPane.showMessageDialog(null, p == null ? "Player not found." : p.toString());
|
||||
break;
|
||||
case 3:
|
||||
players.sortByNames();
|
||||
System.out.println("Sorted by names");
|
||||
JOptionPane.showMessageDialog(null, "Sorted by names");
|
||||
break;
|
||||
case 4:
|
||||
players.sortByPoints();
|
||||
System.out.println("Sorted by points");
|
||||
JOptionPane.showMessageDialog(null, "Sorted by points");
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("Enter the min goals to show:");
|
||||
int minGoals = s.nextInt();
|
||||
s.nextLine();
|
||||
int minGoals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:"));
|
||||
PlayerList pl = players.getPlayersGoalsAbove(minGoals);
|
||||
System.out.println(pl.toString());
|
||||
JOptionPane.showMessageDialog(null, pl.toString());
|
||||
break;
|
||||
case 6:
|
||||
statsExit = true;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Please choose one of the available menu options!");
|
||||
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("k = " + k);
|
||||
System.out.println(e);
|
||||
System.out.println("Please enter the right kind of data");
|
||||
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
|
||||
}
|
||||
return statsExit;
|
||||
}
|
||||
|
||||
49
testInput.java
Normal file
49
testInput.java
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
/**
|
||||
* @(#)testInput.java
|
||||
*
|
||||
* @author
|
||||
* @version 1.00 2017/1/16
|
||||
*/
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class testInput {
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
JTextField field1 = new JTextField();
|
||||
JTextField field2 = new JTextField();
|
||||
JTextField field3 = new JTextField();
|
||||
JTextField field4 = new JTextField();
|
||||
JTextField field5 = new JTextField();
|
||||
|
||||
Object[] message = {
|
||||
"Input name:",
|
||||
field1, // list prompt followed by a JTextField object reference for each data element
|
||||
// in
|
||||
// your object
|
||||
"Input other data:", field2,
|
||||
"meaningful prompt:", field3,
|
||||
"Input value 4:", field4,
|
||||
"Input value 5:", field5,
|
||||
};
|
||||
field1.setText("initialize data such as Henry here");
|
||||
|
||||
int option = JOptionPane.showConfirmDialog(
|
||||
null, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);
|
||||
if (option == JOptionPane.OK_OPTION) { // Read user input data
|
||||
String value1 = field1.getText();
|
||||
String value2 = field2.getText();
|
||||
String value3 = field3.getText();
|
||||
String value4 = field4.getText();
|
||||
String value5 = field5.getText();
|
||||
System.out.println(
|
||||
value1 + " " + value2 + " " + value3 + " " + value4 + " "
|
||||
+ value5); // you would construct an object and add or replace it in the arraylist
|
||||
} else
|
||||
System.out.println("no data");
|
||||
}
|
||||
}
|
||||
116
testJOptionPane2.java
Normal file
116
testJOptionPane2.java
Normal file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @(#)testJOptionPane.java
|
||||
*
|
||||
* @author Horton
|
||||
* @version 1.00 2012/5/15
|
||||
*/
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class testJOptionPane2 {
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// ## creates a loop to display the main menu
|
||||
boolean done = false;
|
||||
|
||||
while (!done) {
|
||||
String menu =
|
||||
"1 - load file"
|
||||
+ "\n"
|
||||
+ "2 - add something "
|
||||
+ "\n"
|
||||
+ "3 - save file "
|
||||
+ "\n"
|
||||
+ "4- quit";
|
||||
String inputValue = JOptionPane.showInputDialog(menu);
|
||||
int n = Integer.parseInt(inputValue);
|
||||
switch (n) {
|
||||
case 1:
|
||||
loadFile(); // Read data from file
|
||||
break;
|
||||
case 2:
|
||||
doSomething();
|
||||
break;
|
||||
case 3:
|
||||
JOptionPane.showMessageDialog(null, "user instructions here ");
|
||||
break;
|
||||
case 4:
|
||||
done = true;
|
||||
break;
|
||||
default:
|
||||
JOptionPane.showMessageDialog(null, "invalid option, please try again ");
|
||||
}
|
||||
}
|
||||
|
||||
// JOptionPane.showMessageDialog(null,"You Chose " + inputValue);
|
||||
|
||||
// *********************************************************************
|
||||
// Custom button text -- returns 0 for the first button
|
||||
Object[] options = {"Yes, please", "No, thanks", "No eggs, no ham!"};
|
||||
int n =
|
||||
JOptionPane.showOptionDialog(
|
||||
null,
|
||||
"Would you like some green eggs to go " + "with that ham?",
|
||||
"A Silly Question",
|
||||
JOptionPane.YES_NO_CANCEL_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE,
|
||||
null,
|
||||
options,
|
||||
options[2]);
|
||||
// ## do not include the following line. It is to help determine what the switch should do
|
||||
JOptionPane.showMessageDialog(null, "You Chose " + n);
|
||||
|
||||
// ****************************************************************
|
||||
// pull down menu
|
||||
// --NOTE: this would be a good place to call a method in your list class to get an array of
|
||||
// objects to display such as all the names of the teams or products in your list.
|
||||
|
||||
Object[] possibilities = {"ham", "spam", "yam"};
|
||||
String s =
|
||||
(String)
|
||||
JOptionPane.showInputDialog(
|
||||
null,
|
||||
"Complete the sentence:\n" + "\"Green eggs and...\"",
|
||||
"Customized Dialog",
|
||||
JOptionPane.PLAIN_MESSAGE,
|
||||
null, // icon if you have one
|
||||
possibilities,
|
||||
"ham");
|
||||
}
|
||||
|
||||
public static void doSomething() {
|
||||
// ## this method is just to show displaying
|
||||
// ## a different menu. You would need to
|
||||
// ## add a switch to handle menu options
|
||||
// ## when option 4 is selected,the loop/method will end
|
||||
// ## and processing can return to the menu that called this
|
||||
|
||||
boolean editMore = true;
|
||||
while (editMore) {
|
||||
String menu =
|
||||
"1 - show all data"
|
||||
+ "\n"
|
||||
+ "2 - show only one thing "
|
||||
+ "\n"
|
||||
+ "3 - save file "
|
||||
+ "\n"
|
||||
+ "4 - done editing ";
|
||||
String inputValue = JOptionPane.showInputDialog(menu);
|
||||
// out write a switch to evaluate the options -- following if would be in your switch
|
||||
if (inputValue.equals("4")) editMore = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadFile() {
|
||||
// loop, read data for one record, construct object of that class, add to the appropriate list
|
||||
/**
|
||||
* Scanner saved; try { saved = new Scanner (new File("itemList.dat")); while(saved.hasNext()) {
|
||||
* itemName = saved.nextLine(); itemPrice = saved.nextDouble(); itemQuantity = saved.nextInt();
|
||||
* saved.nextLine(); //cleans out end of line myCart.addItem(new ItemKEY(itemName, itemPrice,
|
||||
* itemQuantity)); } //end of while block to read } // end of try block catch (IOException e) {
|
||||
* System.out.println ("input read failed " + e); }
|
||||
*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user