我正在为我的数据结构编程课做大学任务。我们正在为这项任务分组工作,所以我目前正在为我的一个队友项目工作。我添加了本地READ / WRITE代码,因此我可以将arraylist保存到本地.dat文件中。但是,对于额外的标记,我可以进行高级文件处理。
我决定采用MySQL方法。
该计划是一个篮球运动员CRUD系统,您可以在其中添加一名球员,他们所在的球队,地址,姓名和号码。
我有5节课。教练班(目前没有做任何事情),球员班和团队班。这些类只有set,get和toString方法。
最后两个类是BasketBallSystem类,它包含所有GUI,方法,菜单等。
package ie.wit.basketball;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class BasketballSystem {
public ArrayList<Team> getTeams() {
return teams;
}
private ArrayList<Team> teams = new ArrayList<>();
public BasketballSystem() throws Exception {
teams=BasketballFile.ArrayListFromFile();
if (teams.size() == 0)
{ Team t = new Team("Golden State Warriors", 10);
teams.add(t);
t.getPlayers().add(new Player("Donna Kinsella", 666));
t.getPlayers().add(new Player("Tommy White", 21));
t.setTheCoach(new Coach("Tom Jones"));
for (Player p : t.getPlayers()) {
System.out.println(p.getPlayerName());
}
t = new Team("Florida Falcons", 10);
t.getPlayers().add(new Player("Don Keating", 76));
t.getPlayers().add(new Player("Tom Green", 22));
teams.add(t);
}
}
public int menuMain() {
JLabel addPlayer = new JLabel("1. Add a Player");
JLabel listPlayers = new JLabel("2. List all Players");
JLabel updateDetails = new JLabel("3. Update Players Details");
JLabel deletePlayers = new JLabel("4. Delete a player");
JLabel exitSys = new JLabel("5. Exit");
JTextField optionField = new JTextField();
Object[] message = new Object[6];
message[0] = addPlayer;
message[1] = listPlayers;
message[2] = updateDetails;
message[3] = deletePlayers;
message[4] = exitSys;
message[5] = optionField;
JOptionPane.showMessageDialog(null, message);
try{
return Integer.parseInt(optionField.getText());
}
catch(NumberFormatException e)
{
return 5;
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// new BasketballSystem();
int choice;
BasketballSystem b = new BasketballSystem();
do {
choice = b.menuMain();
switch (choice) {
case 1:
b.addPlayer();
break;
case 2:
b.listPlayers();
break;
case 3:
b.updatePlayersDetails();
break;
case 4:
b.removePlayer();
break;
case 5:
JOptionPane.showMessageDialog(null, "Thank you for using this system.");
BasketballFile.ArrayListToFile(b.getTeams());
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Entry, Please try again.");
}
} while (choice != 5);
}
private void updatePlayersDetails() {
// TODO Auto-generated method stub
String playerNumber = new String("Player Number");
JTextField number = new JTextField("");
String playerName = new String("Player Name");
JTextField name = new JTextField("");
String playerAddress = new String("Player Address");
JTextField address = new JTextField("");
String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);
Object message[] = new Object[6];
message[0] = playerNumber;
message[1] = number;
message[2] = playerName;
message[3] = name;
message[4] = playerAddress;
message[5] = address;
int response = JOptionPane.showConfirmDialog(null, message, "Player Details Update",
JOptionPane.OK_CANCEL_OPTION);
if (response == JOptionPane.OK_OPTION) {
try {
int pnum = Integer.parseInt(number.getText());
List<Player> players = teams.get(teamnamescombo.getSelectedIndex()).getPlayers();
Iterator<Player> it = players.iterator();
while (it.hasNext()) {
Player p = it.next();
if (p.getPlayerNumber() == pnum) {
p.setPlayerName(playerName);
p.setPlayerNumber(playerNumber);
p.setPlayerName(playerName);
}
// if (p.getPlayerAddress()==pAdd){
JOptionPane.showMessageDialog(null, "Player's address has been successfully updated");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: Player number invalid");
}
}
}
private void removePlayer() {
// TODO Auto-generated method stub
String playerNumber = new String("Player Number");
JTextField number = new JTextField("");
String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);
Object message[] = new Object[3];
message[0] = playerNumber;
message[1] = number;
message[2] = teamnamescombo;
int response = JOptionPane.showConfirmDialog(null, message, "Player Removal", JOptionPane.OK_CANCEL_OPTION);
if (response == JOptionPane.OK_OPTION) {
int pnum = Integer.parseInt(number.getText());
List<Player> players = teams.get(teamnamescombo.getSelectedIndex()).getPlayers();
Iterator<Player> it = players.iterator();
while (it.hasNext()) {
Player p = it.next();
if (p.getPlayerNumber() == pnum) {
it.remove();
JOptionPane.showMessageDialog(null, "Player has been successfully removed");
}
}
}
}
private void listPlayers() {
// TODO Auto-generated method stub
// String
for (Team t : teams) // for all teams
{
System.out.println("Team: " + t.getTeamName());
for (Player p : t.getPlayers()) // for all player
{
System.out.println(
"Player: " + p.getPlayerName() + " " + p.getPlayerNumber() + " " + p.getPlayerAddress());
}
}
}
private void addPlayer() {
// TODO Auto-generated method stub
// String teamName
String playerName = new String("Player name");
String playerNumber = new String("Player Number");
String playerAddress = new String("Player Address");
JTextField name = new JTextField("");
JTextField number = new JTextField("");
JTextField address = new JTextField("");
String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);
Object message[] = new Object[7];
message[0] = playerName;
message[1] = name;
message[2] = playerNumber;
message[3] = number;
message[4] = playerAddress;
message[5] = address;
message[6] = teamnamescombo;
int response = JOptionPane.showConfirmDialog(null, message, "Player Entry", JOptionPane.OK_CANCEL_OPTION);
if (response == JOptionPane.OK_OPTION) {
int pnum = Integer.parseInt(number.getText());
Player newPlay = new Player(name.getText(), pnum);
teams.get(teamnamescombo.getSelectedIndex()).getPlayers().add(newPlay);
JOptionPane.showMessageDialog(null, "Player has been successfully Added");
}
{
}
}
}
最后一个类是BasketBallFile类,它处理文件管理。
package ie.wit.basketball;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class BasketballFile {
public static void ArrayListToFile(ArrayList<Team> al) throws Exception {
try{
File f=new File("mydata.dat");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(al);
System.out.println("Written to file!");
oos.close();
fos.close();
}catch(Exception e) {
System.out.println("Error: "+e.getMessage());
throw e;
}
}
public static ArrayList<Team> ArrayListFromFile() throws Exception {
ArrayList<Team> al=new ArrayList<Team>();
try{
File f=new File("mydata.dat");
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
al=(ArrayList<Team>)ois.readObject();
System.out.println("Read from file!");
ois.close();
fis.close();
}catch(Exception e) {
System.out.println("Error: "+e.getMessage());
}
return al;
}
}
我不指望你告诉我到底该怎么做,我只是不知道从哪里开始。我已经尝试了谷歌搜索,我尝试搜索教程,但我尝试的一切都没有用。我的Java知识非常基础,所以如果你能提供帮助,那就太棒了。谢谢。
很抱歉,如果我没有正确的格式化,这是我第一次使用这个网站。谢谢!
答案 0 :(得分:0)
Java中数据库管理的关键是Java Persistence API。这是一个非常大的话题,我个人只是从Java EE的上下文中知道它,但是it also works with Java SE。
只有基本的Java知识,这可能是一个巨大的飞跃,也许你应该很好地理解关系数据库以及如何在进入ORM的精彩世界之前使用它们。对于大学作业中的额外分数,我可能只是将读/写转换为随机文件访问,而不是将整个列表序列化到文件中。 (这也不容易,但它并不需要你学习新的概念。)