抽象父类Vehicle
其中有一个抽象方法getDescription()用于输入对象信息。
package com.imooc;
/**
* 构造一个抽象汽车类,有序号、名称、价格
*/
abstract class Vehicle {
private String name;
private double price;
private boolean rentable;
/**
* @param name 汽车的名字
* @param price 汽车的价格
* @param rentable 汽车是否可以租用
*/
public Vehicle( String name, double price, boolean rentable) {
this.name = name;
this.price = price;
this.setRentable(rentable);
}
/**
* @return 获取汽车名称
*/
public String getName() {
return name;
}
/**
* @param name 汽车名称
*/
public void setName(String name) {
this.name = name;
}
/**
* @return 汽车价格
*/
public double getPrice() {
return price;
}
/**
*
* @param price 汽车价格
*/
public void setPrice(double price) {
this.price = price;
}
/**
* 描述汽车具体对象
*/
public abstract String getDescription();
/**
* @return 汽车是否可租
*/
public boolean isRentable() {
return rentable;
}
/**
* @param rentable 设置汽车是否可租
*/
public void setRentable(boolean rentable) {
this.rentable = rentable;
}
}
子类Car
在父类Vehicle基础上增加了载人属性
package com.imooc;
/**
* 创建一个乘用车对象,在Vehicle类基础上增加了载客属性
*/
public class Car extends Vehicle {
//增加汽车载客量属性
private int passenNum;
public Car(String name, double price, boolean rentable, int passenNum) {
super( name, price, rentable);
this.passenNum = passenNum;
}
/**
*获得汽车的载客量
*/
public int getPassenNum() {
return passenNum;
}
/**
* @param passenNums 汽车载客量
*/
public void setPassenNum(int passenNum) {
this.passenNum = passenNum;
}
/**
* 描述具体对象
*/
public String getDescription( ) {
String s = this.getName()+"\t"+this.getPrice()+"元/天\t"+"载人:"+this.getPassenNum()+"人";
return s;
}
}
子类Truck
在父类Vehicle基础上增加了载货量属性
package com.imooc;
/**
* 创建一个载货汽车对象,在Vehicle类基础上增加了载货属性
*/
public class Truck extends Vehicle {
//增加汽车载货量属性
private double capacity;
public Truck(String name, double price, boolean rentable, double capacity) {
super( name, price, rentable);
this.capacity = capacity;
}
/**
*获得汽车的载货量
*/
public double getCapacity() {
return capacity;
}
/**
* @param passenNums 汽车载客量
*/
public void setcapacity(double capacity) {
this.capacity = capacity;
}
/**
* 描述具体对象
*/
public String getDescription( ) {
String s = this.getName()+"\t"+this.getPrice()+"元/天\t"+"载货:"+this.capacity+"吨";
return s;
}
}
子类PickUp
在父类基础上增加了载客和载货量属性
package com.imooc;
/**
* 创建一个皮卡对象,在Vehicle类基础上,增加了载货和载人属性;
*/
public class PickUp extends Vehicle {
//增加载人属性
private int passenNum;
//增加载货属性
private double capacity;
/**
* 创建一个皮卡对象
* @param name 皮卡名称
* @param price 皮卡租车价格
* @param passenNum 皮卡载客数量
* @param capacity 皮卡载货数量
*/
public PickUp(String name, double price, boolean rentable, int passenNum, double capacity) {
super(name, price, rentable);
this.passenNum = passenNum;
this.capacity = capacity;
}
/**
* @return 获得皮卡载客量
*/
public int getPassenNum() {
return passenNum;
}
/**
* @param passenNum 皮卡载客数量
*/
public void setPassenNum(int passenNum) {
this.passenNum = passenNum;
}
/**
* @return 获得皮卡载货量
*/
public double getCapacity() {
return capacity;
}
/**
* @param capacity 皮卡载货数量
*/
public void setCapacity(double capacity) {
this.capacity = capacity;
}
public String getDescription() {
String s = this.getName()+"\t"+this.getPrice()+"元/天\t"+"载人:"+this.getPassenNum()+"人"+" "+"载货:"+this.getCapacity()+"吨";
return s;
}
}
主程序
程序中用到了太多循环来判断输入是否合法,结构太臃肿。
求解决办法!!!
package com.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;
import com.imooc.*;
/**
* 答答租车系统
* 根据消费者的操作,提供相应的车型
*/
public class DadaCarRent {
public static void main(String[] args) {
//初始化语句
initialWords();
//创建键盘录入
Scanner scanner = new Scanner(System.in);
//判断录入信息
for( ; ; ) {
int choice = 0;
try {
//scanner.nextInt()方法会抛出InputMismatchException,捕获该异常之后,catch块的continue会一直执行
choice = Integer.parseInt(scanner.next());
} catch (Exception e) {
System.out.println("您输入数字有误,请重新输入:");
continue;
}
if (choice == 1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号"+"\t汽车名称"+"\t租金"+"\t容量");
Vehicle[] list = new Vehicle[] {
new Car("奥迪A4",500, true, 4),
new Car("马自达6",400, true, 4),
new PickUp("皮卡雪6",450, true, 4, 2),
new Car("金龙",800,true, 20),
new Truck("松花江",400,true, 4),
new Truck("依维柯",1000, true, 20) };
//打印数组信息
printList(list);
System.out.println("请输入您要租汽车的数量:");
//客户租车数量
while(true) {
int carCount;
try {
carCount = Integer.parseInt(scanner.next());
} catch (Exception e) {
System.out.println("您的输入有误,请重新输入数字1-"+list.length+":");
continue;
}
if (!(carCount <= list.length && carCount >0)) {
System.out.println("您输入数字有误,请重新输入:");
continue;
}
int[] indexArr = new int[carCount];
for (int i = 1; i <=carCount; i++) {
System.out.println("请输入第"+i+"辆车的序号:");
int carNum;
try {
carNum = Integer.parseInt(scanner.next());
} catch(Exception e) {
System.out.println("您输入数字有误,请重新输入车辆序号:");
i --;
continue;
}
if (list[carNum -1].isRentable()) {
if (carNum >0 && carNum <= list.length) {
indexArr[i-1] = carNum;
list[carNum-1].setRentable(false);
} else {
System.out.println("您输入数字有误,请重新输入:");
continue;
}
} else {
System.out.println("对不起,您已选择过该车型。");
i--;
continue;
}
}
System.out.println("请输入租车天数");
while (true) {
try {
int days = Integer.parseInt(scanner.next());
printResult(indexArr, list, days);
} catch(Exception e) {
System.out.println("您输入数字有误,请重新输入天数:");
continue;
}
}
}
} else if (choice == 0){
break;
} else {
System.out.println("您输入数字有误,请重新输入:");
continue;
}
}
scanner.close();
}
/**
* 欢迎语
*/
public static void initialWords() {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车: 1 是 0 否");
}
/**
* 打印数组
* @param list Vehicle数组
*/
public static void printList(Vehicle[] list) {
for (int i = 0; i < list.length; i++) {
System.out.println((i+1)+".\t"+list[i].getDescription());
}
}
/**
*客户选择结果打印输出
*/
public static void printResult(int[] indexArr, Vehicle[] list, int days) {
int passenNum = 0;
double capacity = 0.0;
int sumMoney = 0;
int sum = 0;
String s1 = "";
String s2 = "";
for (int j = 0; j < indexArr.length; j++) {
Vehicle vehi =list[indexArr[j] -1] ;
sum += vehi.getPrice();
sumMoney = sum * days;
if (vehi instanceof Car) {
passenNum += ((Car) vehi).getPassenNum();
s1 = s1+ "\t" + vehi.getName();
} else if (vehi instanceof Truck) {
capacity += ((Truck) vehi).getCapacity();
s2 = s2 + "\t" + vehi.getName();
} else {
passenNum += ((PickUp) vehi).getPassenNum();
capacity +=((PickUp) vehi).getCapacity();
s1 = s1 + "\t" + vehi.getName();
s2 = s2 + "\t" + vehi.getName();
}
}
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(s1 + "\t共载人:"+passenNum+"人");
System.out.println("***可载货的车有:");
System.out.println(s2 + "\t共载货:"+capacity+"吨");
System.out.println("租车总价格:");
System.out.println(sumMoney);
}
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123