第一次,烂到爆炸的java程序,第一次总想把一切特殊情况考虑进去,比如两次选中同一款车辆等等,最后还是作罢,实现已经花了一个下午。
//所有的车,是Truck,Coach,Pickup的父类
public abstract class allCar {
protected String name;
protected float price;
protected int passenger;
protected float freight;
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public float getFreight() {
return freight;
}
public void setFreight(float freight) {
this.freight = freight;
}
public void info() {
System.out.println("这是一辆车");
}
public abstract boolean canPassenger();
public abstract boolean canFreight();
}
//客车类,包括大型客车和小客车
public class Coach extends allCar {
private String name;
private float price;
private int passenger;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载客量:"+passenger+"人");
}
//用到构造函数
public boolean canPassenger(){
return true;
}
public boolean canFreight(){
return false;
}
public Coach(String name,float price,int passenger){
this.name=name;
this.price=price;
this.passenger=passenger;
}
}
public class Car extends Coach {
//继承父类Coach的构造函数
public Car(String name, float price, int passenger) {
super(name, price, passenger);
// TODO Auto-generated constructor stub
}
}
public class Bus extends Coach {
//继承父类Coach的构造函数
public Bus(String name, float price, int passenger) {
super(name, price, passenger);
// TODO Auto-generated constructor stub
}
}
//货车类,包括重型货车和轻型货车
public class Truck extends allCar{
protected float freight;
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载货量"+freight+"吨");
}
public boolean canPassenger(){
return false;
}
public boolean canFreight(){
return true;
}
}
public class lightTruck extends Truck {
private String name;
private float price;
private float freight;
public float getFreight() {
return freight;
}
public void setFreight(float freight) {
this.freight = freight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public lightTruck(String name, float price, int freight) {
this.name=name;
this.price=price;
this.freight=freight;
// TODO Auto-generated constructor stub
}
@Override
public void info() {
// TODO Auto-generated method stub
System.out.print("类型:轻型货车 ");
System.out.println("名称:"+name+" 价格:"+price+"元/天 载货量"+freight+"吨");
}
}
public class heavyTruck extends Truck {
private String name;
private float price;
private float freight;
public float getFreight() {
return freight;
}
public void setFreight(float freight) {
this.freight = freight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public heavyTruck(String name,float price,int freight){
this.name=name;
this.price=price;
this.freight=freight;
}
@Override
public void info() {
// TODO Auto-generated method stub
System.out.print("类型:重型货车 ");
System.out.println("名称:"+name+" 价格:"+price+"元/天 载货量"+freight+"吨");
}
}
//皮卡单独一类
public class Pickup extends allCar {
private String name;
private float price;
private float freight;
private int passenger;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getFreight() {
return freight;
}
public void setFreight(float freight) {
this.freight = freight;
}
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public boolean canPassenger(){
return true;
}
public boolean canFreight(){
return true;
}
public void info(){
System.out.println("名称:"+name+" 价格:"+price+"元/天 载客量"+passenger+"人 载货量:"+freight+"吨");
}
public Pickup(String name, float price, int passenger, float freight){
this.name=name;
this.price=price;
this.passenger=passenger;
this.freight=freight;
}
}
//测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//首先初始化现有的车辆数据
//使用多态
allCar car1=new Car("奥迪A4",500,4);
Coach car2=new Car("马自达6",400,4);
Pickup car3=new Pickup("皮卡雪6",450,4,2);
Coach car4=new Bus("金龙",800,20);
//不使用多态,为了区分heavy/light分别写构造函数
lightTruck car5=new lightTruck("松花江",400,4);
heavyTruck car6=new heavyTruck("依维柯",1000,20);
//将车辆信息存入类的数组中
allCar carArray[]={car1,car2,car3,car4,car5,car6};
//欢迎、提示信息
System.out.println("欢迎来到答答租车系统!");
System.out.println("租车请按“1”,退出请按任意键并回车");
//等待用户键盘输入
Scanner in=new Scanner(System.in);
int start=in.nextInt();
if(start==1){
//展示所有车辆信息
int cnt=0;
for(allCar carx:carArray){
cnt++;
System.out.print(cnt+": ");
carx.info();
}
//输入要租的车数
allCar[] clientArray=new allCar[6];
int[] numArray=new int[]{0,0,0,0,0,0};
for(cnt=0;cnt<6;){
System.out.println("请您输入要租的车的序号");
in=new Scanner(System.in);
int num=in.nextInt();
if(num<=0num>6) break;
clientArray[cnt]=carArray[num-1];
System.out.println("请您输入要租的车的数量");
int num1=in.nextInt();
if(num1==0) break;
numArray[cnt]=num1;
System.out.println("请您输入要租的车的天数");
int num2=in.nextInt();
if(num2==0) break;
numArray[cnt]*=num2;
cnt++;
System.out.println("结束请按0,继续请按任意键");
int num3=in.nextInt();
if(num3==0) break;
}
System.out.println(numArray[0]+numArray[1]);
//输出总金额,总载客/载货量
int sumPassenger=0;
int sumFreight=0;
float Amount=0;
for(int i=0;i<cnt;i++){
if (clientArray[i].canFreight())
sumFreight+=clientArray[i].getFreight();
if(clientArray[i].canPassenger())
sumPassenger+=clientArray[i].getPassenger();
Amount+=numArray[i]*clientArray[i].getPrice();
}
System.out.println("Amount:"+Amount+" 总载客:"+sumPassenger+" 总载货:"+sumFreight);
}
else{
System.out.println("退出租车系统");
}
}
}