手记

答答租车系统规范代码示例

父类

package practice;

public class Car {
    public String carname;
    public int  busload;//载客量
    public double  burden;//载货量
    public double money;//金额
    public String getCarname() {
        return carname;
    }
    public void setCarname(String carname) {
        this.carname = carname;
    }
    public int getBusload() {
        return busload;
    }
    public void setBusload(int busload) {
        this.busload = busload;
    }
    public double getBurden() {
        return burden;
    }
    public void setBurden(double burden) {
        this.burden = burden;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
}

载货子类

package practice;

public class Busdencar extends Car {

    public String carname;
    public int  busload;//载客量
    public double  burden;//载货量
    public double money;//金额

    public Busdencar(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;

}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

载客子类

package practice;

public class Busloadcar extends Car {

    public String carname;
    public int  busload;//载客量
    public double  burden;//载货量
    public double money;//金额

    public Busloadcar(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;
}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

载客载货子类

package practice;

public class PackUp extends Car {

    public String carname;
    public int  busload;//载客量
    public double  burden;//载货量
    public double money;//金额

    public PackUp(String carname,int busload, double burden,double money){
    this.carname = carname;
    this.busload = busload;
    this.burden = burden;
    this.money = money;
}

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public int getBusload() {
        return busload;
    }

    public void setBusload(int busload) {
        this.busload = busload;
    }

    public double getBurden() {
        return burden;
    }

    public void setBurden(double burden) {
        this.burden = burden;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

用户测试代码

package practice;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] car = {new Busloadcar("奥迪A4",4,0,500),new Busloadcar("马自达6",4,0,400),
                new PackUp("皮卡雪6",4,2,450),new Busloadcar("金龙",20,0,800),new Busdencar("松花江",0,4,400),
                new Busdencar("依维柯",0,20,1000)};

        //询问用户
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1是 0否");
        Scanner console = new Scanner(System.in);
        int answer = console.nextInt();
        int i = 1;
        if(answer == 1){
            //输出可租车型及其价目表
            System.out.println("您可租输的类型及其价目表:");
            System.out.println("序号" + " " + "汽车名称" + " " + "租金" +  " " + "容量");
            for(Car foreachcar : car){
                System.out.println(i + " " + foreachcar.getCarname() + " " + foreachcar.getMoney() + "元/天" + " "+ "载人:" + 
            foreachcar.getBusload() + "人"  + " " + "载货" + foreachcar.getBurden() + "吨");
                i++;
            }

            //询问用户租车数量
            System.out.println("请输入您要租汽车的数量:");
            int count = console.nextInt();
            Car[] rentcar = new Car[count];
            for(int j = 0 ; j<count ; j++){
                System.out.println("请输入第" + (j+1) + "输车的序号");
                int ct = console.nextInt();
                rentcar[j] = car[ct-1];
            }

            //询问租车天数
            System.out.println("请输入租车天数:");
            double day = console.nextInt();

            //输出用户账单
            System.out.println("您的账单:");
            System.out.println("***可载人的车有");
            double sum = 0;//总人数
            double price = 0;//总价
            double sbur = 0;//总载货砘数
            for(Car pcar : rentcar){
                if(pcar.getBusload()!=0){
                System.out.print(pcar.getCarname() + " ");
                sum = sum + pcar.getBusload();
                price = price + pcar.getMoney();
                }else{
                    continue;
                }
            }
            System.out.println("共载人:" + sum + "人");

            System.out.println("***可货的车有");
            for(Car pcar : rentcar){
                if(pcar.getBurden()!=0){
                System.out.print(pcar.getCarname() + " ");
                if(pcar.getBusload()== 0){
                price = price + pcar.getMoney();
                }
                sbur = sbur + pcar.getBurden();
                }else{
                    continue;
                }
            }
            System.out.println("共可载货量为:" + sbur + "砘");

            price = price * day;
            System.out.println("***租车总价格为" + price + "元");

        }else{
            System.out.println("感谢您的使用!");
        }
    }

}

3人推荐
随时随地看视频
慕课网APP

热门评论

父类定义的常量用public,没有封装,后面又创建set与get方法,不是很懂

查看全部评论