刚做出来的,感觉类和对象还是不大会设计。请老师多提提改进的方法。
package student.carcental; /** * 车类 --抽象类 */ public abstract class Car { private String carName; //车的名 private int carNum; //车编号 private int carRent; //车租金 public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarNum() { return carNum; } public void setCarNum(int carNum) { this.carNum = carNum; } public int getCarRent() { return carRent; } public void setCarRent(int carRent) { this.carRent = carRent; } }
package student.carcental; /** * 货车类 */ public class Truck extends Car { private int cargoNum; //载货量 public Truck(String carName,int carRent,int cargoNum){ //构造方法 this.setCarName(carName); this.setCarRent(carRent); this.setCargoNum(cargoNum); } public int getCargoNum() { return cargoNum; } public void setCargoNum(int cargoNum) { this.cargoNum = cargoNum; } }
package student.carcental; /** * 客车类 */ public class Passengercar extends Car { private int capacity; //载客量 public Passengercar(String carName,int carRent,int capacity){ this.setCarName(carName); this.setCarRent(carRent); this.setCapacity(capacity); } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } }
package student.carcental; /** * 皮卡类 */ public class Pickup extends Car { private int capacity; //载客量 private int cargoNum; //载货量 public Pickup(String carName,int carRent,int capacity,int cargoNum){ this.setCarName(carName); this.setCarRent(carRent); this.setCargoNum(cargoNum); this.setCapacity(capacity); } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getCargoNum() { return cargoNum; } public void setCargoNum(int cargoNum) { this.cargoNum = cargoNum; } }
package student.carcental; import java.util.Arrays; /** * 车辆管理类 增删查存储 * 1。货车 2.客车 3.皮卡 */ public class CarManager { private Car[] carSave = new Car[3]; //数组对象 private int count=0; //存储数据数 public static Car getInstance(int carNum,String carName,int carRent,int...loadBearing){ //初始化类 if(carNum==1){ return new Truck(carName,carRent,loadBearing[0]); }else if(carNum==2){ return new Passengercar(carName,carRent,loadBearing[0]); }else if(carNum==3){ return new Pickup(carName,carRent,loadBearing[0],loadBearing[1]); } return null; } public void addCar(Car car){ //增加 //判断是否超出长度,扩充数组 if(count>=carSave.length){ int newLen = (carSave.length*3)/2 +1; carSave = Arrays.copyOf(carSave, newLen); } if(!searchCar(car.getCarName())){ //判断是否存在此车型 //存在时 System.out.println(car.getCarName()+"此车型已经存在"); }else{ //不存在 car.setCarNum(count+1); carSave[count] = car; count++; } } public void delCar(String carName){ //删除 int index = searchCarNum(carName); if(index != -1){ for (int i = index; i < count-1; i++) { carSave[i]=carSave[i+1]; carSave[i].setCarNum(i+1); //重置序号 } carSave[count-1]=null; count--; }else{ System.out.println("车型不存在"); } } public void showCar(){ //显示 for (Car car:carSave) { if(car!=null){ System.out.print(car.getCarNum()+";\t"); System.out.print(car.getCarName()+";\t"); if (car instanceof Truck) { System.out.print("载货:"+((Truck) car).getCargoNum()+"吨;\t\t"); } if (car instanceof Passengercar) { System.out.print("载人:"+((Passengercar) car).getCapacity()+"人;\t\t"); } if (car instanceof Pickup) { System.out.print("载货:"+((Pickup) car).getCargoNum()+"吨\\"); System.out.print("载人:"+((Pickup) car).getCapacity()+"人;\t"); } System.out.print(car.getCarRent()+";\t"); System.out.println(); } } } public boolean searchCar(String carName){ //查询 for (int i = 0; i < count; i++) { if(carName.equals(carSave[i].getCarName())){ return false; } } return true; } public int searchCarNum(String carName){ //查询id for (int i = 0; i < count; i++) { if(carName.equals(carSave[i].getCarName())){ return i; } } return -1; } public Car returnCar(int carNum){ //返回车对象 return carSave[carNum]; } }
package student.carcental; /** * 租车管理接口 */ public interface IRentalManager { public void rentaldata(); //租车数据 public void totalList(); //租车统计 }
package student.carcental; import java.util.Iterator; import java.util.Scanner; /** * 个人租赁类 */ public class PersonRental implements IRentalManager { public void rentaldata(){ System.out.println("欢迎使用答答租车系统"); Scanner in = new Scanner(System.in); System.out.println("你是否要租车?1.是 0.否"); int input = in.nextInt(); if (input==1) { System.out.println("可租车辆清单"); System.out.println("序号\t"+"汽车名称\t"+"容量\t\t"+"租金"); //租车清单 CarManager cm = new CarManager(); cm =new StartData().startData(cm); cm.showCar(); System.out.println("请输入租车的数量:"); int rentalNum = in.nextInt(); //租的车辆 int[] carArrays = new int[rentalNum]; for (int i = 0; i < carArrays.length; i++) { System.out.println("请输入要租"+(i+1)+"号车的序号"); carArrays[i]=in.nextInt()-1;//和存储的车的序号对其来 } System.out.println("请输入天数:"); int days = in.nextInt(); totalList(carArrays, days, cm); }else if(input==0){ System.out.println("感谢使用答答租车系统."); }else{ System.out.println("输入信息错误!"); } } public void totalList(){ } public void totalList(int[] ca,int day,CarManager cm){ int total=0; //总额 String passengercar=""; //可载人的车 String truck=""; //可载货的车 for (int i = 0; i < ca.length; i++) { Car car = cm.returnCar(ca[i]); total+=car.getCarRent()*day; if(car instanceof Truck){ if(truck.indexOf(car.getCarName()+";")==-1){ truck=truck.concat(car.getCarName()+";"); } } if(car instanceof Passengercar){ if(passengercar.indexOf(car.getCarName()+";")==-1){ passengercar=passengercar.concat(car.getCarName()+";"); } } if(car instanceof Pickup){ if(passengercar.indexOf(car.getCarName()+";")==-1){ passengercar=passengercar.concat(car.getCarName()+";"); } if(truck.indexOf(car.getCarName()+";")==-1){ truck=truck.concat(car.getCarName()+";"); } } } System.out.println("可载人的车辆有:"); System.out.println(passengercar); System.out.println("可载货的车辆有:"); System.out.println(truck); System.out.println("总金额"); System.out.println(total); } }
package student.carcental; /** * * 滴滴租车系统 * 1.展示所有可租车辆 * 2.选择车型、租车量 * 3.展示租车清单,包含:总金额、总载货量、总载人量及车型 * */
public class ShowMain { public static void main(String[] args){ IRentalManager p = new PersonRental(); p.rentaldata(); } }
请老师和同学多提意见。刚开始学习java,面向对象老是觉得分析起来困难