服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现简单租车系统

java实现简单租车系统

2021-07-13 12:25浅然_ Java教程

这篇文章主要为大家详细介绍了java实现简单租车系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现租车系统demo,供大家参考,具体内容如下

这也是参考了mooc上的一个基础项目,所以拿来写一下。不过我的demo肯定有不好或者错误的地方,欢迎指出

1、项目功能/需求

a、展示所有可租车辆
b、选择车型、租车量
c、展示租车清单,包含:总金额、总载货量、总载客量

2、项目系统uml图

java实现简单租车系统

3、项目源码demo

car类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package car.rental;
 
public class car {
 private string name;
 private int rent;
 private int passenger;
 private double volume;
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public int getrent() {
 return rent;
 }
 public void setrent(int rent) {
 this.rent = rent;
 }
 public int getpassenger() {
 return passenger;
 }
 public void setpassenger(int passenger) {
 this.passenger = passenger;
 }
 public double getvolume() {
 return volume;
 }
 public void setvolume(double volume) {
 this.volume = volume;
 }
 
}

truck类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package car.rental;
 
public class truck extends car {
 private string name;
 private int rent;
 private double volume;
 public truck(string name,int rent,double volume)
 {
 this.name=name;
 this.rent=rent;
 this.volume=volume;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public int getrent() {
 return rent;
 }
 public void setrent(int rent) {
 this.rent = rent;
 }
 public double getvolume() {
 return volume;
 }
 public void setvolume(double volume) {
 this.volume = volume;
 }
 
}

coach类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package car.rental;
 
public class coach extends car {
 
 private string name;
 private int rent;
 private int passenger;
 public coach(string name,int rent,int passenger) {
 this.name=name;
 this.passenger=passenger;
 this.rent=rent;
 }
 public int getpassenger() {
 return passenger;
 }
 public void setpassenger(int passenger) {
 this.passenger = passenger;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public int getrent() {
 return rent;
 }
 public void setrent(int rent) {
 this.rent = rent;
 }
}

pickup类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package car.rental;
 
public class pickup extends car {
 private string name;
 private int rent;
 private int passenger;
 private double volume;
 public pickup(string name,int rent,int passenger,double volume)
 {
 this.name=name;
 this.passenger=passenger;
 this.rent=rent;
 this.volume=volume;
 }
 public int getpassenger() {
 return passenger;
 }
 public void setpassenger(int passenger) {
 this.passenger = passenger;
 }
 public double getvolume() {
 return volume;
 }
 public void setvolume(double volume) {
 this.volume = volume;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public int getrent() {
 return rent;
 }
 public void setrent(int rent) {
 this.rent = rent;
 }
 
}

测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package car.rental;
 
import java.util.scanner;
 
public class main_car {
 
 public static void main(string[] args) {
 // todo auto-generated method stub
 /*****创建车对象*******/
 /*car tru=new truck("拉丝货车",200,1.1);
 car coa=new coach("法法客车",150,20);
 car pic=new pickup("无名皮卡车",300,4,0.5);*/
 car[] allcar= {new truck("拉丝货车",200,1.1),new coach("法法客车",150,20),
   new pickup("无名皮卡车",300,4,0.5)};
 
 system.out.println("请问您是否要租车:1.是 2.否");
 scanner scan=new scanner(system.in);
 string input=scan.next();
 if(input.equals("1"))
 {
  int all_rent = 0;//总租金
  int all_passenger=0;//总载客
  double all_volume=0.0;//总载货
  system.out.println("您可租车的类型及其价目");
  system.out.println("序号\t汽车名称\t租金\t容量\t");
  for(int i=0;i<allcar.length;i++)
  {
  if(allcar[i] instanceof truck)
  {
   system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载货:"+allcar[i].getvolume());
  }
  if(allcar[i] instanceof coach)
  {
   system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载客:"+allcar[i].getpassenger());
  }
  if(allcar[i] instanceof pickup)
  {
   system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载客:"+allcar[i].getpassenger()+"载货:"+allcar[i].getvolume());
  }
  }
  system.out.println("请输入您要租车的数量");
  int sum=scan.nextint();
  int car_order;
  for(int j=0;j<sum;j++)
  {
  system.out.print("你要租的第"+(j+1)+"辆车的序号是:");
  car_order=scan.nextint();
  system.out.println();
  if(allcar[car_order-1] instanceof truck)
  {
   all_rent+=allcar[car_order-1].getrent();
   all_volume+=allcar[car_order-1].getvolume();
  }
  if(allcar[car_order-1] instanceof coach)
  {
   all_rent+=allcar[car_order-1].getrent();
   all_passenger+=allcar[car_order-1].getpassenger();
  }
  if(allcar[car_order-1] instanceof pickup)
  {
   all_rent+=allcar[car_order-1].getrent();
   all_passenger+=allcar[car_order-1].getpassenger();
   all_volume+=allcar[car_order-1].getvolume();
  }
  }
  system.out.println("您的总租金是:"+all_rent);
  system.out.println("您的总载货是:"+all_volume);
  system.out.println("您的总载客是:"+all_passenger);
 }
 
 }
 
}

4、项目命令行演示结果

java实现简单租车系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/w_linux/article/details/79181092

延伸 · 阅读

精彩推荐