问答详情
源自:12-1 综合练习

不知道在怎么改,第三条要求没实现

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
        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;
                    }    
                }    
            }
        System.out.println("请输入租车天数:");
        int k=input.nextInt();    
        System.out.println("总金额为:"+sum*k);   
        }
    
}


提问者:玉龙BB 2015-02-12 19:38

个回答

  • 云端漫漫步
    2015-12-21 21:58:35

    仅对你的代码做了轻微修改,思想就是用一个car数组存放选择的车型基本信息,如:numbe、name和租金。然后再累加计算,代码如下:
    
    package com.sun;
    
    import java.util.*;
    
    public class Demo {
    
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int sum = 0;
    		int j = 0;
    		int peopleCapSum = 0, cargoCapSum = 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();
    			Car[] carSelect = new Car[n];
    			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;
    					carSelect[i - 1] = car;
    					}
    				}
    			}
    			System.out.println("请输入租车天数:");
    			int k = input.nextInt();
    			System.out.println("您选择了租用如下车型:");
    			for (Car car : carSelect) {
    				System.out.println(car.number + "\t" + car.name + "\t" + car.rent + "元/天");
    				if (car instanceof PassengerCar) {
    					PassengerCar c = (PassengerCar) car;
    					peopleCapSum += c.getPeopleCapacity();
    				}
    				if (car instanceof PickUp) {
    					PickUp p = (PickUp) car;
    					peopleCapSum += p.getPeopleCapacity();
    					cargoCapSum += p.getCargoCapacity();
    				}
    				if (car instanceof Trunk) {
    					Trunk t = (Trunk) car;
    					cargoCapSum += t.getCargoCapacity();
    				}
    			}
    			System.out.println("以上车型共计可载人:" + peopleCapSum);
    			System.out.println("以上车型共计可载货:" + cargoCapSum);
    			System.out.println("总金额为:" + sum * k);
    		}
    
    	}
    }