我的Java之旅——答答租车系统的改进
之前的答答租车系统虽然可以实现项目的要求,但是没有用Java面向对象,今天用面向对象的三大特性封装、继承和多态来改进原来的代码。题目和之前的代码参考上篇博客,这里不再述说。
改进后的代码:
Vehicle.java
/*车的父类,包含:
车名和租金两个私有属性name和rent;
访问属性的对应方法setter和getter;
带参数的构造方法;
打印属性值的方法Display();
*/
public class Vehicle {
private String name; //车名
private int rent; //租金
public Vehicle(String name, int rent) {
this.name = name;
this.rent = rent;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setRent(int rent) {
this.rent = rent;
}
public int getRent() {
return rent;
}
public void Display(int num) {
System.out.println(num + " " + this.getName() +
" " + this.getRent());
}
}
Cars.java
/*汽车类,车的子类,包含:
父类继承的属性和方法;
新添加的私有属性载人数seatingCapacity;
seatingCapacity对应的访问方法;
Display()方法的重写;
含参构造方法;
*/
public class Cars extends Vehicle {
private int seatingCapacity; //载人量
public Cars(String name, int rent, int seatingCapacity) {
super(name, rent); //父类构造方法带参数,要用super显示调用
this.setName(name);
this.setRent(rent);
this.seatingCapacity = seatingCapacity;
}
public int getSeatingCapacity() {
return seatingCapacity;
}
public void Display(int num) {
System.out.println(num + " " + this.getName() +
" " + this.getRent() + " " +
"载人:" + seatingCapacity + "人" + " ");
}
}
Trucks.java
/*卡车类,车的子类,包含:
父类继承的属性和方法;
新添加的私有属性载货数cargoCapacity;
cargoCapacity对应的访问方法;
Display()方法的重写;
含参构造方法
*/
public class Trucks extends Vehicle {
private int cargoCapacity; //载货量
public int getCargoCapacity() {
return cargoCapacity;
}
public Trucks(String name, int rent, int cargoCapacity) {
super(name, rent); //父类构造方法带参数,要用super显示调用
this.setName(name);
this.setRent(rent);
this.cargoCapacity = cargoCapacity;
}
public void Display(int num) {
System.out.println(num + " " + this.getName() +
" " + this.getRent() + " " +
"载货:" + cargoCapacity + "吨");
}
}
PickUpCars.java
/*皮卡车类,车的子类,包含:
父类继承的属性和方法;
新添加的私有属性载人数seatingCapacity;
seatingCapacity对应的访问方法;
新添加的私有属性载货数cargoCapacity;
cargoCapacity对应的访问方法;
Display()方法的重写;
含参构造方法
*/
public class PickUpCars extends Vehicle {
private int seatingCapacity; //载人量
private int cargoCapacity; //载货量
public int getSeatingCapacity() {
return seatingCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public PickUpCars(String name, int rent, int seatingCapacity, int cargoCapacity) {
super(name, rent); //父类构造方法带参数,要用super显示调用
this.setName(name);
this.setRent(rent);
this.seatingCapacity = seatingCapacity;
this.cargoCapacity = cargoCapacity;
}
public void Display(int num) {
System.out.println(num + " " + this.getName() +
" " + this.getRent() + " " +
"载人:" + seatingCapacity + "人" +
" " + "载货:" + cargoCapacity + "吨");
}
}
VehicleInfo.java
/*车的信息类,记录可租用车的相关信息,
包括车名,租金,载人数,载货量
包含:
记录车信息的方法information();
私有常量KINDS,记录车的种类;
KINDS的访问方法
*/
public class VehicleInfo {
Vehicle[] vehicle = new Vehicle[7];
private final int KINDS = 6;
public int getKINDS() {
return KINDS;
}
/*添加可租用的车的信息*/
public void information() {
vehicle[1] = new Cars("奥迪A4 ", 500, 4);
vehicle[2] = new Cars("马自达6", 400, 4);
vehicle[3] = new PickUpCars("皮卡雪6", 450, 4, 2);
vehicle[4] = new Cars("金龙 ", 800, 20);
vehicle[5] = new Trucks("松花江 ", 400, 4);
vehicle[6] = new Trucks("依维柯", 1000, 20);
}
}
printInfo.java
/*打印车的信息类,车的信息类的子类
包含一个打印车信息的方法printInfo()
*/
public class PrintInfo extends VehicleInfo {
/*打印可租用的车的信息*/
public void printInfo() {
super.information();
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号" + " " + "汽车名称" + " "
+ "租金(元/天)" + " " + "容量");
int i;
for (i = 1; i <= super.getKINDS(); i++) {
vehicle[i].Display(i);
}
}
}
Renting.java
/*租车类,包含:
私有属性租车数量rentNum;
私有属性租期rentDays;
属性对应的访问方法;
描述用户租车过程方法renting()
*/
import java.util.Scanner;
public class Renting {
private int rentNum; //记录租车的总数量
private int rentDays; //租期
int[] record = {0, 0, 0, 0, 0, 0, 0}; //记录各种车的被租数量
public int getRentNum() {
return rentNum;
}
public int getRentDays() {
return rentDays;
}
/*用户的租车过程*/
public void renting() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要租车的数量:");
if (scanner.hasNextInt()) {
rentNum = scanner.nextInt();
}
int carNO = 0; //选择的车序号
for (int i = 1; i <= rentNum; i++) {
System.out.println("请输入您要租的第" + i + "辆车的序号");
if (scanner.hasNextInt()) {
carNO = scanner.nextInt();
}
record[carNO]++;
}
System.out.println("请输入租车天数:");
if (scanner.hasNextInt()) {
rentDays = scanner.nextInt();
}
}
}
PrintBills.java
/*打印账单类,车信息类的子类,包含:
私有属性总载人数totalPerson;
私有属性总载货数totalCargo;
私有属性总租金rental;
打印账单的方法printBills()
*/
public class PrintBills extends VehicleInfo {
private int totalPerson = 0; //总载人数
private int totalCargo = 0; //总载货数
private int rental = 0; //总租金
/*打印账单*/
public void printBills() {
super.information();
Renting rentobj = new Renting();
rentobj.renting();
System.out.println("************************************");
System.out.println("您本次租车的账单如下:");
System.out.println("******你本次共租车" + rentobj.getRentNum() + "辆:");
System.out.println("汽车名字 出租数量");
for (int i = 1; i <= super.getKINDS(); i++) {
if (rentobj.record[i] != 0) {
System.out.println(vehicle[i].getName() + " " + rentobj.record[i]);
}
}
System.out.println("*****可载人的有:");
for (int i = 1; i <= super.getKINDS(); i++) {
if (rentobj.record[i] != 0) {
rental += vehicle[i].getRent(); //计算租一天总费用
if (vehicle[i] instanceof Cars) {
Cars car = (Cars) vehicle[i];
System.out.println(vehicle[i].getName());
totalPerson += car.getSeatingCapacity();
} else if (vehicle[i] instanceof PickUpCars) {
PickUpCars pickUpCar = (PickUpCars) vehicle[i];
System.out.println(vehicle[i].getName());
totalPerson += pickUpCar.getSeatingCapacity();
}
}
}
System.out.println("共可载人:" + totalPerson + "人");
System.out.println("*****可载货的有:");
for (int i = 1; i <= super.getKINDS(); i++) {
if (rentobj.record[i] != 0) {
if (vehicle[i] instanceof Trucks) {
Trucks truck = (Trucks) vehicle[i];
System.out.println(vehicle[i].getName());
totalCargo += truck.getCargoCapacity();
} else if (vehicle[i] instanceof PickUpCars) {
PickUpCars pickUpCar = (PickUpCars) vehicle[i];
System.out.println(vehicle[i].getName());
totalCargo += pickUpCar.getCargoCapacity();
}
}
}
rental *= rentobj.getRentDays(); //全部费用
System.out.println("共可载货:" + totalCargo + "吨");
System.out.println("*****租车总价格:" + rental + "元");
System.out.println("************************************");
}
}
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("欢迎使用嗒嗒租车系统!");
System.out.println("您是否需要租车?");
System.out.println("是:请输入数字1 不是:请输入数字2");
int ensure = 0;
if (scan.hasNextInt()) {
ensure = scan.nextInt();
}
//对错误输入的处理
while (ensure != 1 && ensure != 2) {
System.out.println("您的输入有误!请重新输入!");
System.out.println("您是否需要租车?");
System.out.println("是:请输入数字1 不是:请输入数字2");
if (scan.hasNextInt()) {
ensure = scan.nextInt();
}
}
//若用户输入数字1,则进入租车流程
if (ensure == 1) {
PrintInfo print = new PrintInfo(); //打印可租用的车的类型及价目表
print.printInfo();
PrintBills printBill = new PrintBills(); //打印账单
printBill.printBills();
}
}
}
遇到的问题:
在修改的过程中,一开始是把VehicleInfo类里的information()方法和Renting类里的renting()方法放在main()方法里面调用,然后调用两个打印方法printInfo()和printBills(),结果发现,这样在打印的时候vehicle[]和record[]数据丢失了,无法打印相关数据。后来几经尝试,把information()方法放在printInfo()里调用,同时在printBills()里面调用information()和renting()方法,在main()方法里只调用两个打印方法,这样就可以。也就是说,information()方法和renting()方法所产生的数据没有被保存,当要使用到相关数据时,都要再次调用这两个方法,重新生成数据。不知道这是为什么,求解答。
关于上述问题的解答:
vehicle[]和record[]的生存周期只在本类中,要打印时,之前的数据已经被回收,所以必须再次调用information()和renting()方法,重新生成数据。另一个解决办法是把vehicle[]和record[]用static修饰,这样生存周期就扩展到整个应用结束,数据也就不会丢失。
我的Java之旅——答答租车系统的改进的更多相关文章
- 我的Java之旅——答答租车系统
今天试着写了一个新的程序,叫做"答答租车系统",是慕课网上的一个综合练习,戳我看原题. 项目要求截图如下: 我的代码(简单粗暴版): Vehicle.java public cla ...
- Java练习 SDUT-3349_答答租车系统(面向对象综合练习)
答答租车系统(面向对象综合练习) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 各位面向对象的小伙伴们,在学习了面向对 ...
- [代码审计]某租车系统JAVA代码审计[前台sql注入]
0x00 前言 艰难徘徊这么久,终于迈出第一步,畏畏缩缩是阻碍大多数人前进的绊脚石,共勉. 系统是租车系统,这个系统是Adog师傅之前发在freebuf(http://www.freebuf.com/ ...
- Java学习笔记三十:Java小项目之租车系统
Java小项目之租车系统 一:项目背景介绍: 根据所学知识,编写一个控制台版的“呱呱租车系统” 功能: 1.展示所有可租车辆: 2.选择车型.租车量: 3.展示租车清单,包含:总金额.总载货量以及其车 ...
- 基于JSP+Servlet开发在线租车系统 java 源码
运行环境: 最好是java jdk 1.8,我们在这个平台上运行的.其他版本理论上也可以.IDE环境: Eclipse,Myeclipse,IDEA都可以tomcat环境: Tomcat 7.x,8. ...
- java学习之租车系统
背景:有三种类型的车供给用户来租用 要求:控制台用户交互界面,根据用户需求输出租车价格,结果如下: 创建租车类主要设计过程: 创建租车类 创建Car父类,包含四种属性成员,重写构造方法 创建三种 ...
- Java学习笔记(四)——好记性不如烂键盘(答答租车)
根据所学知识,编写一个控制台版的租车系统. 功能: 1. 展示所有可租车辆 2. 选择车型.租车辆 3. 展示租车清单,包含:总金额.总载货量及其车型.总载人量及其车型 代码参考imooc中Java课 ...
- JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!
JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
- JAVA之旅(三十四)——自定义服务端,URLConnection,正则表达式特点,匹配,切割,替换,获取,网页爬虫
JAVA之旅(三十四)--自定义服务端,URLConnection,正则表达式特点,匹配,切割,替换,获取,网页爬虫 我们接着来说网络编程,TCP 一.自定义服务端 我们直接写一个服务端,让本机去连接 ...
随机推荐
- webapi找到了与该请求匹配的多个操作
关于这个问题是路由在找方法的时候层没有指定对,同一个链接,同样的参数导致路由找不到方法导致的报错,可以在WebApiConfig中多配置一层,比如"api/{controller}/{act ...
- springcloud-断路器hystrix
Netflix的创造了一个调用的库 Hystrix 实现了断路器.在微服务架构中,通常有多层服务调用. 底层服务出现故障可能导致用户级联故障.当调用特定服务达到一定阈值时(Hystrix中的默认值为5 ...
- spark算子集锦
Spark 是大数据领域的一大利器,花时间总结了一下 Spark 常用算子,正所谓温故而知新. Spark 算子按照功能分,可以分成两大类:transform 和 action.Transform 不 ...
- 算法 - 排序数组中与x最近一点
条件: a[j] + a[j+1] < x*2 int findClosestPoint(int x,int a []) { int res = 0; int j = 0; while(j< ...
- python中新式类和经典类
python中的类分为新式类和经典类,具体有什么区别呢?简单的说, 1.新式类都从object继承,经典类不需要. Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Pyth ...
- 使用sshkey连接github等服务器
平常使用git时因为用了https的方式,所以经常要输入密码,其实我们是可以通过这个公钥连接github git.oschina.net等服务器,这样可以省去了我们输入用户名密码这么一个步骤了. 1. ...
- dynamics 365 AI 解决方案 —— 介绍
Digital transformation has been reshaping our world and artificial intelligence (AI) is one of the n ...
- 最长斐波那契序列-LeetCode-873
英文版A sequence X_1, X_2, ..., X_n is fibonacci-like if: - n >= 3- X_i + X_{i+1} = X_{i+2} for all ...
- MVP+Dagger2+Rxjava+Retrofit+GreenDao 小应用,包含新闻、图片、视频3个大模块,代码整洁干练
练习MVP架构开发的App,算是对自己学过的知识做一个总结,做了有一段时间,界面还算挺多的,代码量还是有的,里面做了大量封装,整体代码整理得很干净,这个我已经尽力整理了.不管是文件(Java.xml. ...
- mac系统使用介绍
点击左上角苹果-->关于本机 Dock-->偏好设置 推荐按装mac系统:OS X V10.8(山狮) Finder-->应用程序(安装程序)<==>我的电脑 Safar ...