就是JAVA第二季最后一个综合练习,就是想问如果运用抽象和接口时是不是这样处理和拆分。
package com.imocc;
public abstract class CarSystem {
private int number;
private String name;
private int money;
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;
}
}
package com.imocc;
public interface Passenger {
public void setPassenger(int passengere);
public int getPassenger();
}
package com.imocc;
public interface Goods {
public void setGoods(int goods);
public int getGoods();
}
package com.imocc;
public class Bus extends CarSystem {
private int passenger;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public Bus(int number,String name,int money,int passenger){
this.setNumber(number);
this.setName(name);
this.setMoney(money);
this.setPassenger(passenger);
}
}
package com.imocc;
public class Lorries extends CarSystem implements Goods {
private int goods;
@Override
public void setGoods(int goods) {
// TODO Auto-generated method stub
this.goods=goods;
}
@Override
public int getGoods() {
// TODO Auto-generated method stub
return goods;
}
public Lorries(int number,String name,int money,int goods){
this.setNumber(number);
this.setName(name);
this.setMoney(money);
this.setGoods(goods);;
}
}
package com.imocc;
public class Pika extends CarSystem implements Goods, Passenger {
private int passenger;
private int goods;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
public Pika(int number,String name,int money,int passenger,int goods){
this.setNumber(number);
this.setName(name);
this.setMoney(money);
this.setPassenger(passenger);
this.setGoods(goods);
}
}
package com.imocc;
public class CarList {
CarSystem [] car={new Bus(1,"奥迪A4",500,4),new Bus(2,"马自达6",400,4),new Pika(3,"皮卡雪6",450,4,2),new Bus(4," 金龙 ",800,20),
new Lorries(5,"松花江",400,4),new Lorries(6,"依维柯",1000,20)};
}
package com.imocc;
import java.util.Scanner;
public class CarText {
public static void main(String[] args) {
// TODO Auto-generated method stub
CarList cl=new CarList();
System.out.println("欢迎来到滴滴租车系统!");
System.out.println("请问是否租车:1,是2.否");
Scanner c=new Scanner(System.in);
int k=c.nextInt();
if(k==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"乘客/载重");
for(CarSystem cs:cl.car){
if(cs instanceof Bus){
System.out.println(cs.getNumber()+"\t"+cs.getName()+"\t"+cs.getMoney()+"\t"+((Bus)cs).getPassenger());
}
if(cs instanceof Lorries){
System.out.println(cs.getNumber()+"\t"+cs.getName()+"\t"+cs.getMoney()+"\t"+"\t"+((Lorries)cs).getGoods());
}
if(cs instanceof Pika){
System.out.println(cs.getNumber()+"\t"+cs.getName()+"\t"+cs.getMoney()+"\t"+((Pika)cs).getPassenger()+"\t"+((Pika)cs).getGoods());
}
}
System.out.println("请输入您要租的汽车的");
int a=c.nextInt()-1;
System.out.println(cl.car[a].getName());
System.out.println("请输入您要租的天数");
int b=c.nextInt();
System.out.println("你需要支付的金额:"+b*cl.car[a].getMoney());
}
else if(k==2){
System.out.println("欢迎再次光临");
}
}
}
添加回答
举报
0/150
提交
取消