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

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

标签:
Java

父类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("欢迎您下次光临!");
    }
    }

}
点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消