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

答答租车系统,使用interface的简单版本~(java第二季综合练习)

标签:
Java

Car类

public abstract class Car {
    int carNum;
    String carName;
    int carRent;
    int carDays;
    public int price(){
        return carRent*carDays;
    }   
}

passengerCar类

public  class passengerCar extends Car implements carryPeople{

    public passengerCar(int carNum,String carName,int carRent){
        this.carNum = carNum;
        this.carName = carName;
        this.carRent = carRent;

    }

    @Override
    public int carryPeople(int peopleNum) {
        // TODO Auto-generated method stub
        return peopleNum;
    }
}

goodsVan类

public class goodsVan extends Car implements carryGoods{
    public goodsVan(int carNum,String carName,int carRent){
        this.carNum = carNum;
        this.carName = carName;
        this.carRent = carRent;

    }

    @Override
    public int carryGoods(int goodsNum) {
        // TODO Auto-generated method stub
        return  goodsNum;
    }
}

peopleAndGoodsCar类

public class peopleAndGoodsCar extends Car implements carryPeople,carryGoods{

    public peopleAndGoodsCar(int carNum,String carName,int carRent){
        this.carNum = carNum;
        this.carName = carName;
        this.carRent = carRent;

    }
    @Override
    public int carryGoods(int goodsNum) {
        // TODO Auto-generated method stub
        return goodsNum;
    }

    @Override
    public int carryPeople(int peopleNum) {
        // TODO Auto-generated method stub
        return peopleNum;
    }

}

carryPeople接口

public interface carryPeople {
    public abstract int carryPeople(int peopleNum);
}

carryGoods接口

public interface carryGoods {
    public int carryGoods(int goodsNum);
}

Test类

import java.util.Scanner;
public class Test 
{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        passengerCar p1 = new passengerCar(1,"奥迪A4",500);       
        passengerCar p2 = new passengerCar(2,"马自达6",400);       
        passengerCar p4 = new passengerCar(4,"金龙",800);     
        goodsVan p5 = new goodsVan(5,"松花江",400);        
        goodsVan p6 = new goodsVan(6,"依维柯",1000);       
        peopleAndGoodsCar p3 = new peopleAndGoodsCar(3,"皮卡雪6",450);     
        System.out.println("欢迎使用答答租车系统!\n"+"请问您是否需要租车:1是0否");

        Scanner input = new Scanner(System.in);
        int receive = input.nextInt();          
        if(receive == 1){
                System.out.println("你可租车的类型及其价目表:"+"\n"+"序号 汽车名称    租金      容量"+"\n"+
            "1.  奥迪A4    500元/天     载人:4人"+"\n"+"2.  马自达6    400元/天     载人:4人"+
            "\n"+"3.     皮卡雪6    450元/天     载人:4人 载货:2吨"+"\n"+"4.    金龙      800元/天     载人:20人"
            +"\n"+"5.    松花江     400元/天     载货:4吨"+"\n"+"6.  依维柯     1000元/天        载货:20吨");
                System.out.println("请输入您要租车的数量:");          
                int needCarNum = input.nextInt();
                int sumPeople = 0 ;
                int sumGoods = 0 ;
                int sumPrice = 0 ;
                int[] num = new int[needCarNum];
                for(int i = 1 ;i<=needCarNum;i++){
                    System.out.println("请输入第"+i+"辆车的序号:");
                    num[i-1]=input.nextInt();
                }
                System.out.println("请输入租车的天数:");
                int needDays = input.nextInt(); 
                System.out.println("您的账单:\n"+"***可载人的车有:");     
                for(int i = 0;i<num.length;i++){
                    switch(num[i]){
                    case 1:
                        System.out.print("奥迪A4  ");
                        sumPeople +=p1.carryPeople(4); 
                        sumPrice +=p1.carRent;
                        break;
                    case 2:
                        System.out.print("马自达6  ");
                        sumPeople +=p2.carryPeople(4);
                        sumPrice +=p2.carRent;
                        break;
                    case 3: 
                        System.out.print("皮卡雪6  ");
                        sumPeople +=p3.carryPeople(4);
                        sumPrice +=p3.carRent;
                        break;
                    case 4:
                        System.out.print("金龙    ");
                        sumPeople +=p4.carryPeople(20);
                        sumPrice +=p4.carRent;
                        break;
                    case 5:             
                    case 6:

                        break;//不是载客的两种情况           
                    }           
                }
                System.out.println("共载客"+sumPeople+"人\n***可载货的车有:");
                for(int i = 0;i<num.length;i++){
                    switch(num[i]){
                    case 1:             
                    case 2:                                             
                    case 4:             
                        break;
                    case 3: 
                        System.out.print("皮卡雪6  ");
                        sumGoods +=p3.carryGoods(2);
                        //sumPrice +=p3.carRent;//计算钱数加一遍就够了
                        break;
                    case 5:
                        System.out.print("松花江   ");
                        sumGoods+=p5.carryGoods(4);
                        sumPrice +=p5.carRent;
                        break;

                    case 6:
                        System.out.print("依维柯   ");
                        sumGoods+=p6.carryGoods(20);
                        sumPrice +=p6.carRent;
                        break;//不是载客的两种情况
                    default:
                        System.out.println("序号"+(i+1)+"的汽车并不具有载货的功能");
                    }           
                }
                System.out.println("共载货"+sumGoods+"吨\n***租车的总价格为:"+sumPrice*needDays+"元");
        }   else {
                if(receive<0||receive>1){
                    System.out.println("输入有误!");
                }
                else{
                    System.out.println("感谢您的使用");

                }
            }
    }

}   
点击查看更多内容
8人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消