java简单租车系统 慕课手记_Java入门 第二季 租车系统

欢迎沟通交流@__@

运行效果:

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

Car.java //车基类

public abstract class Car {

public String name; //车名

public int price; //租金 元/天

public int manCap;//载人

public int goodsCap;//载货

public Car(String name, int price) {

this.name = name;

this.price = price;

}

}

MGCar.java //载客载货车

public class MGCar extends Car {

public MGCar(String name, int price, int manCap, int goodsCap) {

super(name, price);

this.manCap = manCap;

this.goodsCap = goodsCap;

}

@Override

public String toString() {

return ""+name+"\t "+price+"元/天\t载人:"+manCap+"人\t载货:"+goodsCap+"吨";

}

}

ManCar.java //载客车

public class ManCar extends Car {

public ManCar(String name, int price, int manCap) {

super(name, price);

this.manCap = manCap;

}

@Override

public String toString() {

return ""+name+"\t "+price+"元/天\t载人:"+manCap+"人\t";

}

}

GoodsCar.java //载货车

public class GoodsCar extends Car {

public GoodsCar(String name,int price,int goodsCap){

super(name,price);

this.goodsCap = goodsCap;

}

@Override

public String toString() {

return ""+name+"\t "+price+"元/天\t载货:"+goodsCap+"吨\t";

}

}

CarRentSys.java //租车系统入口

import java.util.Arrays;

import java.util.Scanner;

/**

* 哒哒租车系统

* @author Anders

*

*/

public class CarRentSys {

public static void main(String[] args) {

int first = 0;//首次

int[] rentNum = new int[100];//租车代码

int rentDays = 0;//租车天数

Car[] cars = {new ManCar("奥迪A4", 500, 4),new ManCar("马自达6", 400, 4),new MGCar("皮卡雪6", 450, 4, 2),

new ManCar("金龙", 800, 20),new GoodsCar("松花江", 400, 4),new GoodsCar("依维柯", 1000, 20)};

if(first == 0){

System.out.println("~~~~~~~~~~欢迎使用哒哒租车系统~~~~~~~~~~");

}

Scanner input = new Scanner(System.in);

System.out.println("您是否要租车?\t1是\t0否,请输入对应数字:");

while(first == 0){

first++;

String sel = input.next();

if("1".equals(sel) || "0".equals(sel)){

if("1".equals(sel)){

showProduct(cars);

select(input,cars,rentNum);

Arrays.sort(rentNum);

System.out.println("请输入租车天数:");

while(first==1){

first++;

try{

rentDays = input.nextInt();

}catch(Exception e){

System.out.println("输入有误,请重新输入:");

first = 1;

input.next();

}

}

printList(rentNum,cars,rentDays);

}else{

System.out.println("谢谢光临,下次再见!");

input.close();

return;

}

}else{

System.out.println("输入有误,请重新输入:");

first=0;

}

}

input.close();

}

/*

* 展示车辆

*/

private static void showProduct(Car[] cars) {

System.out.println("您可租车的类型及其价目表:\n\r序号\t汽车名称\t 租金\t\t容量");

for (int i=1;i<=cars.length;i++) {

System.out.println(i+"\t"+cars[i-1]);

}

}

/*

* 选择租车数量,类型

*/

private static void select(Scanner input,int[] rentNum) {

int first = 0;

int num = 0;//租车数量

if(first==0){

System.out.println("请输入您要租车的数量:");

}

try{

num = input.nextInt();

first++;

}catch(Exception e){

System.out.println("输入有误,请重新输入:");

first=0;

select(input,rentNum);

}

System.out.println("请选择要租车的序号(1-6):");

int selNum;

for(int i=0; i

System.out.println("请选择第"+(i+1)+"辆车的序号:");

try{

selNum = input.nextInt();

if(selNum!=1 && selNum!=2 && selNum!=3 && selNum!=4 && selNum!=5 && selNum!=6){

System.out.println("输入有误,请重新选择:");

i--;

continue;

}

rentNum[i] = selNum;

}catch(Exception e){

System.out.println("输入有误,请重新选择:");

i--;

input.next();//如输入a,缓存区会一直保留a,导致死循环,此操作可以解决问题

}

}

}

/*

* 打印订单

*/

private static void printList(int[] rentNum, Car[] cars, int rentDays) {

int sum = 0;

int manCapSum = 0;

int goodsCapSum = 0;

int selNum = 0;

int[] counts = new int[cars.length];

System.out.println("序号\t汽车名称\t 租金\t\t容量\t\t\t数量");

int i=0;

for(int r=0;r

selNum = rentNum[r]-1;

if(selNum!=-1){

i++;

sum+=cars[selNum].price;

if(cars[selNum] instanceof ManCar){

manCapSum+=cars[selNum].manCap;

}else if(cars[selNum] instanceof GoodsCar){

goodsCapSum+=cars[selNum].goodsCap;

}else{

goodsCapSum+=cars[selNum].goodsCap;

manCapSum+=cars[selNum].manCap;

}

if(r

counts[selNum]++;

i--;

continue;

}

System.out.println(""+i+"\t"+cars[selNum]+"\t\t"+(counts[selNum]+1));

}

}

System.out.println("总载人:"+manCapSum+"人/日\t总载货:"+goodsCapSum+"吨/日\t总价:"+sum*rentDays+"元");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值