使用List容器是否可以降低一些类与类之间的耦合,增加可扩展性
//程序入口 package dada; import java.util.ArrayList; import java.util.Scanner; public class Test { static int price; static int weight; static int numberOfPeople; public ArrayList<Car> listCars = new ArrayList<Car>(); public void add(Car car){ listCars.add(car); } public void list(){ for(Car car : listCars){ //输出车属性 car.out(); } } public void out(int number) { ArrayList<String> nameGoodsList = new ArrayList<String>(); ArrayList<String> namePeopleList = new ArrayList<String>(); Scanner in = new Scanner(System.in); for (int i = 0; i < number; i++) { System.out.println("请输入第"+(i+1)+"辆车的序号:"); int num = in.nextInt(); Car c1 = (Car)listCars.get(num-1); Test.price += c1.getMoney(); if(c1 instanceof People){ namePeopleList.add(c1.getName()); Test.numberOfPeople += ((People) c1).getPeople(); }else if(c1 instanceof Goods){ nameGoodsList.add(c1.getName()); Test.weight += ((Goods) c1).getGoods(); }else{ namePeopleList.add(c1.getName()); nameGoodsList.add(c1.getName()); Test.numberOfPeople += ((Pickup) c1).getPeople(); Test.weight += ((Pickup) c1).getGoods(); } } System.out.println("请输入租车天数:"); int day = in.nextInt(); Test.price = price * day; System.out.println("您的账单:\n***可载人的车有:"); for (int i=0; i<namePeopleList.size(); i++) { System.out.print(namePeopleList.get(i)+"\t"); } System.out.println("共载人:"+numberOfPeople+"人"); System.out.println("***载货的车有:"); for(int i=0; i<nameGoodsList.size(); i++){ System.out.print(nameGoodsList.get(i)+"\t"); } System.out.println("共载货:"+weight+"吨"); System.out.println("***租车总价格:"+ price+"\n"); price = weight = numberOfPeople = 0; in.close(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int choose=0; Test outList = new Test(); outList.add(new People(1, "奥迪A4 ", 500, 4 )); outList.add(new People(2, "马自达6", 400, 4)); outList.add(new Pickup(3, "皮卡雪6", 450, 4, 2)); outList.add(new People(4, "金龙", 800, 20)); outList.add(new Goods(5, "松花江", 400, 4)); outList.add(new Goods(6, "依维柯", 1000, 20)); while(true){ System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是 0否"); while ((choose=in.nextInt())==1) { System.out.println("您可租车的类型及价目表:\n序号\t汽车名称\t租金(元/天)\t容量"); outList.list(); System.out.println("请输入您要租车的数量:"); int num = in.nextInt(); outList.out(num); System.out.println("您是否要租车:1是 0否"); } in.close(); if(choose==0) System.out.println("感谢您的使用,再见!"); else System.out.println("输入错误,请重新输入!"); } } }
//父类
package dada;
public class Car {
private int number;
private String name;
private int money;
public Car(int number, String name, int money) {
this.money = money;
this.name = name;
this.number = number;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void out(){
System.out.print(number+"\t"+name+"\t"+money+"(元/天)\t");
}
public void nameOut(){
System.out.print(name);
}
}
//载人接口
package dada;
public interface IPeople {
public int getPeople();
public void setPeople(int people);
}
//载货接口
package dada;
public interface IGoods {
public int getGoods();
public void setGoods(int Goods);
}
//客车类
package dada;
public class People extends Car implements IPeople{
private int people;
public People(int number, String name, int money, int people) {
super(number, name, money);
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public void setPeople(int people) {
this.people = people;
}
public void out() {
super.out();
System.out.println("载人:"+people+"人");
}
}
//货车类
package dada;
public class Goods extends Car implements IGoods{
private int goods;
public Goods(int number, String name, int money, int goods) {
super(number, name, money);
this.goods = goods;
}
@Override
public int getGoods() {
return goods;
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
public void out() {
super.out();
System.out.println("载货:"+goods+"吨");
}
}
//皮卡类
package dada;
public class Pickup extends Car implements IPeople, IGoods{
private int people;
private int goods;
public Pickup(int number, String name, int money, int people, int goods) {
super(number, name, money);
this.goods = goods;
this.people = people;
}
@Override
public int getPeople() {
return people;
}
@Override
public void setPeople(int people) {
this.people = people;
}
@Override
public int getGoods() {
return goods;
}
@Override
public void setGoods(int goods) {
this.goods = goods;
}
public void out() {
super.out();
System.out.println("载人:"+people+"人\t"+"载货:"+goods+"吨");
}
}