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

答答租车系统作业 (初版)

正义的小伙伴
关注TA
已关注
手记 1
粉丝 0
获赞 17

父类Car

package com.imooc;

public class Car {
   private String CarName;//车名
   private double Price;//租用价格
   private int peopleLoad;//载客量
   private double goodLoad;//载货量
   //get和set方法
public String getCarName() {
    return CarName;
}
public void setCarName(String carName) {
    CarName = carName;
}
public double getPrice() {
    return Price;
}
public void setPrice(double price) {
    Price = price;
}
public int getPeopleLoad() {
    return peopleLoad;
}
public void setPeopleLoad(int peopleLoad) {
    this.peopleLoad = peopleLoad;
}
public double getGoodLoad() {
    return goodLoad;
}
public void setGoodLoad(double goodLoad) {
    this.goodLoad = goodLoad;
}

}

子类 bus

package com.imooc;

public class bus extends Car {

public bus(String CarName, double Price, int peopleLoad  ){
       this.setCarName(CarName);
       this.setPrice(Price);
       this.setPeopleLoad(peopleLoad);

   }
}

子类Trunk

package com.imooc;

public class Trunk extends Car {

    public  Trunk(String CarName, double Price, double goodLoad  ){
           this.setCarName(CarName);
           this.setPrice(Price);
           this.setGoodLoad(goodLoad);
       }
}

子类Pika

package com.imooc;

public class Pika extends Car {

    public  Pika(String CarName, double Price, int peopleLoad, double goodLoad  ){
           this.setCarName(CarName);
           this.setPrice(Price);
           this.setPeopleLoad(peopleLoad);
           this.setGoodLoad(goodLoad);
       }
}

测试程序 Test

package com.imooc;
import java.util.Scanner;
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[]carsForRent = {new bus("奥迪A4",500,4),new bus("马自达6",400,4),new Pika("皮卡雪",450,4,2),new bus("金龙",800,20),new Trunk("松花江",400,4),new Trunk("依维柯",1000,20)};

    System.out.println("欢迎使用答答租车系统:");
    System.out.println("您是否要租车:1是   0否");
    Scanner scan = new Scanner(System.in);//创建Scanner对象
    String input = scan.next();//存储
    if(input.equals("1")){
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号\t汽车名称\t租金\t\t容量");
        int i=1;
        for(Car currentCar:carsForRent){//遍历已存数组内的数据

            if(currentCar instanceof bus){//指向bus
                System.out.println(""+i+"\t"+currentCar.getCarName()+"\t"+currentCar.getPrice()+"\t\t载人:"+currentCar.getPeopleLoad());

            }else if(currentCar instanceof Trunk){//指向Trunk
                System.out.println(""+i+"\t"+currentCar.getCarName()+"\t"+currentCar.getPrice()+"\t\t载货:"+currentCar.getGoodLoad());
            }else{//指向Pika
                System.out.println(""+i+"\t"+currentCar.getCarName()+"\t"+currentCar.getPrice()+"\t\t载人:"+currentCar.getPeopleLoad()+"\t载货:"+currentCar.getGoodLoad());
            }
            i++;
        }
        System.out.println("请输入您要租车的数量:");
        Scanner carNum = new Scanner(System.in);
        int num = carNum.nextInt();
        while(num<=0){
            System.out.println("您输入有误,请重新输入:");
             num = carNum.nextInt();
        }
        double sum=0;
        int sumPeople=0;
        double sumGood=0;
        for(int j=1;j<=num;j++){

            System.out.println("请选择第"+j+"辆车");
            Scanner order = new Scanner(System.in);
            int NewNum = order.nextInt();
            while(NewNum<=0 NewNum>carsForRent.length){
                System.out.println("您输入有误,请重新输入:");
                NewNum = order.nextInt();
            }
            if(carsForRent[NewNum-1] instanceof bus){
            System.out.println("您选择了"+carsForRent[NewNum-1].getCarName()+"\t租金为每天"+carsForRent[NewNum-1].getPrice()+"元\t载客量为"+carsForRent[NewNum-1].getPeopleLoad()+"人");
            }else if(carsForRent[NewNum-1] instanceof Trunk){
                System.out.println("您选择了"+carsForRent[NewNum-1].getCarName()+"\t租金为每天"+carsForRent[NewNum-1].getPrice()+"元\t载货量为"+carsForRent[NewNum-1].getGoodLoad()+"人");
            }else{
                System.out.println("您选择了"+carsForRent[NewNum-1].getCarName()+"\t租金为每天"+carsForRent[NewNum-1].getPrice()+"元\t载客量为"+carsForRent[NewNum-1].getPeopleLoad()+"人\t载货量为"+carsForRent[NewNum-1].getGoodLoad()+"吨");
            }
             sum=sum+carsForRent[NewNum-1].getPrice();
             sumPeople=sumPeople+carsForRent[NewNum-1].getPeopleLoad();
             sumGood=sumGood+carsForRent[NewNum-1].getGoodLoad();
        }
          System.out.println("总共需要"+sum+"元\t总载客量为"+sumPeople+"人\t总载货量为"+sumGood+"吨");
    }else{
        System.out.println("欢迎您下次光临!");
    }
    }

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