好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现汽车租赁系统

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

?

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

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

//车类

public abstract class vehicle {

  //车牌号  品牌  日租金

  private string id;

  private string brand;

  private int perrent;

 

  public vehicle(){}

  //vehicle的带参构造方法

  public vehicle(string id, string brand, int perrent) {

  this .id = id;

  this .brand = brand;

  this .perrent = perrent;

  }

 

  public void setid(string id){

  this .id=id ;

  }

  public string getid(){

  return id;

  }

 

  public void setbrand(string brand){

  this .brand=brand;

  }

  public string getbrand(){

  return brand;

  }

 

  public void setperrent( int perrent){

  this .perrent=perrent;

  }

  public int getperrent(){

  return perrent;

  }

  //抽象方法计算租金

  public abstract double calcrent( int days);

 

}

 

//轿车类

public class car extends vehicle{

  //型号

  private string type;

  public void settype(string type){

  this .type=type;

  }

  public string gettype(){

  return type;

  }

 

  public car(){}

  //car的带参构造方法

  public car(string id, string brand, int perrent,string type) {

  super (id,brand,perrent);

  this .type = type;

  }

  //重写父类的计算租金方法:根据自己的计算租金规则

  public double calcrent( int days) {

  double price = this .getperrent()*days;

  if (days> 7 && days<= 30 ){

   price *= 0.9 ;

  } else if (days> 30 && days<= 150 ){

   price *= 0.8 ;

  } else if (days> 150 ){

   price *= 0.7 ;

  }

  return price;

  }

}

 

//客车类

public class bus extends vehicle{

  //座位数

  private int seatcount;

  public void setseatcount( int seatcount){

  this .seatcount=seatcount;

  }

  public int getseatcount(){

  return seatcount;

  }

 

 

  public bus(){}

  //bus的带参构造方法

  public bus(string id,string brand, int perrent, int seatcount){

  super (id,brand,perrent);

  this .seatcount = seatcount;

  }

  //重写父类的计算租金方法:根据自己的计算租金规则

  public double calcrent( int days) {

  double price = this .getperrent()*days;

  if (days>= 3 && days< 7 ){

   price *= 0.9 ;

  } else if (days>= 7 && days< 30 ){

   price *= 0.8 ;

  } else if (days>= 30 && days< 150 ){

   price *= 0.7 ;

  } else if (days> 150 ){

   price *= 0.6 ;

  }

  return price;

 

  }

}

 

//汽车业务类

public class operation {

  public vehicle[] vehicle = new vehicle[ 8 ];

//初始化汽车信息

  public void init(){

  vehicle[ 0 ] = new car( "京ny28588" , "宝马" , 800 , "x6" );  //vehicle v = new car();

  vehicle[ 1 ] = new car( "京cny32584" , "宝马" , 600 , "550i" );  //vehicle v = new car();

  vehicle[ 2 ] = new car( "京nt37465" , "别克" , 300 , "林荫大道" );  //vehicle v = new car();

  vehicle[ 3 ] = new car( "京nt96968" , "别克" , 600 , "gl8" );  //vehicle v = new car();

  vehicle[ 4 ] = new bus( "京6566754" , "金杯" , 800 , 16 );  //vehicle v = new bus();

  vehicle[ 5 ] = new bus( "京8696997" , "金龙" , 800 , 16 );  //vehicle v = new bus();

  vehicle[ 6 ] = new bus( "京9696996" , "金杯" , 1500 , 34 );  //vehicle v = new bus();

  vehicle[ 7 ] = new bus( "京8696998" , "金龙" , 1500 , 34 );  //vehicle v = new bus();

  }

  //租车:根据用户提供的条件去汽车数组中查找相应车辆并返回

  //如果租赁的是轿车  需要条件:品牌    型号

  //如果租赁的是客车  需要条件:品牌    座位数

  //简单工厂模式

  public vehicle zuche(string brand,string type, int seatcount){

  vehicle che = null ;

  //for循环遍历数组vehicle

  for (vehicle myche : vehicle){

   //判断vehicle类的myche的类型是否和car一样

   if (myche instanceof car){

   //vehicle类的myche向下转型变成子类car

   car car = (car)myche;

   if (car.getbrand().equals(brand) && car.gettype().equals(type)){

    che=car;

    break ;

   }

   } else {

   //vehicle类的myche向下转型变成子类bus

   bus bus = (bus)myche;

   if (bus.getbrand().equals(brand) && bus.getseatcount() == seatcount){

    che=bus;

    break ;

   }

   }

  }

  return che;

 

  }

}

 

//汽车租赁

public class rent {

  public static void main(string[] args) {

  scanner input = new scanner(system.in);

  operation operation = new operation();

  //租赁公司界面

  operation.init();

  system.out.println( "**********欢迎光临租赁公司**********" );

  system.out.println( "1.轿车\t\t2.客车" );

  system.out.println( "请选择您要租赁的汽车类型:(1.轿车  2.客车)" );

  int chetype = input.nextint();

  string brand = "" ; //品牌

  string type = "" ; //型号

  int seatcount = 0 ; //座位数

  //收集用户条件

  if (chetype == 1 ){

   //租赁轿车

   system.out.println( "请选择您要租赁的轿车品牌:(1.别克  2.宝马)" );

   int choose = input.nextint();

   if (choose == 1 ){

   brand= "别克" ;

   system.out.println( "请选择您要租赁的汽车型号:(1.林荫大道  2.gl8)" );

   type=(input.nextint() == 1 )? "林荫大道" : "gl8" ;

   } else if (choose == 2 ){

   brand= "宝马" ;

   system.out.println( "请选择您要租赁的汽车型号:(1.x6  2.550i)" );

   type=(input.nextint() == 1 )? "x6" : "550i" ;

   }

  

  } else if (chetype == 2 ){

   //租赁客车

   type= "" ;

   system.out.println( "请选择您要租赁的客车品牌:(1.金杯  2.金龙)" );

   brand=(input.nextint()== 1 )? "金杯" : "金龙" ;

   system.out.println( "请选择您要租赁的客车座位数:(1.16座  2.32座)" );

   seatcount=(input.nextint() == 1 )? 16 : 34 ;

   }

  //租车

  vehicle che = operation.zuche(brand, type, seatcount);

  system.out.println( "请输入您的租赁天数:" );

  int days = input.nextint();

  double money = che.calcrent(days);

  system.out.println( "租车成功,请按照如下车牌号提车:" +che.getid());

  system.out.println( "您需要支付:" +money+ "元" );

  }

 

}

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

原文链接:https://blog.csdn.net/baidu_29343517/article/details/81153722

查看更多关于java实现汽车租赁系统的详细内容...

  阅读:13次