DarkHorseCele
2015-04-28 21:23
车类
package com.DiDiCar; public abstract class Car { private String carName; //车名 private int carId; //车ID private float price; //租价 private float carCargo; //载货量 private int carPerson; //载客量 public abstract void showInfo(); // 显示车信息 public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarId() { return carId; } public void setCarId(int carId) { this.carId = carId; } public float getPrice() { return price; } public void setPrice(int price) { this.price = price; } public float getCarCargo() { return carCargo; } public void setCarCargo(int carCargo) { this.carCargo = carCargo; } public int getCarPerson() { return carPerson; } public void setCarPerson(int carPerson) { this.carPerson = carPerson; } }
汽车类
package com.DiDiCar; public class Automobile extends Car { private String carName; //车名 private int carId; //车ID private float price; //租价 private int carPerson; //载客量 public Automobile(String carName, int carId, int price, int carPerson) { super(); this.carName = carName; this.carId = carId; this.price = price; this.carPerson = carPerson; } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarId()+"."+'\t' + getCarName() + '\t' +getPrice() +"元/天" + '\t' + "载人:"+getCarPerson() +"人"); } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarId() { return carId; } public void setCarId(int carId) { this.carId = carId; } public float getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getCarPerson() { return carPerson; } public void setCarPerson(int carPerson) { this.carPerson = carPerson; } }
货车类
package com.DiDiCar; public class Truck extends Car { private String carName; //车名 private int carId; //车ID private float price; //租价 private float carCargo; //载货量 public Truck(String carName, int carId, int price, int carCargo) { super(); this.carName = carName; this.carId = carId; this.price = price; this.carCargo = carCargo; } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarId()+"."+'\t' + getCarName() + '\t' +getPrice() +"元/天" + '\t' + "载货:"+getCarCargo() +"吨"); } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarId() { return carId; } public void setCarId(int carId) { this.carId = carId; } public float getPrice() { return price; } public void setPrice(int price) { this.price = price; } public float getCarCargo() { return carCargo; } public void setCarCargo(int carCargo) { this.carCargo = carCargo; } }
皮卡车类
package com.DiDiCar; public class Pickup extends Car { private String carName; //车名 private int carId; //车ID private float price; //租价 private float carCargo; //载货量 private int carPerson; //载客量 public Pickup(String carName, int carId, int price, int carCargo, int carPerson) { super(); this.carName = carName; this.carId = carId; this.price = price; this.carCargo = carCargo; this.carPerson = carPerson; } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println(getCarId()+"."+'\t' + getCarName() + '\t' +getPrice() +"元/天" + '\t' + "载人:"+getCarPerson() +"人"+"载货:"+getCarCargo() +"吨"); } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarId() { return carId; } public void setCarId(int carId) { this.carId = carId; } public float getPrice() { return price; } public void setPrice(int price) { this.price = price; } public float getCarCargo() { return carCargo; } public void setCarCargo(int carCargo) { this.carCargo = carCargo; } public int getCarPerson() { return carPerson; } public void setCarPerson(int carPerson) { this.carPerson = carPerson; } }
实现
package com.DiDiCar; import java.util.Scanner; public class Realize { public static void main(String[] args) { // TODO Auto-generated method stub //创建车辆对象数组,分别指向不同类型车辆 Car[] car ={new Automobile("奥迪A4",1,500,4),new Automobile("马自达6",2,400,4),new Pickup("皮卡雪6",3,450,2,4),new Automobile("金龙 ",4,800,20),new Truck("松花江",5,400,4),new Truck("依维河",6,1000,20)}; Scanner input = new Scanner(System.in); System.out.println("*******************欢迎使用滴滴租车系统:*******************"); //显示租车信息 while (true) { System.out.println("您是否要租车:1 是 0 否"); int choise = input.nextInt(); if (choise == 0) { System.out.println("感谢您使用滴滴租车系统,再见!"); break; } else if (choise == 1) //进入系统 { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号" + '\t' + "汽车名称" + '\t' + "租金" + '\t' +"容量"); for(int i=0;i<car.length;i++) { car[i].showInfo(); //显示车的信息 } break; } else //输入错误,从新输入 { System.out.println("您的输入信息有误,请从新输入:"); } } //输入租车数量、种类、日期 System.out.println("请输入您要租汽车的数量:"); int num = input.nextInt(); //租车数量 if(num>10||num<1) { System.out.println("超出租车数量范围,请从新输入:"); num = input.nextInt(); } Car[] carChoice = new Car[num]; //客户选择的车辆对象数组 for(int i=0;i<carChoice.length;i++) { System.out.println("请输入第"+(i+1)+"辆车的序号:"); int carNum = input.nextInt(); //客户选择的车序号 if(carNum>6||carNum<1) { System.out.println("输入有误,请从新输入:"); carNum = input.nextInt(); } carChoice[i] = car[carNum-1]; } System.out.println("请输入租车天数:"); int Day = input.nextInt(); //租车天数 if(Day>30||Day<1) { System.out.println("输入有误,请从新输入:"); Day = input.nextInt(); } //计算并显示账单 int totalCarPerson=0; //总的载客量 float totalCarCargo=0; //总的载货量 float dayPrice=0; //每天的租金 float totalPrice=0; //总价 System.out.println("*******************您的账单:*******************"); System.out.println("*******可载人车有:"); for(int i=0;i<carChoice.length;i++) { if(carChoice[i] instanceof Automobile||carChoice[i] instanceof Pickup) { System.out.print('\t'+carChoice[i].getCarName()); totalCarPerson += carChoice[i].getCarPerson(); } dayPrice += carChoice[i].getPrice(); } System.out.println('\t'+"共载人:"+totalCarPerson+"人"); System.out.println("*******可载货车有:"); for(int i=0;i<carChoice.length;i++) { if(carChoice[i] instanceof Truck||carChoice[i] instanceof Pickup) { System.out.print('\t'+carChoice[i].getCarName()); totalCarCargo += carChoice[i].getCarCargo(); } } System.out.println('\t'+"共载货:"+totalCarCargo+"吨"); totalPrice = dayPrice*Day; System.out.println("********租车总价格:"+totalPrice+"元"); input.close(); } }
get()和set()和抽象类一样就不用重写了
Java入门第二季 升级版
530649 学习 · 6091 问题
相似问题