慕课网--答答打车系统

package com.java.java_test3;

public class Car{
	public String name;   //车的名称
	public float rent;    //每日租金
	public int people;    //可载人数
	public float goods;   //可载货物吨数

	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}

	public float getRent(){
		return rent;
	}
	public void setRent(float rent){
		this.rent = rent;
	}

	public int getPeople(){
		return people;
	}
	public void setPeople(int people){
		this.people = people;
	}

	public float getGoods(){
		return goods;
	}
	public void setGoods(float goods){
		this.goods = goods;
	}
}

package com.java.java_test3;

public class PassangerCar extends Car{
	public PassangerCar(){
		System.out.println("Please enter the PassangerCar parameter");
	}

	public PassangerCar(String name,float rent,int people){
		this.name = name;
		this.rent = rent;
		this.people = people;
	}
}

package com.java.java_test3;

public class Pickup extends Car{
	public Pickup(){
		System.out.println("Please enter the Pickup parameter");
	}

	public Pickup(String name,float rent,int people,float goods){
		this.name = name;
		this.rent = rent;
		this.people = people;
		this.goods = goods;
	}
}

package com.java.java_test3;

public class Truck extends Car{
	public Truck(){
		System.out.println("Please enter the Truck parameter");
	}

	public Truck(String name,float rent,float goods){
		this.name = name;
		this.rent = rent;
		this.goods = goods;
	}
}

package com.java.java_test3;
import java.util.Scanner;

public class Initial{
	int total_People;   //总人数
	float total_goods;  //可载货物总吨数
	float total_price;  //租金总价格
	int[] count = new int[6]; //用来统计每种车分别租没租,以及租了几辆
	int days;           //租借天数
	//用多态的方式将父类引用指向子类实例,由于子类的属性父类都有,故父类都可以访问到
	//用这种方式比较方便遍历
	Car[] car = {new PassangerCar("aodiA4",500,4),
				 new PassangerCar("mazida6",400,4),
				 new Pickup("pikaxue",450,4,2),
				 new PassangerCar("jinlong",800,20),
				 new Truck("songhj",400,4),
				 new Truck("yiweike",1000,20)};
	
	public static void main(String[] args){
		System.out.println("welcom to the car rent system:");
		System.out.println("do you want to rent car:1YES 0NO");
		Scanner scanner = new Scanner(System.in);
		if(1 == scanner.nextInt()){
			System.out.println("the cars that you can choose and their price:");
			System.out.println("number"+"\t"+"name"+"\t\t"+"price"+"\t\t"+"capacity");
			Initial initial = new Initial();
			initial.printPrice();    //打印出车辆的信息
			initial.getInfo();       //指示用户选择车辆及天数
			initial.printBill();     //打印统计出的总人数,总吨数,价格等

		}
		else{
			System.out.println("bye bye");
		}
		scanner.close();

	}

	public void printPrice(){
		for(int i=0,j=1; i<6; i++,j++){
			//根据引用属于的类来分别打印出车辆信息
			if(car[i] instanceof PassangerCar){
				System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getPeople()+"People");
			}
			else if(car[i] instanceof Pickup){
				System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getPeople()+"People "+car[i].getGoods()+"T");
			}
			else if(car[i] instanceof Truck){
				System.out.println(j+"."+"\t"+car[i].getName()+"\t\t"+car[i].getRent()+"/DAY"+"\t"+car[i].getGoods()+"T");
			}
		}
	}

	public void getInfo(){
		System.out.println("please enter the total amount that you want:");
		Scanner scanner = new Scanner(System.in);
		int amount = scanner.nextInt();
		for(int i=1; i<=amount; i++){
			System.out.println("please enter the "+i+" car:");
			int number = scanner.nextInt();
			if(number>0 && number<=6){
				count[number-1]++;
			}
			else{
				System.out.println("number is invalid");
			}
		}
		System.out.println("please enter the days:");
		days = scanner.nextInt();
		scanner.close();
	}

	public void printBill(){
		System.out.println("your bill:");
		System.out.println("***the cars that can manned people are:");
		for(int i=0; i<6; i++){
			if(count[i]>0){
				if(car[i].getPeople()>0){
					System.out.print(car[i].getName()+"  ");
					total_People += count[i] * car[i].getPeople();
				}
			}	
		}
		System.out.println("total_People:"+total_People);
		System.out.println("***the cars that can carry cargo are:");
		for(int i=0; i<6; i++){
			if(count[i]>0){
				if(car[i].getGoods()>0){
					System.out.print(car[i].getName()+"  ");
					total_goods += count[i] * car[i].getGoods();
				}
				total_price += count[i] * car[i].getRent();
			}
		}
		total_price *= days;
		System.out.println("total_goods:"+total_goods);
		System.out.println("***the total_price is:"+total_price);
	}
	
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值