汽车租赁系统的简单实现

来源:互联网 发布:手机淘宝旺旺号是什么 编辑:程序博客网 时间:2024/05/01 16:06
java基础,简单实现汽车租赁功能,适合初学者。实现选车 租车 租赁天数 计算价格功能。

机动车类 我这里把它定义为抽象类,因为我要实现一个ClacRent的方法,我这里还没有想好要怎么实现,所以就先写着一个抽象方法。
  1. public abstract class MotoVehicle {
  2. private String no;
  3. private String brand;
  4. private String color;
  5. private int mileage;
  6. public MotoVehicle(){}
  7. public MotoVehicle(String no, String brand, String color, int mileage) {
  8. super();
  9. this.no = no;
  10. this.brand = brand;
  11. this.color = color;
  12. this.mileage = mileage;
  13. }
  14. abstract int ClacRent(/*int days*/);
  15. public String getNo() {
  16. return no;
  17. }
  18. public void setNo(String no) {
  19. this.no = no;
  20. }
  21. public String getBrand() {
  22. return brand;
  23. }
  24. public void setBrand(String brand) {
  25. this.brand = brand;
  26. }
  27. public String getColor() {
  28. return color;
  29. }
  30. public void setColor(String color) {
  31. this.color = color;
  32. }
  33. public int getMileage() {
  34. return mileage;
  35. }
  36. public void setMileage(int mileage) throws Exception {
  37. if(mileage<0){
  38. mileage = 10;
  39. throw new Exception("里程必须大于0");
  40. }
  41. this.mileage = mileage;
  42. }
  43. }

Bus类
  1. public class Bus extends MotoVehicle {
  2. private int seatCount;
  3. public Bus(String no, String brand, String color, int mileage, int seatCount) {
  4. super(no,brand,color,mileage);
  5. this.seatCount = seatCount;
  6. }
  7. public int getSeatCount() {
  8. return seatCount;
  9. }
  10. public void setSeatCount(int seatCount) {
  11. this.seatCount = seatCount;
  12. }
  13. @Override
  14. int ClacRent(/*int days*/) {
  15. int rent = 0;
  16. if (getSeatCount() <= 16) {
  17. rent = 800;
  18. } else {
  19. rent = 1500;
  20. }
  21. return rent;
  22. }
  23. }

Car类
  1. public class Car extends MotoVehicle {
  2. private String type;
  3. public Car(String no, String brand, String color, int mileage, String type) {
  4. super(no,brand,color,mileage);
  5. this.type = type;
  6. }
  7. public String getType() {
  8. return type;
  9. }
  10. public void setType(String type) {
  11. this.type = type;
  12. }
  13. @Override
  14. int ClacRent(/*int days*/) {
  15. int rent = 0;
  16. if ("别克商务GL8".equals(getType())) {
  17. rent = 600;
  18. } else if ("宝马550i".equals(getType())) {
  19. rent = 500;
  20. } else if ("别克林荫大道".equals(getType())) {
  21. rent = 300;
  22. }
  23. return rent;
  24. }
  25. }

Main方法:有数据初始化,还有租车模块的实现。
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. //初始化
  5. Car[] c = new Car[3];
  6. Bus[] b = new Bus[4];
  7. c[0] = new Car("粤A10001", "别克", "黑", 0, "别克商务GL8");
  8. c[1] = new Car("粤A10002", "宝马", "蓝", 0, "宝马550i");
  9. c[2] = new Car("粤A10003", "别克", "红", 0, "别克林荫大道");
  10. b[0] = new Bus("粤A10004", "金杯", "黄", 0, 16);
  11. b[1] = new Bus("粤A10005", "金杯", "金", 0, 34);
  12. b[2] = new Bus("粤A10006", "金龙", "白", 0, 16);
  13. b[3] = new Bus("粤A10007", "金龙", "银", 0, 34);
  14. System.out.println("----欢迎租车----\n");
  15. for(int i = 0; i<c.length; i++) {
  16. System.out.println("序号:"+(i+1)+", 品牌:"+c[i].getBrand()
  17. +", 车型:"+c[i].getType()+", 租金:"+c[i].ClacRent(/*i*/));
  18. }
  19. for(int i = 0; i<b.length; i++) {
  20. System.out.println("序号:"+(i+4)+", 品牌:"+b[i].getBrand()+", 座位数:"+b[i].getSeatCount()+", 租金:"+b[i].ClacRent(/*i*/));
  21. }
  22. //租车模块
  23. byte[] byts = new byte[1024];
  24. int k = 0;
  25. int num = 0;
  26. int d = 0;
  27. //int sum1 = 0;int sum2 = 0;int sum3 = 0;int sum4 = 0;int sum5 = 0; int sum6 = 0; int sum7 =0;
  28. int sum[] = {0,0,0,0,0,0,0};
  29. int sumRent = 0;
  30. Scanner sc = new Scanner(System.in);
  31. while (true) {
  32. try {
  33. System.out.println("\n\n是否需要租车(0-是,其他键退出):");
  34. int n = System.in.read(byts);
  35. String s = new String(byts, 0, n);
  36. if (s.trim().length() == 0) {
  37. continue;
  38. }
  39. int input = Integer.parseInt(s.trim()); // 强制转换为需要的类型
  40. if (input != 0) {
  41. System.out.println("退出!");
  42. return;
  43. }
  44. do{
  45. System.out.print("请选择您需要的车型(选序号):");
  46. n = System.in.read(byts);
  47. s = new String(byts,0,n);
  48. int key = Integer.parseInt(s.trim());
  49. switch(key) {
  50. case 1:
  51. System.out.print("您选择的是"+c[0].getType()+"\n请输入租赁数量:");
  52. num = sc.nextInt();
  53. System.out.print("请输入租赁天数:");
  54. d = sc.nextInt() * c[0].ClacRent(/*0*/);
  55. sum[0] = num * d;
  56. System.out.print("价格为:"+sum[0] );
  57. break;
  58. case 2:
  59. System.out.print("您选择的是"+c[1].getType()+"\n请输入租赁数量:");
  60. num = sc.nextInt();
  61. System.out.print("请输入租赁天数:");
  62. d = sc.nextInt() * c[1].ClacRent(/*0*/);
  63. sum[1] = num * d;
  64. System.out.print("价格为:"+sum[1] );
  65. break;
  66. case 3:
  67. System.out.print("您选择的是"+c[2].getType()+"\n请输入租赁数量:");
  68. num = sc.nextInt();
  69. System.out.print("请输入租赁天数:");
  70. d = sc.nextInt() * c[2].ClacRent(/*0*/);
  71. sum[2] = num * d;
  72. System.out.println("价格为:"+sum[2] );
  73. break;
  74. case 4:
  75. case 5:
  76. case 6:
  77. case 7:
  78. System.out.println("暂时没有此类车型!");
  79. break;
  80. default:
  81. System.out.println("输入有误!");
  82. break;
  83. }
  84. System.out.println("\n如果还有租赁需求请按0,按任意键提交订单!");
  85. k = sc.nextInt();
  86. }while(k == 0);
  87. sumRent = sum[0]+sum[1]+sum[2];
  88. System.out.println("请您支付"+sumRent+"元,凭票出门右转提车,欢迎再次光临!");
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
  94. }
这就是简单的汽车租赁系统,希望对初学者有帮助。有问题的朋友可以留言!


日后有时间我会更新如何用SSM框架实现一些简单的项目,如博客平台,管理系统等。

0 0
原创粉丝点击