迷你共享单车

news/2024/11/2 9:19:16/

思路分析

面向对象思维,三个公司当作子类,创建公司父类,创建接口规则

主要是思想,刚开始我是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;}
}
  1. 单车公司类

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() {}
}
  1. 接口定义公司规则

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 ();}
  1. 管理系统类:

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("输入数据非法,请重新输入:");}}}
}
  1. 三个公司子类

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();}
}


http://www.ppmy.cn/news/118641.html

相关文章

主机与台式计算机的关系,迷你主机和笔记本电脑、台式机的区别

【PConline 资讯】迷你主机(mini PC)可以这么来理解&#xff0c;比起笔记本电脑来说&#xff0c;没有光驱、扬声器和显示屏&#xff0c;一般来说需要外接音响设备和显示设备&#xff1b;比起台式机来说&#xff0c;性能应该会稍逊一筹&#xff0c;因为目前行业内的迷你主机并没…

Leaflat迷你小地球仪

一、效果展示 二、代码 根据leaflet-globeminimap模块化改写leaflet-globeminimap.js // L, d3, topojson import * as L from "leaflet"; import * as d3 from "d3"; import * as topojson from "topojson"; import { wordJson } from ".…

新手怎样自己DIY一套音质不错的音响?

看你预算。建议还是先玩成品箱子&#xff0c;知道了好的声音&#xff0c;才能在DIY的时候知道怎么调成好听的声音。 如果DIY的话&#xff0c;也是种玩法&#xff0c;建议如下配置&#xff1a;成品3寸小箱空箱&#xff1b;吸音棉&#xff1b;惠威全频3寸喇叭&#xff1b;接线柱…

迷你播放器

这是一个支持多种视频格式&#xff0c;支持多个1~9个播放窗口的播放器。 屌炸天的播放器。 下载地址&#xff1a;迷你播放器-C工具类资源-CSDN下载

迷你小功能之通讯录

文章目录 contact.h 类型、函数声明&#xff1b;头文件包含contact.c 功能实现mail.c 通讯录逻辑 contact.h 类型、函数声明&#xff1b;头文件包含 #pragma once #include <string.h> #include <stdio.h> #include <assert.h> #include <stdlib.h>//…

自制音响系统

这件事大概在一八年四月份就开始了&#xff0c;只不过最近才把它完成了&#xff0c;也算达到了自己满意的程度。 下面主要介绍一下它的主要组成部分。因为是拼装&#xff0c;而且原理也不复杂&#xff0c;所以就不详细介绍它的细节和制作过程了。想交流的话可以留言&#xff0c…

基于Arduino的迷你电子琴

前言 一直以来都没有系统地学过微控制器&#xff0c;使用 Arduino 时&#xff0c;也是通过现成的制作和代码进行修改&#xff0c;东一榔头西一锤&#xff0c;很多基础的东西并不了解。 前段时间&#xff0c;搜索 matlab 的教程时&#xff0c;知道了台大生机系的郭彦甫老师&am…

智能家居控制系统MECOOL KA1智能音响

MECOOL KA1是智能音箱和4K安卓电视流媒体设备的结合。该设备采用Amlogic S905Y4 SoC四核ARM Cortex -A35处理器&#xff0c;支持远场语音和免提电视控制。 MECOOL KA1通过ART&#xff08;谷歌智能语音测试&#xff09; ART 旨在验证集成了 Google 助理的设备的助理功能。要被…