达达租车
一共5个class文件,我就直接全部贴出来,注释全在里面.
包名叫ddcar,不要在意我的变量名方法名 (〃>-<)
注意有bug:只能输入整型数据,不然就崩溃了(第三季有教如何解决该bug)
.
首先是运行效果:
.
一共5个class文件,我就直接全部贴出来,注释全在里面.
包名叫ddcar,不要在意我的变量名方法名 (〃>-<)
注意有bug:只能输入整型数据,不然就崩溃了(第三季有教如何解决该bug)
.
首先是运行效果:
.
第一个class文件,取名:Run.class
package ddcar;
import java.util.Scanner;
public class Run {
private static int a,b;//变量a用于判断是否租车,b用于判断租车数量
static Scanner input = new Scanner(System.in);
static int cartypeTotal=6; //车型总数
static int carTotal=0; //用于保存全部一共的车数
static int[] carType = new int[cartypeTotal]; //用于记录每种类型车租的数量
static int aMoney=0; //用于记录一天的钱数
/*下面利用 数组+多态 创建每一种类型的车
* 客车参数:"名字",价格,人容量,数量
* 卡车参数:"名字",价格,货容量,数量
* 皮卡参数:"名字",价格,人容量,货容量,数量*/
static Car[] car = {new Passenger_car("奥迪A4",500,4,0), new Passenger_car("马自达",400,4,1),
new Pickup("皮卡雪6",450,4,2,2), new Passenger_car("金龙",800,20,3),
new Truck("松花江",400,4,4), new Truck("依维柯",998,20,5)};
public static void main(String[] args) { //main函数
System.out.println("欢迎使用达达租车"); //欢迎语
isNeeded(); //询问是否租车
if(a==0) {System.out.println("请您自己退出程序");} //不使用则结束
else { //使用则正式运行租车系统
displayList(); //显示车辆列表
howMany(); //询问租多少车
carNum(); //车型选择
rentDay(); //借租天数并计帐
}
}
private static void isNeeded(){
System.out.print("您是否需要租车[1是 0否]:");
do {
a = input.nextInt(); //do while循环,若输入的不是1或者0,则循环
if(a!=0 && a!=1){
System.out.print("您输入的值有误,请重新输入:");
}
}while(a!=1 && a!=0); //若输入为0或1则跳出循环
}
private static void displayList(){
System.out.println("您可租车的类型及其价目表如下:\n序号 汽车名称\t租金\t载人容量\t载货容量\t车型总量");
for(int i=0;i<cartypeTotal;i++) { //3种类型的车的列表文字输出方式不同
if(car[i] instanceof Passenger_car) { //判断实例所属的类
System.out.println((i+1)+". "+car[i].getName()+"\t"+car[i].getRent()+"元/天\t载人:"+car[i].getPeoplecapacity()+"人\t\t剩有"+car[i].getNum()+"辆");
}
else if(car[i] instanceof Truck) {
System.out.println((i+1)+". "+car[i].getName()+"\t"+car[i].getRent()+"元/天\t\t载货:"+car[i].getCapacity()+"吨\t剩有"+car[i].getNum()+"辆");
}
else if(car[i] instanceof Pickup) {
System.out.println((i+1)+". "+car[i].getName()+"\t"+car[i].getRent()+"元/天\t载人:"+car[i].getPeoplecapacity()+"人\t载货:"+car[i].getCapacity()+"吨\t剩有"+car[i].getNum()+"辆");
}
}
}
private static void howMany(){
System.out.print("请确认要租赁车型数量:");
for(int i=0;i<cartypeTotal;i++) {
carTotal += car[i].getNum(); //计算一下 全部一共有多少辆车
}
do {b = input.nextInt();
if(b>0&&b<=carTotal){continue;}
System.out.print("您输入的数值有误,请重新输入:");
}while(!(b>0&&b<=carTotal));
}
private static void carNum() {
for(int n=0;n<cartypeTotal;n++) {carType[n]=0;}//把这个数组的值初始化为0
for(int i=1;i<=b;i++) {
System.out.print("请输入要租的第"+i+"辆车的序号:");
int carcar;
do {
carcar = input.nextInt();
if(!(carcar>=1&&carcar<=cartypeTotal)){
System.out.print("您输入的数值有误,请重新输入:");
continue;
}
else if(car[carcar-1].getNum() < 1){
System.out.print("很抱歉,该型号的车已被租空,请重新输入:");
carcar=0;
continue;
}
else {
car[carcar-1].setNum();
aMoney += car[carcar-1].getRent();//这个用于记录一天的所需要的钱数
carType[carcar-1]++;
}
}while(!(carcar>=1&&carcar<=cartypeTotal));
}
}
private static void rentDay() {
System.out.print("请输入要借租的天数:");
int rentday;
do {
rentday = input.nextInt();
if(!(rentday>=1&&rentday<=365)){
System.out.print("您输入的数值有误,最多只能借365天,请重新输入:");
}
}while(!(rentday>=1&&rentday<=365));
/************下面是账单计算*************/
int renshu1=0,huoshu1=0,renshu2=0,huoshu2=0;//记录各类型车的总载人、载货数
System.out.println("您的账单:");
System.out.println("***客车有:");
for(int i1=0;i1<cartypeTotal;i1++) {
if(car[i1] instanceof Passenger_car && carType[i1]>0) {
System.out.print(car[i1].getName()+"x"+carType[i1]+"\t");
renshu1 += (carType[i1]*car[i1].getPeoplecapacity());}}
System.out.println("\n共载人:"+renshu1+"人");
System.out.println("***卡车有:");
for(int i2=0;i2<cartypeTotal;i2++) {
if(car[i2] instanceof Truck&&carType[i2]>0) {
System.out.print(car[i2].getName()+"x"+carType[i2]+"\t");
huoshu1 += (carType[i2]*car[i2].getCapacity());}}
System.out.println("\n共载货:"+huoshu1+"吨");
System.out.println("***皮卡有:");
for(int i3=0;i3<cartypeTotal;i3++) {
if(car[i3] instanceof Pickup&&carType[i3]>0) {
System.out.print(car[i3].getName()+"x"+carType[i3]+"\t");
renshu2 += (carType[i3]*car[i3].getPeoplecapacity());
huoshu2 += (carType[i3]*car[i3].getCapacity());}}
System.out.print("\n共载人:"+renshu2+"人");
System.out.println("\n共载货:"+huoshu2+"吨");
System.out.println("********");
System.out.println("一共"+aMoney*rentday+"元");
System.out.print("是否确认租车[1是 0否]:");
int ok;
do {
ok = input.nextInt();
if(ok!=0 && ok!=1){
System.out.print("您输入的值有误,请重新输入:");
}
}while(ok!=1 && ok!=0);
if(ok==1) {
System.out.println("恭喜,您已成功租车!已经从您的账户中扣除"+aMoney*rentday+"元");
System.out.println("这些车将会在两分钟内砸到您的家门口,请小心!");
System.out.println("感谢您使用达达租车,再见!");
}
else {
System.out.println("哦,其实你是付不起对吧?");
System.out.println("感谢您使用达达租车,再见!");
}
}
}
第二个class文件,取名:Car.class,即父类class
package ddcar;
public class Car {
public String getName() {return "";}
public int getRent() {return 0;}
public int getPeoplecapacity() {return 0;}
public int getCapacity() {return 0;}
public int getNum() {return 0;}
public void setName(String setName) {};
public void setRent(int setRent) {};
public void setPeoplecapacity(int setPeoplecapacity) {};
public void setCapacity(int setCapacity) {};
public void setNum() {};
}
第三个class文件,取名:Passenger_car.class,即子类客车class
package ddcar;
public class Passenger_car extends Car{
private String name;
private int rent;
private int peoplecapacity;
private int num;
public Passenger_car(String name,int rent,int peoplecapacity,int num){
this.name = name;
this.rent =rent;
this.peoplecapacity = peoplecapacity;
this.num = num;
}
public String getName(){return name;}
public int getRent() {return rent;}
public int getPeoplecapacity(){return peoplecapacity;}
public int getNum(){return num;}
public void setName(String setName) {name = setName;}
public void setRent(int setRent){rent = setRent;}
public void setPeoplecapacity(int setPeoplecapacity){peoplecapacity = setPeoplecapacity;}
public void setNum(){num--;}
}
第四个class文件,取名:Truck.class,即子类卡车class
package ddcar;
public class Truck extends Car{
private String name;
private int rent;
private int capacity;
private int num;
public Truck(String name,int rent,int capacity,int num){
this.name = name;
this.rent =rent;
this.capacity = capacity;
this.num = num;
}
public String getName(){return name;}
public int getRent() {return rent;}
public int getCapacity(){return capacity;}
public int getNum(){return num;}
public void setName(String setName) {name = setName;}
public void setRent(int setRent){rent = setRent;}
public void setCapacity(int setCapacity){capacity = setCapacity;}
public void setNum(){num--;}
}
第五个class文件,取名:Pickup.class,即子类皮卡class
package ddcar;
public class Pickup extends Car{
private String name;
private int rent;
private int peoplecapacity;
private int capacity;
private int num;
public Pickup(String name,int rent,int peoplecapacity,int capacity,int num){
this.name = name;
this.rent =rent;
this.peoplecapacity = peoplecapacity;
this.capacity = capacity;
this.num = num;
}
public String getName(){return name;}
public int getRent() {return rent;}
public int getPeoplecapacity(){return peoplecapacity;}
public int getCapacity(){return capacity;}
public int getNum(){return num;}
public void setName(String setName) {name = setName;}
public void setRent(int setRent){rent = setRent;}
public void setPeoplecapacity(int setPeoplecapacity){peoplecapacity = setPeoplecapacity;}
public void setCapacity(int setCapacity){capacity = setCapacity;}
public void setNum(){num--;}
}
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦