参考了别人的代码,有些代码还是很繁琐,第二季中学的知识点用的不多。载货量和载客量同时定义在了父类里,主要是因为写到后面计算总量的时候实在想不出其他办法了。
Car 父类:
package imooc;
public class Car {
String name;
int price;
int passengerCapacity;
int goodsCapacity;
public Car(String name,int price,int passengerCapacity,int goodsCapacity){
this.name=name;
this.price=price;
this.passengerCapacity=passengerCapacity;
this.goodsCapacity=goodsCapacity;
}
public void print()
{
System.out.println(""+name+"t"+price+"元/天"+"t"+"载人:"+passengerCapacity+" 载货:"+goodsCapacity+"吨");
}
public String getName(){
return this.name=name;
}
public int getPassengerCapacity()
{
return this.passengerCapacity=passengerCapacity;
}
public int getGoodsCapcity()
{
return this.goodsCapacity=goodsCapacity;
}
public int getPrice()
{
return this.price=price;
} }
Passenger 子类
package imooc;
public class Passenger extends Car {
public Passenger(String name, int price,int passengerCapacity,int goodsCapacity) {
super(name, price, passengerCapacity, goodsCapacity);
} }
Pickup子类
package imooc;
public class Pickup extends Car {
public Pickup(String name,int price,int passengerCapacity,int goodsCapacity){
super(name,price, passengerCapacity, goodsCapacity);
} }
Goods子类
package imooc;
public class Goods extends Car {
public Goods(String name,int price,int passengerCapacity,int goodsCapacity){
super(name,price, passengerCapacity, goodsCapacity);
} }
主代码
package imooc;
import java.util.ArrayList;
import java.util.Scanner;
public class Item {
private ArrayList
public void add(Car i){
car.add(i);
}
public void list(){
int k=1;
for(Car i:car){
System.out.print(k+"t");
i.print();
k++;
}
}
public static void main(String[] args) {
Item item=new Item();
System.out.println("欢迎使用达达租车系统。");
System.out.println("您是否要租车?1:租车,2:退出系统");
Scanner in =new Scanner(System.in);
int input=in.nextInt();
if(input==1)
{
System.out.println("可选车型一览表:");
System.out.println("序号t"+"汽车名车t"+"租金t"+"容量t");
item.add(new Passenger("奥迪",500,4,0));
item.add(new Passenger("马自达",400,4,0));
item.add(new Pickup("皮卡雪",450,4,20));
item.add(new Passenger("金龙",800,20,0));
item.add(new Goods("松花江",400,0,4));
item.add(new Goods("依维柯",1000,0,20));
item.list();
Car [] car={new Passenger("奥迪",500,4,0),new Passenger("马自达",400,4,0),new Pickup("皮卡雪",450,4,20),new Passenger("金龙",800,20,0),new Goods("松花江",400,0,4),new Goods("依维柯",1000,0,20)};
System.out.print("请输入租车数量:");
int rentNums=in.nextInt();
int[] carQuantity=new int[rentNums];
for (int i=0;i }
运行结果:
欢迎使用达达租车系统。
您是否要租车?1:租车,2:退出系统
1
可选车型一览表:
序号 汽车名车 租金 容量
1 奥迪 500元/天 载人:4 载货:0吨
2 马自达 400元/天 载人:4 载货:0吨
3 皮卡雪 450元/天 载人:4 载货:20吨
4 金龙 800元/天 载人:20 载货:0吨
5 松花江 400元/天 载人:0 载货:4吨
6 依维柯 1000元/天 载人:0 载货:20吨
请输入租车数量:3
请输入第1辆车的序号:4
请输入第2辆车的序号:2
请输入第3辆车的序号:3
请输入租车天数:2
你的账单:
车名 租金 数量 载客量 载货量
金龙 800元/天 1 20 0吨
马自达 400元/天 1 4 0吨
皮卡雪 450元/天 1 4 20吨
总量为:
租车数量 总载客量 总载货量 总金额
3 28 20 3300
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2157/viewspace-2797977/ ,如需转载,请注明出处,否则将追究法律责任。