package carRent;
import java.util.Scanner;
public class ShopTest {
public static void main(String[] args) {
Car[] cars={new AodiA4("奥迪A4",500,6),
new Pika6("皮卡雪6",450,4,2),
new Yiweike("依维柯",1000,20)};
System.out.println("欢迎使用V租车系统:");
System.out.println("\n您是否需要租车:1是 0否\n");
Scanner scan=new Scanner(System.in);
int input=scan.nextInt();
if(input==1){
double totalcargoCapacity=0;
int totalpeopleCapacity=0;
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t"+"汽车名称\t\t"+"租金\t\t"+"容量");
int num=1;
for(Car currentCar:cars){ //循环遍历foreach
if(currentCar instanceof AodiA4){
System.out.println(
"NO、"+num+"\t"+currentCar.getName()+"\t\t"
+currentCar.getRent()+"元/天"+"\t"
+currentCar.getPeopleCapacity()+"人");
num++;
}
if(currentCar instanceof Yiweike){
System.out.println(
"NO、"+num+"\t"+currentCar.getName()+"\t\t"
+currentCar.getRent()+"元/天"+"\t"
+currentCar.getCargoCapacity()+"吨");
num++;
}
if(currentCar instanceof Pika6){
System.out.println(
"NO、"+num+"\t"+currentCar.getName()+"\t\t"
+currentCar.getRent()+"元/天"
+"\t"+currentCar.getPeopleCapacity()+"人"
+"\t"+currentCar.getCargoCapacity()+"吨");
num++;
}
}
int totalDailyMoney=0;
while(true){ System.out.println("请输入您要租车的数量:");
Scanner wqq=new Scanner(System.in);
int n=wqq.nextInt();
if(n>0&&n<20){
int[] cnum=new int[n];
for(int i=1;i<=n;i++){
System.out.println("请输入第"+i+"辆车的序号:");
Scanner xuhao=new Scanner(System.in);
int number=xuhao.nextInt();
cnum[i-1]=number-1;
totalDailyMoney+=cars[number-1].getRent();
totalpeopleCapacity+=cars[number-1].getPeopleCapacity();
totalcargoCapacity+=cars[number-1].getCargoCapacity();
}
System.out.println("您总共选择车辆:"+n+"辆");
System.out.println("所需金钱:"+totalDailyMoney+"元/天");
System.out.println("总载客量:"+totalpeopleCapacity+"人");
System.out.println("总载货量:"+totalcargoCapacity+"吨");
System.out.println("请输入您需要租车的天数:");
Scanner nt=new Scanner(System.in);
int nday=nt.nextInt();
System.out.println("您的帐单:");
System.out.println("\t总共花费:"+totalDailyMoney*nday+"元");
break;
}
else{
System.out.println("您的输入有误,请重新输入");
}
}
}
else
{
System.out.println("V租车系统期待您的光临");
}
}
}
package carRent;
public class Car {
public String name;
public double rent;
public double peopleCapacity;
public double cargoCapacity;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public double getRent(){
return rent;
}
public void setRent(double rent){
this.rent=rent;
}
public double getPeopleCapacity(){
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity){
this.peopleCapacity=peopleCapacity;
}
public double getCargoCapacity(){
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity){
this.cargoCapacity=cargoCapacity;
}
}
package carRent;
public class AodiA4 extends Car{
private double peopleCapacity;
public AodiA4(String name,double rent,double peopleCapacity){
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
}
public double getPeopleCapacity(){
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity){
this.peopleCapacity=peopleCapacity;
}
}
package carRent;
public class Yiweike extends Car{
private double cargoCapacity;
public Yiweike(String name,double rent,double cargoCapacity){
this.name=name;
this.rent=rent;
this.cargoCapacity=cargoCapacity;
}
public double getCargoCapacity(){
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity){
this.cargoCapacity=cargoCapacity;
}
}
package carRent;
public class Pika6 extends Car{
private double cargoCapacity;
private double peopleCapacity;
public Pika6(String name,double rent,double peopleCapacity,double cargoCapacity){
this.name=name;
this.rent=rent;
this.peopleCapacity=peopleCapacity;
this.cargoCapacity=cargoCapacity;
}
public double getCargoCapacity(){
return cargoCapacity;
}
public void setCargoCapacity(double cargoCapacity){
this.cargoCapacity=cargoCapacity;
}
public double getPeopleCapacity(){
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity){
this.peopleCapacity=peopleCapacity;
}
}
热门评论
有个小建议:AodiA4类中已经继承了Car的所有方法,所以getName(),setName()方法不需要再写(因为你并没有修改这些方法,代码重复了),其中getPeopleCapacity()方法需要保留,原因是你在该类中重新定义了PeopleCapacity变量,该变量的修饰符由public变为private。另外两个类也有这样的问题。希望继续讨论
本文非常简单易懂,适合初学者借鉴