0


【JAVASE开发】带你零基础学JAVA项目(二嗨租车项目篇)

哈喽~大家好呀,时隔一个月,这次的一个小项目来喽,这次的 “二嗨租车项目” 使用的是的 oracle + JDBC + 集合 + 面向对象 + 分层思想(MVC),接下来就来看看吧。

🥇个人主页:个人主页​​​​​

🥈 系列专栏:【JAVASE开发】

🥉与这篇相关的文章:
【JAVASE开发】带你零基础学JAVA项目(学生管理系统篇)【JAVASE开发】带你零基础学JAVA项目(学生管理系统篇)_程序猿追的博客-CSDN博客

项目需求细明

首先是用户登入界面,账号登录总界面部分分为登入与注册选择,账号分为管理员和普通用户,如果是管理员(admin)那么进入到另一个界面(与普通用户不同可以对汽车的一些信息进行修改,eg:上架汽车与删除汽车信息等操作),如果是普通用户的话,也是进入到不一样的界面(对汽车进行租借与还车支付金额等操作)

结构思想

三层架构设计思想
通常意义上的三层架构就是将整个业务应用划分为:表现层、业务逻辑层、数据访问层。区分层次的目的即为了“高内聚,低耦合”的思想。
表现层(View):通俗讲就是展现给用户的界面,即用户在使用一个系统的时候他的所见所得。
业务逻辑层(Control):针对具体问题的操作,也可以说是对数据层的操作,对数据业务逻辑处理。   
数据访问层(Model):该层所做事务直接操作数据库,针对数据的增添、删除、修改、更新、查找等。

结构思路

在 V 层输入访问条件(操作1),条件传到 C 层(操作2),然后 C 层调用 M 层里面的方法直接访问数据库(DB)(操作3),然后 DB 返回的结果集转到 M 层(操作4),M 返回到C层(操作5),最后回到 V 层(操作6),然后依次在界面上显示出来.

** 工具:**IntelliJ IDEA 2021.3 + oracle 12e + PLSQL Developer 13 (64 bit)

数据库部分:数据表(T_BRAND(品牌表)、汽车表(T_CAR)、类型表(T_CATEGORY)、租车记录表(T_RECORD)、用户表(T_USER))

效果展示

实现代码

首先先完成普通用户,完成之后直接复制到管理员在进行修改,这里就写一个查看所有汽车信息的操作吧。

View层

  1. package com.itxzw.view.main;
  2. public class CarGeneralViewInterface {
  3. public void showCarGeneralViewInterface(){
  4. System.out.println("----------------------------------------------------------------------------");
  5. System.out.println("登录成功!你是普通用户,请选择服务:");
  6. System.out.println("1、查看所有汽车");
  7. System.out.println("2、按照价格来升序或降序查询汽车");
  8. System.out.println("3、按照类别查看汽车");
  9. System.out.println("4、按照品牌查看汽车");
  10. System.out.println("5、查看本人所有租车记录");
  11. System.out.println("6、租车");
  12. System.out.println("7、还车");
  13. System.out.println("8、退出");
  14. System.out.println("----------------------------------------------------------------------------");
  15. }
  16. public void showAdminCarGeneralViewInterface(){
  17. System.out.println("----------------------------------------------------------------------------");
  18. System.out.println("登录成功!你是管理员,请选择服务:");
  19. System.out.println("1、查看所有汽车信息");
  20. System.out.println("2、根据指定编号查看汽车信息");
  21. System.out.println("3、添加汽车");
  22. System.out.println("4、修改汽车信息");
  23. System.out.println("5、查看所有用户全部租车记录");
  24. System.out.println("6、查看指定用户租车记录");
  25. System.out.println("7、查看指定汽车租车记录");
  26. System.out.println("8、退出");
  27. System.out.println("----------------------------------------------------------------------------");
  28. }
  29. public void LoginAndRegister(){
  30. System.out.println("----------------------------------------------------------------------------");
  31. System.out.println("尊敬的用户,您好!!");
  32. System.out.println("欢迎使用二嗨租车系统,请输入数字来进行操作:");
  33. System.out.println("说明:输入数字1是进行登录,输入数字2进行注册");
  34. System.out.println("1、登录");
  35. System.out.println("2、注册");
  36. System.out.println("----------------------------------------------------------------------------");
  37. }
  38. }

TextMain.java

  1. carGeneralViewInterface.showCarGeneralViewInterface();
  2. System.out.println("请输入数字来进行操作:");
  3. int scan = Scan.scan.nextInt();
  4. if (scan == 1) {
  5. System.out.println("所有的汽车信息是:");
  6. carControl.viewAllCars();
  7. }

C层

  1. // 查看所有汽车
  2. public void viewAllCars() throws SQLException {
  3. carService.viewAllCars();
  4. }

接口

  1. public void viewAllCars() throws SQLException;

相对应的实现方法

  1. @Override
  2. public void viewAllCars() throws SQLException {
  3. conn = JdbcUtil.getConnection();
  4. stmt = conn.createStatement();
  5. String sql = "select * from T_CAR";
  6. rs = stmt.executeQuery(sql);
  7. ArrayList<Car> list = new ArrayList<Car>();
  8. while (rs.next()) {
  9. Integer id = ((BigDecimal) rs.getObject("id")).intValue();
  10. String Car_Number = rs.getString("Car_Number");
  11. Integer Brand_Id = ((BigDecimal) rs.getObject("Brand_Id")).intValue();
  12. String model = rs.getString("model");
  13. String Color = rs.getString("Color");
  14. Integer Category_Id = ((BigDecimal) rs.getObject("Category_Id")).intValue();
  15. String T_Comments = rs.getString("T_Comments");
  16. Double price = rs.getDouble("price");
  17. Double rent = rs.getDouble("rent");
  18. Integer status = ((BigDecimal) rs.getObject("status")).intValue();
  19. Integer useable = ((BigDecimal) rs.getObject("useable")).intValue();
  20. car = new Car(id, Car_Number, Brand_Id, model, Color, Category_Id, T_Comments, price, rent, status, useable);
  21. list.add(car);
  22. }
  23. for (int i = 0; i < list.size(); i++) {
  24. System.out.println(list.get(i));
  25. }
  26. }

Car.java 实体类

  1. package com.itxzw.client.model;
  2. public class Car {
  3. private int id;
  4. private String Car_Number;
  5. private int Brand_Id;
  6. private String model;
  7. private String Color;
  8. private int Category_Id;
  9. private String T_Comments;
  10. private double price;
  11. private double rent;
  12. private int status;
  13. private int useable;
  14. public Car() {
  15. }
  16. public Car(int id, String car_Number, int brand_Id, String model, String color, int category_Id, String t_Comments, double price, double rent, int status, int useable) {
  17. this.id = id;
  18. Car_Number = car_Number;
  19. Brand_Id = brand_Id;
  20. this.model = model;
  21. Color = color;
  22. Category_Id = category_Id;
  23. T_Comments = t_Comments;
  24. this.price = price;
  25. this.rent = rent;
  26. this.status = status;
  27. this.useable = useable;
  28. }
  29. public int getId() {
  30. return id;
  31. }
  32. public void setId(int id) {
  33. this.id = id;
  34. }
  35. public String getCar_Number() {
  36. return Car_Number;
  37. }
  38. public void setCar_Number(String car_Number) {
  39. Car_Number = car_Number;
  40. }
  41. public int getBrand_Id() {
  42. return Brand_Id;
  43. }
  44. public void setBrand_Id(int brand_Id) {
  45. Brand_Id = brand_Id;
  46. }
  47. public String getModel() {
  48. return model;
  49. }
  50. public void setModel(String model) {
  51. this.model = model;
  52. }
  53. public String getColor() {
  54. return Color;
  55. }
  56. public void setColor(String color) {
  57. Color = color;
  58. }
  59. public int getCategory_Id() {
  60. return Category_Id;
  61. }
  62. public void setCategory_Id(int category_Id) {
  63. Category_Id = category_Id;
  64. }
  65. public String getT_Comments() {
  66. return T_Comments;
  67. }
  68. public void setT_Comments(String t_Comments) {
  69. T_Comments = t_Comments;
  70. }
  71. public double getPrice() {
  72. return price;
  73. }
  74. public void setPrice(double price) {
  75. this.price = price;
  76. }
  77. public double getRent() {
  78. return rent;
  79. }
  80. public void setRent(double rent) {
  81. this.rent = rent;
  82. }
  83. public int getStatus() {
  84. return status;
  85. }
  86. public void setStatus(int status) {
  87. this.status = status;
  88. }
  89. public int getUseable() {
  90. return useable;
  91. }
  92. public void setUseable(int useable) {
  93. this.useable = useable;
  94. }
  95. @Override
  96. public String toString() {
  97. return "Car{" +
  98. "id=" + id +
  99. ", Car_Number='" + Car_Number + '\'' +
  100. ", Brand_Id=" + Brand_Id +
  101. ", model='" + model + '\'' +
  102. ", Color='" + Color + '\'' +
  103. ", Category_Id=" + Category_Id +
  104. ", T_Comments='" + T_Comments + '\'' +
  105. ", price=" + price +
  106. ", rent=" + rent +
  107. ", status=" + status +
  108. ", useable=" + useable +
  109. '}';
  110. }
  111. }

运行一下没有问题,其他的都差不多,改一下 sql 语句操作。

不积跬步无以至千里,趁年轻,使劲拼,给未来的自己一个交代!向着明天更好的自己前进吧!


本文转载自: https://blog.csdn.net/aasd23/article/details/126076295
版权归原作者 程序猿追 所有, 如有侵权,请联系我们删除。

“【JAVASE开发】带你零基础学JAVA项目(二嗨租车项目篇)”的评论:

还没有评论