模拟共享单车系统满足了三个单车公司的 投放 查看 删除 外借 归还功能;
此系统里使用了集合,继承....方法
首先先创建一个单车父类对象模板,模板里添加单车的共享属性:单车编号,外借时间,外借状态
创建单车公司共用对象模板,模板里添加属性:公司ID 公司名 公司拥有单车数量 公司拥有单车总数 单车借出次数;
生成每个对应的方法;
生成 set get tostring 方法; 有参构造 无参构造器;
创建测试类,main方法用来给每个公司对象里的属性赋值;
/**
* 单车测试
*/
public class TestBikes {
public static void main(String[] args) {
//HashMap创建集合,在集合中传对应的泛型;
HashMap<Integer,CompanyBikes> map = new HashMap<>();
CompanyBikes haLoCompany = new HaLoCompany(1001,"哈罗",new HashMap<Integer, BikeHome>(),0,0);
CompanyBikes ofoBikes = new OFOCompany(1002,"OFO",new HashMap<Integer, BikeHome>(),0,0);
CompanyBikes moBikeCompany = new MOBikeCompany(1003,"摩拜",new HashMap<Integer, BikeHome>(),0,0);
//使用添加map添加方法添加每个公司的属性
map.put(1,haLoCompany);
map.put(2,ofoBikes);
map.put(3,moBikeCompany);
//创建共享单车管理系统对象,将map传进去,这样管理系统就可以得到每个公司的属性;
ManagerBikesCompany managerBikesCompany = new ManagerBikesCompany(map);
//调用共享单车管理系统对象的menu()实现方法;
managerBikesCompany.menu();
}
}
创建一个共享单车管理器对象,实现(管理)三家公司对象的每个实现方法;
实现每个方法都要调用map中的单车公司,使用对应的 投放,删除,查看,外借,归还.... 方法(这些方法都是继承自单车公司父类模板的)
接收用户选择,找到对应的接收值,调用对应的方法;
public class ManagerBikesCompany {
private ShareBike shareBike;
private HashMap<Integer, CompanyBikes> map;
public ManagerBikesCompany() {
}
public ManagerBikesCompany(HashMap<Integer, CompanyBikes> map) {
this.map = map;
}
public void menu() {
Scanner sc = new Scanner(System.in);
System.out.println("欢迎进入单车管理系统,请选择要执行的操作");
String answer = "";
do {
System.out.println("**************************");
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("**************************");
System.out.println("请选择");
int num = sc.nextInt();
switch (num) {
case 1:
touFang(sc);
break;
case 2:
chaKan();
break;
case 3:
shanChu(sc);
break;
case 4:
jieChe(sc);
break;
case 5:
guiHuan(sc);
break;
case 6:
paiHang();
break;
case 7:
System.out.println("退出程序");
break;
}
System.out.println("是否继续 Y/N");
answer = sc.next();
} while ("y".equalsIgnoreCase(answer));
System.out.println("程序结束");
}
投放:
让用户选择投放哪家公司;用switch case去寻找到对应的公司名称;找到后调用map集合中的每个公司的投放(添加)单车方法;
/**
* 投放单车
* @param sc
*/
private void touFang(Scanner sc) {
System.out.println("*** 投放模块 ***");
System.out.println("[1] ofo");
System.out.println("[2] halo");
System.out.println("[3] MoBike");
System.out.println("请选择公司");
int Num = sc.nextInt();
System.out.println("请输入投放数量");
int bikeNum = sc.nextInt();
switch (Num) {
case 1:
map.get(1).addBike(bikeNum);
break;
case 2:
map.get(2).addBike(bikeNum);
break;
case 3:
map.get(3).addBike(bikeNum);
break;
default:
System.out.println("输入错误");
break;
}
}
删除:
/**
* 删除单车
* @param sc
*/
private void shanChu(Scanner sc) {
System.out.println("***删除模块***");
System.out.println("[1] ofo");
System.out.println("[2] halo");
System.out.println("[3] MoBike");
System.out.println("请选择公司");
int companyNum = sc.nextInt();
System.out.println("请输入要删除的单车");
int bikeId = sc.nextInt();
switch (companyNum) {
case 1:
map.get(1).deleteBike(bikeId);
break;
case 2:
map.get(2).deleteBike(bikeId);
break;
case 3:
map.get(3).deleteBike(bikeId);
break;
default:
System.out.println("输入错误");
break;
}
}
外借:
/**
* 外借单车
* @param sc
*/
private void jieChe(Scanner sc) {
System.out.println("***借车模块***");
System.out.println("[1] ofo");
System.out.println("[2] halo");
System.out.println("[3] MoBike");
System.out.println("请选择公司");
int companyNum = sc.nextInt();
System.out.println("请输入要借用的单车ID");
int bikeId = sc.nextInt();
switch (companyNum){
case 1:
map.get(1).borrowBike(bikeId);
break;
case 2:
map.get(2).borrowBike(bikeId);
break;
case 3:
map.get(3).borrowBike(bikeId);
break;
default:
System.out.println("输入错误");
break;
}
}
查看:
/**
* 查看
*
* @param
*/
private void chaKan() {
Set<Map.Entry<Integer, CompanyBikes>> entries = map.entrySet();
for (Map.Entry<Integer, CompanyBikes> entry : entries) {
System.out.println("|公司序号|公司名称|公司单车数量|公司单车借出次数|");
Integer key = entry.getKey();
CompanyBikes value = entry.getValue();
System.out.print(value.getId() + "|");
System.out.print(value.getName() + "|");
System.out.print(value.getSum() + "|");
System.out.print(value.getCount() + "|");
System.out.println();
System.out.println("单车ID|单车状态|单车借出时间");
HashMap<Integer, BikeHome> bikeHomes = value.getBikeHomes();
Set<Map.Entry<Integer, BikeHome>> entries1 = bikeHomes.entrySet();
for (Map.Entry<Integer, BikeHome> integerBikeHomeEntry : entries1) {
System.out.print(integerBikeHomeEntry.getValue().getId()+ "|");
System.out.print(integerBikeHomeEntry.getValue().getStatus() + "|");
System.out.println(integerBikeHomeEntry.getValue().getBorrowTime() + "|");
}
}
}
归还:
/**
* 归还单车
* @param sc
*/
private void guiHuan(Scanner sc) {
System.out.println("***还车模块***");
System.out.println("[1] ofo");
System.out.println("[2] halo");
System.out.println("[3] MoBike");
System.out.println("请选择公司");
int companyNum = sc.nextInt();
System.out.println("请输入要归还单车的ID");
int bikeId = sc.nextInt();
switch (companyNum){
case 1:
map.get(1).backBike(bikeId);
break;
case 2:
map.get(2).backBike(bikeId);
break;
case 3:
map.get(3).backBike(bikeId);
break;
default:
System.out.println("输入错误");
break;
}
}