继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java入门第二季:哒哒租车系统

风吹过的季节w
关注TA
已关注
手记 2
粉丝 5
获赞 17

父类Car.java
public class Car {
public String name;//车的名字
public int rent;//车的租金
public int peopleCapacity;//载客量
public int cargoCapacity;//载货量
public int num;//车的数量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public int getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
三个子类PeopleCar.java,Trunk.java,PickUp.java
(People.java)
public class PeopleCar extends Car {
private String name;
private int rent;
private int peopleCapacity;//载客量
private int num;
public PeopleCar(String name,int rent,int peopleCapacity,int num){
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public int getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
(Trunk.java)
public class Trunk extends Car {
private String name;
private int rent;
private int cargoCapacity;//装货数量
private int num;
public Trunk(String name,int rent,int cargoCapacity,int num){
this.name=name;
this.rent=rent;
this.cargoCapacity=cargoCapacity;
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
(PickUp)
public class PickUp extends Car {
private String name;
private int rent;
private int peopleCapacity;//载客量
private int cargoCapacity;//载货量
private int num;
public PickUp(String name,int rent,int peopleCapacity,int cargoCapacity,int num){
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
this.cargoCapacity=cargoCapacity;
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public int getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
测试函数(参考一位幕友http://www.imooc.com/article/19663发表的哒哒租车系统设计的,改了一些小地方,有兴趣的可以看看他的)
package project5;

import java.util.Scanner;

public class Test {
private static int a,b;//变量a用于判断是否租车,b用于判断租车数量
static Scanner input=new Scanner(System.in);
//static int carTypeTotal=5;//车型总数
static int carTotal=0;//用于保存全部一共的车数
static int aMoney=0;//用于记录一天的钱数
static Car[] carForRent = { new PeopleCar("奔驰", 400, 5,5), new PeopleCar("奥迪", 300, 6,2), new Trunk("大型货车", 800, 9,3),
new Trunk("小型货车", 500, 5,6), new PickUp("皮卡车", 300, 3, 1,5) };
static int carType[]=new int[carForRent.length];//用于记录每种类型车租的数量
private static void isNeeded(){//询问是否租车
System.out.print("你是否要租车【1是 0否】:");
do{
a=input.nextInt();//do while 循环,若输入的不是1或者0,则循环
if(a!=0&&a!=1){
System.out.print("你输入的值有误,请重新输入:");
}
}while(a!=1&&a!=0);//若输出0或者1则跳出循环
}
private static void displayList(){//显示车辆列表
System.out.println("你可以租车的类型及其价格表 如下:");
System.out.println("\t╭───────────────────────────────────────────────────────╮");
System.out.println("\t│序号\t│汽车名称\t│租金\t│载人量\t│载货量\t│车型的总量\t┃");
System.out.println("\t ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
for(int i=0;i<carForRent.length;i++){//三种类型的车的列表文字输出方式不同
if(carForRent[i] instanceof PeopleCar){//判断实例所属的类
System.out.println("\t│"+(i+1)+".\t│"+carForRent[i].getName()+"\t│"+carForRent[i].getRent()+"元/天│载人:"+carForRent[i].getPeopleCapacity()+"人│\t│剩有:"+carForRent[i].getNum()+"辆车\t┃");
System.out.println("\t ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
}else if(carForRent[i] instanceof Trunk){
System.out.println("\t│"+(i+1)+".\t│"+carForRent[i].getName()+"\t│"+carForRent[i].getRent()+"元/天│\t│载货:"+carForRent[i].getCargoCapacity()+"吨│剩有:"+carForRent[i].getNum()+"辆车\t┃");
System.out.println("\t ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
}else if(carForRent[i] instanceof PickUp){
System.out.println("\t│"+(i+1)+".\t│"+carForRent[i].getName()+"\t│"+carForRent[i].getRent()+"元/天│载人:"+carForRent[i].getPeopleCapacity()+"人│载货:"+carForRent[i].getCargoCapacity()+"吨│剩有:"+carForRent[i].getNum()+"辆车\t┃");
}
}
System.out.println("\t╰━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╯");
}
private static void howMany(){//询问租多少车
System.out.print("请确认要租赁的车型数量:");
for(int i=0;i<carForRent.length;i++){
carTotal+=carForRent[i].getNum();//计算一下 全部-共有多少车
}
do{
b=input.nextInt();
if(b>0&&b<=carTotal){
continue;
}
System.out.print("你输入的数值有误,请重新输入:");
}while(b<0&&b>carTotal);
}
private static void carNum(){//车型选择
for(int n=0;n<carForRent.length;n++){
carType[n]=0;
}//把这个数组的值初始化为0
for(int i=1;i<=b;i++){
System.out.print("("+i+")请输入要租的车的序号:");
int c;
do{
c=input.nextInt();
if(!(c>=1&&c<=carForRent.length)){
System.out.print("你输入的数值有误,请重新输入:");
continue;
}else if(carForRent[c-1].getNum()<1){
System.out.println("很抱歉,改类型的车已被租空,请重新选择:");
c=0;
continue;
}else{
carForRent[c-1].getNum();
aMoney+=carForRent[c-1].getRent();//用于记录一天所需的钱数
carType[c-1]++;
}
}while(!(c>=1&&c<=carForRent.length));
}
}
private static void rentDay(){//租车天数并计算账单
System.out.print("请输入要租车的天数:");
int day;
do{
day=input.nextInt();
if(!(day>=1&&day<=365)){
System.out.print("你输入的数值有误,天数介于1-365之间,请重新输入:");
}
}while(!(day>=1&&day<=365));
//计算账单
int peoples=0;//记录在人车的载人总数
int trunks=0;//记录货车 的载货总数
int pickuppeoples=0;//记录皮卡车的载人总数
int pickupcargos=0;//记录皮卡车的载货总数
System.out.println("你的账单有:\n");
System.out.println("客车有:");
for(int i=0;i<carForRent.length;i++){
if(carForRent[i] instanceof PeopleCar&&carType[i]>0){
System.out.print(carForRent[i].getName()+":"+carType[i]+"辆\t\t");
peoples+=(carType[i]carForRent[i].getPeopleCapacity() );
}
}
System.out.println("\n共载人:"+peoples+"人\n");
System.out.println("货车有:");
for(int j=0;j<carForRent.length;j++){
if(carForRent[j] instanceof Trunk&&carType[j]>0){
System.out.print(carForRent[j].getName()+":"+carType[j]+"辆\t");
trunks+=(carType[j]
carForRent[j].getCargoCapacity());
}
}
System.out.println("\n共载货:"+trunks+"吨\n");
System.out.println("皮卡有:");
for(int k=0;k<carForRent.length;k++){
if(carForRent[k] instanceof PickUp&&carType[k]>0){
System.out.print(carForRent[k].getName()+":"+carType[k]+"辆\t");
pickuppeoples+=(carType[k]carForRent[k].getPeopleCapacity());
pickupcargos+=(carType[k]
carForRent[k].getCargoCapacity());
}
}
System.out.print("\n共载人:"+pickuppeoples+"人");
System.out.println("\n共载货:"+pickupcargos+"吨\n");
System.out.println("租这些车一共能载客:"+(peoples+pickuppeoples)+"人");
System.out.println("租这些车一共能载货"+(trunks+pickupcargos)+"吨");
System.out.println("租这些车一共需要"+aMoneyday+"元");
System.out.print("是否确认租车【1是 0否】:");
int ok;
do{
ok=input.nextInt();
if(ok!=0&&ok!=1){
System.out.print("你输入的值有误,请从新输入:");
}
}while(ok!=1&&ok!=0);
if(ok==1){
System.out.println("你已成功租车!我们将在你的账户中扣除"+aMoney
day+"元");
System.out.println("感谢你使用哒哒租车系统,再见!");
}else{
System.out.println("欢迎下次光临,再见!");
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用哒哒租车系统");
isNeeded();
if(a==0){
System.out.println("欢迎下次光临,再见!");
System.exit(0);
}else{
displayList();
howMany();
carNum();
rentDay();
}
}}
程序实现:
图片描述图片描述

打开App,阅读手记
14人推荐
发表评论
随时随地看视频慕课网APP

热门评论

就是你的代码。。有一个就是你计算人数的地方没有加乘号,,

查看全部评论