📄 源代码.java
字号:
//Sports Result DataBase -- NBA
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
class TeamDetails
{
private String name;
private String location;
private String photoId;
private int scores;
private int rank;
// Constructor
public TeamDetails()
{
name = "";
location = "";
scores = 0;
rank = 0;
photoId = "";
} // end of TemDetails
// Constructor whit arguments
public TeamDetails(String teamName, String teamLocation, String photo, int teamScore, int teamRank)
{
name = teamName;
location = teamLocation;
scores = teamScore;
rank = teamRank;
photoId = photo;
} // end of TeamDetails
public String getName()
{
return name;
} // end method getName
public String getPhotoId()
{
return photoId;
} // end method getPhotoId
public String getLocation() // get the team's location
{
return location;
} // end method getLocation
public int getScore() // get the team's score
{
return scores;
} // end method getScores
public int getRank() // get the team's rank
{
return rank;
} // end method GetRank
// Set the team's information
public void set(String n, String l,String photo, int s,int r)
{
name = n;
location = l;
scores = s;
rank = r;
photoId = photo;
} // end method Set
public void addScore(int add)
{
scores = scores + add;
} // end method addScore
public void setRank(int r)
{
rank = r;
} // end method SetRank
} // end class TeamDetails
class GameResult
{
// Strings to store the teamA's name and teamB's name
private String teamNameA, teamNameB;
// to store the teams' score
private int scoreA, scoreB;
// to store the time that the game was hold
private int year, month, date;
// Consturctor to the class
public GameResult()
{
teamNameA = teamNameB = "";
scoreA = scoreB = 0;
year = month = date = 0;
} // end Constructor
// set the game's details
public void setResult(String ta, String tb, int sa, int sb, int y,int m,int d)
{
teamNameA = ta;
teamNameB = tb;
scoreA = sa;
scoreB = sb;
year = y;
month = m;
date = d;
} // end method SetRusult
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDate()
{
return date;
}
public int getScoreA()
{
return scoreA;
}
public int getScoreB()
{
return scoreB;
}
public String getTeamA()
{
return teamNameA;
}
public String getTeamB()
{
return teamNameB;
}
} // endl class GameResult
class DataBase
{
// Array of Object
private TeamDetails[] teams;
private GameResult[] result;
public GameResult temp;
// two variables to save how many teams and how many results
private int teamsNumber;
private int resultNumber;
// Constructor to the Class
public DataBase()
{
teamsNumber = resultNumber = 0;
teams = new TeamDetails[100];
temp = new GameResult();
for(int i = 0; i < 100; i++)
{
teams[i]= new TeamDetails();
}
result = new GameResult[500000];
for(int i = 0; i < 500000; i++)
{
result[i] = new GameResult();
}
}
// to add a new data to the database
public void addNewGameResult(GameResult temp)
{
int index = resultNumber;
resultNumber++;
result[index] = temp;
String nameA = temp.getTeamA();
String nameB = temp.getTeamB();
int scoreA, scoreB;
scoreA = temp.getScoreA();
scoreB = temp.getScoreB();
if(scoreA > scoreB)
{
scoreA = 1;
scoreB = 0;
}
else
{
scoreA = 0;
scoreB = 1;
}
for(int i = 0; i < teams.length; i++)
{
if(nameA.equals(teams[i].getName()))
{
teams[i].addScore(scoreA);
}
if(nameB.equals(teams[i].getName()))
{
teams[i].addScore(scoreB);
}
}
update();
} // end the method AddNewGameResult
// Update the information of the database
public void update()
{
for(int i = 1; i <= teamsNumber - 1; i++)
{
for(int j = 0; j < teamsNumber - i;j++)
{
TeamDetails tmp = new TeamDetails();
if(teams[j+1].getScore() > teams[j].getScore())
{
tmp = teams[j+1];
teams[j+1] = teams[j];
teams[j] = tmp;
}
}
}
for(int k = 0; k < teams.length; k++)
{
teams[k].setRank(k+1);
}
} // end method Update
// method to query the database ( Using Searching Algorithm)
public String queryResult(int year, int mon, int date)
{
String str;
str = String.valueOf(year) + "-" + String.valueOf(mon) + "-" + String.valueOf(date) + "\n";
for(int i = 0; i < resultNumber; i++)
{
if(result[i].getYear() == year && result[i].getMonth() == mon && result[i].getDate() == date)
{
str = str + "teams: " + result[i].getTeamA() + " vs " + result[i].getTeamB() + " " + "\n" +
"score: " + String.valueOf(result[i].getScoreA()) + " vs " + String.valueOf(result[i].getScoreB()) + "\n" + "\n";
}
}
return str;
} // end method QueryResult
// anothoer mothod to query the database ( Also Using the Searching Algorithm)
public String queryTeamsDetails(String name)
{
for(int i = 0; i < teamsNumber; i++)
{
if(name.equals(teams[i].getName()))
{
String str = name + "'s Information:\n" + "Team name:" + name + "\n" +
"Location:" + teams[i].getLocation() + "\n" + "Score:" + String.valueOf(teams[i].getScore()) + "\n" +
"Rank:" + String.valueOf(teams[i].getRank()) + "\n";
return str;
}
}
return "";
} // end method QueryTeamsDetails
// initialize the database
public void initialize()
{
try
{
File teamsDetailsFile = new File( "teamsDetails.txt");
FileReader currentFile = new FileReader(teamsDetailsFile);
BufferedReader input = new BufferedReader(currentFile);
String contents1 = input.readLine();
String contents2,contents3,contents4;
int i = 0;
while(contents1 != null)
{
contents2 = input.readLine();
contents4 = input.readLine();
TeamDetails tmp = new TeamDetails();
teams[i].set(contents1,contents2,contents4,0,0);
contents1 = input.readLine();
i++;
}
teamsNumber = i;
input.close();
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null,"Please make sure the file name is correct");
}
} // end method initialize
// mothod to read from file
public void readFromFile()
{
try
{
File resultFile = new File( "sportsResult.txt");
FileReader currentFile = new FileReader(resultFile);
BufferedReader input = new BufferedReader(currentFile);
String nameA, nameB, scoreA, scoreB, year, month, date;
nameA = input.readLine();
while(nameA != null)
{
GameResult tmp = new GameResult();
nameB = input.readLine();
scoreA = input.readLine();
scoreB = input.readLine();
year = input.readLine();
month = input.readLine();
date = input.readLine();
tmp.setResult(nameA, nameB, Integer.parseInt(scoreA),Integer.parseInt(scoreB),Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(date));
addNewGameResult(tmp);
nameA = input.readLine();
}
input.close();
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null,"Please make sure the file name is correct");
}
update();
} // end method readFromFile
// mothod to write data to file
public void writeToFile()
{
try
{
File currentFile = new File("SportsResult.txt");
FileWriter currentWriter = new FileWriter(currentFile);
PrintWriter output = new PrintWriter(currentFile);
int i;
String content;
for(i = 0; i < resultNumber; i++)
{
content = result[i].getTeamA() + "\n" + result[i].getTeamB() + "\n" +
String.valueOf(result[i].getScoreA()) + "\n" +
String.valueOf(result[i].getScoreB()) + "\n" +
String.valueOf(result[i].getYear()) + "\n" +
String.valueOf(result[i].getMonth()) + "\n" +
String.valueOf(result[i].getDate()) + "\n";
output.print(content);
}
output.close();
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null,"Please make sure the file name is correct");
}
} // end method writetoFile
public int getTeamsNumber()
{
return teamsNumber;
}
public int getResutlNumber()
{
return resultNumber;
}
public TeamDetails getTeam(int i)
{
return teams[i];
}
public GameResult getGameResult(int i)
{
return result[i];
}
} // end class DataBase
public class SportsResultDataBase extends JFrame
{
private DataBase database;
// JPanel
private JPanel windowJPanel1;
private JPanel queryResultJPanel;
private JPanel windowJPanel2;
private JPanel addResultJPanel;
//JButton
private JButton readFromFileJButton;
private JButton rankListJButton;
private JButton enterTeamJButton;
private JButton enterGameJButton;
private JButton saveToFileJButton;
private JButton quitJButton;
private JButton enterQueryJButton;
// JTextArea and JScrollPane to display information
private JTextArea descriptionJTextArea;
private JScrollPane descriptionJScrollPane;
private JTextArea addDescriptionJTextArea;
private JScrollPane addDescriptionJScrollPane;
//JLabel
private JLabel selectJLabel;
private JLabel logoJLabel;
private JLabel descriptionJLabel;
private JLabel queryResultJLabel;
private JLabel addResultJLabel;
private JLabel timeJLabel;
private JLabel yearJLabel;
private JLabel monthJLabel;
private JLabel dateJLabel;
private JLabel vsJLabel;
private JLabel vssJLabel;
private JLabel scoreJLabel;
private JLabel adddateJLabel;
private JLabel addTimeJLabel;
private JLabel addYearJLabel;
private JLabel addMonthJLabel;
private JLabel addDateJLabel;
//JcombBox
private JComboBox selectJComboBox;
private JComboBox teamAJComboBox;
private JComboBox teamBJComboBox;
// JSpinner
private JSpinner yearJSpinner;
private JSpinner monthJSpinner;
private JSpinner dateJSpinner;
private JSpinner addYearJSpinner;
private JSpinner addMonthJSpinner;
private JSpinner addDateJSpinner;
//JTextField to ...
private JTextField scoreAJTextField;
private JTextField scoreBJTextField;
private String str1;
private String str2;
// no-argument constructor
public SportsResultDataBase()
{
database = new DataBase();
database.initialize();
createUserInterface();
}
// create and position GUI components; register event handlers
public void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
contentPane.setLayout( null );
// set up readFromFileJButton
readFromFileJButton = new JButton();
readFromFileJButton.setBounds(130,10,200,20);
readFromFileJButton.setText("Read data from File");
contentPane.add(readFromFileJButton);
readFromFileJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when previousJButton is clicked
public void actionPerformed( ActionEvent event )
{
readFromFileJButtonActionPerformed(event);
}
} // end anonymous inner class
); // end call to addActionListener
//set up windowJPanel1
windowJPanel1 = new JPanel();
windowJPanel1.setBounds( 10, 40, 460, 400 );
windowJPanel1.setBorder( new TitledBorder( "Teams Information" ) );
windowJPanel1.setLayout( null );
contentPane.add( windowJPanel1 );
//set JLabel
selectJLabel = new JLabel();
selectJLabel.setBounds(20,30,150,20);
selectJLabel.setText("Select Team Name:");
windowJPanel1.add(selectJLabel);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -