思路分析
面向对象思维,三个公司当作子类,创建公司父类,创建接口规则
主要是思想,刚开始我是c转过来的,用面向过程的思想,差点用指向把自己绕晕。
1.单车类:
public class Bike {private int bikeId;// 单车编号private String bikeName;// 单车名称-->ofo 哈罗 摩拜private int status;// 单车状态--> 0 已借出 1 可借状态private String borrowTime;// 单车借出时间private long startTime;public long getStartTime() {return startTime;}public void setStartTime(long startTime) {this.startTime = startTime;}public Bike() {}public int getBikeId() {return bikeId;}public void setBikeId(int bikeId) {this.bikeId = bikeId;}public String getBikeName() {return bikeName;}public void setBikeName(String bikeName) {this.bikeName = bikeName;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getBorrowTime() {return borrowTime;}public void setBorrowTime(String borrowTime) {this.borrowTime = borrowTime;}public Bike(int bikeId, String bikeName, int status, String borrowTime) {this.bikeId = bikeId;this.bikeName = bikeName;this.status = status;this.borrowTime = borrowTime;}
}
单车公司类
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;public abstract class BikeCompany implements BikeMethod{private int bikeCompanyId;// 公司idprivate String bikeCompanyName;// ofo 哈罗 摩拜private Bike[] bikes;// 公司持有共享单车private int sum;//公司单车总量private int count;// 公司单车借出次数private int flagNum = 1;//记录单车序号的值Scanner sc = new Scanner(System.in);public int getBikeCompanyId() {return bikeCompanyId;}public void setBikeCompanyId(int bikeCompanyId) {this.bikeCompanyId = bikeCompanyId;}public String getBikeCompanyName() {return bikeCompanyName;}public void setBikeCompanyName(String bikeCompanyName) {this.bikeCompanyName = bikeCompanyName;}public Bike[] getBikes() {return bikes;}public void setBikes(Bike[] bikes) {this.bikes = bikes;}public int getSum() {return sum;}public void setSum(int sum) {this.sum = sum;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public BikeCompany() {}public BikeCompany(int bikeCompanyId, String bikeCompanyName, Bike[] bikes, int sum, int count) {this.bikeCompanyId = bikeCompanyId;this.bikeCompanyName = bikeCompanyName;this. bikes = bikes;this.sum = sum;this.count = count;}@Overridepublic void addBike() {System.out.println("请录入投放的数量:");int addNum = sc.nextInt();//判断如果超出范围就进行数组扩容if (sum + addNum > bikes.length) {System.out.println("空间不足,已按照配置进行扩容");bikes = Arrays.copyOf(bikes,bikes.length * 2 + addNum);}for(int i = 0;i < addNum ;i ++){bikes[sum] = new Bike(flagNum ++,bikeCompanyName+"单车",1,null);//公司bike数量自增sum ++;}System.out.println("投放"+addNum+" 辆单车成功");}@Overridepublic void selectBike() {System.out.print("单车序号 ");System.out.print("单车品牌 ");System.out.print("单车状态 ");System.out.println("借出时间 ");for(int j = 0 ;j < sum;j ++){System.out.print(bikes[j].getBikeId()+" |");System.out.print(bikes[j].getBikeName()+" |");if(bikes[j].getStatus() == 1){System.out.print("可借 |");}else{System.out.print("已借出 |");}System.out.println(bikes[j].getBorrowTime());}}@Overridepublic void borrowBike() {System.out.println(bikeCompanyName+"单车有共享单车如下");selectBike();System.out.println("请录入要借出的单车编号:");int bikeId = sc.nextInt();for(int i = 0 ; i < sum ;i ++){if(bikeId == bikes[i].getBikeId()){if (bikes[i].getStatus() != 0) {bikes[i].setStatus(0) ;count ++;//获取系统当前时间Date date = new Date();bikes[i].setStartTime(date.getTime());bikes[i].setBorrowTime(date.toString());System.out.println("您的借出时间为:"+date);System.out.println("借出"+bikeCompanyName+"单车公司下的<<"+bikeCompanyName+"单车"+bikes[i].getBikeId()+">>成功" );System.out.println("---------------------------------");return;}else {System.out.println("抱歉,该单车已借出");return;}}}System.out.println("抱歉,不存在该编号的单车");}@Overridepublic void backBike() {System.out.println("请录入要归还的编号:");int bikeId = sc.nextInt();for (int i = 0 ;i < sum; i++) {if (bikeId == bikes[i].getBikeId()) {if (bikes[i].getStatus() == 0) {bikes[i].setStatus(1);Date date = new Date();long endTime = date.getTime();long time = (endTime - bikes[i].getStartTime())/1000;System.out.println(endTime);System.out.println(time);System.out.println("您的归还时间为:" + date);System.out.println("您的借车时间为:"+ bikes[i].getBorrowTime());System.out.println("用车时间为<<"+ time+">>秒,需要支付"+time/60.0+"元");System.out.println("---------------------------------");return;}else {System.out.println("该单车未借出,不需要归还");return;}}}System.out.println("不存在该编号的单车");}@Overridepublic void delBike() {System.out.println(bikeCompanyName+"单车有共享单车如下:");selectBike();System.out.println("请输入要删除的单车编号:");int bikeId = sc.nextInt();for(int i = 0 ; i < sum;i ++){if(bikeId == bikes[i].getBikeId()){if(bikes[i].getStatus() == 1){//SUM-1防止越界//数组前移for(int j = i;j < sum - 1;j ++){bikes[i] = bikes[ i + 1];bikes[--sum] = null;}System.out.println("删除"+bikeCompanyName+"单车公司的"+bikeCompanyName + "单车"+ bikeId+"成功");return;}else{System.out.println("该单车已借出,不能删除");return;}}}System.out.println("此编号的单车不存在,无法删除");}//等级,赋值给一个数组,不要影响原数组//原数组的公司都有标号@Overridepublic void rank() {}
}
接口定义公司规则
package com.qy50gyk.edu.work.bikedemo;public interface BikeMethod {public abstract void addBike();public abstract void selectBike();public abstract void delBike();public abstract void borrowBike();public abstract void backBike();public abstract void rank ();}
管理系统类:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;public class BikeSystem {Scanner sc = new Scanner(System.in);BikeCompany[] bikeCompanies;String system_in ;int system_inNum;//初始化公司信息public void initial(){bikeCompanies = new BikeCompany[3];bikeCompanies[0] = new OfoBikeCompany(1001,"Ofo",new Bike[5],0,0);bikeCompanies[1] = new HaloBikeCompany(1002,"Halo",new Bike[5],0,0);bikeCompanies[2] = new MoBikeCompany(1003,"MoBike",new Bike[5],0,0);}public void menu(){initial();while (true) {System.out.println("欢迎使用共享单车系统");System.out.println("*********************************************************");System.out.println(" 1.投放Bike");System.out.println(" 2.查看Bike");System.out.println(" 3.删除Bike");System.out.println(" 4.借出Bike");System.out.println(" 5.归还Bike");System.out.println(" 6.Bike排行榜");System.out.println(" 7.退 出");System.out.println("*********************************************************");System.out.println("请选择:");//键入数据是否合法loop1:while(true){system_in = sc.next();switch (system_in){case "1":system_inNum = 1;tianjia();break loop1;case "2":system_inNum = 2;chakan();break loop1;case "3":system_inNum = 3;shanchu();break loop1;case "4" :system_inNum = 4;jiechu();break loop1;case "5":system_inNum = 5;guihuan();break loop1;case "6":system_inNum = 6;rank();break loop1;case "7":system_inNum = 7;System.out.println("感谢使用本系统");System.exit(0);break loop1;default:System.out.println("数据非法,请重新输入:");}}}}private void guihuan() {System.out.println("----->5.归还Bike");System.out.println("1.ofo单车");System.out.println("2.halo单车");System.out.println("3.mobike单车");System.out.println("选择要操作的单车品牌:");system_in = sc.next();loop2:while (true) {switch (system_in){case"1":bikeCompanies[0].backBike();retMenu();//判断用户输入break loop2;case"2":bikeCompanies[1].backBike();retMenu();break loop2;case"3":bikeCompanies[2].backBike();retMenu();break loop2;default:System.out.println("数据非法,请重新输入");}}}private void rank() {//用临时数组进行排行榜System.out.println("6.---->Bike排行榜");BikeCompany[] tempBikeCompany =Arrays .copyOf(bikeCompanies,bikeCompanies.length);Arrays.sort(tempBikeCompany, new Comparator<BikeCompany>() {@Overridepublic int compare(BikeCompany o1, BikeCompany o2) {return o1.getCount() - o2.getCount();}});System.out.print("公司名称");System.out.println("\t\t借车次数");for(int i = 0 ;i < bikeCompanies.length;i ++){System.out.print(bikeCompanies[i].getBikeCompanyName()+ "单车\t\t");System.out.println(bikeCompanies[i].getCount());}}private void shanchu() {System.out.println("------> 3.删除Bike");System.out.println("1.ofo单车");System.out.println("2.halo单车");System.out.println("3.mobike单车");System.out.println("选择要操作的单车品牌:");loop2:while (true) {system_in = sc.next();switch (system_in){case"1":bikeCompanies[0].delBike();retMenu();//判断用户输入break loop2;case"2":bikeCompanies[1].delBike();retMenu();break loop2;case"3":bikeCompanies[2].delBike();retMenu();break loop2;default:System.out.println("数据非法,请重新输入");}}}private void chakan() {System.out.println("-----> 2.查看Bike");for(int i = 0 ;i < bikeCompanies.length;i ++){System.out.print("| 公司序号 | ");System.out.print("| 公司名称 | ");System.out.print("| 单车数量 | ");System.out.println("| 单车借出次数 |");System.out.print("| "+bikeCompanies[i].getBikeCompanyId()+" | ");System.out.print("| "+bikeCompanies[i].getBikeCompanyName()+" | ");System.out.print("| "+bikeCompanies[i].getSum()+" | ");System.out.println("| "+bikeCompanies[i].getCount()+" | ");System.out.println("*********************************************************");bikeCompanies[i].selectBike();}retMenu();}private void tianjia() {System.out.println("------> 1.投放Bike");System.out.println("1.ofo单车");System.out.println("2.halo单车");System.out.println("3.mobike单车");System.out.println("选择要投放的单车品牌:");loop2:while (true) {system_in = sc.next();switch (system_in){case"1":bikeCompanies[0].addBike();retMenu();//判断用户输入break loop2;case"2":bikeCompanies[1].addBike();retMenu();break loop2;case"3":bikeCompanies[2].addBike();retMenu();break loop2;default:System.out.println("数据非法,请重新输入");}}}private void jiechu() {System.out.println("----->4.借出Bike");System.out.println("1.ofo单车");System.out.println("2.halo单车");System.out.println("3.mobike单车");System.out.println("选择要操作的单车品牌:");system_in = sc.next();loop2:while (true) {switch (system_in){case"1":bikeCompanies[0].borrowBike();retMenu();//判断用户输入break loop2;case"2":bikeCompanies[1].borrowBike();retMenu();break loop2;case"3":bikeCompanies[2].borrowBike();retMenu();break loop2;default:System.out.println("数据非法,请重新输入");}}}public void retMenu(){System.out.println("输入0返回:");while (true) {system_in = sc.next();if(system_in.equals("0")){break ;}else{System.out.println("输入数据非法,请重新输入:");}}}
}
三个公司子类
public class HaloBikeCompany extends BikeCompany {public HaloBikeCompany() {}public HaloBikeCompany(int bikeCompanyId, String bikeCompanyName, Bike[] bikes, int sum, int count) {super(bikeCompanyId, bikeCompanyName, bikes, sum, count);}}public class MoBikeCompany extends BikeCompany {public MoBikeCompany() {}public MoBikeCompany(int bikeCompanyId, String bikeCompanyName, Bike[] bikes, int sum, int count) {super(bikeCompanyId, bikeCompanyName, bikes, sum, count);}
}public class OfoBikeCompany extends BikeCompany {public OfoBikeCompany() {}public OfoBikeCompany(int bikeCompanyId, String bikeCompanyName, Bike[] bikes, int sum, int count) {super(bikeCompanyId, bikeCompanyName, bikes, sum, count);}
}
6.测试类
public class Test {public static void main(String[] args) {new BikeSystem().menu();}
}