花了一晚上,和大家比较后发现我的方法太复杂,,分别构造了一堆子类。
不过效果基本上达到了要求。
抽象类Cars
package com.projectrent;
public abstract class Cars {
protected int rent=0;
protected String name="";
protected int num=0;
protected boolean forMan=false;
protected int numOfPassengers;
protected boolean forGoods=false;
protected int tonsOfGoods;
protected int totalRent;
protected int totalNumOfPassengers;
protected int totalTonsOfGoods;
public abstract void setRent();
public abstract void setName();
public abstract void setNum();
public abstract void display();
public abstract void dispARentInfo(int[] nums,int[] days);
}
Car1
package com.projectrent;
public class Car1 extends Cars implements Carforman{
public Car1(){
setRent();
setName();
setNum();
numOfPassengers();
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=500;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="奥迪A4";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=1;
}
@Override
public void numOfPassengers() {
// TODO Auto-generated method stub
forMan=true;
numOfPassengers=4;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[0]!=0){
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[0]*days[0];
totalNumOfPassengers=nums[0]*numOfPassengers;
totalTonsOfGoods=nums[0]*tonsOfGoods;
System.out.print("\t"+nums[0]+"\t"+days[0]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Car2
package com.projectrent;
public class Car2 extends Cars implements Carforman {
public Car2(){
setRent();
setName();
setNum();
numOfPassengers();
}
@Override
public void numOfPassengers() {
// TODO Auto-generated method stub
forMan=true;
numOfPassengers=4;
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=400;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="马自达6";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=2;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println();
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[1]!=0){
System.out.println();
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[1]*days[1];
totalNumOfPassengers=nums[1]*numOfPassengers;
totalTonsOfGoods=nums[1]*tonsOfGoods;
System.out.print("\t"+nums[1]+"\t"+days[1]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Car3
package com.projectrent;
public class Car3 extends Cars implements Carforman, Carforgoods {
public Car3(){
setRent();
setName();
setNum();
numOfPassengers();
tonsOfGoods();
}
@Override
public void tonsOfGoods() {
// TODO Auto-generated method stub
forGoods=true;
tonsOfGoods=2;
}
@Override
public void numOfPassengers() {
// TODO Auto-generated method stub
forMan=true;
numOfPassengers=4;
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=450;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="皮卡雪6";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=3;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println();
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[2]!=0){
System.out.println();
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[2]*days[2];
totalNumOfPassengers=nums[2]*numOfPassengers;
totalTonsOfGoods=nums[2]*tonsOfGoods;
System.out.print("\t"+nums[2]+"\t"+days[2]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Car4
package com.projectrent;
public class Car4 extends Cars implements Carforman {
public Car4(){
setRent();
setName();
setNum();
numOfPassengers();
}
@Override
public void numOfPassengers() {
// TODO Auto-generated method stub
forMan=true;
numOfPassengers=20;
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=800;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="金龙";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=4;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println();
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[3]!=0){
System.out.println();
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[3]*days[3];
totalNumOfPassengers=nums[3]*numOfPassengers;
totalTonsOfGoods=nums[3]*tonsOfGoods;
System.out.print("\t"+nums[3]+"\t"+days[3]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Car5
package com.projectrent;
public class Car5 extends Cars implements Carforgoods {
public Car5(){
setRent();
setName();
setNum();
tonsOfGoods();
}
@Override
public void tonsOfGoods() {
// TODO Auto-generated method stub
forGoods=true;
tonsOfGoods=4;
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=400;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="松花江";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=5;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println();
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[4]!=0){
System.out.println();
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[4]*days[4];
totalNumOfPassengers=nums[4]*numOfPassengers;
totalTonsOfGoods=nums[4]*tonsOfGoods;
System.out.print("\t"+nums[4]+"\t"+days[4]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Car6
package com.projectrent;
public class Car6 extends Cars implements Carforgoods {
public Car6(){
setRent();
setName();
setNum();
tonsOfGoods();
}
@Override
public void tonsOfGoods() {
// TODO Auto-generated method stub
forGoods=true;
tonsOfGoods=20;
}
@Override
public void setRent() {
// TODO Auto-generated method stub
rent=1000;
}
@Override
public void setName() {
// TODO Auto-generated method stub
name="依维柯";
}
@Override
public void setNum() {
// TODO Auto-generated method stub
num=6;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println();
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
System.out.print("载人:"+numOfPassengers+"人");
if(forGoods){
System.out.print(",载货:"+tonsOfGoods+"吨");
}
}else if(forGoods){
System.out.print("载货:"+tonsOfGoods+"吨");
}
}
@Override
public void dispARentInfo(int[] nums, int[] days) {
// TODO Auto-generated method stub
if(nums[5]!=0){
System.out.println();
int m=0;
System.out.print(num+"\t"+name+"\t"+rent+"元/天"+"\t");
if(forMan){
m=1;
if(forGoods){
m=3;
}
}else if(forGoods){
m=2;
}
switch(m){
case 1:System.out.print("载人:"+numOfPassengers+"人\t");break;
case 2:System.out.print("载货:"+tonsOfGoods+"吨\t");break;
case 3:System.out.print("载人:"+numOfPassengers+"人");System.out.print(",载货:"+tonsOfGoods+"吨");break;
default:System.out.println("system error");
}
totalRent=rent*nums[5]*days[5];
totalNumOfPassengers=nums[5]*numOfPassengers;
totalTonsOfGoods=nums[5]*tonsOfGoods;
System.out.print("\t"+nums[5]+"\t"+days[5]+"\t"+totalRent+"元\t");
switch(m){
case 1:System.out.print("载人:"+totalNumOfPassengers+"人");break;
case 2:System.out.print("载货:"+totalTonsOfGoods+"吨");break;
case 3:System.out.print("载人:"+totalNumOfPassengers+"人");System.out.print(",载货:"+totalTonsOfGoods+"吨");break;
default:System.out.println("system error");
}
}
}
}
Initial
package com.projectrent;
import java.util.Scanner;
public class Initial {
static Scanner input;
static int[] nums=new int[6];
static int[] days=new int[6];
static boolean endOfInput=false;
static int allNumOfCars;
static int allRent;
static int allPassengers;
static int allGoods;
static boolean exit=false;
//display cars information
public static void display(){
Cars car1=new Car1();
Cars car2=new Car2();
Cars car3=new Car3();
Cars car4=new Car4();
Cars car5=new Car5();
Cars car6=new Car6();
System.out.println("下面是可租车辆类型与租金信息:");
System.out.println("序号\t名称\t租金\t载人/载货");
car1.display();
car2.display();
car3.display();
car4.display();
car5.display();
car6.display();
}
//获得租车信息
public static void getRentInfo(){
int rentOrNot,No;
do{
System.out.print("所租车序号:");
No=input.nextInt();
System.out.print("该类车数量:");
nums[No-1]=input.nextInt();
System.out.print("共租借天数:");
days[No-1]=input.nextInt();
System.out.println("是否继续输入:(1是 0否)");
do{
rentOrNot=input.nextInt();
if(rentOrNot==1){
break;
}
else if(rentOrNot==0){
endOfInput=true;
System.out.println("正在输出您的租车信息,请稍候...");
break;
}
else System.out.println("请重新输入:");
}while(true);
}while(!endOfInput);
}
//输出租车信息
public static void dispRentInfo(){
Cars car1=new Car1();
Cars car2=new Car2();
Cars car3=new Car3();
Cars car4=new Car4();
Cars car5=new Car5();
Cars car6=new Car6();
System.out.println("您的租车信息为:");
System.out.println("序号\t名称\t租金\t载人/载货\t\t数量\t天数\t总金额\t总载重(人/货)");
car1.dispARentInfo(nums, days);
car2.dispARentInfo(nums, days);
car3.dispARentInfo(nums, days);
car4.dispARentInfo(nums, days);
car5.dispARentInfo(nums, days);
car6.dispARentInfo(nums, days);
System.out.println();
for(int a=0;a<6;a++){
allNumOfCars+=nums[a];
}
allRent=car1.totalRent+car2.totalRent+car3.totalRent+car4.totalRent+car5.totalRent+car6.totalRent;
allPassengers=car1.totalNumOfPassengers+car2.totalNumOfPassengers+car3.totalNumOfPassengers+car4.totalNumOfPassengers+car5.totalNumOfPassengers+car6.totalNumOfPassengers;
allGoods=car1.totalTonsOfGoods+car2.totalTonsOfGoods+car3.totalTonsOfGoods+car4.totalTonsOfGoods+car5.totalTonsOfGoods+car6.totalTonsOfGoods;
System.out.print("合计\t--\t--\t--\t\t"+allNumOfCars+"\t-\t"+allRent+"元\t");
if(allPassengers!=0){
System.out.print("载人:"+allPassengers+"人");
if(allGoods!=0){
System.out.print(",载货:"+allGoods+"吨");
}
}else if(allGoods!=0){
System.out.print("载货:"+allGoods+"吨");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//verification & display
input=new Scanner(System.in);
int rentOrNot,No;
System.out.println("*******欢迎使用答答租车系统*******\n您是否要租车?(1是 0否)");
do{
rentOrNot=input.nextInt();
if(rentOrNot==1){
display();break;
}
else if(rentOrNot==0){
System.out.println("成功退出。欢迎再次使用。");
exit=true;break;
}
else System.out.println("请重新输入:");
}while(!exit);
if(!exit){
System.out.println();
System.out.println("*****请输入租车信息:*****");
//get info of rent cars, include Nos, nums of cars, nums of days
getRentInfo();
dispRentInfo();
//confirmation module
System.out.println();
System.out.println("*****以上为您的租车信息,请确认:*****");
do{
System.out.println("1确认 0取消");
rentOrNot=input.nextInt();
if(rentOrNot==1){//rent info confirmation
System.out.println("正在处理,请稍候...");break;
}
else if(rentOrNot==0){//cancel confirmation
System.out.println("确定要取消吗?(1确定 0返回确认信息)");
do{
rentOrNot=input.nextInt();
if(rentOrNot==1){//really cancel
System.out.println("成功取消。欢迎再次使用。");
exit=true;break;
}else if(rentOrNot==0){//get back
break;
}else System.out.println("请重新输入:");
}while(!exit);
}
else System.out.println("请重新输入:");
}while(!exit);
if(!exit){
//payment module
do{
System.out.println("您需要支付的金额为:"+allRent+"元\n请选择支付方式:");
System.out.println("1支付成功 0支付失败 7直接退出");
rentOrNot=input.nextInt();
if(rentOrNot==1){
System.out.println("您已成功支付,可打印以上信息作为凭证。\n******谢谢使用,再见******");break;
}
else if(rentOrNot==0){
System.out.println("支付失败,请重试。");
}
else if(rentOrNot==7){
System.out.println("成功退出。欢迎再次使用。");
exit=true;break;
}
else System.out.println("请重新输入:");
}while(!exit);
}
}
}
}
点击查看更多内容
7人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦