刚看完视频写的。。繁琐
测试代码
package com.imooc;
import java.util.Scanner;
/**
* Created by Administrator on 2017/3/9.
*/
public class Test {
public static void main(String[] args) {
int zuCheShuLiang = 0;
int num4 = 0;
int zongRenShu = 0;
int zongZaiHuo = 0;
int zongTianShu = 0;
int zongZuJin = 0; //定义变量
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner numShuRu1 = new Scanner(System.in);
int num1 = numShuRu1.nextInt();
final String[][] Xinxi = {{"序号 ", "汽车名称 ", "租金 ", "容量 "}, {
"1. ", "奥迪A4 ", "500元/天 ", "载人:4人 "}, {
"2. ", "马自达6 ", "400元/天 ", "载人:4人 "}, {
"3. ", "皮卡雪6 ", "450元/天 ", "载人:4人,载货:2吨"}, {
"4. ", "金龙 ", "800元/天 ", "载人:20人 "}, {
"5. ", "松花江 ", "400元/天 ", "载货:4吨 "}, {
"6. ", "依维柯 ", "1000元/天 ", "载货:20吨 "}};
if (num1 == 1) {
System.out.println("您可租车的类型及其价目表:");
for (int i = 0; i < Xinxi.length; i++) {
for (int j = 0; j < Xinxi[i].length; j++) {
System.out.print(Xinxi[i][j]);
}
System.out.println();
}
System.out.println("近期租车人数过多,每类车型限租1辆,请输入您要租汽车的数量:");
Scanner numShuRu2 = new Scanner(System.in);
int num2 = numShuRu2.nextInt();
zuCheShuLiang = num2;
if (zuCheShuLiang <= 0 || zuCheShuLiang > 6) {
do {
System.out.println("您输入的值有误,请再次输入您要租汽车的数量:");
Scanner numShuRu3 = new Scanner(System.in);
int num3 = numShuRu3.nextInt();
zuCheShuLiang = num3;
num2=zuCheShuLiang;
} while (zuCheShuLiang <= 0||zuCheShuLiang > 6);
}
String[] cheName = new String[zuCheShuLiang];
AoDi car1 = new AoDi();
MaZiDa car2 = new MaZiDa();
PiKaXue car3 = new PiKaXue();
JinLong car4 = new JinLong();
SongHuaJiang car5 = new SongHuaJiang();
YiWeiKe car6 = new YiWeiKe();
for (int x = 0; zuCheShuLiang > 0; zuCheShuLiang--, x++) {
System.out.println("请输入第" + (x + 1) + "辆车的序号:");
Scanner numShuRu4 = new Scanner(System.in);
num4 = numShuRu4.nextInt();
switch (num4) {
case 1:
cheName[x] = car1.carName;
zongRenShu = zongRenShu + car1.zaiRen;
zongZaiHuo = zongZaiHuo + car1.zaiHuo;
zongZuJin = zongZuJin + car1.zuJin;
continue;
case 2:
cheName[x] = car2.carName;
zongRenShu = zongRenShu + car2.zaiRen;
zongZaiHuo = zongZaiHuo + car2.zaiHuo;
zongZuJin = zongZuJin + car2.zuJin;
continue;
case 3:
cheName[x] = car3.carName;
zongRenShu = zongRenShu + car3.zaiRen;
zongZaiHuo = zongZaiHuo + car3.zaiHuo;
zongZuJin = zongZuJin + car3.zuJin;
continue;
case 4:
cheName[x] = car4.carName;
zongRenShu = zongRenShu + car4.zaiRen;
zongZaiHuo = zongZaiHuo + car4.zaiHuo;
zongZuJin = zongZuJin + car4.zuJin;
continue;
case 5:
cheName[x] = car5.carName;
zongRenShu = zongRenShu + car5.zaiRen;
zongZaiHuo = zongZaiHuo + car5.zaiHuo;
zongZuJin = zongZuJin + car5.zuJin;
continue;
case 6:
cheName[x] = car6.carName;
zongRenShu = zongRenShu + car6.zaiRen;
zongZaiHuo = zongZaiHuo + car6.zaiHuo;
zongZuJin = zongZuJin + car6.zuJin;
continue;
}
}
System.out.println("请输入您租车的天数:");
Scanner numShuRu5 = new Scanner(System.in);
zongTianShu = numShuRu5.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for (int y = 0; y < num2; y++) {
if ((cheName[y]==car1.carName) && (car1.zaiRen > 0))System.out.print(cheName[y]+" ");
if ((cheName[y]==car2.carName) && (car2.zaiRen > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car3.carName) && (car3.zaiRen > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car4.carName) && (car4.zaiRen > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car5.carName) && (car5.zaiRen > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car6.carName) && (car6.zaiRen > 0)) System.out.print(cheName[y]+" ");
}
System.out.println(" 共载人:" + zongRenShu + "人");
System.out.println("***可载货的车有:");
for (int y = 0; y < num2; y++) {
if ((cheName[y]==car1.carName) && (car1.zaiHuo > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car2.carName) && (car2.zaiHuo > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car3.carName) && (car3.zaiHuo > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car4.carName) && (car4.zaiHuo > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car5.carName) && (car5.zaiHuo > 0)) System.out.print(cheName[y]+" ");
if ((cheName[y]==car6.carName) && (car6.zaiHuo > 0)) System.out.print(cheName[y]+" ");
}
System.out.println(" 共载货:" + zongZaiHuo + "吨");
System.out.println("租车总价格:" + zongTianShu * zongZuJin + "元");
}
}
}
父类
public class Car {
String carName;
int zuJin;
int zaiRen;
int zaiHuo;
int bianHao;
}
子类
public class AoDi extends Car {
String carName="奥迪A4";
int zuJin=500;
int zaiRen=4;
int zaiHuo=0;
int bianHao=1;
}
public class JinLong extends Car {
String carName="金龙";
int zuJin=800;
int zaiRen=20;
int zaiHuo=0;
int bianHao=4;
}
public class MaZiDa extends Car {
String carName="马自达6";
int zuJin=400;
int zaiRen=4;
int zaiHuo=0;
int bianHao=2;
}
public class SongHuaJiang extends Car {
String carName="松花江";
int zuJin=400;
int zaiRen=0;
int zaiHuo=4;
int bianHao=5;
}
public class YiWeiKe extends Car {
String carName="依维柯";
int zuJin=1000;
int zaiRen=0;
int zaiHuo=20;
int bianHao=6;
}