Sciences
2018-02-10 16:34:47浏览 2131
//创建父类Car
package com.car;
public abstract class Car {
protected String name;
protected int price;
protected int zkl;
protected int zhl;
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getZkl() {
return zkl;
}
public int getZhl() {
return zhl;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setZkl(int zkl) {
this.zkl = zkl;
}
public void setZhl(int zhl) {
this.zhl = zhl;
}
}
//创建客车类
package com.car;
public class PassengerCar extends Car {
public PassengerCar(String name,int price,int zkl){
this.name=name;
this.price=price;
this.zkl=zkl;
}
}
//创建货车类
package com.car;
public class Cargo extends Car {
public Cargo(String name,int price,int zhl){
this.price=price;
this.name=name;
this.zhl=zhl;
}
}
//创建皮卡车类
package com.car;
public class Pickup extends Car {
public Pickup(String name,int price,int zkl,int zhl){
this.price=price;
this.name=name;
this.zkl=zkl;
this.zhl=zhl;
}
}
//创建业务处理类
package com.car;
import java.util.Scanner;
public class Operation {
Car[] cangKu={new PassengerCar("奥迪A4",500,4),new PassengerCar("马自达",400,4),new Pickup("皮卡雪",450,4,2),new PassengerCar("金龙",800,20),new Cargo("松花江",500,4),new Cargo("依维柯",1000,20)};
Scanner in=new Scanner(System.in);
int sumPrice;
int sumBusload;
int sumWeight;
public void begin(){
System.out.println("欢迎您使用答答租车系统\n是否进入租车系统?\n进入系统:按1 退出:按任意键");
if(in.nextInt()!=1){
in.close();
System.exit(-1);
}
System.out.println("您可租用的汽车类型及其价目表:\n序号\t汽车名称\t租金\t载客量/人\t载货量/吨");
for(int i=0;i<cangKu.length;i++){
System.out.println((i+1)+"\t"+cangKu[i].getName()+"\t"+cangKu[i].getPrice()+"\t"+cangKu[i].getZkl()+"\t"+cangKu[i].getZhl());
}
System.out.println("请输入您的租车数量");
int userNum=in.nextInt();
Car[] userKu=new Car[userNum];
for(int i=0;i<userNum;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号");
int index=in.nextInt();
while(index<0||index>6){
System.out.println("您输入的序号不在可选范围之内,请重新输入");
index=in.nextInt();
}
userKu[i]=cangKu[index-1];
sumPrice+=userKu[i].getPrice();
sumBusload+=userKu[i].getZkl();
sumWeight+=userKu[i].getZhl();
}
System.out.println("请输入租车天数:");
int day=in.nextInt();
sumPrice*=day;
System.out.println("您的账单:");
System.out.println("***可载人的车有***");
for(int i=0;i<userKu.length;i++){
if(userKu[i] instanceof PassengerCar||userKu[i] instanceof Pickup){
System.out.print(userKu[i].getName()+"\t");
}
}
System.out.println("共"+sumBusload+"人");
System.out.println("***可载货的车有***");
for(int i=0;i<userKu.length;i++){
if(userKu[i] instanceof Cargo||userKu[i] instanceof Pickup){
System.out.print(userKu[i].getName()+"\t");
}
}
System.out.println("共"+sumWeight+"吨");
System.out.println("租车总价格"+sumPrice+"元");
}
}
//创建主方法类
package com.car;
public class Initail {
public static void main(String[] args) {
Operation start=new Operation();
start.begin();
}
}