昨天刚看完《Java入门第二季》的视频,凭着自己的有限能力完成了“答答租车系统”。
1.读取用户的输入,并对错误的输入有相应的提示和重新输入。
2.考虑到用户可能一种车租很多辆,每一辆车租用的天数不一样。系统采用先让用户选择租一种车的数量,再选择该种车第N辆的租用天数。最后打印订单。
贴代码:
package com.carrent;
//新建Car 抽象类
public abstract class Car {
private String type;
private int price;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return type + "\t" + price;
}
}
因为载客量和载货量可能被同时实现,所以将其定义为接口:
package com.carrent;
//载客量
public interface IBusLoad {
public int getBusLoad();
public void setBusLoad(int busLoad);
}
package com.carrent;
//载货量
public interface ITrunkLoad {
public int getTrunkLoad();
public void setTrunkLoad(int trunkLoad);
}
接着定义三个类,分别继承抽象类Car
package com.carrent;
//客车
public class Bus extends Car implements IBusLoad {
private int busLoad;
public Bus(String type, int price, int busLoad){
this.setType(type);
this.setPrice(price);
this.setBusLoad(busLoad);
}
@Override
public int getBusLoad() {
return busLoad;
}
@Override
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
@Override
public String toString() {
return super.toString() + "\t\t" + "载人:" + this.getBusLoad() + "人";
}
}
package com.carrent;
//货车
public class Trunk extends Car implements ITrunkLoad {
private int trunkLoad;
public Trunk(String type, int price, int trunkLoad){
this.setType(type);
this.setPrice(price);
this.setTrunkLoad(trunkLoad);
}
@Override
public int getTrunkLoad() {
return trunkLoad;
}
@Override
public void setTrunkLoad(int trunkLoad) {
this.trunkLoad = trunkLoad;
}
@Override
public String toString() {
return super.toString() + "\t\t" + "载货:" + this.getTrunkLoad() + "吨";
}
}
package com.carrent;
//皮卡车
public class Pickup extends Car implements IBusLoad, ITrunkLoad {
private int busLoad;
private int trunkLoad;
public Pickup(String type, int price, int busLoad, int trunkLoad){
this.setType(type);
this.setPrice(price);
this.setBusLoad(busLoad);
this.setTrunkLoad(trunkLoad);
}
@Override
public int getTrunkLoad() {
return trunkLoad;
}
@Override
public void setTrunkLoad(int trunkLoad) {
this.trunkLoad = trunkLoad;
}
@Override
public int getBusLoad() {
return busLoad;
}
@Override
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
@Override
public String toString() {
return super.toString() + "\t\t" + "载人:" + this.getBusLoad() + "人" + "\t载货:" + this.getTrunkLoad() + "吨";
}
}
程序入口:
package com.carrent;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class InitCarRent {
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统!");
for (;;) {
System.out.println("您是否要租车:1是 0否");
Scanner in=new Scanner(System.in);
int choice = 3;
try{
choice = in.nextInt();
}catch(Exception e){
System.out.println("指令有误!请输入正确的指令。");
continue;
}
if(choice == 0) {
in.close();
break;
}
else if(choice == 1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金(元/天)\t容量");
// TODO rentCars(carList);
List<Car> carList = initCarList();
int[][] carNumber = selectCar(carList, in);
printOrder(carList, carNumber);
System.out.println();
in.close();
break;
}
else{
System.out.println("指令有误!请输入正确的指令。");
continue;
}
}
System.out.println("感谢您使用答答租车系统!");
}
public static List<Car> initCarList(){
List<Car> carList = new ArrayList<Car>();
carList.add(new Bus("奥迪A4", 500, 4));
carList.add(new Bus("马自达6", 400, 4));
carList.add(new Pickup("皮卡雪6", 450, 4, 2));
carList.add(new Bus("金龙", 800, 20));
carList.add(new Trunk("松花江", 400, 4));
carList.add(new Trunk("依维柯", 1000, 20));
//打印可用车型
for (int i = 0; i < carList.size(); i++) {
System.out.println((i+1) + "\t" + carList.get(i).toString());
}
return carList;
}
public static int[][] selectCar(List<Car> carList, Scanner in){
int carTypeNumber = carList.size();
int[][] carNumber = new int[carTypeNumber][3];
for (int i = 0; i < carTypeNumber; i++) {
carNumber[i][0] = i + 1;
//每辆车的数量
System.out.println("请输入第" + (i+1) + "种车(" + carList.get(i).getType() + ")的数量,没有请输0:");
try{
carNumber[i][1] = in.nextInt();
}catch(Exception e){
System.out.println("由于您操作异常,自动帮您退出系统。");
return carNumber;
}
if(carNumber[i][1] == 0){
continue;
}else{
//一种车每辆车的租金
carNumber[i][2] = 0;
int carRentDay = 0;
for(int j = 1; j <= carNumber[i][1]; j++){
System.out.println("请输入第" + j + "辆车(" + carList.get(i).getType() + ")的天数:");
try{
carRentDay = in.nextInt();
}catch(Exception e){
System.out.println("由于您操作异常,自动帮您退出系统。");
return carNumber;
}
if(carRentDay <= 0){
j--;
System.out.println("请输入一个>=1的数字。");
continue;
}else{
carNumber[i][2] += carRentDay;
}
}
}
}
return carNumber;
}
public static void printOrder(List<Car> carList, int[][] carNumber){
List<Car> selectedCars = new ArrayList<Car>();
int carTypeNumber = carList.size();
int[] rental = new int[carTypeNumber];
int rentalSum = 0;
System.out.println();
System.out.println("--------------------------------------");
System.out.println("您的订单:");
System.out.println("汽车名称\t数量\t天数\t每日租金\t总租金");
for(int i = 0; i < carTypeNumber; i++){
for(int j = 0; j < carNumber[i][1]; j++){
selectedCars.add(carList.get(i));
//System.out.println((i+1) + "\t" + selectedCars.get(i).toString());
}
rental[i] = carNumber[i][2] * carList.get(i).getPrice();
rentalSum += rental[i];
System.out.println(carList.get(i).getType() + "\t" + carNumber[i][1] + "\t" + carNumber[i][2] + "\t"
+ carList.get(i).getPrice() + "\t" + rental[i]);
}
System.out.println("------------------------------------");
System.out.println("\t\t\t总金额(¥):" + rentalSum);
}
}
最终效果:
点击查看更多内容
68人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦