package com.demo;
import java.util.Scanner;
/*
- 运行基本跟视频中的显示效果一样,评论处有全部代码截图 看的清晰一点 这个自动排版有些乱 写的有些乱 有时间再改下
*/
public class Demo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Car [] conut = {new Bus("奥迪A4", 500, 4),new Bus("马自达", 400, 4),new Bus("金龙", 800, 20),
new Pick("皮卡雪", 450, 4, 2),new Truck("松花江", 400, 4),new Truck("依维柯", 1000, 20)};
int sum=0; //载人数
String name1="";
String name2="";
int mnoey=0;
int day=0;
int again=0;
System.out.println("欢迎使用达达租车系统");
System.out.println("租车请按 1,退出请按 2");
int go = new Scanner(System.in).nextInt();
if (go == 1) {
System.out.println("下面是可租车的详细信息:");
System.out.println("序号\t" + "车名\t"+ "元/天\t"+ "载人数\t"+ "载货数\t" );
for (int i = 0; i < conut.length; i++) {
if(conut[i] instanceof Bus){
System.out.println(i+1 + ".\t" + conut[i].getName() + "\t" + conut[i].getMoney() + "\t" + conut[i].getSum());
}else if (conut[i] instanceof Truck){
System.out.println(i+1 + ".\t" + conut[i].getName() + "\t" + conut[i].getMoney() + "\t\t" + conut[i].getAgain());
}else {
System.out.println(i+1 + ".\t" + conut[i].getName() + "\t" + conut[i].getMoney() + "\t" + conut[i].getSum() + "\t" + conut[i].getAgain());
}
}
System.out.println("请输入你要租车的数量:");
int go1 = new Scanner(System.in).nextInt();
// int x = 1; //更新删掉变量x,将i初始值改为1即可
for (int i = 1; i < go1; i++) {
System.out.println("请输入你要租的第" + i +"辆车的序号:");
int go2 = new Scanner(System.in).nextInt();
// x++; //删除
if(go2>6 go2<=0){
System.out.println("您输入有误!请重新选择!");
System.out.println();
// --x;/删除
--i;
}else if (conut[go2-1] instanceof Bus){
name1 = conut[go2-1].getName()+"\t"+name1;
sum += conut[go2-1].getSum();
mnoey += conut[go2-1].getMoney();
}else if (conut[go2-1] instanceof Truck) {
name2 = conut[go2-1].getName()+"\t"+name2;
again += conut[go2-1].getAgain();
mnoey += conut[go2-1].getMoney();
}else if(conut[go2-1] instanceof Pick){
name1 = conut[go2-1].getName()+"\t"+name1;
name2 = conut[go2-1].getName()+"\t"+name2;
again += conut[go2-1].getAgain();
mnoey += conut[go2-1].getMoney();
sum = conut[go2-1].getSum() + sum;
}
}
System.out.println("请输入租车天数:");
day = new Scanner(System.in).nextInt();
if (day < 0) {
System.out.println("请重新输入大于0的数:");
day = new Scanner(System.in).nextInt();
}else {
mnoey = day*mnoey;
}
System.out.println("您的账单:");
if("".equals(name1)!= true){
System.out.println("***可载人的车有:");
System.out.print(name1);
System.out.println("载人数:" + sum);
}
if("".equals(name2)!= true){
System.out.println("***可载货的车有:");
System.out.print(name2);
System.out.println("载货数:" + again);
}
System.out.println("共计金额:" + mnoey);
main(args); //测试用 不退出程序
System.out.println();
System.out.println();
}else if (go == 2) {
System.out.println("已退出,期待您的下次使用!");
System.exit(0);
}else {
System.out.println("您输入有误!请重新选择!");
System.out.println();
System.out.println();
main(args); //重新执行主函数
}
}
}
//父类
package com.demo;
public class Car {
private String name;//名称
private int money; //价格
private int sum; //载人数
private int again; //载货量
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 getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getAgain() {
return again;
}
public void setAgain(int again) {
this.again = again;
}
}
//子类
package com.demo;
public class Bus extends Car {
public Bus(String newName,int newMoney,int newSum) {
this.setName(newName);
this.setMoney(newMoney);
this.setSum(newSum);
}
}
//子类
package com.demo;
public class Truck extends Car {
public Truck(String newName,int newMoney,int newAgain) {
this.setName(newName);
this.setMoney(newMoney);
this.setAgain(newAgain);
}
}
package com.demo;
public class Pick extends Car{
public Pick(String newName,int newMoney,int newSum,int newAgain){
this.setName(newName);
this.setMoney(newMoney);
this.setSum(newSum);
this.setAgain(newAgain);
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章