博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三大特性应用之YY出租系统的设计
阅读量:2346 次
发布时间:2019-05-10

本文共 4758 字,大约阅读时间需要 15 分钟。

Java基础项目:

项目描述:设计一个车辆出租系统,在设计中,利用面向对象编程的三大特性:封装性、继承性以及多态性;

完成的功能:在控制台进行出租车辆的信息,接收用户的输入,根据用户的输入,完成对应的操作,实现简单的请求-响应的业务功能;

实现的难点:

项目源码:

package com.boker_han;//抽象类(出租车辆共有的基本信息的描述方法)public abstract class Vehicle {
public abstract String function(); public abstract int getPrice(); public abstract String getBrand();}package com.boker_han;//小车对应的类public class Car extends Vehicle {
private int price; private int amount; private String brand; public Car(String brand,int amount,int price){ this.brand = brand; this.amount = amount; this.price = price; } @Override public String function() { return (brand+"\t"+amount+"人/辆\t"+0+"吨/辆\t"+price+"元/天"); } @Override public int getPrice(){ return price; } @Override public String getBrand(){ return brand; }}package com.boker_han;//卡车对应的类public class Truck extends Vehicle {
private int price; private int amount; private String brand; public Truck(String brand,int amount,int price){ this.brand = brand; this.amount = amount; this.price = price; } @Override public String function() { return (brand+"\t"+0+"人/辆\t"+amount+"吨/辆\t"+price+"元/天"); } @Override public int getPrice(){ return price; } @Override public String getBrand(){ return brand; }}package com.boker_han;//皮卡对应的类public class Mpv extends Vehicle {
private int price; private int amount; private int weight; private String brand; public Mpv(String brand,int amount,int weight,int price){ this.brand = brand; this.amount = amount; this.weight = weight; this.price = price; } @Override public String function() { // TODO Auto-generated method stub return (brand+"\t"+amount+"人/辆\t"+weight+"吨/辆\t"+price+"元/天"); } @Override public int getPrice(){ return price; } @Override public String getBrand(){ return brand; }}package com.boker_han;import java.util.Scanner;//主类(控制台)public class InitialDada {
public static void main(String[] args) { System.out.println("欢迎使用YY租车系统:"); System.out.println("您是否需要使用YY租车:1是 0否"); Scanner keyboard = new Scanner(System.in); int answer = keyboard.nextInt(); while((answer!=0)&&(answer!=1)){ System.out.println("输入有误,请重新输入"); answer = keyboard.nextInt(); } if(answer == 1) { Vehicle []v = {
new Car("奥迪",4,500),new Car("福特",4,300),new Car("哈弗",7,400),new Truck("东风",3,600),new Truck("重卡",6,650),new Mpv("依维柯",4,1,500)};//多态性的应用/********************输出租车系统提供用户的租车信息*******************/ System.out.println("以下是可以提供的租车信息:"); System.out.println("序号\t"+"品牌 \t"+"载人\t"+"载货\t"+"租金\t"); for(int i=0;i<6;i++){ System.out.println((i+1)+".\t"+v[i].function());//多态性的应用 } /********************用户输入租车信息,并保存在系统中*******************/ System.out.println("请输入需要租车的种类:"); int number = keyboard.nextInt(); int sequence[] = new int[number]; int day[] = new int[number]; int total=0; for(int i = 0; i < number; i++) { System.out.println("请输入第"+(i+1)+"种车的序号:"); sequence[i] = keyboard.nextInt(); while((sequence[i]>6)||(sequence[i]<0)){ System.out.println("输入有误,请重新输入!提醒:输入的数字在0~6之间"); sequence[i] = keyboard.nextInt(); } System.out.println("请输入租用该车的天数:"); day[i] = keyboard.nextInt(); total = total+(v[sequence[i]-1].getPrice())*day[i]; } /*****************计算并输出用户的租车具体情况**********************/ System.out.println("你的账单信息如下:"); System.out.println("****车辆信息:"); System.out.println("载人的汽车有:"); int flag1 = 0; int flag2 = 0; for(int i=0;i
3)&&(sequence[i]<=6)){ flag2++; System.out.print(v[i].getBrand()+" "); } } if(flag2 == 0){ System.out.println("未租用载货汽车"); } System.out.println(); System.out.println("****租车总金额:"+total); } else{ System.out.println("即将退出YY租车系统!"); } }}

测试效果:

欢迎使用YY租车系统:
您是否需要使用YY租车:1是 0否
1
以下是可以提供的租车信息:
序号 品牌 载人 载货 租金
1. 奥迪 4人/辆 0吨/辆 500元/天
2. 福特 4人/辆 0吨/辆 300元/天
3. 哈弗 7人/辆 0吨/辆 400元/天
4. 东风 0人/辆 3吨/辆 600元/天
5. 重卡 0人/辆 6吨/辆 650元/天
6. 依维柯 4人/辆 1吨/辆 500元/天
请输入需要租车的种类:
2
请输入第1种车的序号:
1
请输入租用该车的天数:
13
请输入第2种车的序号:
2
请输入租用该车的天数:
3
你的账单信息如下:
**车辆信息:
载人的汽车有:
奥迪 福特
载货的汽车有:
未租用载货汽车

**租车总金额:7400

转载地址:http://htsvb.baihongyu.com/

你可能感兴趣的文章
unity3d 4 assert store
查看>>
tab bar control 注意事项
查看>>
sql优化部分总结
查看>>
IDEA运行时动态加载页面
查看>>
UML总结(对九种图的认识和如何使用Rational Rose 画图)
查看>>
js遍历输出map
查看>>
easeui分页
查看>>
20个非常有用的Java程序片段
查看>>
Enterprise Architect使用教程
查看>>
Enterprise Architect 生成项目类图
查看>>
浅入深出 MySQL 中事务的实现
查看>>
UML总结(对九种图的认识和如何使用Rational Rose 画图)
查看>>
Java中使用HttpRequest获取用户真实IP地址端口
查看>>
easyUI下拉列表点击事件的使用
查看>>
js遍历map
查看>>
单例模式
查看>>
JDBC连接数据库核心代码
查看>>
java生成随机汉字
查看>>
Java反射的基本应用
查看>>
HTML5常用标签
查看>>