Java
近期学习Java关于类的知识中,做了一个练手的小题目,达达租车系统,现将个人代码分享出来,如有错误或可进一步精简的地方,欢迎大家指出并交流。
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc =new Scanner(System.in); 6 int exitflag =1; //用于终止循环 7 do { 8 System.out.println("欢迎使用达达租车系统:"); 9 System.out.println("请问是否要租车:1、是 2、否"); 10 String choose =sc.next(); 11 switch (choose){ 12 case "1": 13 Function.AllStep(); 14 exitflag =0; 15 break; 16 case "2": 17 System.out.println("用户不需要租车,已成功退出!"); 18 exitflag =0; 19 break; 20 default: 21 System.out.println("输入有误,请重新输入!"); 22 break; 23 } 24 }while (exitflag == 1); 25 26 27 } 28 }
1 import java.util.Scanner; 2 3 public class Function { 4 public static Scanner sc = new Scanner(System.in); 5 public static void AllStep(){ 6 //生成车辆数组 7 AllCar[] cars = {new Truck(1,"松花江",400,4,2), 8 new Truck(2,"依维柯",1000,20,2), 9 new PickUpTruck(3,"皮卡雪6",450,4,2,1), 10 new Car(4,"奥迪A4",500,4,0), 11 new Car(5,"马自达6",400,4,0), 12 new Car(6,"金龙 ",800,50,0)}; 13 System.out.printf("%-4s %-10s %-10s %-10s ","序号","汽车名称","租金","容量"); 14 System.out.println(); 15 // 显示车辆所有的信息 16 for (int i = 0; i <cars.length ; i++) { 17 cars[i].print(); 18 System.out.println(); 19 } 20 // 进入租车流程 21 System.out.println("请输入您要租的汽车数量:"); 22 int carAmount =sc.nextInt(); 23 int[] carNum = new int[carAmount]; //用于存储租出的车辆序号 24 for (int i = 1; i <=carAmount ; i++) { 25 System.out.println("请输入第"+i+"辆车序号:"); 26 carNum[i-1] = sc.nextInt(); 27 } 28 System.out.println("请输入租车天数:"); 29 int day = sc.nextInt(); 30 System.out.println("您的账单:"); 31 System.out.println("***可载人的车有:"); 32 int peopeleNum =list(carNum,cars,0); 33 System.out.println("共可以载:"+peopeleNum+"人"); 34 System.out.println("***可载货的车有:"); 35 int loadNum =list(carNum,cars,2); 36 System.out.println("共可以载:"+loadNum+"吨"); 37 // 计算总的租车费用 38 int sumMoney=0; 39 for (int i = 0; i <carNum.length ; i++) { 40 sumMoney +=cars[carNum[i]-1].carPrice*day; 41 } 42 System.out.println("***租车总价格:"+sumMoney+"元"); 43 } 44 public static int list(int[] carNum, AllCar[] cars,int state){ 45 int sumNum=0; 46 int carFlag=1; //用于判断车名是否重复 47 for (int i = 0; i <carNum.length ; i++) { 48 carFlag=1; 49 if( cars[carNum[i]-1].type==state || cars[carNum[i]-1].type==1){ 50 for (int j = 0; j <i ; j++) { //用于去除重复的车名 51 if(carNum[j]==carNum[i]){ 52 carFlag=0; 53 break; 54 } 55 } 56 if (carFlag ==1){ 57 System.out.print(cars[carNum[i]-1].carName+" "); 58 } 59 if ( state==0) { 60 sumNum +=cars[carNum[i]-1].loadMan; 61 62 } 63 else { 64 sumNum +=cars[carNum[i]-1].loadThing; 65 66 } 67 68 } 69 } 70 return sumNum; 71 } 72 73 }
1 public class AllCar { //所有车的父类 2 public int carNum; 3 public String carName; 4 public int carPrice; 5 public int loadMan; 6 public int loadThing; 7 public int type; // 0 载人 ,1 载人 载货 ,2 载货 8 public AllCar( int carNum,String carName, int carPrice,int loadMan ,int loadThing, int type){ 9 this.carNum = carNum; 10 this.carName = carName; 11 this.carPrice = carPrice; 12 this.loadMan = loadMan; 13 this.loadThing = loadThing; 14 this.type =type; 15 } 16 public void print(){ //用于输出车辆信息 17 if(type ==0){ 18 System.out.printf("%-4s %-10s %-10s %-10s",this.carNum+".",this.carName,this.carPrice+"元/天","载人:"+this.loadMan+"人"); 19 } 20 else { 21 System.out.printf("%-4s %-10s %-10s %-10s", this.carNum+".",this.carName,this.carPrice+"元/天","载货:"+this.loadThing+"吨"); 22 } 23 24 } 25 }
public class Car extends AllCar { public Car(int carNum, String carName, int carPrice, int loadMan,int type) { super(carNum, carName, carPrice, loadMan,0, type); } } public class PickUpTruck extends AllCar { public PickUpTruck(int carNum, String carName, int carPrice, int loadMan, int loadThing, int type) { super(carNum, carName, carPrice, loadMan, loadThing, type); } @Override public void print() { System.out.printf("%-4s %-10s %-10s %s %s ",this.carNum+".",this.carName,this.carPrice+"元/天","载人:"+this.loadMan+"人","载货:"+this.loadThing+"吨"); } } public class Truck extends AllCar { public Truck(int carNum, String carName, int carPrice, int loadThing, int type) { super(carNum, carName, carPrice,0,loadThing, type); } }
下图是结果
版权声明:本文为zhou7507原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。