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

Java入门第二季 答答租车系统

标签:
Java
public class Car {
    public String name;
    public int price;
}

public class Manned extends Car {
              private int carry;
              public Manned(String name,int price ,int carry){
                  this.name=name;
                  this.price=price;
                  this.carry=carry;
              }
         public int getCarry(){
             return carry;
         }
         public String toString(){
             return(name+"\t"+price+"元/天 \t"+carry+"人\t");

         }
}

public class Truck extends Car {
     private int load;
   public Truck(String name,int price ,int load){
       this.name=name;
       this.load=load;
       this.price=price;
   }
   public int getLoad(){
       return load;
   }
   public String  toString(){
       return (name+"\t"+price+"元/天"+"\t"+load+"吨\t");

   }
}

public class Both extends Car  {
   private int carry;
   private int load;
   public Both(String name,int price,int carry,int load){
       this.name=name;
       this.carry=carry;
       this.load=load;
       this.price=price;
   }
   public int  getCarry(){
       return carry;}
       public int  getLoad(){
           return load;

   }
       public String toString(){
           return(name+"\t"+price+"元/天\t"+carry+"人\t"+load+"吨\t");

       }
}
import java.util.*;
public class Test {
 public static void main(String[] args) {
    Car[] car=new Car[]{
                         new Manned("奥迪A4",500,4),new Manned("马自达6",400,4),new Both("皮卡雪6",450,4,2),
                         new Manned("金龙",800,20),  
                         new Truck("松花江",400,4),new Truck("依维柯",1000,20) 

    };
    Scanner input=new Scanner(System.in);
    System.out.println("欢迎使用答答租车系统:");
    System.out.println("您是否要租车:1是 0否");
    if(input.nextInt()==1){
         System.out.println("您可租车的类型及其价目表:");
         System.out.println("序号\t汽车名称 \t租金\t 容量\t");
            for(int i=0;i<car.length;i++){
             System.out.println((i+1)+"\t"+car[i]);
         }
            System.out.println("请输入您要租汽车的数量");
            int carNum=input.nextInt();
            Car[] userCar=new Car[carNum];
            for(int j=1;j<=carNum;j++){
                System.out.println("请输入第"+j+"辆车的序号");
                int num;
                num=input.nextInt();
                userCar[j-1] = car[num-1];
               }
            System.out.println("请输入租车天数");
            int rentday;
            rentday=input.nextInt();
            System.out.println("您的帐单");
            totalCarry(userCar); 
            totalLoad(userCar);
            totalPay(userCar,rentday);

      }
    input.close();

    }
 public static void totalCarry(Car[] userCar){
     int k=0;
     System.out.println("***可载人的车有:");
     for(int i=0;i<userCar.length;i++){
         if(userCar[i]instanceof Manned){
             System.out.print(userCar[i].name+"\t");
             k=k+((Manned)userCar[i]).getCarry();
         }
         else if(userCar[i]instanceof Both){
             System.out.print(userCar[i].name+"\t");
             k=k+((Both)userCar[i]).getCarry();
         }
     }
     System.out.println("共载人数:"+k+"人");
 }
 public static void totalLoad(Car[] userCar){
     int k=0;
     System.out.println("***可载货的车有:");
     for(int i=0;i<userCar.length;i++){
         if(userCar[i]instanceof Truck){
             System.out.print(userCar[i].name+"\t");
             k=k+((Truck)userCar[i]).getLoad();
         }
         else if(userCar[i]instanceof Both){
             System.out.print(userCar[i].name+"\t");
             k=k+((Both)userCar[i]).getLoad();
         }
     }
     System.out.println("共载物:"+k+"吨");
 }

 public static void totalPay(Car[]userCar,int rentday ){
     int k=0;
     for(int i=0;i<userCar.length;i++)
     {
         k=k+userCar[i].price;
     }
     k=k*rentday;
     System.out.println("租车的总费用为:"+k+"元");
 }

}

自己写不出来 就借鉴一下 ???

点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消