• Java租车系统


    1. package 项目3;
    2. public abstract class MotoVehicle {
    3. private String numberPlate;//车牌号
    4. private String brand;//牌子
    5. private double dailyRent;//租金
    6. public String getVehicleId;
    7. public MotoVehicle() {
    8. super();
    9. }
    10. public MotoVehicle(String numberPlate, String brand, double dailyRent) {
    11. super();
    12. this.numberPlate = numberPlate;
    13. this.brand = brand;
    14. this.dailyRent = dailyRent;
    15. }
    16. public String getNumberPlate() {
    17. return numberPlate;
    18. }
    19. public void setNumberPlate(String numberPlate) {
    20. this.numberPlate = numberPlate;
    21. }
    22. public String getBrand() {
    23. return brand;
    24. }
    25. public void setBrand(String brand) {
    26. this.brand = brand;
    27. }
    28. public double getDailyRent() {
    29. return dailyRent;
    30. }
    31. public void setDailyRent(double dailyRent) {
    32. this.dailyRent = dailyRent;
    33. }
    34. public abstract double calRent(int days);//计算租金
    35. }
    1. package 项目3;
    2. public class MotoOperation {
    3. public MotoVehicle motos[] = new MotoVehicle[8];
    4. public void init() {
    5. motos[0] = new Car("京NY28588", "宝马", 800, "X6");
    6. motos[1] = new Car("京CNY3284", "宝马", 600, "550i");
    7. motos[2] = new Car("京NT37465", "别克", 300, "林荫大道");
    8. motos[3] = new Car("京NT96968", "别克", 600, "GL8");
    9. motos[4] = new Bus("京6566754", "金杯", 800,16 );
    10. motos[5] = new Bus("京8696997", "金龙", 800,16 );
    11. motos[6] = new Bus("京9696996", "金杯", 1500,34 );
    12. motos[7] = new Bus("京8696998", "金龙", 1500,34 );
    13. }
    14. public MotoVehicle motoLeaseOut(String brand, String model, int seats) {
    15. MotoVehicle moto = null;
    16. for (MotoVehicle mymoto : motos) {//增强for循环
    17. if (mymoto instanceof Car) {//instanceof防止向下转型出问题
    18. Car car = (Car) mymoto;//向下转型,确保可以使用子类特有属性
    19. if (car.getBrand().equals(brand) && car.getModel().equals(model)) {
    20. moto = car;
    21. break;
    22. }
    23. } else {
    24. Bus bus = (Bus) mymoto;
    25. if (bus.getBrand().equals(brand) && bus.getSeats() == seats) {
    26. moto = bus;
    27. break;
    28. }
    29. }
    30. }
    31. return moto;
    32. }
    33. }
    1. package 项目3;
    2. public class Bus extends MotoVehicle {
    3. private int seats;//座位
    4. public Bus() {
    5. super();
    6. }
    7. public Bus(String numberPlate, String brand, double dailyRent, int seats) {
    8. super(numberPlate, brand, dailyRent);
    9. this.seats = seats;
    10. }
    11. public int getSeats() {
    12. return seats;
    13. }
    14. public void setSeats(int seats) {
    15. this.seats = seats;
    16. }
    17. public double calRent(int days){//计算租金
    18. double price=this.getDailyRent()*days;//租金计算公式
    19. if (days>0&&days<3){
    20. price*=1;
    21. } else if (days>=3&&days<7){
    22. price*=0.9;
    23. } else if (days>=7&&days<30) {
    24. price*=0.8;
    25. } else if (days>=30&&days<150) {
    26. price*=0.7;
    27. } else if (days>=150) {
    28. price*=0.6;
    29. }
    30. return price;
    31. }
    32. }
    1. package 项目3;
    2. public class Car extends MotoVehicle {
    3. private String model;
    4. public Car() {
    5. super();
    6. }
    7. public Car(String numberPlate, String brand, double dailyRent, String model) {
    8. super(numberPlate, brand, dailyRent);
    9. this.model = model;
    10. }
    11. public String getModel() {
    12. return model;
    13. }
    14. public void setModel(String model) {
    15. this.model = model;
    16. }
    17. public double calRent(int days){//计算租金
    18. double price=this.getDailyRent()*days;//租金计算公式
    19. if(days>0&&days<=7){
    20. price*=1;
    21. }else if(days>7&&days<=30){
    22. price*=0.9;
    23. }else if(days>30&&days<=150){
    24. price*=0.8;
    25. } else if (days>150) {
    26. price*=0.7;
    27. }
    28. return price;
    29. }
    30. }
    1. package 项目3;
    2. import java.util.Scanner;
    3. public class RentMgrSys {
    4. public static void main(String[] args) {
    5. Scanner sc=new Scanner(System.in);
    6. MotoOperation motoMgr=new MotoOperation();
    7. motoMgr.init();//初始化
    8. MotoVehicle moto=null;
    9. System.out.println("*****欢迎光临租赁公司*****");
    10. System.out.print("请选择你要租赁的汽车类型:");
    11. int motoType=sc.nextInt();
    12. String brand="";
    13. String type="";
    14. int seats=0;
    15. if (motoType==1){
    16. System.out.println("请选择你要租赁的汽车品牌:1、别克 2、宝马");
    17. int choose2=sc.nextInt();
    18. if(choose2==1){
    19. brand="别克";
    20. System.out.println("请选择你要租赁的汽车类型:1、林荫大道2、GL8");
    21. type=(sc.nextInt()==1)?"林荫大道":"GL8";
    22. }else if (choose2==2){
    23. brand="宝马";
    24. System.out.println("请选择你要租赁的汽车类型:1、X6 2、550i");
    25. type=(sc.nextInt()==1)?"X6":"550i";
    26. }
    27. } else if (motoType==2) {
    28. //租车
    29. type="";
    30. System.out.println("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
    31. brand=(sc.nextInt()==1)?"金龙":"金杯";
    32. System.out.println("请选择你要租赁的汽车座位数:1、16座2、34座");
    33. seats=(sc.nextInt()==1)?16:34;
    34. }
    35. moto =motoMgr.motoLeaseOut(brand,type,seats);
    36. System.out.println("请选择你要租赁的天数:");
    37. int days=sc.nextInt();
    38. double money=moto.calRent(days);//计算租金
    39. System.out.println("租车成功,请拿车牌号去取车:"+moto.getVehicleId);
    40. System.out.println("需要支付:"+money+"元");
    41. }
    42. }

  • 相关阅读:
    【数据结构】二叉树
    ssm毕设项目学生公寓信息管理系统4g400(java+VUE+Mybatis+Maven+Mysql+sprnig)
    第四篇文章:Object类(equals方法和hashcode方法)
    JS懒加载模块封装
    【linux】nmon 工具使用
    设计模式——工厂模式
    浅析Java中的LinkedList和ArrayList特点和底层
    关于malloc源码中的bin_at宏定义的个人见解
    02 线程安全问题
    CrossOver22全新版功能简介 免费mac虚拟机工具
  • 原文地址:https://blog.csdn.net/m0_58295980/article/details/126272476