本系统用java语言所写,不涉及任何框架。纯JavaSE基础,本人练习所写。
需求
设计一个简易的租车系统,要求实现租车,并统计车型、载客数、载人数、租赁总价钱。
题目来自: imooc的《Java入门第二季》
思路
- 设计一个Car类,作为各个汽车类的父类,然后设计几种汽车类型继承该父类。
- 汽车出租类。完成租赁任务
Car类
/*** Project: Lease System* Cars: 汽车类(客车、货车、卡车)* @author Gyl*/
public class Car {private String s_name; // 汽车名字private int i_price; // 租赁价格public String getName() {return s_name;}public void setName(String name) {this.s_name = name;}public int getPrice() {return i_price;}public void setPrice(int price) {this.i_price = price;}
}
Bus类
/*** 客车类* @author Gyl**/
class Bus extends Car{private int capcity; // 客车载客量public Bus(String name, int price, int capcity) {super(); // 不写,系统默认调用this.capcity = capcity;super.setName(name);super.setPrice(price);}public int getCapcity() {return capcity;}public void setCapcity(int capcity) {this.capcity = capcity;}public String toString() {return this.getName() + "\t" +this.getPrice()+ "元/天\t载人:" + capcity + "人";}
}
Pickup类
/*** 卡车类* @author Gyl**/
class Pickup extends Car{private int personNum;private int goodsNum;public Pickup(String name, int price, int personNum, int goodsNum) {super();this.goodsNum = goodsNum;this.personNum = personNum;super.setName(name);;super.setPrice(price);}public int getPersonNum() {return personNum;}public void setPersonNum(int personNum) {this.personNum = personNum;}public int getGoodsNum() {return goodsNum;}public void setGoodsNum(int goodsNum) {this.goodsNum = goodsNum;}public String toString() {return this.getName() + "\t" + this.getPrice() + "元/天\t载人:" + personNum + "人\t载货:" + goodsNum;}
}
Trunk类
/*** 货车类* @author Gyl**/
class Trunk extends Car{private int goodsNum;public Trunk(String name, int price, int goodsNum) {super();this.goodsNum = goodsNum;super.setName(name);super.setPrice(price);}public int getGoodsNum() {return goodsNum;}public void setGoodsNum(int goodsNum) {this.goodsNum = goodsNum;}public String toString() {return this.getName() + "\t" + this.getPrice() + "元/天\t载货:" + goodsNum + "吨";}
}
租赁类
import java.util.Scanner;public class Lease {public static void main(String[] args) {System.out.println("-----欢迎使用答答租车系统-----\n 您是否要租车:1是\t0否");int finalPrice = 0; // 最终的价格int finalPerson = 0; // 最终的载客数int finalGoods = 0; // 最终的载物量StringBuffer buf_busName = new StringBuffer();StringBuffer buf_trunkName = new StringBuffer();Scanner in = new Scanner(System.in);if (in.nextInt() != 1) {System.out.println("谢谢惠顾,欢迎下次再来。");in.close();return ;}System.out.println("您可租车的类型及其价目表:\n序号\t汽车名称\t租金\t容量");Bus car1 = new Bus("奥迪A4",500,4); //实例化奥迪A4Bus car2 = new Bus("马自达6",400,4); //实例化马自达6Bus car3 = new Bus("金龙",800,20); //实例化金龙Trunk car4 = new Trunk("松花江",400,4); //实例化松花江Trunk car5 = new Trunk("依维柯",1000,20); //实例化依维柯Pickup car6 = new Pickup("皮卡雪6",450,4,2); //实例化皮卡雪6Car[] cars = {car1,car2,car3,car4,car5,car6};/*展示车辆信息*/for(int i = 0; i < cars.length; i++){System.out.println((i + 1) + ".\t" + cars[i].toString());}System.out.println("请输入您要租汽车的数量:");int num = in.nextInt(); //记录租车数量int[] car = new int[num]; //记录租车序号for(int i = 0; i < num; i++){System.out.println("请输入第" + (i + 1) + "辆车的序号:");int key = in.nextInt();if(key >=1 && key <= 6){car[i] = key;}else{System.out.println("您输入的序列有误,请重新输入(1-6):");i--; //输入错误,重新输入}}for (int i = 0; i < car.length; i ++) {switch (car[i]) {case 1:buf_busName.append(car1.getName()+" ");finalPrice += car1.getPrice();finalPerson += car1.getCapcity();break;case 2:buf_busName.append(car2.getName()+" ");finalPrice += car2.getPrice();finalPerson += car2.getCapcity();break;case 3:buf_busName.append(car3.getName()+" ");finalPrice += car3.getPrice();finalPerson += car3.getCapcity();break;case 4:buf_trunkName.append(car4.getName()+" ");finalPrice += car4.getPrice();finalGoods += car4.getGoodsNum();break;case 5:buf_trunkName.append(car5.getName()+" ");finalPrice += car5.getPrice();finalGoods += car5.getGoodsNum();break;case 6:buf_busName.append(car6.getName()+" ");buf_trunkName.append(car6.getName()+" ");finalPrice += car6.getPrice();finalPerson += car6.getPersonNum();finalGoods += car6.getGoodsNum();break;default:break;}}System.out.println("请输入租车天数:");int days = in.nextInt();//最终信息输出System.out.println("您的账单:\n***可载人的车有:");System.out.println(buf_busName + "共载人:" + finalPerson + "人");System.out.println("***载货的车有:\n" + buf_trunkName + "共载货:" + finalGoods + "吨");System.out.println("***租车总价格:" + finalPrice * days);System.out.println("感谢使用答答租车系统");in.close(); //关闭用户输入}
}
运行效果: