为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第二季大作业

标签:
Java

Cars类

public abstract class Car {
	private int sNum;
	private String name;
	private double price;
	private int peopleNum;
	private double goodsNum;

	public abstract void printInfo();

	public Car(int sNum, String name, double price, int peopleNum, double goodsNum) {
		this.sNum = sNum;
		this.name = name;
		this.price = price;
		this.peopleNum = peopleNum;
		this.goodsNum = goodsNum;
	}

	public int getsNum() {
		return sNum;
	}

	public void setsNum(int sNum) {
		this.sNum = sNum;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public void setGoodsNum(double goodsNum) {
		this.goodsNum = goodsNum;
	}

	public int getPeopleNum() {
		return peopleNum;
	}

	public void setPeopleNum(int peopleNum) {
		this.peopleNum = peopleNum;
	}

	public double getGoodsNum() {
		return goodsNum;
	}

	public double getPrice() {
		return price;
	}

	public String getName() {
		return name;
	}

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

PassengerCar 客车类

public class PassengerCar extends Car {

	public PassengerCar(int sNum, String name, double price, int peopleNum, double goodsNum) {
		super(sNum, name, price, peopleNum, 0);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void printInfo() {
		// TODO Auto-generated method stub
		System.out.println("载人:" + this.getPeopleNum() + "人");
	}
}

Trucks 货车类

public class Trucks extends Car {

	public Trucks(int sNum, String name, double price, int peopleNum, double goodsNum) {
		super(sNum, name, price, peopleNum, goodsNum);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void printInfo() {
		// TODO Auto-generated method stub
		System.out.println("载货:" + this.getGoodsNum() + "吨");
	}
}

PickUp 皮卡类

public class PickUp extends Car {

	public PickUp(int sNum, String name, double price, int peopleNum, double goodsNum) {
		super(sNum, name, price, peopleNum, goodsNum);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void printInfo() {
		// TODO Auto-generated method stub
		System.out.println("载人:" + this.getPeopleNum() + "人" + " 载货:" + this.getGoodsNum() + "吨");
	}
}

Test类

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
	static ArrayList<Car> aList = new ArrayList();
	static int carNum;
	static int rentDay;
	static int allPeople;
	static double allGoods;
	static double allPrice = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		aList.add(new PassengerCar(1, "奥迪A6", 500, 4, 0));
		aList.add(new PassengerCar(2, "马自达6", 400, 4, 0));
		aList.add(new PickUp(3, "皮卡雪6", 450, 4, 2));
		aList.add(new PassengerCar(4, "金龙", 800, 20, 0));
		aList.add(new Trucks(5, "松花江", 400, 0, 4));
		aList.add(new Trucks(6, "依维柯", 1000, 0, 20));

		Scanner sc = new Scanner(System.in);
		welcome(sc);
		introduceCar();
		System.out.println("请输入您要租汽车的数量:");
		carNum = sc.nextInt();

		for (int i = 1; i <= carNum; i++) {
			System.out.print("请输入第" + i + "辆车的序号:");
			int temp;
			while (true) {
				sc = new Scanner(System.in);
				try {
					temp = sc.nextInt();
					if (temp <= aList.size() && temp > 0) {
						getNumAndPrice(temp);
						break;
					} else {
						System.out.println("输入错误,请输入1到" + aList.size() + "的整数!");
						continue;
					}
				} catch (InputMismatchException e) {
					System.out.println("输入错误,请输入1到" + aList.size() + "的整数!");
					continue;
				}
			}
		}

		System.out.println("请输入租车天数:");
		rentDay = sc.nextInt();
		allPrice = allPrice * rentDay;

		printBills();

	}

	static void printBills() {
		System.out.println("您的账单:\n***可载人的车有:");
		System.out.print("  ");
		for (Car c : aList) {
			if ((c instanceof PickUp) || (c instanceof PassengerCar))
				System.out.print(c.getName() + " ");
		}
		System.out.println("共载人:" + allPeople + "人");
		System.out.println("***载货的车有:");
		System.out.print("  ");
		for (Car c : aList) {
			if ((c instanceof PickUp) || (c instanceof Trucks))
				System.out.print(c.getName() + " ");
		}
		System.out.println("共载货:" + allGoods + "吨");
		System.out.println("***租车总价格:" + allPrice + "元");
	}

	static void welcome(Scanner sc) {
		System.out.println("欢迎使用哒哒租车系统:");
		int userInput;
		for (;;) {
			System.out.println("您是否要租车:1是 0否");
			try {
				userInput = Integer.parseInt(sc.nextLine().trim());
				if (userInput == 0) {
					System.out.println("欢迎下次光临^_^");
					return;
				}
				break;
			} catch (NumberFormatException e) {
				System.out.println("输入错误!");
				continue;
			}
		}
	}

	static void introduceCar() {
		System.out.println("您可租车的类型及其价目表:\n序号 \t汽车名称\t租金\t\t容量");
		for (Car c : aList) {
			System.out.print(c.getsNum() + ".\t" + c.getName() + "\t" + c.getPrice() + "元/天\t");
			c.printInfo();
		}
	}

	static void getNumAndPrice(int carIndex) {
		for (Car c : aList) {
			if (carIndex == c.getsNum()) {
				allPeople += c.getPeopleNum();
				allGoods += c.getGoodsNum();
				allPrice += c.getPrice();
				break;
			}
		}
	}

}

执行结果如下:
欢迎使用哒哒租车系统:
您是否要租车:1是 0否
a
输入错误!
您是否要租车:1是 0否
1
您可租车的类型及其价目表:
序号 汽车名称 租金 容量

  1.  奥迪A6		500.0元/天	载人:4人
    
  2.  马自达6		400.0元/天	载人:4人
    
  3.  皮卡雪6		450.0元/天	载人:4人 载货:2.0吨
    
  4.  金龙		800.0元/天	载人:20人
    
  5.  松花江		400.0元/天	载货:4.0吨
    
  6.  依维柯		1000.0元/天	载货:20.0吨
    

请输入您要租汽车的数量:
4
请输入第1辆车的序号:a
输入错误,请输入1到6的整数!
1
请输入第2辆车的序号:7
输入错误,请输入1到6的整数!
2
请输入第3辆车的序号:3
请输入第4辆车的序号:4
请输入租车天数:
3
您的账单:
***可载人的车有:
奥迪A6 马自达6 皮卡雪6 金龙 共载人:32人
***载货的车有:
皮卡雪6 松花江 依维柯 共载货:2.0吨
***租车总价格:6450.0元

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消