package imooc;
import java.util.Scanner;
public class text {
@SuppressWarnings("resource")
public static void main(String[] args) {
// 创建一个数组,存放已有的车型
Vehicle [] carForRent={new Bus(1,"奥迪A4",500,4),new Bus(2,"马自达6",400,4),
new Pickup(3,"皮卡雪6",450,4,2),new Bus(4,"金龙 ",800,20),
new truck(5,"松花江",400,4),new truck(6,"依维柯",1000,20)};
System.out.println("欢迎使用租车系统!");
System.out.println("您是否要租车:1是 0否");
Scanner temp =new Scanner(System.in);
int a=temp.nextInt();
if(a==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金/元"+"\t"+"载客数/人"+"\t"+"载货数/吨");
for(int i=0;i<carForRent.length;i++){
System.out.println(carForRent[i]);
}
}
else {
System.out.println("退出租车系统,欢迎下次使用");
System.exit(0);
}
System.out.print("请输入您要租汽车的数量:");
int b =temp.nextInt();
int calc[]=new int [b];
for(int i=0;i<calc.length;i++){
System.out.println("请输入第"+ (i+1) +"辆车的序号");
calc[i]=temp.nextInt();
}
System.out.print("请输入租车的天数:");
int c =temp.nextInt();
System.out.println("您的账单" );
System.out.println("***可载人的车有:" );
int men =0;
for(int i =0 ;i <calc.length ;i++){
if (carForRent[calc[i]-1].getCarCapacity_man() != 0 ){
System.out.println(carForRent[calc[i]-1].getCarName()+"\t");
men=men+carForRent[calc[i]-1].getCarCapacity_man();
}
}
System.out.println("共载"+ men +"人");
System.out.println("***可载货的车有:" );
int goods=0;
for(int i =0 ;i <calc.length ;i++){
if (carForRent[calc[i]-1].getCarCapacity_goods() != 0 ){
System.out.println(carForRent[calc[i]-1].getCarName()+"\t");
goods=goods+carForRent[calc[i]-1].getCarCapacity_goods();
}
}
System.out.println("共载货"+goods+"吨");
System.out.print("租车总价格:");
int money=0;
for(int i=0;i<calc.length;i++){
money=money+carForRent[calc[i]-1].getCarRentMoney();
}
money=money*c;
System.out.println("***租车总价格:"+money);
}
}
package imooc;
//定义父类
public class Vehicle {
private int carNumber; //定义汽车的序号
private String carName; //定义汽车的名字
private int carRentMoney; //定义汽车的租金
private int carCapacity_man;//定义汽车的载客量
private int carCapacity_goods;//定义汽车的载货量
public int getCarNumber() {
return carNumber;
}
public void setCarNumber(int carNumber) {
this.carNumber = carNumber;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarRentMoney() {
return carRentMoney;
}
public void setCarRentMoney(int carRentMoney) {
this.carRentMoney = carRentMoney;
}
public int getCarCapacity_man() {
return carCapacity_man;
}
public void setCarCapacity_man(int carCapacity_man) {
this.carCapacity_man = carCapacity_man;
}
public int getCarCapacity_goods() {
return carCapacity_goods;
}
public void setCarCapacity_goods(int carCapacity_goods) {
this.carCapacity_goods = carCapacity_goods;
}
@Override
public String toString() {
return carNumber + "\t" + carName + "\t" + carRentMoney
+ "\t" + carCapacity_man + "\t" + carCapacity_goods;
}
}
package imooc;
public class Bus extends Vehicle {
public Bus(int carNumber,String carName,int carRentMoney,int carCapacity_man){
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.setCarCapacity_man(carCapacity_man);
}
}
package imooc;
public class Pickup extends Vehicle {
public Pickup(int carNumber, String carName, int carRentMoney, int carCapacity_goods, int carCapacity_man) {
// TODO Auto-generated constructor stub
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.setCarCapacity_goods(carCapacity_goods);
this.setCarCapacity_man(carCapacity_man);
}
}
package imooc;
public class truck extends Vehicle {
public truck(int carNumber, String carName, int carRentMoney, int carCapacity_goods) {
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.setCarCapacity_goods(carCapacity_goods);
}
}
热门评论
父类用private定义变量,子类不会继承呀~~~