java的第二季答答租车系统,参考大神代码作了改进

2017-09-02 10:21

原标题:java的第二季答答租车系统,参考大神代码作了改进

定义了汽车抽象类,客车,货车,皮卡三个子类。放在zucheSystem包中定义载客和载货两个接口。放在Icar包中最后一个实现类。放在carSystem包中

欢迎大家一起改进讨论。 封装zucheSystem; //定义一个汽车抽象类 public abstract class CarStore { private String name; 私人双重钱; 私有int id public int getId(){ return id; } public void setId(int id){ this.id = id; } public double getMoney(){ return money; } public void setMoney(double money){ this.money = money; } public String getName(){ return name; } public void setName(String name){ this.name = name; } } 封装zucheSystem; 进口Icar.zaihuo; //货车类 public class huoche extends CarStore implements zaihuo { private int newGood; public huoche(int id,String name,double money,int newGood){ super.setId(id); super.setName(名称); super.setMoney(钱); this.setGood(newGood); } @覆盖 公共无效setGood(INT newGood){ // TODO自动生成的方法存根 this.newGood = newGood; } @覆盖 公众诠释getGood(){ // TODO自动生成的方法存根 返回newGood; } //重写输出语句 public String toString(){ return super.getId()+“\ t”+ super.getName()+“\ t”+ super.getMoney()+“ 元/天\ t”+ “”+“载货:”+ this.getGood()+“吨”; } } 封装zucheSystem; 进口Icar.zaike; //客车类 public class keche extends CarStore implements zaike { private int newPeople; public keche(int id,String name,double money,int newPeople){ super.setId(id); super.setName(名称); super.setMoney(钱); this.setPeople(newPeople); } @覆盖 公共无效setPeople(INT newPeople){ // TODO自动生成的方法存根 this.newPeople = newPeople; } @Override public int getPeople(){ // TODO自动生成的方法存根 返回newPeople; } @覆盖 //重写输出语句 公共字符串的toString(){ 返回super.getId()+ “\ t”的+ super.getName()+ “\ t”的+ super.getMoney()+“ 元/天\吨“+”“+”载人:“+ this.getPeople()+”人“; } } 封装zucheSystem; 进口Icar.zaihuo; 进口Icar.zaike; //皮卡类 public class pika extends CarStore implements zaike,zaihuo { private int newGood; private int newPeople; public pika(int id,String name,double money,int newPeope,int newGood){ super.setId(id); super.setName(名称); super.setMoney(钱); this.setPeople(newPeope); this.setGood(newGood); } @覆盖 公共无效setGood(INT newGood){ // TODO自动生成的方法存根 this.newGood = newGood; } @覆盖 公众诠释getGood(){ // TODO自动生成的方法存根 返回newGood; } @覆盖 公共无效setPeople(INT newPeople){ // TODO自动生成的方法存根 this.newPeople = newPeople; } @Override public int getPeople(){ // TODO自动生成的方法存根 返回newPeople; } @覆盖 //重写输出语句 公共字符串的toString(){ 返回super.getId()+ “\ t”的+ super.getName()+ “\ t”的+ super.getMoney()+“ 元/天\吨“+”“+”载人“+ this.getPeople()+”\ t“+”载货:“+ this.getGood()+”吨“ } } 包装Icar; //载货接口 public interface zaihuo { public void setGood(int n); public int getGood(); } 包装Icar; //载客接口 public interface zaike { public void setPeople(int n); public int getPeople(); } 包装系统; import java.util.Scanner; 进口zucheSystem.CarStore; 进口zucheSystem.huoche; 进口zucheSystem.keche; 进口zucheSystem.pika; //租车系统类 public class use { //初始化一个车库 CarStore [] car = {new keche(1,“奥迪A4”,500,4), new keche(2,“马自达”,400,4), new鼠(3, “皮卡雪”,450,4,20), 新克彻(4, “金龙”,800,20), 新huoche(5 “松花江”,400,4), 新huoche(6, “依维柯”,1000,20) }; 扫描仪sc =新扫描仪(System.in); public void useSysem(){ System.out.println(“欢迎使用答案租车系统:\ n是否租车1是0否”); int input = sc.nextInt(); if(input == 1){ System.out.println(“您可租车的类型和价目表:”+“\ n序号”+“\ t”+“车名”+“\ t”+“ 租金” +“\ t”+“\ t”+“容量”); for(int i = 0; i = car.length ||Ĵ<0); //获得对应序号的车名,租金,容量 if(car [j] .getClass()== keche.class){outname = car [j] .getName(); 的System.out.println(outname); Rmbsum =汽车[j]的.getMoney()+ Rmbsum; Peoplesum =((keche)car [j])。getPeople()+ Peoplesum; } else if(car [j] .getClass()== huoche.class){outname = car [j] .getName(); 的System.out.println(outname); Rmbsum =汽车[j]的.getMoney()+ Rmbsum; Goodsum = Goodsum +((huoche)汽车[J])getGood(); } else if(car [j] .getClass()== pika.class){outname = car [j] .getName(); 的System.out.println(outname); Rmbsum =汽车[j]的.getMoney()+ Rmbsum; Goodsum = Goodsum +((鼠兔)汽车[J])getGood(); Peoplesum =((pika)car [j])。getPeople()+ Peoplesum; } count ++; } System.out.println(“请输入您要租的天数:”); int day = sc.nextInt(); 的System.out.println( “您的账单是:”); 的System.out.println( “载客数” + Peoplesum + “人” + “\ n” + “载货量” + Goodsum + “吨” + “\ n” + “ 总金额”+ Rmbsum *天+“元”) ; 的System.out.println( “欢迎下次光临!”); } //测试类 public static void main(String [] args){ use u = new use(); u.useSysem(); System.exit(0); } }