package com.imooc; public class Car { int CarNo;//汽车编号 String CarName;//汽车名字 int Carrental;//汽车租金 int passenger;//载客 int cargo;//载货 public Car(int carNo, String carName, int carrental) { super(); CarNo = carNo; CarName = carName; Carrental = carrental; } public int getPassenger() { return passenger; } public void setPassenger(int passenger) { this.passenger = passenger; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } public int getCarNo() { return CarNo; } public void setCarNo(int carNo) { CarNo = carNo; } public String getCarName() { return CarName; } public void setCarName(String carName) { CarName = carName; } public int getCarrental() { return Carrental; } public void setCarrental(int carrental) { Carrental = carrental; } }
package com.imooc; public class Bus extends Car { int passenger;//载客 public Bus(int carNo, String carName, int carrental, int passenger) { super(carNo, carName, carrental); this.passenger = passenger; } public int getPassenger() { return passenger; } public void setPassenger(int passenger) { this.passenger = passenger; } public void say() { System.out.println(CarNo+". "+CarName+" "+Carrental+"元/天 载人:"+passenger+"人"); } }
package com.imooc; public class Jeep extends Car { int passenger;//载客 int cargo;//载货 public Jeep(int carNo, String carName, int carrental, int passenger, int cargo) { super(carNo, carName, carrental); this.passenger = passenger; this.cargo = cargo; } public int getPassenger() { return passenger; } public void setPassenger(int passenger) { this.passenger = passenger; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } public void say() { System.out.println(CarNo+". "+CarName+" "+Carrental+"元/天 载人:"+passenger+"人载货:"+cargo+"吨"); } }
package com.imooc; public class Truck extends Car{ int cargo;//载货 public Truck(int carNo, String carName, int carrental, int cargo) { super(carNo, carName, carrental); this.cargo = cargo; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } public void say() { System.out.println(CarNo+". "+CarName+" "+Carrental+"元/天 载货:"+cargo+"吨"); } }
下面是主类
package com.imooc; import java.util.Scanner; public class Test { public static void main(String []args) { Bus c1=new Bus(1,"奥迪A4",500,4); Bus c2=new Bus(2,"马自达6",400,4); Jeep c3=new Jeep(3,"皮卡雪6",450,4,2); Bus c4=new Bus(4,"金龙",800,20); Truck c5=new Truck(5,"松花江",400,4); Truck c6=new Truck(6,"依维柯",1000,20); Car [] cars = {c1,c2,c3,c4,c5}; System.out.println("欢迎使用答答租车系统"); System.out.println("你是否需要租车:1是0否"); Scanner sc=new Scanner(System.in); int x=sc.nextInt(); if(x==1) { System.out.println("你可租车得类型以及价目标如下"); c1.say();c2.say();c3.say();c4.say();c5.say();c6.say(); System.out.println("请输入你要租汽车的数量:"); int num=sc.nextInt(); int arr []=new int [num]; for(int i=1;i<=num;i++) { System.out.println("请输入要租的第"+i+"辆车的序号:"); arr[i-1]=sc.nextInt(); } System.out.println("请输入租车天数:"); int day=sc.nextInt(); System.out.println("你的账单:"); System.out.println("***可载人的车有***"); int count=0;//记录载人的数量 for(int i=0;i<arr.length;i++) { if(cars[arr[i]-1].getPassenger()>0) { System.out.print(cars[arr[i]-1].getCarName()+" "); count=count+cars[arr[i]-1].getPassenger(); } } System.out.print("共载人:"+count+"人"); System.out.println(); System.out.println("***可载货的车有***"); int t=0;//记录载货的吨数 int money=0;//租一天要用到的前 for(int i=0;i<arr.length;i++) { if(cars[arr[i]-1].getCargo()>0) { System.out.print(cars[arr[i]-1].getCarName()+" "); t=t+cars[arr[i]-1].getCargo(); } money=money+cars[arr[i]-1].getCarrental(); } System.out.print("共载货:"+t+"T"); System.out.println(); System.out.println("***租车总价格:"+money*day+"元"); }else if(x==0){ System.out.println("退出答答租车系统 成功"); }else { System.out.println("输入错误"); } } }
热门评论
作者:二白19990717
链接:https://www.imooc.com/article/305559
来源:慕课网