为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第二季租车系统

标签:
Java

测试类

package dadazuche;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double totalPrice=0; //总价格
        int totalPerson = 0;//总人数;
        double totlaGoods=0;//总货重量

        System.out.println("*****欢迎使用DADA租车系统*****"); 
        System.out.println("您是否需要租车:1是  0否");

        Scanner input =new Scanner(System.in);
        int choice = input.nextInt();
        if(choice==1){
            System.out.println("*****您可租车的类型及价目表如下所示*****");
            Car[] car ={new XiaoCar("奥迪",500,4),
                        new PickUp("皮卡",300,2,2.0)};
            System.out.println("序号\t汽车名称\t租金\t\t人数\t载重T");
            for (int i=0;i<car.length;i++) {
                System.out.print(i+1 +"\t" + car[i].carName+"\t"+car[i].carPrice+"元/天\t");
                if(car[i] instanceof XiaoCar){
                    System.out.println("载人:"+car[i].getManNum());
                }else if(car[i] instanceof PickUp){
                    System.out.println("载人:"+car[i].getManNum()+"\t载重:"+car[i].getLoad());
                }
            }
            System.out.println("=============================");
            System.out.println("请输入你要租车的数量(最多3辆):");

            int rentNum = input.nextInt();
            int[]rentArray = new int[rentNum];//输入的数量就是数组的长度
            for(int i=1;i<=rentNum; i++)
            {
                System.out.println("请输入第"+i+"的序号");
                rentArray[i-1]=input.nextInt()-1;
            }
            System.out.println("请输入租车的天数:");
            int rentDay =input.nextInt();
            System.out.println("你的账单:");
            System.out.println("===========");
            int CarNum=0; //车的数量 
                for (int i = 0; i < rentArray.length; i++) {
                    if(car[rentArray[i]] instanceof XiaoCar ||car[rentArray[i]] instanceof PickUp){

                        if(car[rentArray[i]]  instanceof XiaoCar){
                            System.out.println(car[rentArray[i]].getCarName()+"车");
                        }else{
                            System.out.println(car[rentArray[i]].getCarName()+"车");
                        }
                        CarNum++;  
                        //总人数
                        totalPerson +=car[rentArray[i]].getManNum();
                        //总价格
                        totalPrice +=car[rentArray[i]].getCarPrice() * rentDay;
                        //总重量
                        totlaGoods +=car[rentArray[i]].getLoad();
                    }
                }
            System.out.println("总共租车"+CarNum+"辆,"+"租"+rentDay+"天,"+"可以载"+totalPerson+"人,"+"总载重"+totlaGoods+"T,"+"租金总共"+totalPrice+"元.");
            }else{
                System.out.println("退出系统,感谢你的使用!");

            }
        input.close();  
    }   
}
父类
package dadazuche;

public class Car {
    String carName; //汽车名称

    double carPrice;//汽车租金
    int    manNum;//载人数
    double    load;     //汽车载重
    /*载人*/
    public Car(String carName, double carPrice, int manNum) {
        super();
        this.carName = carName;
        this.carPrice = carPrice;
        this.manNum = manNum;
    }
    //载人载货
    public Car(String carName, double carPrice, int manNum, double load) {
        super();
        this.carName = carName;
        this.carPrice = carPrice;
        this.manNum = manNum;
        this.load = load;
    }

    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public double getCarPrice() {
        return carPrice;
    }
    public void setCarPrice(double carPrice) {
        this.carPrice = carPrice;
    }
    public int getManNum() {
        return manNum;
    }
    public void setManNum(int manNum) {
        this.manNum = manNum;
    }
    public double getLoad() {
        return load;
    }
    public void setLoad(double load) {
        this.load = load;
    }

}

两个子类都一样

package dadazuche;

public class PickUp extends Car {

    public PickUp(String carName, double carPrice, int manNum, double load) {
        super(carName, carPrice, manNum, load);
        // TODO Auto-generated constructor stub
    }
}
package dadazuche;

public class XiaoCar extends Car {

    public XiaoCar(String carName, double carPrice, int manNum) {
        super(carName, carPrice, manNum);
        // TODO Auto-generated constructor stub
    }

}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消