完美呈现输出

来源:12-1 综合练习

玉龙BB

2015-02-12 20:40

package com.sun;

public class Car {
    int number;
    public String name;
    public int rent;
    
}
package com.sun;

public class PassengerCar extends Car {
    private int peopleCapacity;
    public PassengerCar(){
        
    }
    public PassengerCar(int number,String name,int rent,int peopleCapacity){
        this.number=number;
        this.name=name;
        this.rent =rent;
        this.setPeopleCapacity(peopleCapacity);
        
    }
    public double getPeopleCapacity() {
        return peopleCapacity;
    }
    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }

}
package com.sun;

public class Trunk extends Car {
    private int cargoCapacity;
    public Trunk(int number,String name,int rent,int cargoCapacity){
        this.number=number;
        this.name =name;
        this.rent =rent;
        this.setCargoCapacity(cargoCapacity);
        
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(int cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
}
package com.sun;

public class PickUp extends Car {
    private int peopleCapacity;
    private int cargoCapacity;
    public PickUp(int number,String name,int rent,int peopleCapacity,int cargoCapacity){
        this.number=number;
        this.name=name;
        this.rent =rent;
        this.peopleCapacity=peopleCapacity;
        this.cargoCapacity=cargoCapacity;
        
    }
    public double getPeopleCapacity() {
        return peopleCapacity;
    }
    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(int cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
}
package com.sun;
import java.util.*;
public class Demo {
    
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Car> l=new ArrayList<Car>();//载人
        ArrayList<Car> l2=new ArrayList<Car>();//载货
        int sum = 0;
        int j=0;
        Car[] carsForRent={new PassengerCar(1,"奥迪A4",500,4),new PassengerCar(2,"马自达6",400,4),new PickUp(3,"皮卡雪6",450,4,2),new PassengerCar(4,"金龙",800,20),new Trunk(5,"松花江",400,4),new Trunk(6,"依维柯",1000,20)};
        System.out.println("欢迎使用答答租车系统\n您是否要租车:1 是 0否");
        Scanner input=new Scanner(System.in);
        switch(input.nextInt()){
        case 0:
            System.out.println("谢谢使用");
            break;
        case 1:
            System.out.println("您可租车的类型及其价目表:\n序号\t汽车名称\t租金\t容量");
            for(Car car: carsForRent){
                if(car instanceof PassengerCar){
                    PassengerCar c=(PassengerCar) car;
                System.out.println(car.number+"\t"+car.name+"\t"+car.rent+"元/天\t载人:"+c.getPeopleCapacity()+"人");
                }
                if(car instanceof PickUp){
                    PickUp p=(PickUp) car;
                    System.out.println(car.number+"\t"+car.name+"\t"+car.rent+"元/天\t载人:"+p.getPeopleCapacity()+"人 载货: "+p.getCargoCapacity()+"吨");
                    }
                if(car instanceof Trunk){
                    Trunk t=(Trunk) car;
                    System.out.println(car.number+"\t"+car.name+"\t"+car.rent+"元/天\t载货: "+t.getCargoCapacity()+"吨");
                    }
                }
                 System.out.println("请输入您要租汽车的数量:");
                int n=input.nextInt();
                for(int i=1;i<=n;i++){
                    System.out.println("请输入第"+i+"辆车的序号:");
                    j=input.nextInt();
                    for(Car car: carsForRent){
                    if(j==car.number){
                        sum+=car.rent;
                        if(j==1||j==2||j==4){
                        l.add(car);
                        }else if(j==5||j==6){
                            l2.add(car);
                        }else{
                            l.add(car);
                            l2.add(car);
                        }
                    }    
                }    
            }
    
        System.out.println("请输入租车天数:");
        int k=input.nextInt();    
        System.out.println("您的账单:");
        
        System.out.println("**********可载人的车有");
        int s=0;
        for(Car car:l){
            if(car instanceof PassengerCar){
            PassengerCar c1=(PassengerCar) car;
    
            
            s+=(int) c1.getPeopleCapacity();
            if(car instanceof PickUp){
                PickUp p1=(PickUp) car;
                s=(int) (s+p1.getPeopleCapacity());
            }}
            System.out.print(car.name+" ");}
            System.out.println("     共可载"+s+"人");
            
        System.out.println("**********可载货的车有");
        int s1=0;
        for(Car car:l2){
            if(car instanceof Trunk){
            Trunk t1=(Trunk) car;
            s1+=(int) t1.getCargoCapacity();}
            if(car instanceof PickUp){
                PickUp p2=(PickUp) car;
                s1=(int) (s1+p2.getCargoCapacity());
            }
            System.out.print(car.name+" ");
        }        System.out.println("     共可载"+s1+"吨");
        
        System.out.println("租车总价格:"+sum*k);   
        }
    }
}
写回答 关注

3回答

  • YunleiLiu
    2015-02-27 11:55:43

    大神啊

  • 爱德菲尔德
    2015-02-15 21:38:38

    for(Car car:l)这个foreach是什么意思?


    好逑不君子

    显示一下所有车的信息

    2015-02-28 10:43:03

    共 2 条回复 >

  • 爱德菲尔德
    2015-02-15 19:58:28

    没有封装吧

    玉龙BB

    多谢提点

    2015-02-17 02:13:09

    共 1 条回复 >

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530559 学习 · 6091 问题

查看课程

相似问题