JavaEE实战班第十天
今天將昨天分成的任務(wù)達成了三個,還剩最后一個。其中遇到的問題封裝不會,生成隨機數(shù)不會。
代碼如下:
import java.util.Random;
import java.util.Scanner;
public class work01 {
?? ?public static Scanner input = new Scanner(System.in);
?? ?public static String[] numberArr=new String[100];
?? ?public static String[] companyArr=new String[100];
?? ?public static int[] codeArr=new int[100];
?? ?public static int index = 0;//有多少個快遞
?? ?public static Random random = new Random();
?? ?public static void main(String[] args) {
?? ??? ?while(true) {
?? ??? ?startMenu();
?? ??? ?}
?? ?}
?? ?public static void startMenu() {
?? ??? ?// 展示菜單
?? ??? ??? ??? ?System.out.println("=======歡迎使用新職課快遞柜======");
?? ??? ??? ??? ?System.out.print("請輸入您的身份:1快遞員 2用戶");
?? ??? ??? ??? ?do {
?? ??? ??? ??? ?int id = input.nextInt();
?? ??? ??? ??? ?
?? ??? ??? ??? ?if(id==1) {//快遞員
?? ??? ??? ??? ??? ?deliverymanMenu();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}else if(id==2) {//用戶
?? ??? ??? ??? ??? ?userMenu();
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?System.out.println("選擇有誤,請重新輸入");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?}while(true);
?? ?}
?? ?//--------------------------------------------------------------------------
?? ?public static void userMenu() {
?? ??? ?System.out.print("請輸入取件碼:");
?? ??? ?int code = input.nextInt();
?? ?}
?? ?//--------------------------------------------------------------------------
?? ?
?? ?//--------------------------------------------------------------------------
?? ?public static void deliverymanMenu() {
?? ??? ?System.out.print("請選擇操作:1存快遞 2刪除快遞 3修改快遞信息 4查看所有快遞");
?? ??? ?int id = input.nextInt();
?? ??? ?if(id==1){
?? ??? ??? ?saveExpress();
?? ??? ?}else if(id==2) {
?? ??? ??? ?System.out.println("請輸入要刪除的快遞單號");
?? ??? ??? ?String number=input.next();
?? ??? ??? ?int delIndex=findByNumber(number);
?? ??? ??? ?if(delIndex==-1) {
?? ??? ??? ??? ?System.out.println("未找到快遞");
?? ??? ??? ?}else {
?? ??? ??? ??? ?//刪除
?? ??? ??? ??? ?if(delIndex!=numberArr.length-1) {
?? ??? ??? ??? ?
?? ??? ??? ??? ??? ?for(int i = delIndex;i<index;i++) {
?? ??? ??? ??? ??? ??? ?numberArr[i]=numberArr[i+1];
?? ??? ??? ??? ??? ??? ?companyArr[i]=companyArr[i+1];
?? ??? ??? ??? ??? ??? ?codeArr[i]=codeArr[i+1];
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?index--;
?? ??? ??? ??? ?System.out.println("刪除成功");
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}else if(id==3) {
?? ??? ??? ?System.out.println("修改快遞信息");
?? ??? ?}else if(id==4) {
?? ??? ??? ?printAll();
?? ??? ?}
?? ?}
?? ?//--------------------------------------------------------------------------
?? ?public static void printAll() {
?? ??? ?System.out.println("----------這是所有的快遞信息-----------");
?? ??? ?System.out.println("快遞單號\t快遞公司\t取件碼");
?? ??? ?for(int i = 0;i<index;i++) {
?? ??? ??? ?System.out.println(numberArr[i]+"\t"+companyArr[i]+"\t"+codeArr[i]);
?? ??? ?}
?? ?}
?? ?//--------------------------------------------------------------------------
?? ?public static int findByNumber(String number) {
?? ??? ?for(int i = 0;i<index;i++) {
?? ??? ??? ?if(numberArr[i].equals(number)) {
?? ??? ??? ??? ?return i;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return -1;
?? ?}
?? ?//--------------------------------------------------------------------------
?? ?public static void ?saveExpress() {
?? ??? ?System.out.println("請輸入快遞單號");
?? ??? ?String number=input.next();
?? ??? ?System.out.println("請輸入公司名稱");
?? ??? ?String company=input.next();
?? ??? ?int code =0;?
?? ??? ?numberArr[index]=number;
?? ??? ?companyArr[index]=company;
?? ??? ?do {
?? ??? ??? ?code =random.nextInt(900)+100;
?? ??? ?}while(isExist(code));
?? ??? ?codeArr[index]=code;
?? ??? ?index++;
?? ??? ?System.out.println("取件碼是:"+code);
?? ?}
//--------------------------------------------------------------------------
public static void delExpress() {
?? ??? ?
?? ?}
//--------------------------------------------------------------------------
?? ?public static boolean isExist(int code) {
?? ??? ?for(int i = 0;i<index;i++) {
?? ??? ??? ?if(codeArr[i]==code) {
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return false;
?? ?
?? ?}
}
總結(jié)
以上是生活随笔為你收集整理的JavaEE实战班第十天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人口增量超过北上广!二线城市是怎样逆袭的
- 下一篇: xpe低配置系统解决“写缓存失败”问题