问答详情
源自:12-2 项目问题解析 1

应该和视频里的同学写的一样~~

四个定义类:

package com.imooc;
//Truck类
public class Truck extends Car {
    private int goods;
    public Truck(String name, int price, int goods){
        this.name=name;
        this.price=price;
        this.goods=goods;
    }
    public int getGoods(){
        return goods;
    }
    public void setGoods(int goods){
        this.goods=goods;
    }
}
//皮卡类
package com.imooc;

public class Pickup extends Car {
    private int men;
    private int goods;
    public Pickup(String name,int price,int men,int goods){
        this.name=name;
        this.price=price;
        this.men=men;
        this.goods=goods;
    }
    public int getMen() {
        return men;
    }
    public void setMen(int men) {
        this.men = men;
    }
    public int getGoods() {
        return goods;
    }
    public void setGoods(int goods) {
        this.goods = goods;
    }
    
}
//载人类
package com.imooc;

public class Man_pick extends Car {
    private int men;
    public Man_pick(String name,int price,int men){
        this.name=name;
        this.price=price;
        this.men=men;
    }
    public Man_pick(){
        
    }
    public int getMen() {
        return men;
    }
    public void setMen(int men) {
        this.men = men;
    }
}
//大类:Car类
package com.imooc;

public  class Car {
    public int price;
    public String name;
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}
//主函数: Carsystem
package com.imooc;
import java.util.*;
public class Carsystem {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] cars= {new Man_pick("奥迪A4",500,4),new Man_pick("马自达6",400,4),new Pickup("皮卡雪6",450,4,2),
                new Man_pick("金龙",800,20),new Truck("松花江",400,4),new Truck("依维柯",1000,20)};
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车?1是 0否");
        Scanner sc= new Scanner(System.in);
        int sel=sc.nextInt();
        if(sel==0) System.exit(0);
        else if(sel == 1){
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号\t汽车名称\t租金\t\t容量");
            int i=1;
            for(Car car : cars){
                if(car instanceof Man_pick )
                {
                    Man_pick mtemp =(Man_pick) car;
                    System.out.println(i+".\t"+mtemp.getName()+"\t"+mtemp.getPrice()+"元/天\t\t载人:"+mtemp.getMen()+"人");
                }
                    
                else if(car instanceof Truck )
                {
                    Truck mtemp = (Truck)car;
                    System.out.println(i+".\t"+mtemp.getName()+"\t"+mtemp.getPrice()+"元/天\t\t载货:"+mtemp.getGoods()+"吨");
                }
                    
                else if(car instanceof Pickup )
                {
                    Pickup mtemp= (Pickup)car;
                    System.out.println(i+".\t"+mtemp.getName()+"\t"+mtemp.getPrice()+"元/天\t\t载人:"+mtemp.getMen()+"人,"
                            +"载货:"+mtemp.getGoods()+"吨");
                }
                i++;
            }
            System.out.println("请输入您要租车的数量:");
            int sum=sc.nextInt();
            int b[]=new int[sum];
            for(int j=0;j<sum;j++){
                System.out.println("请输入第"+(j+1)+"辆车的序号:");
                b[j]=sc.nextInt();
            }
            System.out.println("请输入租车天数:");
            int days=sc.nextInt();
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            int ren = 0;
            for(int j=0;j<b.length;j++){
                //System.out.println(cars[b[j]-1].name);    
                if(cars[b[j]-1] instanceof Man_pick|| cars[b[j]-1] instanceof Pickup ){
                    //System.out.println(ren+"");
                    System.out.print(cars[b[j]-1].name+" ");
                    if(cars[b[j]-1] instanceof Man_pick){
                        Man_pick mtemp =(Man_pick) cars[b[j]-1];
                        ren+=mtemp.getMen();
                    }
                    else{
                        Pickup mtemp= (Pickup)cars[b[j]-1];
                        ren+=mtemp.getMen();
                    }
                }
                
                    
            }
            System.out.println("  共载人:"+ren+"人");
            System.out.println("***可载货的车有:");
            int goods=0,all=0;
            for(int j=0;j<b.length;j++){
                all+=cars[b[j]-1].getPrice();
                if(cars[b[j]-1] instanceof Truck|| cars[b[j]-1] instanceof Pickup ){
                    //System.out.println(ren+"");
                    System.out.print(cars[b[j]-1].name+" ");
                    if(cars[b[j]-1] instanceof Truck){
                        Truck mtemp =(Truck) cars[b[j]-1];
                        goods+=mtemp.getGoods();
                    }
                    else{
                        Pickup mtemp= (Pickup)cars[b[j]-1];
                        goods+=mtemp.getGoods();
                    }
                }
                
                    
            }
            System.out.println("  共载货:"+goods+"吨");
            System.out.println("***租车总价格:"+all*days+"元");
        }
        else {
            System.out.println("输入有误,程序退出!");
            System.exit(0);
        }
        
    }

}


提问者:海人丶 2018-07-19 14:07

个回答

  • weixin_慕的地2331628
    2019-09-17 22:06:10

    您好,请问for(Car car : cars)这里怎么理解   for循环结构可以这样写的吗



  • Nutrolize
    2018-08-30 14:51:47

    在计算一日的总价时,这个代码只计算了载货车辆的价格?

  • 慕娘5114374
    2018-08-07 10:05:47

    Man_pick mtemp =(Man_pick) car;   您好,请问一下这边为什么就必须要引用转换了,   我觉得同时自身类不需要的,可是去掉后就报错   想不明白

  • qq_兽耳控_0
    2018-08-02 16:48:21

    为什么子类里已经this.goods=goods了,后面还要写一个setgoods的方法

  • qq_龙之子_1
    2018-07-23 15:03:47

    System.exit(0); 有什么功能,我百度了说是退出状态,但是我用没有反应

  • qq_久未见你未变_0
    2018-07-21 09:18:01

    朋友你好,我jio得你写的代码很好的。想问问你上传代码怎么才能有这样的格式呢?

  • 海人丶
    2018-07-19 14:09:11

    刚开始也不会 看视频里的对象数组有了启发 很顺利就写出来了