狼女
2016-04-13 14:55
父类
package ddzc.imooc; public abstract class Car { int num; String name; int jg; int zz; int zr; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getJg() { return jg; } public void setJg(int jg) { this.jg = jg; } public int getZz() { return zz; } public void setZz(int zz) { this.zz = zz; } public int getZr() { return zr; } public void setZr(int zr) { this.zr = zr; } abstract void kan(); }
子类
package ddzc.imooc; //小货车 public class Xh extends Car { int num=1; String name="小货车"; int jg=400; int zz=5; @Override void kan() { // TODO Auto-generated method stub this.setNum(num); this.setName(name); this.setJg(jg); this.setZz(zz); System.out.println(getNum()+"\t"+getName()+"\t"+getJg()+"元/天\t"+"\t载重:"+getZz()+"吨"); } }
package ddzc.imooc; //大货车 public class Dh extends Car { int num=2; String name="大货车"; int jg=1000; int zz=20; @Override void kan() { // TODO Auto-generated method stub this.setNum(num); this.setName(name); this.setJg(jg); this.setZz(zz); System.out.println(getNum()+"\t"+getName()+"\t"+getJg()+"元/天"+"\t载重:"+getZz()+"吨"); } }
package ddzc.imooc; //皮卡 public class Pk extends Car { int num=3; String name="皮卡"; int jg=500; int zz=5; int zr=4; @Override void kan() { // TODO Auto-generated method stub this.setNum(num); this.setName(name); this.setJg(jg); this.setZz(zz); this.setZr(zr); System.out.println(getNum()+"\t"+getName()+"\t"+getJg()+"元/天\t"+"\t载客:"+getZr()+"人"+"\t载重:"+getZz()+"吨"); } }
package ddzc.imooc; //小客车 public class Xk extends Car { int num=4; String name="小客车"; int jg=400; int zr=5; @Override void kan() { // TODO Auto-generated method stub this.setNum(num); this.setName(name); this.setJg(jg); this.setZr(zr); System.out.println(getNum()+"\t"+getName()+"\t"+getJg()+"元/天\t"+"\t载客:"+getZr()+"人"); } }
package ddzc.imooc; //大客车 public class Dk extends Car { int num=5; String name="大客车"; int jg=800; int zr=20; @Override void kan() { // TODO Auto-generated method stub this.setNum(num); this.setName(name); this.setJg(jg); this.setZr(zr); System.out.println(getNum()+"\t"+getName()+"\t"+getJg()+"元/天\t"+"\t载客:"+getZr()+"人"); } }
main
package ddzc.imooc; import java.util.Scanner; public class Imain { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("*****欢迎来到租车系统*****"); Scanner input=new Scanner(System.in);//输入调用 System.out.print("想要进入租车系统请按:1;退出请按:0:"); int a=input.nextInt(); if(a==1){ System.out.println("*****欢迎进入租车系统*****"); System.out.println("现在可选择的车型有:"); System.out.println("序号\t车型\t价格\t\t容量"); Car xh=new Xh(); xh.kan(); Car dh=new Dh(); dh.kan(); Car pk=new Pk(); pk.kan(); Car xk=new Xk(); xk.kan(); Car dk=new Dk(); dk.kan(); String[] cheXing={xh.name,dh.name,pk.name,xk.name,dk.name}; int[] jG={xh.jg,dh.jg,pk.jg,xk.jg,dk.jg}; int[] xiaoJi=new int[5]; System.out.print("请输入您需要租用几种的车型:"); int b=input.nextInt(); if(b>5||b<1){ System.out.print("您输入的车型错误请重新输入:"); int i=input.nextInt(); b=i; } for(int c=1;b>0&&b<6;b--){ System.out.print("请输入您需要租第"+c+"种车型:"); int chexing=input.nextInt(); for(;chexing>5||chexing<=0;){ System.out.print("您输入的车型错误请重新输入:"); int i=input.nextInt(); chexing=i; } System.out.print("请输入您需要租的天数:"); int tianshu=input.nextInt(); System.out.print("请输入您需要租车的数量:"); int shuliang=input.nextInt(); System.out.println("你要租的第"+c+"种车型为:"+cheXing[chexing-1]+"\t租车的天数为"+tianshu+"天\t租车的数量为"+shuliang+"辆"); xiaoJi[c-1]=jG[chexing-1]*shuliang*tianshu; System.out.println("您租车第"+c+"种车需要的租金为:"+xiaoJi[c-1]); c++; } int zongji=0; for(int d=0;d>=0&&d<5;d++){ zongji=zongji+xiaoJi[d]; } System.out.println("您租车需要的总租金为:"+zongji); }else System.out.println("再见"); }
尤其是总租金的地方。。
ps:~~就不要吐曹命名了。。。。高中开始就英语没及过格的人。现在连代码都记不住。还在一边翻小笔记一边写。。
请帮忙多指正。
写的不错, 大体的效果差不多出来了, 但是面向对象的操作方式不是这么写的.
首先你要把父类, 也就是抽象的 Car 类抽象封装好, 比如不能 int num;, 而应该 protected int num; protected 表示只有父类内/包内/子类可以访问, 而把共有的 getter/setter 方法写好, 子类里完全不需要管这些属性了, 因为继承了, 是直接会拥有的.
然后创建一个构造方法, 表明你想以怎样的方式创建对象, 比如父类
public Car(int num, String name, int jg, int zz, int zr){ this.num = num; this.name = name; this.jg = jg; this.zz = zz; this.zr = zr; }
子类直接创建构造方法
public Xh(int num, ...){ super(num, ...); // 这里写子类或许拥有的其他属性, 如果没有就忽略, 只用上面那个 super(...); }
总的来说是你对这个类的继承还差一点理解, 不过比我当年要好, 尽量使用标准英文命名, 不知道就用字典查, 不记得就写注释, //, 你懂的
package Test; public abstract class Car { //定义 属性 int CarNum; //车型编号 String CarName; //车型名称 double Priec; //每日租金/价格 int Busload; //载客数量 double Load; //载重量 abstract void CarShow(); } package Test; public class Buggy extends Car { //小货车定义 static final int CarNum=1; static final String CarName="小货车"; static final double Priec=400; static final int Busload=0; static final double Load=5.0; void CarShow(){ System.out.println(CarNum+"\t"+CarName+"\t"+Priec+"元/辆/天"+"\t"+Busload+"人\t"+Load+"吨"); } } package Test; public class Truck extends Car { static final int CarNum=2; static final String CarName="大货车"; static final double Priec=1000; static final int Busload=0; static final double Load=15.0; void CarShow(){ System.out.println(CarNum+"\t"+CarName+"\t"+Priec+"元/辆/天"+"\t"+Busload+"人\t"+Load+"吨"); } } package Test; public class Pickup extends Car { //定义皮卡车 static final int CarNum=3; static final String CarName="皮卡车"; static final double Priec=500; static final int Busload=3; static final double Load=3.0; void CarShow(){ System.out.println(CarNum+"\t"+CarName+"\t"+Priec+"元/辆/天"+"\t"+Busload+"人\t"+Load+"吨"); } } package Test; public class Sedan extends Car { //小客车 static final int CarNum=4; static final String CarName="小客车"; static final double Priec=300; static final int Busload=4; static final double Load=0; void CarShow(){ System.out.println(CarNum+"\t"+CarName+"\t"+Priec+"元/辆/天"+"\t"+Busload+"人\t"+Load+"吨"); } } package Test; public class Bus extends Car { static final int CarNum=5; static final String CarName="大客车"; static final double Priec=1500; static final int Busload=20; static final double Load=0; void CarShow(){ System.out.println(CarNum+"\t"+CarName+"\t"+Priec+"元/辆/天"+"\t"+Busload+"人\t"+Load+"吨"); } } package Test; import java.util.*; public class RentCar { public static void main(String[] args){ RentCar.show01(); Scanner input=new Scanner(System.in);//输入调用 int inputChoose=input.nextInt(); int xuanzhe01=RentCar.Legal(inputChoose,0,1);//选择结果得到 if(xuanzhe01==1){ //进入选车租车系统 RentCar.show02(); System.out.print("请输入您需要租用几种的车型:"); int CarNameChoose=input.nextInt(); int xuanzhe02=RentCar.Legal(CarNameChoose,1,5);//选择车型结束 int[][] sum=new int[xuanzhe02][3]; //开始对几类车型分别做类型及数量,天数输入 for(int j=1, i=0;j<=xuanzhe02;i++,j++){ //i控制数组——j控制次数 //车型确定 do{ System.out.print("请输入你选择的第"+j+"类型车辆的序号(不要重复输入已选型号序号):"); CarNameChoose=input.nextInt(); sum[i][0]=RentCar.Legal(CarNameChoose,1,5); }while(!RentCar.Repeat(CarNameChoose, i, sum));//确保车型不重复 //数量确定 System.out.print("请输入你选择的第"+j+"类型车型的租用数量(不超过100台):"); CarNameChoose=input.nextInt(); sum[i][1]=RentCar.Legal(CarNameChoose,1,101); //天数确定 System.out.print("请输入你选择的第"+j+"类型车型的租用天数(不超过100天):"); CarNameChoose=input.nextInt(); sum[i][2]=RentCar.Legal(CarNameChoose,1,101); } //取得输入完成,开始计算需要的数值 String loadCarName=""; double loadSum=0; String BusCarName=""; int BusSum=0; double Amount=0; for(int i=0;i<xuanzhe02;i++){ switch( sum[i][0]){ //分类计算开始 case Buggy.CarNum: loadCarName=loadCarName+"\t"+Buggy.CarName; loadSum=loadSum+Buggy.Load*sum[i][1]; Amount=Amount+sum[i][1]*sum[i][2]*Buggy.Priec; break; case Truck.CarNum: loadCarName=loadCarName+"\t"+Truck.CarName; loadSum=loadSum+Truck.Load*sum[i][1]; Amount=Amount+sum[i][1]*sum[i][2]*Truck.Priec; break; case Pickup.CarNum: loadCarName=loadCarName+"\t"+Pickup.CarName; loadSum=loadSum+Pickup.Load*sum[i][1]; BusCarName=BusCarName+"\t"+Pickup.CarName; BusSum=BusSum+Pickup.Busload*sum[i][1]; Amount=Amount+sum[i][1]*sum[i][2]*Pickup.Priec; break; case Sedan.CarNum: BusCarName=BusCarName+"\t"+Sedan.CarName; BusSum=BusSum+Sedan.Busload*sum[i][1]; Amount=Amount+sum[i][1]*sum[i][2]*Sedan.Priec; break; case Bus.CarNum: BusCarName=BusCarName+"\t"+Bus.CarName; BusSum=BusSum+Bus.Busload*sum[i][1]; Amount=Amount+sum[i][1]*sum[i][2]*Bus.Priec; break; }; } //输出部分 System.out.println("***可载人车有\n"+BusCarName+"\t共可载:"+BusSum+"人"); System.out.println("***可载货车有\n"+loadCarName+"\t共可载:"+loadSum+"吨"); System.out.println("租车总价格"+Amount+"元"); System.out.println(); } else{ //不进入系统,退出 System.out.println("推出系统"); } } public static void show01(){ //第一次显示 System.out.println("*****欢迎来到租车系统*****"); System.out.print("想要进入租车系统请按:1;退出请按:0:"); } public static void show02(){ //第二次显示 System.out.println("*****欢迎进入租车系统*****"); System.out.println("现在可选择的车型有:"); System.out.println("序号\t车型\t价格\t\t载客数\t载重数"); RentCar.Carinitshow(); //定义初始化部分 } public static void Carinitshow(){ //显示个信息 Car Buggy=new Buggy(); Buggy.CarShow(); Car Truck=new Truck(); Truck.CarShow(); Car Pickup=new Pickup(); Pickup.CarShow(); Car Sedan=new Sedan(); Sedan.CarShow(); Car Bus=new Bus(); Bus.CarShow(); //double[][] CarArrye={{Buggy.CarNum,Truck.CarNum,Pickup.CarNum,Sedan.CarNum,Bus.CarNum},{Buggy.Price,Truck.Price,Pickup.Price,Sedan.Price,Bus.Price}}; } public static int Legal(int intnum,int intmin,int intmax){ //判断输入正确性,输入不对一直输入,知道输入正确为止 while((intnum<intmin)||(intnum>intmax)){ System.out.print("请按提示重新输入"+intmin+"到"+intmax+"数值"); Scanner input=new Scanner(System.in);//输入调用 intnum=input.nextInt(); }; return intnum; } public static boolean Repeat(int CarNameChoose,int i,int sum[][]){ //判断是否重复选车型 boolean CarNumfloat=true; if(i!=0){ for(int n=0;n<i;n++){ if(CarNameChoose==sum[n][0]){ CarNumfloat=false; break; } } } return CarNumfloat; } } //我也刚学习,花了好几个小时做好了,复习了很多知识,哈哈! //希望对您有借鉴,还有就是你写的逻辑我不是很清楚,没看完,汗!
Java入门第二季 升级版
530656 学习 · 6091 问题
相似问题