慕沐5238563
2020-03-17 10:14
package test; //载人汽车 public class AManned extends Car{ private int personCout;//载人量 public AManned(String carName,double rent ,int personCout){ super(carName,rent); this.personCout = personCout; } public int getPersonCout(){ return this.personCout; } }
package test; //人+货 public class AMulti extends Car { private int personCout;//载人量 private int productCount;//载货量 public AMulti(String carName, double rent, int personCout ,int productCount) { super(carName, rent); this.personCout = personCout; this.productCount = productCount; } public int getPersonCout() { return personCout; } public int getProductCount() { return productCount; } }
package test; //货车 public class ATruck extends Car { private int productCount;//载货量 public ATruck(String carName, double rent ,int productCount) { super(carName, rent); this.productCount = productCount; } public int getProductCount() { return productCount; } }
package test; public class Calculation { public double CalculationRent(double[] rents ,int day){ double sum = 0; for (int i = 0; i < rents.length; i++) { sum = sum + rents[i] * day; } return sum; } public void Statistics(Car[] cars,int day){ double[] buyRents = new double[cars.length]; double buyProductCounts = 0; int buyPersonCout = 0; String buyCargos = ""; String buyManneds = ""; for (int i = 0; i < cars.length; i++) { Car car = cars[i]; buyRents[i] = car.getRent(); System.out.println(cars); if (car instanceof ATruck){ ATruck aTruck = (ATruck)car; buyProductCounts = buyProductCounts + aTruck.getProductCount(); buyCargos = buyCargos + "\t" + aTruck.getCarName(); }else if(car instanceof AMulti){ AMulti aMulti = (AMulti)car; buyProductCounts = buyProductCounts + aMulti.getProductCount(); buyCargos = buyCargos + "\t" + aMulti.getCarName(); buyManneds = buyManneds + "\t" + aMulti.getCarName(); buyPersonCout = buyPersonCout + aMulti.getPersonCout(); }else if (car instanceof AManned){ AManned aManned = (AManned)car; buyManneds = buyManneds + "\t" + aManned.getCarName(); buyPersonCout = buyPersonCout + aManned.getPersonCout(); } } System.out.println("您的账单:"); System.out.println("***可载人的车有:"); System.out.println(buyManneds + "\t" + buyPersonCout + "人"); System.out.println("***载货的车有:"); System.out.println(buyCargos + "\t" + buyProductCounts + "吨"); /* **价格计算 */ double rents = CalculationRent(buyRents,day); System.out.print("***租车总价格:"); System.out.println(rents); } }
package test; public class Car { private String carName; private double rent; public String getCarName() { return carName; } public double getRent() { return rent; } public Car(String carName , double rent){ this.carName = carName; this.rent = rent; } }
package test; import java.util.Scanner; public class Initail{ public static void main(String[] args) { System.out.println("欢迎使用答答租车系统"); System.out.println("您是否要租车:1 是 0 否"); Scanner input = new Scanner(System.in); int str = input.nextInt(); if (str == 0){ System.out.println("谢谢您的使用,再见!"); return; } System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t\t\t容量"); Car car1 = new AManned("奥迪A4",500,4); Car car2 = new AManned("马自达",400,4); Car car3 = new AMulti("皮卡雪",450,4,2); Car car4 = new AManned("金龙o",800,20); Car car5 = new ATruck("松花江",400,4); Car car6 = new ATruck("依维柯",1000,20); Car[] cars ={car1,car2,car3,car4,car5,car6}; for (int i = 1 ; i < cars.length + 1 ; i++){ Car car = cars[i-1]; if (car instanceof AManned){//载人 System.out.println(i + "、\t" + car.getCarName() + "\t" + car.getRent() + "元/天" + "\t" + "载人:"+ ((AManned) car).getPersonCout() +"人"); }else if (car instanceof AMulti){//人+货 System.out.println(i + "、\t" + car.getCarName() + "\t" + car.getRent() + "元/天" + "\t" + "载人:" + ((AMulti) car).getPersonCout() +"人" + "载货:" +((AMulti) car).getProductCount() + "吨"); }else if (car instanceof ATruck){//货 System.out.println(i + "、\t" + car.getCarName() + "\t" + car.getRent() + "元/天" + "\t" + "载货:" +((ATruck) car).getProductCount() + "吨"); } } System.out.println("请输入您要租汽车的数量:"); int count = new Scanner(System.in).nextInt(); int[] counts = new int[count]; Car[] buyCars = new Car[count] ; for (int i = 0 ; i < count ; i ++){ System.out.println("请输入第" + (i+1) + "辆车的序号"); int carCount = new Scanner(System.in).nextInt(); counts[i] = carCount - 1; buyCars[i] = cars[carCount - 1]; } System.out.println("请输入租车天数:"); int day = new Scanner(System.in).nextInt(); Calculation calculation = new Calculation(); calculation.Statistics(buyCars,day); } }
繁琐难懂 我觉得这个人的代码可以https://www.imooc.com/article/301274
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题