问答详情
源自:12-1 综合练习

终于写完了,达达租车系统,欢迎各位讨论!

Car类


package com.dadacar;


public class Car {

private int number;//车辆编号

    private String name;//车名

    private double price;//租金

    public  Car(int number,String name ,double price) {

    this.number=number;

    this.name = name;

    this.price = price;

    }

    public int getNumber()

    {

    return number;

    }

    public String getName()

    {

    return name;

    }

    public double getPrice()

    {

    return price;

    }

    public String toString()

    {

    return  "车辆序号: "+number+'\t'+"车型: "+name+'\t'+"租金: "+price+'元'+'\t'+'\t';

    }

}

passengerCar类

package com.dadacar;


public class passengerCar extends Car {

   private int persons;//载人量

public passengerCar(int number,String name, double price, int persons) {

super(number,name, price);

this.persons=persons;

}

public int getPersons() 

{

return persons;

}

public String toString()

{

return super.toString()+"载客量: "+persons+'人';

}


}

pickCar类


package com.dadacar;


public class pickCar extends  Car{

   private int  persons;

   private double Capacity;

public pickCar(int number,String name, double price,double pickCapacity,int pickPersons) {

super(number,name, price);

// TODO Auto-generated constructor stub

this.Capacity=pickCapacity;

this.persons=pickPersons;

}

public int getPersons() {

return persons;

}

public double getCapacity() {

return Capacity;

}

public String toString()

   {

   return super.toString()+"载客量:"+Capacity+'吨'+'\t'+"载客量:"+persons+'人';

   }

}

TruckCar类


package com.dadacar;


public class TruckCar extends Car {

private double Capacity;//载货量

public TruckCar(int number,String name,double price,double cargoCapacity)

{

super(number,name,price);

this.Capacity = cargoCapacity;

}

   public double getCapacity() {

return Capacity;

}

   public String toString()

   {

   return super.toString()+"载货量:"+'\t'+Capacity+'吨';

   }

}

test类


package com.dadacar;

import java.lang.System;

import java.util.Scanner;


public class test {


public static void main(String[] args) {

// TODO Auto-generated method stub

       Car[] cars = new Car[5];

       cars[0]=new TruckCar(1,"重型卡车 ",460,4.5);

       cars[1]=new TruckCar(2,"轻型卡车 ",300,3.5);

       cars[2]=new passengerCar(3,"大客车",500,45);

       cars[3]=new passengerCar(4,"小客车",400,30);

       cars[4]=new pickCar(5,"皮卡",450,3.5,6);

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

System.out.println("请问您是否要租车?  1.是   2.否");

Scanner s = new Scanner(System.in);

int yn =s.nextInt();

if(yn==1) {

/*显示车辆类型*/

System.out.println("您可租车的类型及其价目表:");

System.out.println("序号" + '\t' + "汽车名称" + '\t' + "租金" + '\t' +"容量"+'\t');

for(Car c :cars) 

System.out.println(c.toString());

double totalprice=0;//总金额

double totalPersons = 0;//总载客量

double totalCapicity=0;//总载物量

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

int sl=s.nextInt();

   Car[] rentCar = new Car[sl];

   for (int i = 0; i < sl; i++) {

            System.out.printf("请输入第%d辆车的序号:",i+1);

           int num=s.nextInt();//捕捉用户输入车的序号并存入数组

        rentCar[i]=cars[num-1];

   }

   /*计算总人数和总载客量*/

   for(int i=0; i<sl;i++)

   {

   if(rentCar[i] instanceof passengerCar) {

   totalPersons += ((passengerCar)rentCar[i]).getPersons();

   }

   if(rentCar[i] instanceof TruckCar) {

   totalCapicity+=((TruckCar)rentCar[i]).getCapacity();

   }

      if(rentCar[i] instanceof pickCar) {

    totalPersons+=((pickCar)rentCar[i]).getPersons();

        totalCapicity+=((pickCar)rentCar[i]).getCapacity();

        }

   }

   /*计算总金额*/

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

   int days=s.nextInt();

   

               for(int i=0;i<sl;i++) {

               totalprice=totalprice+rentCar[i].getPrice()*days;

               }

               

               /*显示账单*/

   System.out.println("********************您的账单如下:******************** ");

   System.out.println(">>>>>>>已经选择载人车: ");

   for(int i=0;i<sl;i++) {

   if(rentCar[i].getNumber()>2) {

   System.out.println(rentCar[i].getName());

   }else {

   continue;

   }

   }

   System.out.println(">>>>>>>已经选择载货车: ");

   for(int i=0;i<sl;i++) {

   if(rentCar[i].getNumber()<=2) {

   System.out.println(rentCar[i].getName());

   }else {

   continue;

   }

   }

   System.out.println("******************************************************");

   System.out.println("您所租借的车总载客量为: "+totalPersons+"人\t");

   System.out.println("您所租借的车总载货量为: "+totalCapicity+"吨\t");

   System.out.println("总费用为"+totalprice+"元");

   

}else if(yn==0) {

s.close();

}

}

}

生成图

https://img1.mukewang.com/5c50238f0001c98606530817.jpg

欢迎大家给出优化意见。

提问者:check777 2019-01-29 17:47

个回答

  • weixin_慕后端046368
    2019-03-13 11:22:54

    https://img1.mukewang.com/5c88777f0001474d09120258.jpg处理输入错误

  • weixin_慕尼黑9505920
    2019-02-01 16:01:48

    我的写法和你差不多,但是为啥rentCar[ ]这个数组就无法调用那些变量和方法

  • 钢铁直男阿强
    2019-01-30 19:55:51

    进入答租车系统输入的是1和0之外的数会怎么样呢