JAVA实现面对对象编程,这道题怎么做? 50
开发NBA球队管理系统,可实现添加球队,查看球队,删除球员,导出球员,退出等功能。不使用数据库,使用对像和集合存储数据!!!...
开发NBA球队管理系统,可实现添加球队,查看球队,删除球员,导出球员,退出等功能。不使用数据库,使用对像和集合存储数据!!!
展开
2个回答
展开全部
Index.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Index {
// 创建全局键盘输入对象.
static Scanner sc;
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
// 创建Map映射,键为球队编号,值为球队类.
HashMap<String, Team> teams = new HashMap<String, Team>();
// 循环命名,用于指定退出和继续.
Loop: while (true) {
// 打印选项
System.out.println("1.添加球队 2.查看球队 3.删除球员信息 4.导出球队信息 5.退出");
String option = sc.nextLine();
switch (option) {
case "1": // 1.添加球队
addTeam(teams);
break;
case "2": // 2.查看球队
viewTeam(teams);
break;
case "3": // 3.删除球员
deletePlayer(teams);
break;
case "4": // 4.导出信息
exportInfo(teams);
break;
case "5": // 5.退出系统
exit();
sc.close(); // 退出此系统之前关闭输入流.
break Loop;
default: // 其他情况
System.out.println("错误输入,请重新输入:");
continue Loop;
}
}
}
// 根据球队编号添加队伍,添加队伍的同时在球队中添加球员对象.
public static void addTeam(Map<String, Team> teams) {
System.out.println("请输入球队编号:");
String teamID = sc.nextLine();
// 创建新球队对象.
Team team = new Team();
team.setTeamID(teamID);
// 把创建的球队对象加入map映射中.
teams.put(teamID, team);
System.out.println("请输入球队名字:");
String teamName = sc.nextLine();
team.setTeamName(teamName);
System.out.println("请输入球队所在城市:");
String teamCity = sc.nextLine();
team.setTeamCity(teamCity);
// 新建一个布尔变量用于判断是否继续添加.
boolean flag = true;
while (flag) {
System.out.println("请输入球员名字:");
String playerName = sc.nextLine();
System.out.println("请输入球员场均得分:");
double pointsPerGame = sc.nextDouble();
sc.nextLine();
System.out.println("请选择球员类型: 1.前锋 2.中锋 3.后卫");
// 新建一个position变量,判断添加的球员类型.
int position = sc.nextInt();
sc.nextLine();
if (position == 1) {
Forward player = new Forward();
team.setPlayers(player);
player.setPlayerPosition("前锋");
System.out.println("请输入场均篮板:");
double reboundsPerGame = sc.nextDouble();
sc.nextLine();
player.setPlayerName(playerName);
player.setPointsPerGame(pointsPerGame);
player.setReboundsPerGame(reboundsPerGame);
} else if (position == 2) {
Center player = new Center();
team.setPlayers(player);
player.setPlayerPosition("中锋");
System.out.println("请输入场均盖帽:");
double blocksPerGame = sc.nextDouble();
sc.nextLine();
player.setPlayerName(playerName);
player.setPointsPerGame(pointsPerGame);
player.setBlocksPerGame(blocksPerGame);
} else {
Guard player = new Guard();
team.setPlayers(player);
player.setPlayerPosition("后卫");
System.out.println("请输入场均助攻:");
double assistsPerGame = sc.nextDouble();
sc.nextLine();
player.setPlayerName(playerName);
player.setPointsPerGame(pointsPerGame);
player.setAssistsPerGame(assistsPerGame);
}
System.out.println("是否继续添加:(Y/N)");
String isAdd = sc.nextLine();
if (isAdd.equalsIgnoreCase("y"))
flag = true;
else
flag = false;
}
System.out.println("球队编号\t球队名称\t球队所在城市");
System.out.println(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());
}
// 根据队伍,输出球员信息.
public static void viewTeam(Map<String, Team> teams) {
// 用keySet方法取出map映射中的键.用于迭代取Team对象.
Set<String> keyset = teams.keySet();
for (Iterator<String> itt = keyset.iterator(); itt.hasNext();) {
String tmp = itt.next();
Team team = teams.get(tmp);
System.out.println("球队编号\t球队名称\t球队所在城市");
System.out.println(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());
// 得到Team对象之后,迭代其中的ArrayList,其中存储了球员对象.
System.out.println("球员名字\t场均得分\t球员类型");
for (Iterator<Player> itp = team.getPlayers().iterator(); itp.hasNext();) {
Player player = itp.next();
if (player.getPlayerPosition().equals("前锋"))
player = (Forward) player;
else if (player.getPlayerPosition().equals("中锋"))
player = (Center) player;
else
player = (Guard) player;
System.out.println(player);
}
}
}
// 先根据球队编号,再删除球员,如没有球队/员则会提示.
public static void deletePlayer(Map<String, Team> teams) {
System.out.println("请输入要删除的球队编号");
String teamID = sc.nextLine();
// 判断map映射中是否存在输入的球队编号,无则跳回选择界面.
if (teams.containsKey(teamID)) {
Team team = teams.get(teamID);
System.out.println("请输入球员姓名:");
String playerName = sc.nextLine();
boolean flag = false;
for (Iterator<Player> it = team.getPlayers().iterator(); it.hasNext();) {
Player tmp = it.next();
if (playerName.equals(tmp.getPlayerName())) {
flag = true;
it.remove();
System.out.println("删除成功");
break;
}
}
if (!flag)
System.out.println("删除失败,无此球员");
} else
System.out.println("无此球队,请重新选择功能.");
}
// 导出球队信息到当前目录.
public static void exportInfo(Map<String, Team> teams) throws IOException {
Set<String> keyset = teams.keySet();
// 通过判断keySet的长度判断是否输入过球队信息.
if (keyset.size() != 0) {
BufferedWriter bfw = new BufferedWriter(new FileWriter("Teams.txt"));
for (Iterator<String> it = keyset.iterator(); it.hasNext();) {
String tmp = it.next();
Team team = teams.get(tmp);
bfw.write("球队编号\t球队名称\t球队所在城市");
bfw.newLine();
bfw.write(team.getTeamID() + "\t\t" + team.getTeamName() + "\t\t" + team.getTeamCity());
bfw.newLine();
}
bfw.close();
} else {
System.out.println("请先输入球队信息,再导出.");
}
}
// 退出系统.
public static void exit() {
System.out.println("欢迎下次再来.");
}
}
Team.java
import java.util.ArrayList;
public class Team {
private String teamID; // 球队编号
private String teamName; // 球队名字
private String teamCity; // 球队所在城市
private ArrayList<Player> players; // 球员集合
public Team() { // 实例化Team的同时,实例化ArrayList集合.
players = new ArrayList<>();
}
// 各属性get和set方法
public String getTeamID() {
return teamID;
}
public void setTeamID(String teamID) {
this.teamID = teamID;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getTeamCity() {
return teamCity;
}
public void setTeamCity(String teamCity) {
this.teamCity = teamCity;
}
public ArrayList<Player> getPlayers() {
return players;
}
// players的set方法,直接将球员添加到ArrayList中.
public void setPlayers(Player player) {
players.add(player);
}
}
Player.java
public class Player {
protected String playerName; // 球员姓名
protected double pointsPerGame; // 场均得分
protected String playerPosition; // 球员类型
// 各个属性的get,set方法
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public double getPointsPerGame() {
return pointsPerGame;
}
public void setPointsPerGame(double pointsPerGame) {
this.pointsPerGame = pointsPerGame;
}
public String getPlayerPosition() {
return playerPosition;
}
public void setPlayerPosition(String playerPosition) {
this.playerPosition = playerPosition;
}
// 重写toString方法,方便输出
public String toString() {
return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition;
}
}
Forward.java
public class Forward extends Player {
private double reboundsPerGame; // 场均篮板
// get,set方法
public double getReboundsPerGame() {
return reboundsPerGame;
}
public void setReboundsPerGame(double reboundsPerGame) {
this.reboundsPerGame = reboundsPerGame;
}
// 重写toString方法,方便输出
public String toString() {
return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均篮板\t\t" + reboundsPerGame;
}
}
Center.java
public class Center extends Player {
private double blocksPerGame; // 场均盖帽
// get,set方法
public double getBlocksPerGame() {
return blocksPerGame;
}
public void setBlocksPerGame(double blocksPerGame) {
this.blocksPerGame = blocksPerGame;
}
// 重写toString方法,方便输出
public String toString() {
return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均盖帽\t\t" + blocksPerGame;
}
}
Guard.java
public class Guard extends Player {
private double assistsPerGame; // 场均助攻
// get,set方法
public double getAssistsPerGame() {
return assistsPerGame;
}
public void setAssistsPerGame(double assistsPerGame) {
this.assistsPerGame = assistsPerGame;
}
// 重写toString方法,方便输出
public String toString() {
return playerName + "\t\t" + pointsPerGame + "\t\t" + playerPosition + "\t\t" + "场均助攻\t\t" + assistsPerGame;
}
}
除了注释外有不懂的追问,功能都测试过可以使用,求采纳.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询