package zuche;
import java.util.Scanner;
public class Initail {/**
* 做一个答答租车系统,显示车的种类供用户选择
* 车的种类分为三种,载人、载货、即可以载人又可以载货
* 显示出用户选择的车名,同时显示出用户选择的车名
* 最后再计算总金额
* @param args
*/
static Scanner in=new Scanner(System.in);
static int sl;//保存用户租车的
static int n;
static int count=0;//统计输入序号的次数
static int day;//获取天数
static int allMoney=0;//统计总金额
static int allPeople=0;//统计总人数
static double allGoods=0;//统计货物重量
public static void main(String[] args) {
System.out.println("请问是否进入答答租车系统:1是 2否");
n=in.nextInt();//获取用户输入的值来确认是否进入系统
if(n==1){
Car cars[]={new People(1,"奥迪A4",500,4),
/*多态引用多个类(父类Car对上各子类) 用数组遍历多个对象,
People(int no,String name, int money,int people)=new People(1,"奥迪A4",500,4),
People子类定义了自己的属性,然后继承了Car。
Car中的属性可以被引用,
在People子类中是直接输出的。遍历时定下的值在People子类中输出了*/
new People(2,"马自达6",400,4),
new Pika(3,"皮卡雪6",450,4,2),
new People(4,"金龙",800,20),
new Goods(5,"松花江",400,4),
new Goods(6,"依维柯",1000,20)};
System.out.println("请输入租车的数量:");
//_________________________________________分割线______________________________________________
sl=in.nextInt();//获取用户租车的数量
int[] xh=new int[sl];//通过数组保存输入的序号
while(count<sl){
System.out.println("请输入第"+(count+1)+"辆车的序号:");
int x=in.nextInt();//输入的序号
xh[count]=x;//将序号存储在数组中
count++;//通过累加的形式,将序号一一存储在数组中
continue;
}
System.out.println("请输入租车天数:");
//_________________________________________分割线______________________________________________
day=in.nextInt();
//此部分计算
//计算人数
for(int i=0;i<cars.length;i++){//循环cars数组。
for(int j=0;j<sl;j++){//循环用户想租车的序号
if(cars[i].no==xh[j]&&cars[i].people>0&&cars[i].good>0){//此处判定pika类型的车辆,计算结果
System.out.print(cars[i].name+" ");//获取pika车辆的名称
allGoods=allGoods+cars[i].good;//获取pika类型车辆的货物重量,进行计算
allPeople=allPeople+cars[i].people;//获取pika类型的人数,进行计算
allMoney=allMoney+cars[i].money;//获取pika类型的租金,进行计算
}
}
}
System.out.println("***载人的车:");
//_________________________________________分割线_______________________________________________
for(int i=0;i<cars.length;i++){
for(int j=0;j<sl;j++){
if(cars[i].no==xh[j]&&cars[i].people>0){//此处判定载人类型的车辆,计算结果载人数
System.out.print(cars[i].name+" ");
allPeople=allPeople+cars[i].people;
allMoney=allMoney+cars[i].money;
}
}
}
System.out.println("总人数:"+allPeople);
System.out.println("***可载物的车有:");
//_________________________________________分割线_______________________________________________
for(int i=0;i<cars.length;i++){//此处判定载货类型的车辆,计算结果载物重量
for(int j=0;j<sl;j++){
if(cars[i].no==xh[j]&&cars[i].good>0){
System.out.println("***载货的车:");
System.out.print(cars[i].name+" ");
allGoods=allGoods+cars[i].good;
allMoney=allMoney+cars[i].money;
}
}
}
System.out.println("总载货量:"+allGoods+"吨");
System.out.println("总金额"+allMoney*day);
}else{
System.out.println("即将退出答答租车系统");
}
}
}
//父类___________________________________________________
package zuche;
public class Car {
int no;
String name;
int money;
int people;
double good;
public Car(int no,String name,int money,double good){
this.no=no;
this.name=name;
this.money=money;
this.good=good;
}
public Car(int no,String name,int money,int people){
this.no=no;
this.name=name;
this.money=money;
this.people=people;
}
public Car(int no,String name,int money,int people,double good){
this.no=no;
this.name=name;
this.money=money;
this.people=people;
this.good=good;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public double getGood() {
return good;
}
public void setGood(double good) {
this.good = good;
}
}
//子类_____________________________________________
package zuche;
public class Goods extends Car {
double good;
public Goods(int no,String name, int money,double good) {
super(no,name, money,good);
// TODO Auto-generated constructor stub
System.out.println(no+". "+name+" "+money+"元/天 "+" "+good+"吨");
}
}
//__________________________________________
package zuche;
public class People extends Car {
public People(int no,String name, int money,int people) {
super(no,name, money,people);
// TODO Auto-generated constructor stub
System.out.println(no+". "+name+" "+money+"元/天 "+people+"人");
}
// TODO 自动生成的构造函数存根
}
//________________________________________________
package zuche;
public class Pika extends Car {
public Pika(int no,String name, int money,int people,double good) {
super(no,name, money,people,good);
// TODO Auto-generated constructor stub
System.out.println(no+". "+name+" "+money+"元/天 "+people+" "+good+"吨");
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦