继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

没事就写代码啊
关注TA
已关注
手记 3
粉丝 5
获赞 36

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("感谢您的使用");

                }
            }
    }

}   
打开App,阅读手记
8人推荐
发表评论
随时随地看视频慕课网APP

热门评论

兄弟现在什么阶段了找工作了吗

有注释就好了,对于我这种初级菜鸟,有的变量不知道是什么意思。。

还不会怎么在输入错误的情况下提示用户,并且重新输入,有会的小伙伴教我下被

查看全部评论