JAVA-实现汽车租赁系统的计价功能
1. MotoVehicleE
public abstract class MotoVehicleE {
private String no;
private String brand;
public MotoVehicleE(){}
public MotoVehicleE(String no, String brand){
this.no = no;
this.brand = brand;
}
public abstract int callRent(int days);
}
2. CarE Class
public final class CarE extends MotoVehicleE{
private String type;
public CarE(){}
public CarE(String type,String no, String brand){
super(no,brand);
this.type = type;
}
public void setType(String type){
this.type = type;
}
public String getType() {
return type;
}
@Override
public int callRent(int days){
if ("1".equals(type)){
return days * 500;
} else if ("2".equals(type)) {
return days * 600;
}else {
return days * 300;
}
}
}
3. BusE Class
public final class BusE extends MotoVehicleE{
private int seatCount;
public BusE(){}
public BusE(int seatCount,String brand, String no){
super(brand, no);
this.seatCount = seatCount;
}
public void setSeatCount(int seatCount){
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
@Override
public int callRent(int days) {
if (seatCount <= 15 ){
return days*800;
}else {
return days*1500;
}
}
}
4. TestRentE Class
import java.util.Random;
import java.util.Scanner;
public class TestRentE {
public static Random random = new Random();
public static final Scanner scanner = new Scanner(System.in);
public static String getCarNumber(){
StringBuilder sb = new StringBuilder("粤B");
for (int i = 0; i<5;i++){
if(random.nextInt(2)==0){
sb.append((char)(random.nextInt(26)+65));
}else {
sb.append(random.nextInt(10));
}
}
return sb.toString();
}
public static void start(){
System.out.println(">>>欢迎您来到汽车租赁公司: ");
System.out.println(">>>请输入要租几天: ");
int days = scanner.nextInt();
System.out.println(">>>请输入租赁汽车类型: \n1.轿车\t2.客车");
String carType = scanner.next();
int rent;
if ("1".equals(carType)){
System.out.println("1. 宝马\t2. 别克");
String brand = scanner.next();
String type;
if ("1".equals(brand)){
System.out.println(">>>您选择的型号是: 530i");
type = "宝马-530i";
}else {
System.out.println("2.商务舱GL8 \t 3. 林荫大道");
type = scanner.next();
}
String no = getCarNumber();
System.out.println(">>>给您分配的汽车牌号是: "+no);
MotoVehicleE carE = new CarE(type,no,brand);
rent = carE.callRent(days);
}else {
System.out.println(">>>请输入租赁客车的品牌: \n1.金杯\t2.金龙");
String brand = scanner.next();
System.out.println(">>>请输入需要座位数量: ");
int seatCount = scanner.nextInt();
String no = getCarNumber();
MotoVehicleE busE = new BusE(seatCount,brand,no);
rent = busE.callRent(days);
System.out.println(">>>给您分配的汽车牌号是: "+no);
}
System.out.println("\n顾客您好,你需要支付的租赁费用是: "+rent+".");
}
public static void main(String[] args) {
start();
}
}
总结
- 这里面还是有很多问题和缺陷,只是记录一下作业学习
- 参考文献:
https://blog.csdn.net/qq_29163727/article/details/115079357?spm=1001.2014.3001.5506