A_jing
2015-04-17 14:44
package Car; /* 1.展示所有可租车辆 * 2.选择车型,租车量 * 3.展示租车清单,包含:总金额,总载货量及其车型,总载人量及其车型 */ public abstract class Car { private String name; private int carId; private int qian; public Car(String name, int carId, int qian) { super(); this.name = name; this.carId = carId; this.qian = qian; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCarId() { return carId; } public void setCarId(int carId) { this.carId = carId; } public int getQian() { return qian; } public void setQian(int qian) { this.qian = qian; } public abstract void show(); }
package Car; //载人的车 public class ZaiRen extends Car{ private int renshu; public ZaiRen(String name, int carId, int qian,int renshu) { super(name, carId, qian); this.renshu = renshu; } public int getRenshu() { return renshu; } public void setRenshu(int renshu) { this.renshu = renshu; } @Override public void show() { System.out.println(this.getCarId()+" "+this.getName()+" "+this.getQian()+" "+this.renshu+"(人)"); } }
package Car; //载货车 public class ZaiHuo extends Car { private double zaihuoliang; public ZaiHuo(String name, int carId, int qian,double zaihuoliang) { super(name, carId, qian); this.zaihuoliang = zaihuoliang; } public double getZaihuoliang() { return zaihuoliang; } public void setZaihuoliang(double zaihuoliang) { this.zaihuoliang = zaihuoliang; } @Override public void show() { System.out.println(this.getCarId()+" "+this.getName()+" "+this.getQian()+" "+this.zaihuoliang+"(吨)"); } }
package Car; //皮卡 public class PiKa extends Car{ private double zaihuo; private int renshu2; public PiKa(String name, int carId, int qian, int renshu2,double zaihuo) { super(name, carId, qian); this.setRenshu2(renshu2); this.setZaihuo(zaihuo); } public int getRenshu2() { return renshu2; } public void setRenshu2(int renshu2) { this.renshu2 = renshu2; } public void setZaihuo(double zaihuo) { this.zaihuo = zaihuo; } public double getZaihuo() { return zaihuo; } @Override public void show() { System.out.println(this.getCarId()+" "+this.getName()+" "+this.getQian()+" "+this.renshu2+" (人) "+this.zaihuo+" (吨)"); } }
package Car; import java.util.Scanner; public class TestCar1 { public static void main(String[] args) { ZaiRen z1 = new ZaiRen("奥迪", 1, 200, 5); ZaiRen z2 = new ZaiRen("大众", 2, 100, 5); ZaiHuo z3 = new ZaiHuo("菱悦", 3, 1000, 3); ZaiHuo z4 = new ZaiHuo("卡车", 4, 6000, 2); PiKa z5 = new PiKa("解放", 5, 250, 5, 1); Car [] carr = {z1,z2,z3,z4,z5}; System.out.println("欢迎来到答答租车"); System.out.println("你要租车吗?1.租车,2.不租车"); Scanner sc = new Scanner(System.in); int str = sc.nextInt(); if (str == 1) { System.out.println("您可租的车辆信息如下"); System.out.println("序号\t车名\t租金(元/天)\t载人(人)\t载货(吨)"); for (int i = 0; i < carr.length; i++) { carr[i].show(); } System.out.println("请输入你要租的车的序号:"); int rentNum = new Scanner(System.in).nextInt(); // for (int j = 0; j < carr.length; j++) { switch (rentNum) { case 1: System.out.println("请问你要租几天"); int day = new Scanner(System.in).nextInt(); System.out.println("请付款:"+day*carr[0].getQian()); break; case 2: System.out.println("请问你要租几天"); int day1 = new Scanner(System.in).nextInt(); System.out.println("请付款:"+day1*carr[1].getQian()); break; case 3: System.out.println("请问你要租几天"); int day2 = new Scanner(System.in).nextInt(); System.out.println("请付款:"+day2*carr[2].getQian()); break; case 4: System.out.println("请问你要租几天"); int day3 = new Scanner(System.in).nextInt(); System.out.println("请付款:"+day3*carr[3].getQian()); break; case 5: System.out.println("请问你要租几天"); int day4 = new Scanner(System.in).nextInt(); System.out.println("请付款:"+day4*carr[4].getQian()); break; default: break; } // } }else{ System.out.println("欢迎下次光临"); } } }
一:car是父类所以不用调用·super;二:构造方法已经把方法传进去了所以set方法多余了,三:父类有钱数方法运用多态性,没必要用swit方法,向上转型;四;主方法用循环更好。只要不按1,会让顾客继续选择,可以把总钱数加起来;
你这作业是要交给谁~~~要不要这么萌啊~~~
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题