父类
public class Car {
private int num;
private String name;
private int rent;
private int manned;
private int burden;
public int getManned() {
return manned;
}
public void setManned(int manned) {
this.manned = manned;
}
public int getBurden() {
return burden;
}
public void setBurden(int burden) {
this.burden = burden;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public Car(int num,String name,int rent){
this.num=num;
this.name=name;
this.rent=rent;
}
}
载人的子类
public class MannedCar extends Car{
private int manned;
public int getManned(){
return manned;
}
public void setManned(int manned){
this.manned=manned;
}
public MannedCar(int num,String name,int rent,int manned){
super(num,name,rent);
this.manned=manned;
}
}
载货的子类
public class BurdenCar extends Car {
private int burden;
public int getBurden(){
return burden;
}
public void setBurden(int burden){
this.burden=burden;
}
public BurdenCar(int num,String name,int rent,int burden){
super(num,name,rent);
this.burden=burden;
}
}
既载人也载货的子类
public class BothCar extends Car {
private int manned;
private int burden;
public int getManned(){
return manned;
}
public void setManned(int manned){
this.manned=manned;
}
public int getBurden(){
return burden;
}
public void setBurden(int burden){
this.burden=burden;
}
public BothCar(int num,String name,int rent,int manned,int burden){
super(num,name,rent);
this.manned=manned;
this.burden=burden;
}
}
主要方法类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎您来到嗒嗒租车系统:");
System.out.println("您是否要租车呢?1是,0否");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==1){
System.out.println("您可以租车的类型及价目表:");
System.out.println("序号 汽车名称 租金 容量");
Car[] types={new MannedCar(1,"奥迪A4",500,4),
new MannedCar(2,"马自达6",400,4),
new BothCar(3,"皮卡雪6",450,4,2),
new MannedCar(4,"金龙",800,20),
new BurdenCar(5,"松花江",400,4),
new BurdenCar(6,"依维柯",1000,20)};
for(Car type:types){
if(type instanceof MannedCar){
System.out.println(type.getNum()+"\t"+type.getName()+"\t"+type.getRent()
+"\t"+"载人:"+type.getManned());
}else if(type instanceof BurdenCar){
System.out.println(type.getNum()+"\t"+type.getName()+"\t"+type.getRent()
+"\t"+"载货:"+type.getBurden());
}else{
System.out.println(type.getNum()+"\t"+type.getName()+"\t"+type.getRent()
+"\t"+"载人:"+type.getManned()+"\t"+"载货:"+type.getBurden());
}
}
System.out.println("请输入您要租车的数量:");
Scanner input2=new Scanner(System.in);
int num=input2.nextInt();
while(num<=0){
System.out.println("您输入有误,请重新输入:");
num=input2.nextInt();
}
double rentSum=0;
int mannedSum=0;
double burdenSum=0;
for(int j=1;j<=num;j++){
System.out.println("请选择第"+j+"辆车");
Scanner input3=new Scanner(System.in);
int num2=input3.nextInt();
while(num2<=0num2>types.length){
System.out.println("您的输入有误,请重新输入:");
num2=input3.nextInt();
}
if(types[num2-1] instanceof MannedCar){
System.out.println("您选择了"+types[num2-1].getName()+"\t"+"租金为每天"
+types[num2-1].getRent()+"元"+"\t"+"载客量为"+types[num2-1].getManned());
}else if(types[num2-1] instanceof BurdenCar){
System.out.println("您选择了"+types[num2-1].getName()+"\t"+"租金为每天"
+types[num2-1].getRent()+"元"+"\t"+"载货量为"+types[num2-1].getBurden());
}else{
System.out.println("您选择了"+types[num2-1].getName()+"\t"+"租金为每天"
+types[num2-1].getRent()+"元"+"\t"+"载客量为"+types[num2-1].getManned()+"\t"+"载货量为"+types[num2-1].getBurden());
}
rentSum=rentSum+types[num2-1].getRent();
mannedSum=mannedSum+types[num2-1].getManned();
burdenSum=burdenSum+types[num2-1].getBurden();
}
System.out.println("您的账单是:");
System.out.println("***总载客量为:"+mannedSum+"人");
System.out.println("***总载货量为:"+burdenSum+"吨");
System.out.println("***总共需要:"+rentSum+"元");
}else if(a==0){
System.out.println("谢谢您的使用!系统将自动退出!");
}else {
System.out.println("您的输入有误!系统将自动退出!");
}
}
}
热门评论
照着你的打,数组中没有值????????????
为何要那么多scanner,往大神解释一下
我真搞不懂为什么要弄那么多的scanner