有谁有6-1的代码 ‘顺便说下解题思路’ 多谢
import java.util.Scanner; //导入Scanner类
class Car //定义一个父类
{
public String name; //定义名字
public float rent; //租金
public int number; //载人量
public float weight; //载货量
public float getRent()
{
return rent;
}
public String getName()
{
return name;
}
public float getWeight()
{
return weight;
}
public int getNumber()
{
return number;
}
}
interface IBus //定义一个接口,实现了该接口的类,表示该类具有载人的功能
{
}
interface ITruck //定义一个接口,实现了该接口的类,表示该类具有载货的功能
{
}
class Bus extends Car implements IBus //创建一个子类,该类继承了父类Car,实现了 IBus 接口
{
public Bus(String name, int number, float rent) //创建构造方法
{
this.name = name;
this.number = number;
this.rent = rent;
}
}
class Truck extends Car implements ITruck //创建一个子类,该类继承了父类Car,实现了 ITruck 接口
{
public Truck(String name, float weight, float rent) //创建构造方法
{
this.name = name;
this.weight = weight;
this.rent = rent;
}
}
class BusTruck extends Car implements IBus, ITruck //创建一个子类,该类继承了父类Car,实现了 ITruck 接口和 IBus 接口
{
public BusTruck(String name, int number, float weight, float rent) //创建构造方法
{
this.name = name;
this.number = number;
this.weight = weight;
this.rent = rent;
}
}
class CarRental
{
private String bus = ""; //定义一个字符串,用来存放所有载人汽车的名字
private String truck = ""; //定义一个字符串,用来存放所有载货汽车的名字
private int busNum; //定义一个整形变量,用来存放所有载人汽车的总载人量
private float truckWeight; //定义一个浮点型变量,用来存放把有载货汽车的总载货量
private float sumRent; //定义一个浮点型变量,用来存放所有汽车的一天租金的总和,注意是一天
private int date; //定义租用的天数
private Scanner sc = null; //定义一个Scanner工具
private Car[] c = null; //定义一个父类类型的数组,用来接受mian()方法中传递过来的参数
public CarRental(Car[] c) //通过构造函数 使本类中的 c 跟mian()方法中的 c 相关连
{
this.c = c;
}
public void launch() //该方法由mian()方法调用
{
show();
menu();
count();
output();
}
public void show() //显示的信息
{
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:" + "1 是" + ", 0 否");
sc = new Scanner(System.in); //接收从键盘输入的信息
while (true)
{
int s = sc.nextInt(); //将信息保存到变量s中
if (s!=1 && s!=0) //如果输入有误,则重新输入
{
System.out.println("您的输入有误!请重新输入");
continue;
}
else if (s == 0) //当输入0时,程序终止
System.exit(-1);
else //输入合法,则跳出循环,执行接下来的语句
break;
}
}
public void menu() //输出菜单信息
{
System.out.println("\n" + "您可租车的类型及价目表:");
System.out.println("***************************");
System.out.println("序号 " + "汽车名称 " + "租金 " + "容量");
System.out.println("1. " + "奥迪A4 " + "500元/天 " + "载人:4人");
System.out.println("2. " + "马自达6 " + "400元/天 " + "载人:4人");
System.out.println("3. " + "皮卡雪6 " + "450元/天 " + "载人:4人" + ", 载货:2吨");
System.out.println("4. " + "金龙 " + "800元/天 " + "载人:20人");
System.out.println("5. " + "松花江 " + "400元/天 " + "载货:4吨");
System.out.println("6. " + "依维柯 " + "1000元/天 " + "载货:20吨");
System.out.println("***************************" + "\n");
}
public void count() //统计
{
System.out.println("请输入您要租汽车的数量:");
sc = new Scanner(System.in);
int count = sc.nextInt(); //count 表示用户要租的汽车的数量
for(int i=1; i<=count; i++) //使用循环进行统计
{
System.out.println("请输入第" + i + "辆车的序号:");
int num = sc.nextInt();
if (num<1 || num>6) //当输入有误时,程序终止
{
System.out.println("您的输入有误!程序终止!");
System.exit(-1);
}
else if (3 == num) //当用户输入3时,对应上面菜单上的 "皮卡雪6",即能载人,也能载货
{
bus = bus + " " + c[num-1].getName(); //将3对应的车的名字存放到bus里面,因为c是一个数组引用,用其下标表示要用 c[num-1]
busNum = busNum + c[num-1].getNumber();
sumRent = sumRent + c[num-1].getRent();
truck = truck + " " + c[num-1].getName();
truckWeight = truckWeight + c[num-1].getWeight();
}
else if (num < 5) //当num的值为1,2,4的时候
{
bus = bus + " " + c[num-1].getName();
busNum = busNum + c[num-1].getNumber();
sumRent = sumRent + c[num-1].getRent();
}
else //当num的值为5,6的时候
{
sumRent = sumRent + c[num-1].getRent();
truck = truck + " " + c[num-1].getName();
truckWeight = truckWeight + c[num-1].getWeight();
}
}
System.out.println("请输入租车天数:");
date = sc.nextInt();
if (date < 1) //当输入有误时,程序终止
{
System.out.println("您的输入有误!程序终止!");
System.exit(-1);
}
sumRent = sumRent * date; //计算出所有车的租金总和
}
public void output() //输出
{
System.out.println("***************************");
System.out.println("您的账单:");
System.out.println("***可载人的车有:" + "\n" + bus + " 共载人:" + busNum + "人");
System.out.println("***可载货的车有:" + "\n" + truck + " 共载货:" + truckWeight + "吨");
System.out.println("***租车总价格:" + sumRent + "元");
}
}
public class TestCarRental //启动类
{
public static void main(String[] args)
{
Car[] c = {new Bus("奥迪A4", 4, 500),
new Bus("马自达6", 4, 400),
new BusTruck("皮卡雪6", 4, 2, 450),
new Bus("金龙", 20, 800),
new Truck("松花江", 4, 400),
new Truck("依维柯", 20, 1000)
};
new CarRental(c).launch();
}
}
package zuche.com;
public class Pickup extends Car{
int thingcount;
int peoplecount;
public Pickup(String name,String rent,int thingcount,int peoplecount){
this.name=name;
this.rent=rent;
this.peoplecount=peoplecount;
this.thingcount=thingcount;
}
public void setThingcount(int thingcount) {
this.thingcount = thingcount;
}
public int getThingcount() {
return thingcount;
}
public int getPeoplecount() {
return peoplecount;
}
public void setPeoplecount(int peoplecount) {
this.peoplecount = peoplecount;
}
}
package zuche.com;
public class Car {
String name;
String rent;
int peoplecount;
int thingcount;
public String getName() {
return name;
}
public String getRent() {
return rent;
}
public int getPeoplecount() {
return peoplecount;
}
public int getThingcount() {
return thingcount;
}
}
package zuche.com;
public class PassengerCar extends Car{
int peoplecount;
public PassengerCar(String name,String rent,int peoplecount){
this.name=name;
this.peoplecount=peoplecount;
this.rent=rent;
}
public int getPeoplecount() {
return peoplecount;
}
public void setPeoplecount(int peoplecount) {
this.peoplecount = peoplecount;
}
}
package zuche.com;
public class Trunk extends Car{
int thingcount;
public Trunk(String name,String rent,int thingcount){
this.thingcount=thingcount;
this.name=name;
this.rent=rent;
}
public int getThingcount() {
return thingcount;
}
public void setThingcount(int thingcount) {
this.thingcount = thingcount;
}
}
package zuche.com;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Car[] carsForRent={new PassengerCar("劳斯莱斯","500元/天",4)
,new PassengerCar("拉沙马蒂","600元、天",2)
,new Pickup("皮卡","300元/天",2,4)
,new Trunk("松花江","800元/天",5)
,new Trunk("依维柯","1000元/天",20)
};
System.out.println("欢迎使用嗒嗒租车行:");
System.out.println("你是否需要租车:1是 0否");
Scanner scanner=new Scanner(System.in);
String input=scanner.next();
if(input.equals("1")){
System.out.println("你可租用的车的类型及价格等");
System.out.println("序号\t 汽车名称\t租金\t载客量\t载重量\t");
int i=1;
for(Car c:carsForRent){
if(c instanceof PassengerCar){
System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
+"\t"+c.getPeoplecount()+"人");
}else if(c instanceof Trunk){
System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
+"\t"+"\t"+c.getThingcount()+"吨");
}else{
System.out.println(""+i+"\t"+c.getName()+"\t"+c.getRent()
+"\t"+c.getPeoplecount()+"人"+"\t"+c.getThingcount()+"吨");
}
i++;
}
}
System.out.println("请输入你要租车的序号");
@SuppressWarnings("unused")
int e=scanner.nextInt();
}
}