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

两个多小时写的,不是最简单的方法,不过包含了许多知识点,较容易理解。

mapper
关注TA
已关注
手记 3
粉丝 10
获赞 18

先讲一下思路:一个抽象类,其中带有一个抽象方法;然后每种类型的车分别为一个子类,继承这个抽象类,最后一个initial类进行实现。
//首先是抽象类,代码如下:
package com.dadazuche;
public abstract class Car {
public abstract void shuxing();
}
//紧接着为各种车型的子类。
// 奥迪
package com.dadazuche;
public class AoDi extends Car{
int seq=1;
String name="奥迪A4";
int price=500;
int contain=4;
public void shuxing(){
System.out.println(seq+" "+name+" "+price+"元/天"+" "
+"载人:"+contain+"人");
}
}
//马自达
package com.dadazuche;
public class MaZiDa extends Car{
int seq=2;
String name="马自达6";
int price=400;
int contain=4;
public void shuxing(){

    System.out.println(seq+"  "+name+"  "+price+"元/天"+"  "
            +"载人:"+contain+"人");
}

}
//金龙
package com.dadazuche;
public class JinLong extends Car{
int seq=4;
String name="金龙";
int price=800;
int contain=20;
public void shuxing(){

    System.out.println(seq+"  "+name+"  "+price+"元/天"+"  "
    +"载人:"+contain+"人");

}

}
//皮卡雪
package com.dadazuche;

public class PiKaXue extends Car{
int seq=3;
String name="皮卡雪6";
int price=450;
int contain=4;
int contain2=2;
public void shuxing(){

    System.out.println(seq+"  "+name+"  "+price+"元/天"+"  "
            +"载人:"+contain+"人"+" "+"载货:"+contain2+"吨");
}

}
//松花江
package com.dadazuche;

public class SongHuaJiang extends Car{
int seq=5;
String name="松花江";
int price=400;
int contain2=4;
public void shuxing(){

    System.out.println(seq+"  "+name+"  "+price+"元/天"+"  "
    +"载货:"+contain2+"吨");
}

}
//依维柯
package com.dadazuche;
public class YiWeiKe extends Car{
int seq=6;
String name="依维柯";
int price=1000;
int contain2=20;
public void shuxing(){

    System.out.println(seq+"  "+name+"  "+price+"元/天"+"  "
    +"载货:"+contain2+"货");
}

}
//最后是实现类
package com.dadazuche;

import java.util.Scanner;
public class Initial {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
        StringBuffer st1=new StringBuffer();
        StringBuffer st2=new StringBuffer();
        int totalpeople=0;
        double totalcontain=0;
        double totalprice=0;
        int[] temple=new int[]{0,0,0,0,0,0};
        AoDi aodi=new AoDi();
        MaZiDa mazida=new MaZiDa();
        PiKaXue pikaxue=new PiKaXue();
        JinLong jinlong=new JinLong();
        SongHuaJiang songhuajiang=new SongHuaJiang();
        YiWeiKe yiweike=new YiWeiKe();
        System.out.println("欢迎使用嗒嗒租车系统:");
        System.out.println("您是否要租车:1.是  0.否");
        Scanner sc=new Scanner(System.in);
        int jiamubiao=sc.nextInt();
        if(jiamubiao==1){
            System.out.println("您可租车的类型及其价目表如下:");
            System.out.println("序号"+"  "+"汽车名称"+"  "+"租金"+"  "
            +"容量");
            aodi.shuxing();
            mazida.shuxing();
            pikaxue.shuxing();
            jinlong.shuxing();
            songhuajiang.shuxing();
            yiweike.shuxing();

        System.out.println("请输入你要租车的数量");
        int shuliang=sc.nextInt();
        for(int i=1;i<=shuliang;i++){
            System.out.println("请输入第"+i+"辆车的序号");
            int sci=sc.nextInt();

            switch(sci){
            case 1:
                st1.append("奥迪A4  ");
                totalpeople=totalpeople+4;

                temple[0]=temple[0]+aodi.price;

                break;
            case 2:
                st1.append("马自达6  "); 
                totalpeople=totalpeople+4;
                temple[1]=temple[1]+mazida.price;
                break;
            case 3:
                st1.append("皮卡雪6  "); 
                st2.append("皮卡雪6  ");
                totalpeople=totalpeople+4;
                totalcontain=totalcontain+2;
                temple[2]=temple[2]+pikaxue.price;
                break;
            case 4:
                st1.append("金龙  "); 
                totalpeople=totalpeople+20;
                temple[3]=temple[3]+jinlong.price;
                break;
            case 5:
                st2.append("松花江  "); 
                totalcontain=totalcontain+4;
                temple[4]=temple[4]+songhuajiang.price;
                break;
            case 6:
                st2.append("依维柯  "); 
                totalcontain=totalcontain+20;
                temple[5]=temple[5]+yiweike.price;
                break;

        }

        }
        System.out.println("请输入租车天数");
        int daytime=sc.nextInt();
        System.out.println("您的账单");
        System.out.println("***可载人的车有:");
        System.out.println(st1+"共载人:"+totalpeople);
        System.out.println("***可载货的车有:");
        System.out.println(st2+"共载货:"+totalcontain+"吨");
        for(int i=0;i<=5;i++){
            totalprice=totalprice+temple[i];
        }
        totalprice*=daytime;
        System.out.println("***租车总价格为:"+totalprice);    
        System.out.println("欢迎再次使用,再见!");
        }

        else{
            System.out.println("期待你对本系统的宝贵意见。");
        }
}

}

全部代码都展示出来了,直接复制就可以运行,有不明白的地方,或者有宝贵的意见 可以加我QQ:270015100 备注 慕友。
谢谢浏览。

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

热门评论

你可以让不同的车继承同一个父类,这样在实例化的时候统一处理。

查看全部评论