疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java编写租车系统,类的继承用法


 

   

  租车系统实现的功能:

  1,询问用户是否使用租车系统。

  2,向用户展示,车的资源。

  3,用户选择租车总数。

  4,用户逐个选择车。

  5,显示用户所租的车。

  6,计算总价。

  车分为载人客车,载货的货车以及既载人又载客的皮卡。

  所以这里创建了了5个java文件,5个类。

  父类:

  Car.java:

  package cars;

  public class Car {

  public String name;

  public int price;

  public int peoplecaptain;

  public int load;

  public String getName(){

  return name;

  }

  public int getPrice(){

  return price;

  }

  public void setName(String name){

  this.name=name;

  }

  public void setPrice(int price){

  this.price=price;

  }

  public int getPeoplecaptain(){

  return peoplecaptain;

  }

  public void setPeoplecaptain(int peoplecaptain){

  this.peoplecaptain=peoplecaptain;

  }

  public int getLoad(){

  return load;

  }

  public void setLoad(int load){

  this.load=load;

  }

  }

  子类客车:

  Passagecar.java

  package cars;

  public class Passagecar extends Car{

  private int peoplecaptain;

  public Passagecar(String name,int price,int peoplecaptain)

  {

  this.name=name;

  this.price=price;

  this.peoplecaptain=peoplecaptain;

  }

  public int getPeoplecaptain(){

  return peoplecaptain;

  }

  public void setPeoplecaptain(int peoplecaptain){

  this.peoplecaptain=peoplecaptain;

  }

  }

  子类货车:

  Turck.java

  package cars;

  public class Turck extends Car{

  private int load;

  public Turck(String name,int price,int load){

  this.name=name;

  this.price=price;

  this.load=load;

  }

  public int getLoad(){

  return load;

  }

  public void setLoad(int load){

  this.load=load;

  }

  }

  子类皮卡:

  Pickup.java

  package cars;

  public class Pickup extends Car {

  private int peoplecaptain;

  private int load;

  public Pickup(String name,int price,int peoplecaptain,int load){

  this.name=name;

  this.price=price;

  this.peoplecaptain=peoplecaptain;

  this.load=load;

  }

  public int getPeoplecaptain(){

  return peoplecaptain;

  }

  public void setPeoplecaptain(int peoplecaptain){

  this.peoplecaptain=peoplecaptain;

  }

  public int getLoad(){

  return load;

  }

  public void setLoad(int load){

  this.load=load;

  }

  }

  为什么父类也要写入子类的方法呢?有些方法本应该是子类特有的。

  这里要引入一种java的继承机制:

  举出一个例子:

  父类class A{

  int x;

  void method1();

  void method2();

  }

  class B extends A{

  int x,y;

  void method1();

  void method3();

  }

  A a=new B();

  a.x; //可以这样引用,而且引用的是父类属性x;

  a.y;//错误的用法,不能直接引用子类的属性;

  a.method1();//正确的用法,调用子类方法;

  a.method2();//正确,调用父类方法;

  a.method3();//错误,不能直接调用子类的方法;

  这只是java的一种继承实例化对象的方法,java继承的实例化还有其他种类,这是我个人以前一直比较迷茫的一种。这次差不多有点头绪了。

  以及类租车系统:

  Dada.java

  package cars;

  import java.util.Scanner;

  public class Dada {

  public static void main(String[] args)

  {

  Car[] cars={new Passagecar("奥迪A4",500,4),new Passagecar("马自达6",400,4)

  ,new Pickup("皮卡雪",450,4,2),new Passagecar("金龙",800,20),

  new Turck("松花江",400,4),new Turck("依维柯",1000,20) };

  System.out.println("欢迎使用答答租车系统:");

  System.out.println("你是否需要租车:1是0 否");

  Scanner scan=new Scanner(System.in);

  String input=scan.next();

  if(input.equals("1"))

  {

  System.out.println("我们的车类目以及价格:");

  System.out.println("序号 名称 价格 容量");

  int i=1;

  for(Car currentcar:cars)

  {

  if(currentcar instanceof Passagecar)

  {

  System.out.println(" "+i+" "+currentcar.getName()+

  " "+currentcar.getPrice()+"元/天"+" "+" "+currentcar.getPeoplecaptain()+"人");

  i++;

  }

  if(currentcar instanceof Turck)

  {

  System.out.println(" "+i+" "+currentcar.getName()+

  " "+currentcar.getPrice()+"元/天"+" "+" "+currentcar.getLoad()+"吨");

  i++;

  }

  if(currentcar instanceof Pickup)

  {

  System.out.println(" "+i+" "+currentcar.getName()+

  " "+currentcar.getPrice()+"元/天"+" "+" "+currentcar.getPeoplecaptain()+"人"+" "+currentcar.getLoad()+"吨");

  i++;

  }

  }

  }

  System.out.println("请输入你所要租车总数:");

  Scanner scan2=new Scanner(System.in);

  int input2=scan2.nextInt();

  int i=1;

  int k=1;

  int[] oder=new int[10];

  for(int j=1;j<=input2;j++)

  {

  System.out.println("请输入你所要租的第"+k+"辆车:");

  Scanner scan3=new Scanner(System.in);

  int input3=scan3.nextInt();

  oder[k]=input3;

  k++;

  }

  System.out.println("你所要租的车为:");

  System.out.println("序号 名称 价格 容量");

  for(int j=1;j<=k-1;j++)

  {

  if(cars[oder[j]-1] instanceof Passagecar)

  {

  System.out.println(" "+i+" "+cars[oder[j]-1].getName()+

  " "+cars[oder[j]-1].getPrice()+"元/天"+" "+" "+cars[oder[j]-1].getPeoplecaptain()+"人");

  i++;

  }

  if(cars[oder[j-1]] instanceof Turck)

  {

  System.out.println(" "+i+" "+cars[oder[j]-1].getName()+

  " "+cars[oder[j]-1].getPrice()+"元/天"+" "+" "+cars[oder[j]-1].getLoad()+"吨");

  i++;

  }

  if(cars[oder[j]-1] instanceof Pickup)

  {

  System.out.println(" "+i+" "+cars[oder[j]-1].getName()+

  " "+cars[oder[j]-1].getPrice()+"元/天"+" "+" "+cars[oder[j]-1].getPeoplecaptain()+"人"+" "+cars[oder[j]-1].getLoad()+"吨");

  i++;

  }

  }

  System.out.println("请输入你所要租的天数:");

  Scanner scan4=new Scanner(System.in);

  int input4=scan4.nextInt();

  int sum=0;

  for(int j=1;j<=k-1;j++)

  {

  sum+=cars[oder[j]-1].getPrice();

  }

  sum=sum*input4;

  System.out.println("您的总金额为:"+sum);

  }

  }