也算是实现了所有功能
//我用的JCreator编的,不是eclipse,代码的部分地方会略有不同但总体来说没太大差别
import java.util.Scanner;
public class execP20601{
Car Aodi=new MannedCar("奥迪A4",500,4);
Car Mzd=new MannedCar("马自达6",400,4);
Car Pkx=new Truck("皮卡雪6",450,4,2);
Car Jl=new MannedCar("金龙",800,20);
Car Shj=new Van("松花江",400,4);
Car Ywk=new Van("依维柯",1000,20);
Car[] CarInform={Aodi,Mzd,Pkx,Jl,Shj,Ywk};
public static void main(String args[]){
execP20601 Pro=new execP20601();
Pro.Step1();
Pro.Step2();
}
public void Step1(){
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
try{
Scanner scan=new Scanner(System.in);
int Num1=scan.nextInt();
System.out.println();
int Jishu1=4;
while(Jishu1>0){
Jishu1--;
if(Num1!=1&&Num1!=0){
if(Jishu1>1)
System.out.println("输入有误,请重试,需输入整数0或1,还可重试"+Jishu1+"次!");
if(Jishu1==1)
System.out.println("输入有误,请重试,需输入整数0或1,还可重最后一次!!!");
if(Jishu1<=0){
System.out.println("输入失败,程序将自动退出");
System.exit(0);
}
System.out.println("您是否要租车:1是 0否");
Num1=scan.nextInt();
if(Num1==1||Num1==0){
System.out.println();
break;
}
System.out.println();
}
}
if(Num1==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 载量");
System.out.println();
for(int i=0;i<CarInform.length;i++){
System.out.print(i+1);
CarInform[i].Inform();
}
}else if(Num1==0){
System.out.println("不好意思,打扰您了,程序将自动退出,祝您生活愉快!");
System.exit(0);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("输入有误");
}
}
public void Step2(){
System.out.println();
System.out.println("请输入您要租车的数量:");
Scanner scan2=new Scanner(System.in);
int Num1=scan2.nextInt();
int Jishu2=0;
int DayRent=0;
int TotalRent=0;
try{
int[] CarNum=new int[Num1];
if(Num1<=0){
System.exit(0);
}
while(Jishu2<Num1){
//System.out.println("Jishu2:"+Jishu2);
System.out.println();
System.out.println("请输入第"+(Jishu2+1)+"辆车的序号:");
CarNum[Jishu2]=scan2.nextInt()-1;
//System.out.println(CarInform[CarNum[Jishu2]].getName());
DayRent+=CarInform[CarNum[Jishu2]].getPrice();
Jishu2++;
}
System.out.println();
System.out.println("请输入租车天数:");
int Day=scan2.nextInt();
TotalRent=DayRent*Day;
System.out.println();
System.out.println("您的账单:");
System.out.println();
System.out.println("可载人的有:");
int CapaManNum=0;
for(int i=0;i<CarNum.length;i++){
if(CarInform[CarNum[i]].getType().equals("Man")||CarInform[CarNum[i]].getType().equals("Double")){
System.out.print(CarInform[CarNum[i]].getName()+" ");
CapaManNum+=CarInform[CarNum[i]].getCapaMan();
}
}
System.out.println("总共可载"+CapaManNum+"个人");
System.out.println();
System.out.println("可载货的有:");
int CapaCargoNum=0;
for(int i=0;i<CarNum.length;i++){
if(CarInform[CarNum[i]].getType().equals("Cargo")||CarInform[CarNum[i]].getType().equals("Double")){
System.out.print(CarInform[CarNum[i]].getName()+" ");
CapaCargoNum+=CarInform[CarNum[i]].getCapaCargo();
}
}
System.out.println("总共可载"+CapaCargoNum+"吨货");
System.out.println();
System.out.println("累计消费"+TotalRent+"元");
System.out.println();
}catch(Exception e){
e.printStackTrace();
System.out.println("输入有误");
}
}
}
abstract class Car{
private String Type;
private String name;
private int price;
private int CapaMan;
private int CapaCargo;
public String getName(){
return name;
}
public String getType(){
return Type;
}
public int getPrice(){
return price;
}
public int getCapaMan(){
return CapaMan;
}
public int getCapaCargo(){
return CapaCargo;
}
public abstract void Inform();
}
class MannedCar extends Car{
private String Type="Man";
private String name;
private int price;
private int CapaMan;
public MannedCar(String name,int price,int CapaMan){
//Inform();
this.name=name;
this.price=price;
this.CapaMan=CapaMan;
}
public String getName(){
return name;
}
public String getType(){
return Type;
}
public int getPrice(){
return price;
}
public int getCapaMan(){
return CapaMan;
}
public void Inform(){
String DayRent=price+"/天";
String Capacity="载人:"+CapaMan+"人";
String Inform="."+" "+name+" "+DayRent+" "+Capacity;
StringBuilder str=new StringBuilder(Inform);
int Location=Inform.indexOf(DayRent);
int DiffValue=17-Location;
if(DiffValue>0){
for(int i=DiffValue;i>0;i--){
str.insert(Location-DiffValue," ");
}
}
System.out.println(str.toString());
//System.out.println(Inform.indexOf(DayRent));
}
}
class Truck extends Car{
private String Type="Double";
private String name;
private int price=450;
private int CapaMan;
private int CapaCargo;
public Truck(String name,int price,int CapaMan,int CapaCargo){
//Inform();
this.name=name;
this.price=price;
this.CapaMan=CapaMan;
this.CapaCargo=CapaCargo;
}
public String getName(){
return name;
}
public String getType(){
return Type;
}
public int getPrice(){
return price;
}
public int getCapaMan(){
return CapaMan;
}
public int getCapaCargo(){
return CapaCargo;
}
public void Inform(){
String DayRent=price+"/天";
String Capacity="载人:"+CapaMan+"人 "+"载货:"+CapaCargo+"吨";
String Inform="."+" "+name+" "+DayRent+" "+Capacity;
System.out.println(Inform);
}
}
class Van extends Car{
private String Type="Cargo";
private String name;
private int price=400;
private int CapaCargo=4;
public Van(String name,int price,int CapaCargo){
//Inform();
this.name=name;
this.price=price;
this.CapaCargo=CapaCargo;
}
public String getName(){
return name;
}
public String getType(){
return Type;
}
public int getPrice(){
return price;
}
public int getCapaCargo(){
return CapaCargo;
}
public void Inform(){
String DayRent=price+"/天";
String Capacity="载货:"+CapaCargo+"吨";
String Inform="."+" "+name+" "+DayRent+" "+Capacity;
StringBuilder str=new StringBuilder(Inform);
int Location=Inform.indexOf(Capacity);
int DiffValue=26-Location;
if(DiffValue>0){
for(int i=DiffValue;i>0;i--){
str.insert(Location-DiffValue," ");
}
}
System.out.println(str.toString());
//System.out.println(Inform.indexOf(Capacity));
}
}