JavaDay14-球员管理项目
tags :
- java基础
flag: blue
@toc
JavaDay14-球员管理项目
具体代码:
player
package src.com.qfedu.entity;/*球员实体类:成员变量:姓名,编号,年龄,工资,位置成员方法:投篮,传球*/
public class Player {//成员变量private String name;private int id; //实现ID的自动增长,这里要使用计数器private int age;private double salary;private String location;private static int count = 1; //计数器//构造代码块来完成计数器操作{this.id = count++;}public Player() {}public Player(String name, int age, double salary, String location) {//这里使用自己的set方法,来完成赋值成员变量的操作this.setName(name);this.setAge(age);this.setSalary(salary);this.setLocation(location);}public int getId() {return id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {if (age <= 0 || age > 45) {this.age = 19;} else {this.age = age;}}public double getSalary() {return salary;}public void setSalary(double salary) {if (salary <= 0) {this.salary = 1;} else {this.salary = salary;}}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}//普通的成员方法页搞定public void shot() {System.out.println(this.getName() + "投篮练习中~~~");}public void passBall() {System.out.println(this.getName() + "传球练习中~~~");}//重写的Java中超类【Object】类中toString,toString是该对象的描述//当通过System.out.println(player)会自动调用的方法@Overridepublic String toString() {return "[ID:" + this.id + " Name:" + this.name + " Age:" + this.age+ " Salary:" + this.salary + " Location:" + this.location + "]";}
}
TeamManager
package src.com.qfedu.dao;import java.util.Scanner;import src.com.qfedu.entity.Player;public class TeamManager {//球队的名字private String teamName;//保存球员信息的数组private Player[] allPlayers = new Player[defaultCount];//统计当前球队中有多少球员private static int itemCount = 0; //元素个数//球队的默认球员个数private static final int defaultCount = 10; public TeamManager() {}public TeamManager(String teamName) {this.setTeamName(teamName);}public void setTeamName(String teamName) {this.teamName = teamName;}public String getTeamName() {return teamName;}/** 添加球员* 解雇球员* 修改球员信息* 查询球员信息* * 排序算法* * 数组增长*//*** 添加新球员到数组中* @param playerToAdd 要添加的球员类对象*/public void addPlayer(Player playerToAdd) {//参数合法性判断(以后叫【异常处理】)if (null == playerToAdd) {System.out.println("球员信息为空,不可添加");return;}//类内有一个itemCount的静态成员变量,是用来统计插入的元素个数,而且也是下个元素保存的下标位置//因为是插入操作,所以要考虑数组的容量问题,如果插入的数据个数已经大于了数组的长度,需要扩容if (itemCount >= allPlayers.length) {//扩容grow();}allPlayers[itemCount++] = playerToAdd;}/*** 通过球员的ID删除球员* @param playerID*/public void layoffPlayerByPlayerID(int playerID) {//需求查询方法,调用类内私有化通过球员ID查询球员在数组中位置的方法,获取下标int index = findPlayerIndexByPlayerID(playerID);if (index >= 0) {//删除该位置的球员,数组整体左移/* 1 2 3 4 5index 1 << 3index 3 << 1*/for (int i = index; i < itemCount - 1; i++) {allPlayers[i] = allPlayers[i + 1];}//原本最后一个有效元素赋值为nullallPlayers[itemCount - 1] = null;//球员的球员个数 - 1itemCount--;} else {System.out.println("查无此人,无法删除");}}/*** 通过球员的ID,来查询球员的信息* @param playerID 要展示的球员ID号*/public void showPlayerInfoByPlayerID(int playerID) {int index = findPlayerIndexByPlayerID(playerID);if (index > -1) {System.out.println(allPlayers[index]);} else {System.out.println("查无此人");}}/*** 通过球员ID 修改球员信息* @param playerID 需要修改信息的球员ID*/public void modifyPlayerInfoByPlayerID(int playerID) {int index = findPlayerIndexByPlayerID(playerID);Scanner sc = new Scanner(System.in);//表示找到球员,进行修改操作if (index > -1) {//while(true) switch - caseint flag = 0;int choose = -1;Player temp = allPlayers[index];while (true) {System.out.println("修改" + temp.getId() + ":" + temp.getName() + "的信息");System.out.println("***Age:" + temp.getAge());System.out.println("***Salary:" + temp.getSalary());System.out.println("***Location:" + temp.getLocation());System.out.println("1. 修改球员姓名");System.out.println("2. 修改球员年龄");System.out.println("3. 修改球员工资");System.out.println("4. 修改球员位置");System.out.println("5. 退出");choose = sc.nextInt();sc.nextLine();switch (choose) {case 1:System.out.println("请输入球员的姓名:");String name = sc.nextLine();temp.setName(name);break;case 2:System.out.println("请输入球员的年龄:");int age = sc.nextInt();temp.setAge(age);break;case 3:System.out.println("请输入球员的工资:");double salary = sc.nextDouble();temp.setSalary(salary);break;case 4:System.out.println("请输入球员的位置:");String location = sc.nextLine();temp.setLocation(location);break;case 5:flag = 1;break;default:System.out.println("选择错误");break;} //switch(choose) - caseif (1 == flag) {allPlayers[index] = temp;System.out.println("保存退出");break;}} //while (true)} else {System.out.println("查无此人");}//sc.close();} /*** 工资的降序排序*/public void descendingSelectSortBySalary() {//保护源数据!!!//1. 准备一个数组专门用来做排序操作,数组的大小和源数据有效元素个数一致Player[] sortArray = new Player[itemCount];//2. 数据拷贝for (int i = 0; i < itemCount; i++) {sortArray[i] = allPlayers[i];}//外层控制选择排序的次数for (int i = 0; i < itemCount - 1; i++) {int index = i;for (int j = i + 1; j < itemCount; j++) {if (sortArray[index].getSalary() < sortArray[j].getSalary()) {index = j;}}if (index != i) {Player temp = sortArray[index];sortArray[index] = sortArray[i];sortArray[i] = temp;}}showSortResult(sortArray);}/*** 年龄的升序排序*/public void ascendingSelectSortByAge() {//保护源数据!!!//1. 准备一个数组专门用来做排序操作,数组的大小和源数据有效元素个数一致Player[] sortArray = new Player[itemCount];//2. 数据拷贝for (int i = 0; i < itemCount; i++) {sortArray[i] = allPlayers[i];}//外层控制选择排序的次数for (int i = 0; i < itemCount - 1; i++) {int index = i;for (int j = i + 1; j < itemCount; j++) {if (sortArray[index].getAge() > sortArray[j].getAge()) {index = j;}}if (index != i) {Player temp = sortArray[index];sortArray[index] = sortArray[i];sortArray[i] = temp;}}showSortResult(sortArray);}/*** 自己研究一下冒泡排序*/public void descendingBubbleSortByAge() {//保护源数据!!!//1. 准备一个数组专门用来做排序操作,数组的大小和源数据有效元素个数一致Player[] sortArray = new Player[itemCount];//2. 数据拷贝for (int i = 0; i < itemCount; i++) {sortArray[i] = allPlayers[i];}//外层控制比较的轮次for (int i = 0; i < itemCount - 1; i++) {//内层控制两两比较次数for (int j = 0; j < itemCount - i - 1; j++) {if (sortArray[j].getAge() < sortArray[j + 1].getAge()) {Player temp = sortArray[j];sortArray[j] = sortArray[j + 1];sortArray[j + 1] = temp;}}} showSortResult(sortArray);}public void showAllPlayers() {for (Player player : allPlayers) {if (null == player) {break;}System.out.println(player);}}/*** 需要一个扩容方法,方法不需要参数,不要类外调用,只是在类内使用*/private void grow() {//1. 获取原数组元素个数int oldCapacity = this.allPlayers.length;//2. 通过原数组元素计算新的元素个数 , 大约相对于原本元素个数的1.5倍int newCapacity = oldCapacity + (oldCapacity >> 1);//3. 创建新的数组,元素格式是原本的1.5倍Player[] newArray = new Player[newCapacity];//4. 利用循环拷贝数据for (int i = 0; i < oldCapacity; i++) {newArray[i] = this.allPlayers[i];}//5. 地址交换this.allPlayers = newArray;}/*** 私有化的方法,只提供给类内使用,用来获取指定球员ID在数组中下标位置* @param playerID 要查询的球员ID号* @return int类型,返回查询的数据在数组中的下标位置,如果没有找到,返回-1*/private int findPlayerIndexByPlayerID(int playerID) {//参数合法性判断if (playerID < 1 || playerID > 100) {System.out.println("传入球员ID不合法");return -1;}//用来保存球员的下标位置int index = -1;for (int i = 0; i < itemCount; i++) {//拿球员的ID和传入的ID进行比较if (allPlayers[i].getId() == playerID) {index = i;break;}}return index;}private void showSortResult(Player[] sortArray) {//增强for循环for (Player player : sortArray) {System.out.println(player);}//普通for循环// for (int i = 0; i < sortArray.length; i++) {// System.out.println(sortArray[i]);// }}
}
view
package src.com.qfedu.view;import java.util.Scanner;import src.com.qfedu.dao.TeamManager;
import src.com.qfedu.entity.Player;public class View {public static void main(String[] args) {TeamManager tm = new TeamManager("孤狼B组");Player p1 = new Player("森林狼", 21, 10000, "PG");Player p2 = new Player("西伯利亚狼", 19, 9000, "SF");Player p3 = new Player("鸵鸟", 20, 9500, "SG");Player p4 = new Player("卫生院", 22, 9800, "SG");Player p5 = new Player("恶狼", 22, 8800, "PF");Player p6 = new Player("老炮", 22, 9900, "C");tm.addPlayer(p1);tm.addPlayer(p2);tm.addPlayer(p3);tm.addPlayer(p4);tm.addPlayer(p5);tm.addPlayer(p6);int flag = 0;int choose = 0;int playerID = 0;Scanner sc = new Scanner(System.in);while (true) {System.out.println("欢迎来到孤狼B组");System.out.println("1. 展示所有成员");System.out.println("2. 添加新的成员");System.out.println("3. 退役老成员");System.out.println("4. 查询成员资料");System.out.println("5. 修改成员资料");System.out.println("6. 按照年龄排序");System.out.println("7. 按照收入排序");System.out.println("8. 退出");choose = sc.nextInt();sc.nextLine();switch (choose) {case 1://展示所有队员tm.showAllPlayers();break;case 2://添加新成员System.out.println("清输入新成员的名字");String name = sc.nextLine();System.out.println("请输入新成员年龄");int age = sc.nextInt();sc.nextLine();System.out.println("请输入新成员工资");double salary = sc.nextDouble();sc.nextLine();System.out.println("请输入新成员的位置");String location = sc.nextLine();Player playerToAdd = new Player(name, age, salary, location);tm.addPlayer(playerToAdd);break;case 3://退役System.out.println("请输入要退役的老成员ID");playerID = sc.nextInt();sc.nextLine();tm.showPlayerInfoByPlayerID(playerID);System.out.println("是否要确定删除? Y or N");char ch = sc.nextLine().charAt(0);if ('Y' == ch || 'y' == ch) {tm.layoffPlayerByPlayerID(playerID);}break;case 4://查询System.out.println("请输入要查询的老成员ID");playerID = sc.nextInt();sc.nextLine();tm.showPlayerInfoByPlayerID(playerID);break;case 5:System.out.println("请输入要修改成员ID");playerID = sc.nextInt();sc.nextLine();tm.modifyPlayerInfoByPlayerID(playerID);break;case 6:tm.ascendingSelectSortByAge();break;case 7:tm.descendingSelectSortBySalary();break;case 8:flag = 1;break;default:System.out.println("选择错误");break;}if (1 == flag) {System.out.println("啊,朋友再见~~~");break;}}}
}
JavaDay14-球员管理项目相关推荐
- idea从git上拉取并管理项目
idea从git上拉取并管理项目 1:idea从git上拉取项目 (1)FILE --> New --> Project from Version Control --> Git ( ...
- 使用Trello看板管理项目
在大约10年前,我们用面向对象的方式管理项目.五年前,我们又开始使用敏捷方法管理项目.一年前,精益软件开发方法开始流行起来,看板式项目管理方式成为项目管理的主流,那么到底看板式管理到底是如何管理项目的 ...
- agilebpm脑图_干货基于SpringBoot2开发的Activiti引擎流程管理项目脚手架
干货基于SpringBoot2开发的Activiti引擎流程管理项目脚手架 [干货]基于SpringBoot2开发的Activiti引擎流程管理项目脚手架 前言 在工作中,难免会遇到需要开发基于流程管 ...
- 笔记-项目立项管理-项目建议书
1. 项目建议书的核心内容 参考 <信息系统项目管理师教程>第三版 -项目立项管理-项目建议书内容-P157:项目建议书应该包括的核心内容如下: 项目的必要性 项目的市场预测 产品方案或服 ...
- 浙江理工大学信息学院本科生创新项目总结报告——简单的后台用户管理项目
信息学院本科生创新项目总结报告 研究目的与意义 基于Spring Boot框架等技术实现一个简单的后台用户管理系统项目,具有良好的用户交互体验.良好的信息反馈能力.安全防护措施,包括用户登录.用户注册 ...
- 在eclipse使用git管理项目工程代码
在编写一个项目的时候,通常不可能整个项目都由一个人完成,一般都是通过团队分工,不同成员负责不同的部分,这时候,我们会使用git帮助我们管理项目代码,而且,编写java代码,我们一般使用Eclipse这 ...
- Qt Creator管理项目层次结构
Qt Creator管理项目层次结构 管理项目层次结构 显示和隐藏项目 锁定物品 安排项目 管理项目层次结构 该导航视图显示在当前QML文件及其相互关系的项目.项目(1)以树形结构列出,位于其父项(2 ...
- Qt Creator管理项目
Qt Creator管理项目 管理项目 建立专案 使用版本控制系统 配置项目 管理会议 管理项目 Qt Creator的主要优点之一是,它允许一组设计人员和开发人员使用通用的设计,开发和调试工具在不同 ...
- 【Git】码云Gitee.com管理项目全流程
Gitee.com 是 OSCHINA.NET 推出的代码托管平台,支持 Git 和 SVN,提供免费的私有仓库托管.目前已有超过 500 万的开发者选择 Gitee. 官网:https://gite ...
- Composer - PHP 世界里用于管理项目依赖的工具
Composer - PHP 世界里用于管理项目依赖的工具 Composer v1.8 版本正式发布.Composer是 PHP 世界里用于管理项目依赖的工具. 如果你已经安装了 Composer , ...
最新文章
- python四大软件-9个使用Python的世界级软件公司
- php cros跨域处理,php接口设置cors实现ajax跨域
- python编程需要安装什么软件_[零基础学pythyon]安装python编程环境
- sqoop从HDFS导出数据到Mysql,卡在Running job: job_1571036741208_0010不动了,或者map 100% reduce 0%不动了
- 匹配指定内容的div_HTML背景色教程–如何更改Div背景色,并通过代码示例进行了说明...
- 数据结构(字符串)—— 循环旋转字符串的判断
- java课题设计实验报告,JAVA简单记事本程序设计实验报告
- 使用锚标记返回网页顶部的方法
- 为什么函数lamda显示权限不足_C++常用内置函数
- vcredist_x86.exe 静默安装方法
- 用javascript访问操作iframe中的dom对象
- android别踩白块小游戏,手摸手带你实现 小游戏别踩白块儿 -- 内有游戏链接
- 【Linux】Linux中755权限是什么意思
- 百度竞价托管一月多少钱
- 怎样给蔬菜图片抠图?看看这个马路中央的紫茄子
- 【白话机器学习】算法理论+实战之决策树
- android wifi流量监控,Android流量监测的实现原理
- HTML-背景和图片
- openstack--T版—nava计算服务
- OMAP4开发资源总结
热门文章
- 微软打击盗版的“三部曲”
- java源程序的文件名必须与,Java源程序的文件名一定要与文件中某个类的名称一致。()...
- python第一周心得
- 移动端背景图css6,移动端适配之雪碧图(sprite)背景图片定位
- Web前端面试 面试官常问问题
- HOUR 11 Developing Advanced Pointers
- 史上最完整的《指环王》魔戒战争大事记(编年体长文)
- python 学习(三)
- 【练习题】第四章--交互设计(Think Python)
- word文档通配符换行_PDF如何转化成Word文档?