手记

答答租车系统java代码

//测试
package com.imooc;

import java.util.Scanner;

public class Test {

/**
 * @param args
 */

public static void main(String[] args) {
    // TODO Auto-generated method stub
  System.out.println("欢迎使用租车系统:");
  System.out.println("您是否要租车?  0:否  ,1:是");
  //定义车的数组
    Car[] car = {new PassengerCar("奥迪40",500,4),
             new PassengerCar("马自达6",400,4),
             new PickUpCar("皮卡雪6",450,4,2),
             new PassengerCar("金龙",800,20),
             new TruckCar("松花江",400,4),
             new TruckCar("依维柯",1000,20)};
int flag[] = new int[car.length];//标记,方便下面的统计
//初始化
for(int m = 0;m<car.length;m++){
    flag[m] = 0;
}
  Scanner input = new Scanner(System.in);
  int first = input.nextInt();
  if(first==0){
      System.out.println("谢谢使用。");
  }else{
     System.out.println("您可租车的类型及价目表:");
     System.out.println("序号\t汽车名称\t租金\t\t容量\t");
     for(int i = 0;i<car.length;i++){
         System.out.print(i+1+".\t");
         Test senond = new Test();
         Test.Carprint(car[i]);//调用下面的函数
     }
     System.out.println("请输入您要租汽车的数量:");
     int number = input.nextInt();
     for(int n = 0;n < number;n++){         
         System.out.println("请输入第"+(n+1)+"辆车的序号:");
         int number1 = input.nextInt();
         flag[number1-1] = 1;
         if(number1>6||number1<0){
             System.out.println("输入有误。");
         }

     }
     int sum1=0;//载人数
     int boatloadsum = 0;//载货数
     int pricesum;//总金额
     int pricesum1 = 0;//载人金额
     int pricesum2 = 0;//载货金额
     System.out.println("请输入租车的天数:");
     int number2 = input.nextInt();
     System.out.println("您的账单:");
     System.out.println("可载人的车有:");
     for(int x = 0;x<car.length;x++){
         if(flag[x]>0&&car[x]instanceof PassengerCar||car[x] instanceof PickUpCar){
             System.out.print(car[x].getname());
             sum1+=car[x].getbusload();
             pricesum1+=number2*car[x].getrent();
         }
        }
     System.out.println("共载人:"+sum1+"人");
     System.out.println("可载货的车有:");
     for(int y = 0;y<car.length;y++){
         if(flag[y]>0&&car[y]instanceof TruckCar||car[y] instanceof PickUpCar){
             System.out.print(car[y].getname());
             boatloadsum+=car[y].getboatload();
             pricesum2+=number2*car[y].getrent();
         }
        }
     pricesum = pricesum1+pricesum2;
     //可载人可载货的金额会重复,所以这里要减掉
     for(int z = 0;z<car.length;z++){
         if(flag[z]>0&&car[z] instanceof PickUpCar)
             pricesum-=number2*car[z].getrent();
     }
     System.out.println("共载货:"+boatloadsum+"吨");
     System.out.print("租车总价格为:"+pricesum+"元");
     }
  }

public static void Carprint(Car car){
    System.out.print(car.getname()+"\t"+car.getrent()+"元/天\t\t");
    if(car instanceof PassengerCar){
        System.out.println("载人:"+car.getbusload()+"人");
    }else if(car instanceof TruckCar){
        System.out.println("载货:"+car.getboatload()+"吨");
    }else if(car instanceof PickUpCar){
        System.out.println("载人:"+car.getbusload()+"人"+"载货:"+car.getboatload()+"吨");
    }
}

}

//父类car

package com.imooc;

public class Car {
String CarName;
int rent;
int busload;
int boatload;
public String getname(){
return CarName;
}
public void setname(){
this.CarName=CarName;
}
public int getrent(){
return rent;
}
public void setrent(){
this.rent = rent;
}
public int getbusload(){
return busload;
}
public void setboatload(){
this.busload = busload;
}
public int getboatload(){
return boatload;
}
public void setboatload1(){
this.busload = boatload;
}
}

//可载人可载物

package com.imooc;

public class PickUpCar extends Car {
private String CarName;//车名
private int rent;//租金
private int busload;//载客量
private int boatload;//载货量
public PickUpCar(String CarName,int rent,int busload,int boatload){
this.CarName = CarName;
this.rent = rent;
this.busload = busload;
this.boatload=boatload;
}
public String getname(){
return CarName;
}
public void setname(){
this.CarName = CarName;
}
public int getrent(){
return rent;
}
public void setrent(){
this.rent = rent;
}
public int getbusload(){
return busload;
}
public void setboatload(){
this.busload = busload;
}
public int getboatload(){
return boatload;
}
public void setboatload1(){
this.busload = boatload;
}
}

//可载人的

package com.imooc;

public class PassengerCar extends Car {
private String CarName;//车名
private int rent;//租金
private int busload;//载客量
public PassengerCar(String CarName,int rent,int busload){
this.CarName = CarName;
this.rent = rent;
this.busload = busload;
}
public String getname(){
return CarName;
}
public void setname(){
this.CarName = CarName;
}
public int getrent(){
return rent;
}
public void setrent(){
this.rent = rent;
}
public int getbusload(){
return busload;
}
public void setboatload(){
this.busload = busload;
}
}

//载物的

package com.imooc;

public class TruckCar extends Car {
private String CarName;//车名
private int rent;//租金
private int boatload;//载货量
public TruckCar(String CarName,int rent,int boatload){
this.CarName = CarName;
this.rent = rent;
this.boatload=boatload;
}
public String getname(){
return CarName;
}
public void setname(){
this.CarName = CarName;
}
public int getrent(){
return rent;
}
public void setrent(){
this.rent = rent;
}
public int getboatload(){
return boatload;
}
public void setboatload1(){
this.busload = boatload;
}
}

2人推荐
随时随地看视频
慕课网APP

热门评论

为什么要把载客量和载货量封装起来呢?类名.属性名调用子类的属性不可以吗?

查看全部评论