changed I/O to JOptionPane
This commit is contained in:
158
HockeyStats.java
158
HockeyStats.java
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* HockeyStats.java
|
* HockeyStats.java
|
||||||
*
|
*
|
||||||
@@ -8,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
public class HockeyStats {
|
public class HockeyStats {
|
||||||
static PlayerList players = new PlayerList();
|
static PlayerList players = new PlayerList();
|
||||||
@@ -23,7 +25,8 @@ public class HockeyStats {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (showMenu()) break;
|
if (showMenu())
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
@@ -50,6 +53,7 @@ public class HockeyStats {
|
|||||||
Integer.parseInt(st.nextToken()), // gamesWon
|
Integer.parseInt(st.nextToken()), // gamesWon
|
||||||
Integer.parseInt(st.nextToken()))); // gamesLost
|
Integer.parseInt(st.nextToken()))); // gamesLost
|
||||||
}
|
}
|
||||||
|
JOptionPane.showMessageDialog(null, "Data loaded from " + fileName);
|
||||||
br.close();
|
br.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +68,7 @@ public class HockeyStats {
|
|||||||
String save = players.createSaveData();
|
String save = players.createSaveData();
|
||||||
fw.write(save);
|
fw.write(save);
|
||||||
fw.close();
|
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.
|
* @throws Exception If input errors occur.
|
||||||
*/
|
*/
|
||||||
public static boolean showMenu() throws Exception {
|
public static boolean showMenu() throws Exception {
|
||||||
System.out.print(
|
String menu = """
|
||||||
"""
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
Hockey Stats Manager Menu
|
Hockey Stats Manager Menu
|
||||||
|
|
||||||
@@ -88,13 +91,12 @@ public class HockeyStats {
|
|||||||
(5) Quit
|
(5) Quit
|
||||||
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
""");
|
""";
|
||||||
int n;
|
int n;
|
||||||
try {
|
try {
|
||||||
n = s.nextInt();
|
n = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||||
s.nextLine();
|
|
||||||
} catch (Exception e) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
boolean close = false;
|
boolean close = false;
|
||||||
@@ -107,20 +109,22 @@ public class HockeyStats {
|
|||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
while (true) {
|
while (true) {
|
||||||
if (showDataMenu()) break;
|
if (showDataMenu())
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
while (true) {
|
while (true) {
|
||||||
if (showStatsMenu()) break;
|
if (showStatsMenu())
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
System.out.println("Thank you!");
|
JOptionPane.showMessageDialog(null, "Thank you!");
|
||||||
close = true;
|
close = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Please choose one of the available menu options!");
|
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return close;
|
return close;
|
||||||
@@ -133,8 +137,7 @@ public class HockeyStats {
|
|||||||
* @throws Exception If input errors occur.
|
* @throws Exception If input errors occur.
|
||||||
*/
|
*/
|
||||||
public static boolean showDataMenu() throws Exception {
|
public static boolean showDataMenu() throws Exception {
|
||||||
System.out.print(
|
String menu = """
|
||||||
"""
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
Hockey Data Editor Menu
|
Hockey Data Editor Menu
|
||||||
|
|
||||||
@@ -146,81 +149,108 @@ public class HockeyStats {
|
|||||||
(4) Return to main menu
|
(4) Return to main menu
|
||||||
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
""");
|
""";
|
||||||
boolean dataExit = false;
|
boolean dataExit = false;
|
||||||
int k;
|
int k;
|
||||||
try {
|
try {
|
||||||
k = s.nextInt();
|
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||||
s.nextLine();
|
|
||||||
} catch (Exception e) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
switch (k) {
|
switch (k) {
|
||||||
case 1:
|
case 1:
|
||||||
System.out.println("Enter The Following Stats:");
|
JTextField fieldNewName = new JTextField();
|
||||||
System.out.println("\nPlayer Name:");
|
JTextField fieldNewGoals = new JTextField();
|
||||||
String newName = s.nextLine();
|
JTextField fieldNewAssists = new JTextField();
|
||||||
System.out.println("Player Goals Scored:");
|
JTextField fieldNewGamesWon = new JTextField();
|
||||||
int newGoals = s.nextInt();
|
JTextField fieldNewGamesLost = new JTextField();
|
||||||
System.out.println("Player Assists:");
|
Object[] newMessage = {
|
||||||
int newAssists = s.nextInt();
|
"Player Name:",
|
||||||
System.out.println("Games Won:");
|
fieldNewName,
|
||||||
int newGamesWon = s.nextInt();
|
"Player Goals Scored:",
|
||||||
System.out.println("Games Lost:");
|
fieldNewGoals,
|
||||||
int newGamesLost = s.nextInt();
|
"Player Assists:",
|
||||||
s.nextLine();
|
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(
|
players.addPlayer(
|
||||||
new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost));
|
new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
System.out.println("Enter the name of the player you want to remove:");
|
String rmName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:");
|
||||||
String rmName = s.nextLine();
|
JOptionPane.showMessageDialog(
|
||||||
System.out.println(
|
null,
|
||||||
players.removePlayer(rmName)
|
players.removePlayer(rmName)
|
||||||
? rmName + " was successfully removed."
|
? rmName + " was successfully removed."
|
||||||
: "Player not found.");
|
: "Player not found.");
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
System.out.println("Enter The Following Stats:");
|
JTextField fieldAddName = new JTextField();
|
||||||
System.out.println("\nPlayer Name:");
|
JTextField fieldAddGoals = new JTextField();
|
||||||
String addName = s.nextLine();
|
JTextField fieldAddAssists = new JTextField();
|
||||||
System.out.println("Player Goals Scored:");
|
JTextField fieldAddGameWon = new JTextField();
|
||||||
int addGoals = s.nextInt();
|
Object[] addMessage = {
|
||||||
System.out.println("Player Assists:");
|
"Player Name:",
|
||||||
int addAssists = s.nextInt();
|
fieldAddName,
|
||||||
s.nextLine();
|
"Player Goals Scored:",
|
||||||
System.out.println("Games Won? (yes/no)");
|
fieldAddGoals,
|
||||||
boolean addGameWon = s.nextLine().equalsIgnoreCase("yes");
|
"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);
|
HockeyPlayer found = players.findPlayer(addName);
|
||||||
if (found == null) {
|
if (found == null) {
|
||||||
System.out.println("Player not found.");
|
JOptionPane.showMessageDialog(null, "Player not found.");
|
||||||
} else {
|
} else {
|
||||||
found.addGame(addGoals, addAssists, addGameWon);
|
found.addGame(addGoals, addAssists, addGameWon);
|
||||||
|
JOptionPane.showMessageDialog(null, "Game added to " + addName);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
dataExit = true;
|
dataExit = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Please choose one of the available menu options!");
|
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} 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;
|
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.
|
* @return true if the user chooses to return to the main menu, false otherwise.
|
||||||
*/
|
*/
|
||||||
public static boolean showStatsMenu() {
|
public static boolean showStatsMenu() {
|
||||||
System.out.print(
|
String menu = """
|
||||||
"""
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
Hockey Stats Viewer
|
Hockey Stats Viewer
|
||||||
|
|
||||||
@@ -234,53 +264,47 @@ public class HockeyStats {
|
|||||||
(6) Return to Main Menu
|
(6) Return to Main Menu
|
||||||
|
|
||||||
<---------------------------->
|
<---------------------------->
|
||||||
""");
|
""";
|
||||||
boolean statsExit = false;
|
boolean statsExit = false;
|
||||||
int k;
|
int k;
|
||||||
try {
|
try {
|
||||||
k = s.nextInt();
|
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
|
||||||
s.nextLine();
|
|
||||||
} catch (Exception e) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
switch (k) {
|
switch (k) {
|
||||||
case 1:
|
case 1:
|
||||||
System.out.println(players.toString());
|
JOptionPane.showMessageDialog(null, players.toString());
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
System.out.println("Enter the name of the player you want to find:");
|
String findName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to find:");
|
||||||
String findName = s.nextLine();
|
|
||||||
HockeyPlayer p = players.findPlayer(findName);
|
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;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
players.sortByNames();
|
players.sortByNames();
|
||||||
System.out.println("Sorted by names");
|
JOptionPane.showMessageDialog(null, "Sorted by names");
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
players.sortByPoints();
|
players.sortByPoints();
|
||||||
System.out.println("Sorted by points");
|
JOptionPane.showMessageDialog(null, "Sorted by points");
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
System.out.println("Enter the min goals to show:");
|
int minGoals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:"));
|
||||||
int minGoals = s.nextInt();
|
|
||||||
s.nextLine();
|
|
||||||
PlayerList pl = players.getPlayersGoalsAbove(minGoals);
|
PlayerList pl = players.getPlayersGoalsAbove(minGoals);
|
||||||
System.out.println(pl.toString());
|
JOptionPane.showMessageDialog(null, pl.toString());
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
statsExit = true;
|
statsExit = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Please choose one of the available menu options!");
|
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("k = " + k);
|
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
|
||||||
System.out.println(e);
|
|
||||||
System.out.println("Please enter the right kind of data");
|
|
||||||
}
|
}
|
||||||
return statsExit;
|
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