好得很程序员自学网

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

Java实现简单汽车租赁系统

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

需求如下: 

问题分析:

首先应当构建一个motovehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌;还包含一个no属性,表示汽车牌号;

?

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

package cn.jbit.car;

 

public abstract class motovehicle {

  private string no;

  private string brand;

  /**

  * 无参构造方法

  */

  public motovehicle() {

 

  }

  /**

  * 有参构造方法

  * @param no 汽车牌号

  * @param brand 汽车品牌

  */

  public motovehicle(string no,string brand) {

  this .no=no;

  this .brand=brand;

  }

 

  public string getno() {

  return no;

  }

 

  public string getbrand() {

  return brand;

  }

  public abstract int calrent( int days);

}

其次,应有car类继承自motovehicle类,并有一个type属性,表示轿车型号,应有一个计算租金的方法calrent()

 

?

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 cn.jbit.car;

 

public class car extends motovehicle{

  private string type;

  public car() {

 

  }

  public car (string no,string brand,string type) {

  super (no,brand);

  this .type=type;

  }

 

  public string gettype() {

  return type;

  }

 

  public void settype(string type) {

  this .type = type;

  }

  @override

  public int calrent( int days) {

  // todo auto-generated method stub

  if ( "2" .equals(type)) {

   return days* 500 ;

  }

  else if ( "1" .equals(type)) {

   return days* 600 ;

  }

  else {

   return 300 *days;

  }

  }

}

再次,应有bus类继承自motovehicle类,并有一个countset属性,表示客车的容量,同样的,应有一个计算租金的方法calrent();

 

?

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 cn.jbit.car;

 

public class bus extends motovehicle {

  int countset;

  public bus() {

  }

  /**

  * 带参构造函数

  */

  public bus(string brand,string no, int countset) {

  super (brand,no);

  this .countset=countset;

  }

  public int getcountset() {

  return countset;

  }

  public void setcountset( int countset) {

  countset = countset;

  }

 

  @override

  public int calrent( int days) {

  // todo auto-generated method stub

  if (countset< 16 ) {

   return 800 *days;

  }

  else {

   return 1600 *days;

  }

  }

 

}

最后,以上三类应在test类中测试;

?

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

package cn.jbit.car;

import java.util.scanner;

 

public class test {

  public static void main(string[] args) {

  string no,brand,mtype;

  int countset,days;

  scanner input= new scanner(system.in);

  system.out.println( "*****欢迎来到汽车租赁公司!******" );

  system.out.println( "请输入天数:" );

  days=input.nextint();

  system.out.println( "请输入车辆类型:" );

  system.out.println( "1、轿车  2、客车" );

  mtype=input.next();

  if ( "1" .equals(mtype)) {

   system.out.println( "请输入轿车品牌:" );

   system.out.println( "1、宝马 2、别克" );

   brand=input.next();

   if ( "1" .equals(brand)) {

   system.out.println( "2、宝马550i:500" );

   system.out.println( "请输入轿车型号:" );

   mtype=input.next();

   system.out.println( "请输入辆数:" );

   int count=input.nextint();

   car car= new car( "辽b000" ,brand,mtype);

   system.out.println( "您需支付:" +count*car.calrent(days));

  

   }

   else {

   system.out.println( "1、别克商务gl8:600  3、别克林荫大道:300" );

   mtype=input.next();

   system.out.println( "请输入辆数:" );

   int count=input.nextint();

   car car= new car( "辽b000" ,brand,mtype);

   system.out.println( "您需支付:" +count*car.calrent(days));

   }

  }

  else {

   system.out.println( "请输入品牌:" );

   system.out.println( "1、金杯  2、金龙" );

   brand=input.next();

   system.out.println( "请输入座位数:" );

   countset=input.nextint();

   system.out.println( "请输入辆数:" );

   int count=input.nextint();

   bus b= new bus(brand, "辽b000" ,countset);

   system.out.println( "您需支付:" +b.calrent(days)*count);

  }

  }

 

}

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

原文链接:https://blog.csdn.net/HurryRabbit/article/details/80917818

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

  阅读:8次