MrOrangeee
2019-05-24 19:22
感觉写了好多代码,很多地方都可以优化,但是我刚学,不知道怎么修改, 希望各位可以提供点思路,感谢!以下是代码:
//父类: package Dada; public abstract class Vehicle { public int price; public int people; public String name; public double load; abstract void info(); }
子类:奥迪
package Dada; public class Audi extends Vehicle { public final int price = 500; public final int people = 4; public final int id =1; public final String name = "奥迪A4"; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载人:"+people+"人"); } }
子类:马自达
package Dada; public class Mazda extends Vehicle { public final int price = 400; public final int people = 4; public final int id =2; public final String name = "马自达6"; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载人:"+people+"人"); } }
子类:皮卡雪6
package Dada; public class Pika extends Vehicle { public final int price = 450; public final int people = 4; public final int id =3; public final String name = "皮卡雪6"; public final double load = 2; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载人:"+people+"人"+"载货"+load+"吨"); } }
子类:金龙
package Dada; public class Jinlong extends Vehicle { public final int price = 800; public final int people = 20; public final int id =4; public final String name = "金龙"; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载人:"+people+"人"); } }
子类:松花江
package Dada; public class Songhuajiang extends Vehicle { public final int price = 400; public final double load = 4; public final int id =5; public final String name = "松花江"; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载货:"+load+"吨"); } }
子类:依维柯
package Dada; public class Yiweike extends Vehicle { public final int price = 1000; public final double load = 20; public final int id =6; public final String name = "依维柯"; @Override void info() { System.out.println(id+"."+" "+name+" "+price+"/天"+" "+"载货:"+load+"吨"); } }
测试类:
package Dada; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class test { public static void main(String[] args){ List<Integer> data; List<String> passengerVehicleName=new ArrayList<>(); List<String> goodsVehicleName=new ArrayList<>(); System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车? 1 是, 0 否"); Scanner select = new Scanner(System.in); int answer = select.nextInt(); int dayNum; int money =0; int people=0; double goods =0; switch (answer){ case 1: info(); break; case 0: System.out.println("不租车你点进来干嘛,别闹!"); break; default: System.out.println("你好调皮,再见。"); } data = rentNumber(); dayNum = data.get(data.size()-1); data.remove(data.size()-1); for (int i: data){ switch (i){ case 1: Audi audi = new Audi(); money= money+audi.price*dayNum; people= people+audi.people; passengerVehicleName.add(audi.name); break; case 2: Mazda mazda = new Mazda(); money = money+mazda.price*dayNum; people = people+mazda.people; passengerVehicleName.add(mazda.name); break; case 3: Pika piKa = new Pika(); money=money+piKa.price*dayNum; people=people+piKa.people; goods=goods+piKa.load; passengerVehicleName.add(piKa.name); goodsVehicleName.add(piKa.name); break; case 4: Jinlong jinLong = new Jinlong(); money = money + jinLong.price*dayNum; people = people+jinLong.people; passengerVehicleName.add(jinLong.name); break; case 5: Songhuajiang songHuaJiang = new Songhuajiang(); money = money+songHuaJiang.price*dayNum; goods = goods+songHuaJiang.load; goodsVehicleName.add(songHuaJiang.name); break; case 6: Yiweike yiWeiKe = new Yiweike(); money = money + yiWeiKe.price*dayNum; goods = goods + yiWeiKe.load; goodsVehicleName.add(yiWeiKe.name); break; } } System.out.println("您的账单:"); if (people!=0){ System.out.println("***可载人的车有:"); for (String i:passengerVehicleName ) { System.out.print(i+" "); } System.out.println("共载人:"+people+"人"); System.out.println(); } if (goods!=0){ System.out.println("***可载货的车有:"); for (String i:goodsVehicleName ) { System.out.print(i+" "); } System.out.println("共载货:"+goods+"吨"); System.out.println(); } System.out.println("***租车总价格为"+money+"元"); } static void info(){ System.out.println("您可租车的类型及价目表如下:"); System.out.println("序号 汽车名称 租金 容量"); Vehicle Audi = new Audi(); Audi.info(); Vehicle Mazda = new Mazda(); Mazda.info(); Vehicle Pika = new Pika(); Pika.info(); Vehicle Jinlong = new Jinlong(); Jinlong.info(); Vehicle Songhuajiang = new Songhuajiang(); Songhuajiang.info(); Vehicle Yiweike = new Yiweike(); Yiweike.info(); } public static List rentNumber(){ List<Integer> selection = new ArrayList<>(); System.out.println("请输入您要租车的数量:"); Scanner rn = new Scanner(System.in); int number = rn.nextInt(); for (int i = 1; i<= number; i++){ System.out.println("请输入第"+i+"辆车的序号:"); Scanner num = new Scanner(System.in); int a = num.nextInt(); selection.add(a); } System.out.println("请输入租车天数:"); Scanner data = new Scanner(System.in); int dayNum = data.nextInt(); selection.add(dayNum); return selection; } }
不需要写这么多的子类, 可以用不同的参数实例化父类来获得不同的汽车的实例.
Java入门第二季 升级版
530649 学习 · 6091 问题
相似问题