用java写一个车辆管理系统
要求
如有优化之处请指教,非常感谢
根据要求要先初始化数组
//初始化数组
static Scanner input =new Scanner(System.in);//键盘输入
static String[] carNames=new String[10];//命名两个数组存放车辆数据
static String[] carPrices=new String[10];
static int carNumber=3;
- 车辆管理系统:
//车辆管理系统
public static void carSystem(){
System.out.println("1: 车辆展示 2: 添加车辆 3: 删除车辆 4: 修改车辆 5: 返回");
System.out.println("请输入选项编号");
int num=input.nextInt();
switch(num){
case 1:
//车辆展示
showCar();
//调用自己
carSystem();
break;
case 2:
//添加车辆
addCar();
//调用自己
carSystem();
case 3:
//删除车辆
removeCar();
carSystem();
break;
case 4:
//修改车辆
revise();
carSystem();
break;
case 5:
//返回
loginSystem();
carSystem();
break;
}
}
2.车辆展示
首先创建一个方法index方法。像数组中添加3辆车
public static void index(){
carNames[0] = "劳斯莱斯幻影";
carPrices[0] = "1800W";
carNames[1] = "帕拉梅拉";
carPrices[1] = "800W";
carNames[2] = "迈凯伦";
carPrices[2] = "2800W";
- 添加车辆
*//添加车辆方法*
public static void addCar(){
System.out.println("请输入要添加车辆品牌:");
String carName = input.next();//键盘接受信息
System.out.println("请输入车辆标价");
String carPrice = input.next();
*//将车辆信息存入到数组中*
carNames[carNumber]=carName;//添加进数组中
carPrices[carNumber]=carPrice;
System.out.println("添加成功");
carNumber++;
}
- 删除车辆
具体思路就是新创建两个数组接受信息,如果要删除此车辆,则不进行任何操作,把
不需要删除的车辆重新赋值给原数组即可
public static void removeCar(){
System.out.println("请输入删除的车辆名称");
String carName1=input.next();
String[] a=new String[10];//新命名两个数组
String[] b=new String[10];
boolean flag=false;
for(int i=0;i<carNames.length-1;i++){
if(carName1.equals(carNames[i])){
carNames[i]=null;
carPrices[i]=null;
flag=true;
}else{
a[i]=carNames[i];
b[i]=carPrices[i];
}
}
if(!flag){
System.out.println("未找到该车辆");
revise();
}
carNames=a;
carPrices=b;
System.out.println("删除成功");
}
- 修改车辆
与删除方法的思路差不多,只是把修改后的值赋值给原数组即可
public static void revise(){
String[] a=new String[10];
String[] b=new String[10];
for(int k=0;k<carNames.length;k++){
System.out.println("请输入修改车辆的名称");
String carname=input.next();
System.out.println("请输入修改后的名称");
String carname1=input.next();
System.out.println("请输入修改后的价格");
String carprice1=input.next();
for(int i=0;i<carNames.length-1;i++){
if(carname.equals(carNames[i])){
carNames[i]=carname1;
carPrices[i]=carprice1;
}
a[i]=carNames[i];
b[i]=carPrices[i];
}
carNames=a;
carPrices=b;
System.out.println("修改成功");
System.out.println("是否继续修改:Y,N");
String sf=input.next();
if(sf.equals("Y")){
}else if(sf.equals("N")){
break;
}
}
}