java实现租车系统

来源:互联网 发布:java boolean 异或 编辑:程序博客网 时间:2024/04/30 09:32

  1:父类

package com.imooc.car;
/* 
 * 总共有三种车型:载人Passenger,载货Goods,载人载货Loader 
 * Car 为这三种车型的父类 
 * 有4种属性: 
 * 编号 = number 
 * 品牌 = brand 
 * 租金/天 = fee 
 * 载人容量 = personCapacity 
 * 载货容量 = goodCapacity 
 */  
public class Car {  
    int number;  
    String brand;  
    double fee;  
    int personCapacity;  
    double goodCapacity;  
      
    public  Car(int number, String brand, double fee){   //构造方法  
        this.number = number;  
        this.brand = brand;  
        this.fee = fee;  
    }  
      
    public int getNumber(){  
        return number;  
    }  
      
    public String getBrand(){  
        return brand;  
    }  
      
    public double getFee(){  
        return fee;  
    }  
      
    public int getPersonCapacity(){  
        return personCapacity;  
    }  
      
    public double getGoodCapacity(){  
        return goodCapacity;  
    }  
      
}  


 2:子类一:(载人的车)

package com.imooc.car;
/* 
 * PassengerCar为载人汽车,除了Car中的属性之外还有载人容量 personCapacity 
 */  
public class PassengerCar extends Car{  
      
    private int personCapacity;  
  
    public PassengerCar(int number, String brand, double fee, int personCapacity) {  
          
        super(number, brand, fee);  
        this.personCapacity = personCapacity;  
    }  
      
    public int getPersonCapacity() {  
        return personCapacity;  
    }  
      
    @Override  
    public String toString() {  
        return number + "\t" + brand + "\t" + fee + "元/天\t" + personCapacity + "人\n";  
    }  
      
}  
 

 3:子类二(载货汽车)

package com.imooc.car;


/* 
 * Goods为载货汽车,除了Car中的属性之外还有载货容量 goodCapacity 
 */  
public class Goods extends Car{  
      
    private double goodCapacity;  
      
    public Goods(int number, String brand, double fee, double goodCapacity) {  
          
        super(number, brand, fee);  
        this.goodCapacity = goodCapacity;  
          
    }  
      
    public double getGoodCapacity(){  
        return goodCapacity;  
    }  
      
    public String toString() {  
        return number +  "\t" + brand + "\t" + fee + "元/天\t" + goodCapacity + "吨" + "\n";  
    }  
      
}  

 4:子类四(载人载物车)

package com.imooc.car;


/* 
 * Loader为载人载货汽车,除了Car中的属性之外还有载人容量 personCapacity,载货容量goodCapacity 
 */  
public class Loader extends Car{  
  
    private int personCapacity;  
    private double goodCapacity;  
      
    public Loader(int number, String brand, double fee, int personCapacity, double goodCapacity) {  
          
        super(number, brand, fee);  
        this.personCapacity = personCapacity;  
        this.goodCapacity = goodCapacity;  
          
    }  
      
    public int getPersonCapacity() {  
        return personCapacity;  
    }  
      
    public double getGoodCapacity(){  
        return goodCapacity;  
    }  
      
    @Override  
    public String toString() {  
        return number +  "\t" + brand + "\t" + fee + "元/天\t" +  
    personCapacity + "人\t" + goodCapacity + "吨\n";  
    }  
}  


       5:主方法

package com.imooc.car;
import java.util.Scanner;  
  
public class RentCars {  
  
    public static void main(String[] args){  
          
        Scanner input = new Scanner(System.in);  
        Car[] cars = new Car[6];  
          
        System.out.println("欢迎使用答答租车系统:");  
        System.out.println("您是否要租车?1、是0、否(请输入1或0)");  
        int input1 = input.nextInt();  
        if (input1 == 1){  
            System.out.println("下面是所有车的信息:");  
              
            cars[0] = new PassengerCar(1, "奥迪A4", 500.0, 4);  
            cars[1] = new PassengerCar(2, "马自达6", 400.0, 4);  
            cars[2] = new Loader(3, "皮卡雪6", 450.0, 4, 2);  
            cars[3] = new PassengerCar(4, "金龙", 800.0,  20);  
            cars[4] = new Goods(5, "松花江", 400.0, 4);  
            cars[5] = new Goods(6, "依维柯", 1000.0, 20);  
              
            System.out.println("序号\t" + "汽车名称\t" + "租金\t\t" + "容量(载人/载货)");  
            for(int i = 0; i < cars.length; i++){  
             System.out.println("编号:"+ (i+1) +"   品牌:"+ cars[i].getBrand()   
                          +"   租金:"+ cars[i].getFee() +"/天       载客量:"+ cars[i].getPersonCapacity()+"人"  
                         +"   载货量:"+ cars[i].getGoodCapacity()+"吨" );  
          }    
        }else{  
            System.out.println("谢谢使用,再见!");  
        }  
      
        System.out.print("请输入你要租几种车:");  
        int rentNum = input.nextInt();  
          
        //selected用来保存客户选中了什么车型,以及每种车型的辆数,与car数组是对应关系  
        int[] selected = new int[6];  
          
        for (int i = 1; i <= rentNum; i++){  
            System.out.println("请输入第" + i + "种车型的序号:" );  
            int nums = input.nextInt() - 1;  
            System.out.println(cars[nums].getBrand() +"总共需要多少辆:");  
            int num = input.nextInt();  
            selected[nums] = num;  
        }  
          
        System.out.println("请输入租车天数:");  
        int daysNum = input.nextInt();  
  
        System.out.println("您的账单:--------------------------");  
        double total = 0;  
        for (int i = 0; i < cars.length; i++){  
            if (selected[i] !=0 ){  
                System.out.println(selected[i] + "辆" + cars[i].getBrand() +  
                        "   总共载客量:"+selected[i]*cars[i].getPersonCapacity()+"人"+  
                        "   总共载货量:"+selected[i]*cars[i].getGoodCapacity()+"吨"+  
                        "   "+daysNum+"天单项费用:"+selected[i]*cars[i].getFee()*daysNum+"元");  
                total += selected[i]*cars[i].getFee()*daysNum;  
            }  
        }  
        System.out.println("租车总费用:" + total + "元" + "\n" + "欢迎下次光临!------------------------");  
    }  
}  

0 0
原创粉丝点击