java入门第二季答答租车系统代码

278 查看

/*车子的抽象类*/
package com.imooc;

public abstract class Car {
    String name;//车型名称
    int passenger;//载客量
    int  huowu;//载货量
    int price;//价格
    String feature;//功能描述
    public abstract void  feature();
}
/*载客车的类,继承车子抽象类*/
package com.imooc;

public class PersonCar extends Car {

    @Override
    public void feature() {
        // TODO Auto-generated method stub
        this.feature="载客"+passenger+"人";
    }
        /*利用有参构造方法初始化类属性*/
    public PersonCar(String newName,int newPrice,int newPassenger){
        this.name=newName;
        this.passenger=newPassenger;
        this.price=newPrice;
        this.huowu=0;
        this.feature();
    }
}
/*皮卡车类继承车子抽象类*/
package com.imooc;

public class Picar extends Car {

    @Override
    public void feature() {
        // TODO Auto-generated method stub
        this.feature="载客"+passenger+"人并且载货"+huowu+"吨";
    }
        /*利用有参构造方法初始化类属性*/
    public Picar(String newName,int newPrice,int newPassenger,int newHuowu){
        this.name=newName;
        this.passenger=newPassenger;
        this.price=newPrice;
        this.huowu=newHuowu;
        this.feature();
    }
}
/*火车类继承车子抽象类*/
package com.imooc;

public class Tunk extends Car {

    @Override
    public void feature() {
        // TODO Auto-generated method stub
        this.feature="载货"+huowu+"吨";
    }
         /*利用有参构造方法初始化类属性*/
    public Tunk(String newName,int newPrice,int newHuowu){
        this.name=newName;
        this.passenger=0;
        this.price=newPrice;
        this.huowu=newHuowu;
        this.feature();
    }
}
/*测试类*/
package com.imooc;
import java.util.Scanner;
public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int money=0;
        int Person=0;
        int Huowu=0;
        Car[] car={new PersonCar("奥迪A6",500,4),new PersonCar("马自达6",400,4),new Picar("皮卡雪6",450,4,2),new PersonCar("金龙",800,20),new Tunk("松花江",400,4),new Tunk("依维柯",1000,20)};//初始化车型
        Scanner input=new Scanner(System.in);//创建Scanner对象
        System.out.println("***欢迎进入嗒嗒租车系统***");
        System.out.println("请问是否要租车?输入0代表否,1代表是");
        int key=input.nextInt();
        if(key==1){
            System.out.println("***有以下车型供选择***");
            System.out.println("序号\t车型\t价格(元/天)\t特点");
            for(int i=1;i<=car.length;i++){
                System.out.println(i+"\t"+car[i-1].name+"\t"+car[i-1].price+"\t"+car[i-1].feature);
            }
            System.out.println("***请输入各种车型租车数量***");
            int[] num=new int[20];
            for(int i = 1;i<=car.length;i++){
                System.out.println("请输入"+i+"号车租车数量(大于等于0的整数):");
                num[i-1]=input.nextInt();//输入每种车需要的数量
            }
            System.out.println("请输入租车天数:");
            int time=input.nextInt();//输入租车天数
            for(int i=0;i<car.length;i++){
                money=money+num[i]*car[i].price*time;//计算总钱数
                Huowu=Huowu+num[i]*car[i].huowu;//计算总的载货量
                Person=Person+num[i]*car[i].passenger;//计算总的载客人数
            }
            /*显示租车清单*/
            System.out.println("您的帐单如下:");
            for(int i=0;i<car.length;i++){
                if(num[i]!=0){
                    System.out.println(car[i].name+" "+num[i]+"辆,价格为:"+car[i].price+"元每天");
                }
            }
            System.out.println("每次运输可载货"+Huowu+"吨,可载客"+Person+"人。");
            System.out.println("共租"+time+"天,总价格"+money+"元。");
        }else{
            System.exit(0);
        }
    }

}