为了账号安全,请及时绑定邮箱和手机立即绑定

写了一天,终于写完了,求大神指点

package mydadacent;

public class Car {
    private int number;
    private String name;
    private int busload;
    private  double centMoney;
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getBusload() {
        return busload;
    }
    public void setBusload(int busload) {
        this.busload = busload;
    }
    public double getCentMoney() {
        return centMoney;
    }
    public void setCentMoney(double centMoney) {
        this.centMoney = centMoney;
    }
    
    
    

}

package mydadacent;

import duotai.Bus;

public class PassengerCar extends Car {

    public PassengerCar(int number,String name,int busload,double centMonoy) {
        this.setNumber(number);
        this.setName(name);
        this.setBusload(busload);
        this.setCentMoney(centMonoy);
    }
    public void show(){
        System.out.println(getNumber() + ".    " + getName() + "\t" + getCentMoney() + "元/天" + "\t" + "载人" + getBusload() + "人" );
    }
}

package mydadacent;

public class PickUp extends Car {
    private double cargo;
    public double getCargo() {
        return cargo;
    }
    public void setCargo(double cargo) {
        this.cargo = cargo;
    }
    public PickUp(int number,String name,int busload,double cargo,double centMoney){
        this.setNumber(number);
        this.setName(name);
        this.setBusload(busload);
        this.setCargo(cargo);
        this.setCentMoney(centMoney);
    }
    public void show(){
        System.out.println(getNumber() + ".    " + getName() + "\t" + getCentMoney() + "元/天" + "\t" + "载人" + getBusload() + "载货" + getCargo() + "吨" );
    }
}



package mydadacent;

public class Truck extends Car{
    private double cargo;
    public Truck(int number,String name,double cargo,double centMoney){
        this.setNumber(number);
        this.setName(name);
        this.setCentMoney(centMoney);
        this.setCargo(cargo);
    }
    public double getCargo() {
        return cargo;
    }
    public void setCargo(double cargo) {
        this.cargo = cargo;
    }
    public void show(){
        System.out.println(getNumber() + ".    " + getName() + "\t" + getCentMoney() + "元/天" + "\t" + "载货" + getCargo() + "吨" );
    }
}


package mydadacent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class DaDaSystem {
     
     
    public static void main(String[] args) {
        System.out.println("欢迎进入哒哒租车系统!");
        System.out.println("您是否要租车?" + " " + "租车请按:1" + " " + "不租请按:0" );
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        if (i == 1) {
            System.out.println("您可租车的类型及其价目表:");
            Car[] car = {new PassengerCar(1, "奥迪A4", 4, 500),new PassengerCar(2, "马自达6", 4, 400),
                    new PassengerCar(3, "金龙", 30, 1000),new Truck(4, "松花江", 4, 300),new Truck(5, "依维柯", 20, 800),
                    new PickUp(6, "皮卡", 2, 4, 700)};
            System.out.println("序号" + "    汽车名称" + "        租金" + "\t" + "                 容量");
            for (Car c : car) {
                if (c instanceof PassengerCar) {
                     ((PassengerCar) c).show();
                }
                if(c instanceof Truck){
                    ((Truck) c).show();
                }
                if (c instanceof PickUp) {
                    ((PickUp) c).show();
                }
                
            }
            
            List<Car> sentCar = new ArrayList<Car>() ;//存储要租的车;
            System.out.println("请输入您要租车的数量:");
            int sentCarDay = s.nextInt();
            for(int j = 1;j <= sentCarDay;j++){
                System.out.println("请输入第"+ j + "辆车的序号");
                int num = s.nextInt();
                
                if(num <= 6){
                    
                    sentCar.add(car[num - 1]);//向ArrayList中添加选中的车
                    System.out.println("成功添加:" + car[num - 1].getName());
                }else {
                    System.out.println("输入有误!");
                    break;
                }
            }
            System.out.println("请输入要租车的天数:");
            int sentDay = s.nextInt();
            
            List<Car> sentPassCar = new ArrayList<Car>();
            List<Car> sentTruckCar = new ArrayList<Car>();
            int sumPeople = 0;
            double sumCargo = 0; 
            double money = 0;
            //用迭代器遍历出载人的车和载货的车
            Iterator<Car> li = sentCar.iterator();

            while(li.hasNext()){
                Car selectCar = li.next();
                if(selectCar instanceof PassengerCar){
                    sentPassCar.add(selectCar);
                    sumPeople += selectCar.getBusload();
                    money += selectCar.getCentMoney();
                }
                
                else if(selectCar instanceof Truck){
                    sentTruckCar.add(selectCar);
                    sumCargo += ((Truck) selectCar).getCargo();
                    money += selectCar.getCentMoney();
                }
                else if (selectCar instanceof PickUp){
                    sentPassCar.add(selectCar);
                    sentTruckCar.add(selectCar);
                    sumPeople += selectCar.getBusload();
                    sumCargo += ((PickUp) selectCar).getCargo();
                    money += selectCar.getCentMoney();
                }
                
        }
            System.out.println("可用于载人的车有------------------------------------");
            Iterator<Car> iPass= sentPassCar.listIterator();
            while (iPass.hasNext()) {
                
                System.out.print(iPass.next().getName()+ "\t");
            }
            
            System.out.println("共载人:" + sumPeople + "人");
            System.out.println("可用于载货的车有------------------------------------");
            Iterator<Car> iTruck= sentTruckCar.listIterator();
            while (iTruck.hasNext()) {
                
                System.out.print(iTruck.next().getName() + "\t");
            }
            
            System.out.println( "\t" + "共载货:" + sumCargo + "吨");    
            
            System.out.println("租车总价格为:" + money*sentDay + "元");
                
            
        }
        else if (i == 0) {
            System.out.println("感谢您使用哒哒租车系统!");
        }
        else {
            System.out.println("输入有误!请重新输入!");
        }
    }

}


正在回答

4 回答

 ((PassengerCar) c).show();

 ((Truck) c).show();

((PickUp) c).show();不太了解这三段代码的使用,大神求赐教!!

0 回复 有任何疑惑可以回复我~

另外语句import duotai.Bus;好像是多余的。

0 回复 有任何疑惑可以回复我~

你才是大神啊!问一下cargo参数是干嘛用的??

0 回复 有任何疑惑可以回复我~

Your are the big supernatural being

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

写了一天,终于写完了,求大神指点

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信