我是时生
2019-06-08 10:14
//父类 package com.dadazuche; public abstract class GongJu { //载货量 public int zaihuo; //载人数 public int zairen; //租金 public int zujin; //车名 public String name; //车序号 public int id; //封装 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public abstract void chexing(); public int getZaihuo() { return zaihuo; } public void setZaihuo(int zaihuo) { this.zaihuo = zaihuo; } public int getZairen() { return zairen; } public void setZairen(int zairen) { this.zairen = zairen; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } } //子类奥迪a4 public class Aodi extends GongJu { //给每个属性赋值 public int zairen=4; public int zujin=500; public String name="奥迪A4"; public int id=1; @Override //封装,下面子类都一样 public void chexing() { System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getZairen() { return zairen; } public void setZairen(int zairen) { this.zairen = zairen; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } } //子类马自达6 public class Mazida extends GongJu { public int zairen=4; public int zujin=400; public String name="马自达6"; public int id=2; public void chexing() { System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { t his.id = id; } public int getZairen() { return zairen; } public void setZairen(int zairen) { this.zairen = zairen; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } } //子类皮卡雪6 public class Pikaxue extends GongJu { public int zaihuo=2; public int zairen=4; public int zujin=450; public String name="皮卡雪6"; public int id=3; public void chexing() { System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen+" 载货:"+zaihuo); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getZaihuo() { return zaihuo; } public void setZaihuo(int zaihuo) { this.zaihuo = zaihuo; } public int getZairen() { return zairen; } public void setZairen(int zairen) { this.zairen = zairen; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } } //子类金龙 public class Jinlong extends GongJu { public int zairen=20; public int zujin=800; public String name="金龙"; public int id=4; public void chexing() { System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen); } public int getZairen() { return zairen; } public void setZairen(int zairen) { this.zairen = zairen; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } //子类松花江 public class Songhuajiang extends GongJu { public int zaihuo=4; public String name ="松花江"; public int zujin=400; public int id=5; public void chexing() { System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载货:"+zaihuo); } public int getZaihuo() { return zaihuo; } public void setZaihuo(int zaihuo) { this.zaihuo = zaihuo; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } //子类依维柯 public class Yiweike extends GongJu { public int zaihuo=20; public int zujin=1000; public String name="依维柯"; public int id=6; @Override public void chexing() { // TODO 自动生成的方法存根 System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载货:"+zaihuo); } public int getZaihuo() { return zaihuo; } public void setZaihuo(int zaihuo) { this.zaihuo = zaihuo; } public int getZujin() { return zujin; } public void setZujin(int zujin) { this.zujin = zujin; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } //测试类 package com.dadazuche; import java.util.Scanner; public class Test extends GongJu { public static void main(String[] args) { GongJu p1=new Aodi(); GongJu p2=new Mazida(); GongJu p3=new Pikaxue(); GongJu p4=new Jinlong(); GongJu p5=new Songhuajiang(); GongJu p6=new Yiweike(); String[]car= {"奥迪A4","马自达6","皮卡雪6","金龙"}; String[]bus= {"松花江","依维柯"}; Scanner sc=new Scanner(System.in); System.out.println("欢迎使用嗒嗒租车系统:"); System.out.println("您是否要租车:1是 0否"); int a=sc.nextInt(); if(a==1) { System.out.println("您可租车的类型及其价目表:"); System.out.println("***序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量***"); //获取子类数据 p1.chexing(); p2.chexing(); p3.chexing(); p4.chexing(); p5.chexing(); p6.chexing(); //获取每辆车的数量 String x = " "; String x1 = " "; String x2 = " "; String x3 = " "; String x4 = " "; String x5 = " "; System.out.println("请输入租车天数:"); int b=sc.nextInt(); System.out.println("请输入您要租汽车的数量:"); int c=sc.nextInt(); //获取每个车的数量 System.out.println("请输入第1辆车的数量"); int h=sc.nextInt(); //如果为0则为空 if(h==0) { System.out.print(" "); //如果不为零则输出车名。下同 }else { x=car[0]; } //第二辆车 System.out.println("请输入第2辆车的数量"); int i=sc.nextInt(); if(i==0) { System.out.print(" "); }else { x1=car[1]; } //第三辆车 System.out.println("请输入第3辆车的数量"); int j=sc.nextInt(); if(j==0) { System.out.print(" "); }else { x2=car[2]; } //第四辆车 System.out.println("请输入第4辆车的数量"); int k=sc.nextInt(); if(k==0) { System.out.print(" "); }else { x3=car[3]; } //第五辆车 System.out.println("请输入第5辆车的数量"); int l=sc.nextInt(); if(l==0) { System.out.print(" "); }else { x4=bus[0]; } //第六辆车 System.out.println("请输入第6辆车的数量"); int m=sc.nextInt(); if(m==0) { System.out.print(" "); }else { x5=bus[1]; } //计算总座位 int zuowei=p1.getZairen()*h+p2.getZairen()*i+p3.getZairen()*j+p4.getZairen()*k; //计算吨位 int dunwei=p3.getZaihuo()*j+p5.getZaihuo()*l+p6.getZaihuo()*m; //计算租金b为天数 int zujin=(p1.getZujin()*h+p2.getZujin()*i+p3.getZujin()*j+p4.getZujin()*k+p5.getZujin()*l+p6.getZujin()*m)*b; System.out.println("您的账单:"); System.out.println("***可载人的车有:"); System.out.println(x+"\t"+x1+"\t"+x2+"\t"+x3+"\t"+"共载人:"+zuowei+"人"); System.out.println("***可载货的车有:"); System.out.println(x2+"\t"+x4+"\t"+x5+"\t"+"共载货:"+dunwei+"吨"); System.out.println("***租车总价格:"+zujin+"元"); }else { System.out.println("谢谢光临"); } } public void chexing() { } }
兄弟 你那个几辆车完全可以使用for循环来写啊 我没有使用封装、动态什么的 然后直接写的代码 有些地方你可一参考一下
package com.noahcode; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int money; static int people; static int thing; static String zPeople=""; static String zThing=""; public static void Menu() { int isZu = 0; do { System.out.println("您是否要租车:1是 0否"); isZu = sc.nextInt(); if (isZu == 1) { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名称 租金 容量"); System.out.println("1. 奥迪A4 500元/天 载人:4人"); System.out.println("2. 马自达6 400元/天 载人:4人"); System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货2吨"); System.out.println("4. 金龙 800元/天 载人:20人"); System.out.println("5. 松花江 400元/天 载货:4吨"); System.out.println("6. 依维柯 1000元/天 载货:20吨"); System.out.println("请输入您要组汽车的数量:"); } else if (isZu == 0) { System.exit(0); } else { System.out.println("您输入的信息有问题,请重新输入!"); } } while (isZu!=0&&isZu!=1); } public static void sumCarMoney(int n) { switch (n) { case 1: money += 500; zPeople += "奥迪A4 "; people += 4; break; case 2: money += 400; zPeople += "马自达6 "; people += 4; break; case 3: money += 450; zPeople += "皮卡雪6 "; zThing += "皮卡雪6 "; people += 4; thing += 2; break; case 4: money += 800; zPeople += "金龙 "; people += 20; break; case 5: money += 400; zThing += "松花江 "; thing += 4; break; case 6: money += 1000; zThing += "依维柯 "; thing += 20; break; } } public static void main(String[] args) { System.out.println("欢迎使用答答租车系统"); Menu(); int count = sc.nextInt(); for (int i = 1; i <= count; i++) { System.out.println("请输入第" + i + "辆车的序号"); int xh = sc.nextInt(); sumCarMoney(xh); } System.out.println("请输入租车天数:"); int day = sc.nextInt(); money *= day; System.out.println("您的账单:"); System.out.println("***可载人的车有:\n" + zPeople + "共载人:" + people+"人"); System.out.println("***载货的车有:\n" + zThing + "共载货:" + thing+"吨"); System.out.println("***租车总价格:" + money + "元"); } }
封装使用的是pravite,父类变量基本租金,车名,载货载人可以用接口,
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题