2013-05-22 31 views
-5

我正在嘗試將順序搜索功能連接到菜單,以便它接受用戶輸入並搜索apartA()apartB()中的tp數組。還有,我們如何將報告功能連接到功能apartA()apartB()中的值。如何將序列搜索連接到數組和主菜單

package jachi; 

import java.util.Scanner; 

public class Jachi { 

    public static void main(String[] args) { 
     menu(); 
    } 

    public static void menu(){ 

     int j,k,choice; 


     System.out.println("Menu"); 
     System.out.println("1. Register"); 
     System.out.println("2. Update"); 
     System.out.println("3. Report"); 
     System.out.println("4. Search"); 

     Scanner scan=new Scanner(System.in); 
     choice=scan.nextInt(); 

     switch(choice) { 
      case 1: 
       System.out.println("Select Apartment type"); 
       System.out.println("1. Apartment A"); 
       System.out.println("2. Apartment B."); 
       choice=scan.nextInt(); 

       if (choice==1) 
       { 
        apartA(); 
       } 
       else if (choice==2) 
       { 
        apartB(); 
       } 
       break; 

      case 2: 
       System.out.println("Update the student information"); 
       break; 

      case 3: 
       System.out.println("Rooms Available"); 
       report(); 
       break; 

      case 4: 
       System.out.print("Search for student. Please enter the TP number:"); 
       break; 

      default: 
       System.out.println("Incorrect input"); 
     } 
    } 

    public static int apartA() 
    {  
     final int a = 9; 


     //Normal Rooms Arrays 
     int[] tp; 
     tp = new int[a]; 

     String[] stname; 
     stname = new String[a]; 

     String[] mob; 
     mob = new String[a]; 

     int ttt = get.nextInt(); 
     int test = sequencialSearch(ttt); 

     Scanner get = new Scanner(System.in); 


     //Data Entry 
     for (int index = 0; index < a; index++) 
      { 
      System.out.println("Student Information"); 

      System.out.println("Enter the student TP NUMBER:"); 
      tp[index] = get.nextInt(); 

      System.out.println("Enter the STUDENT NAME:"); 
      stname[index] = get.nextLine(); 

      System.out.println("Enter the MOBILE NUMBER:"); 
      mob[index] = get.nextLine(); 

      rent(); 
     } 

     System.out.println("Rooms Full"); 

     /**Report Data, Test for Data presence 
     *for(a=0;a<=2;a++) 
     *{ 
     *System.out.println("TP NUMBER: "+tp[a]+"\t NAME: "+stname[a]+"\t MOBILE: "+mob[a]); 
     *}*/ 
     return a; 
    } 

    public static void apartB(){ 

     System.out.println("Select Room Type"); 
     System.out.println("\t1. Normal Room"); 
     System.out.println("\t2. Master Room"); 

     int choice; 
     final int j = 6; 
     final int k = 3; 

     Scanner get = new Scanner(System.in); 
     choice = get.nextInt(); 
     Scanner get1 = new Scanner(System.in); 

    if (choice==1) 
    { 
      System.out.println("1. Normal Bedroom"); 

       // Normal Bedroom Arrays 
       String[] tp; 
       tp = new String[j]; 

       String[] stname; 
       stname = new String[j]; 

       String[] mob; 
       mob = new String[j]; 

       //Data Entry 
       for (int index = 0; index < j; index++) 
        { 
        System.out.println("Student Information"); 

        System.out.println("Enter the student TP NUMBER:"); 
        tp[index] = get1.nextLine(); 

        System.out.println("Enter the STUDENT NAME:"); 
        stname[index] = get1.nextLine(); 

        System.out.println("Enter the MOBILE NUMBER:"); 
        mob[index] = get1.nextLine(); 

        rent(); 
        } 

        if(j<=5) 
        { 
        menu(); 
        } 
        else 
        { 
         System.out.println("All Houses Full"); 
        } 

    } 

    else if (choice==2) 
    { 
      System.out.println("Master bedroon"); 

       //Master Bedroom Arrays 
       String[] mastertp; 
       mastertp = new String[k]; 

       String[] mastername; 
       mastername = new String[k]; 

       String[] mastermob; 
       mastermob = new String[k]; 


       //Data Entry 
       for (int index = 0; index < k; index++) 
        { 
        System.out.println("Student Information"); 



        System.out.println("Enter the student TP NUMBER:"); 
        mastertp[index] = get1.nextLine(); 

        System.out.println("Enter the STUDENT NAME:"); 
        mastername[index] = get1.nextLine(); 

        System.out.println("Enter the MOBILE NUMBER:"); 
        mastermob[index] = get1.nextLine(); 

        int x,y,z,pay; 
        x=100; 
        pay=300; 
        y=3*pay; 
        z=x+y; 


        System.out.println("Charges to be Paid"); 
        System.out.println("Charges     Amount"); 
        System.out.println("Utilities charge:  rm100"); 
        System.out.println("Three month rental:  rm"+y); 
        System.out.println("Total:     rm"+z); 
        menu(); 
       } 

       System.out.println("Full House Man"); 
    } 
} 

    public static void rent(){ 
     int x,y,z,pay; 
     x=100; 
     pay=300; 
     y=3*pay; 
     z=x+y; 


     System.out.println("Charges to be Paid"); 
     System.out.println("Charges     Amount"); 
     System.out.println("Utilities charge:  rm100"); 
     System.out.println("Three month rental:  rm"+y); 
     System.out.println("Total:     rm"+z); 
     menu(); 
    } 

    public static void report(){ 

     int x,y,z; 
     x=0; 
     y=x+1; 
     z=6-y; 

     System.out.println("Rooms occupied; "+y); 
     System.out.println("Rooms available; "+z); 

     menu(); 
    } 

    public static int sequencialSearch(int Tnumbr){ 
     int index, 
      element; 
     boolean found; 

     index = 0; 
     element = 0; 
     found = false; 

     while(!found && index == Tnumbr){ 
     /* if(array[index] == 9{ 
      * found = true; 
      * element = index; 
      * } 
      * index++; 
     */ 

     } 

    return element; 
} 
} 
+1

您的項目的SRS在哪裏?恕我直言,你應該讓問題更清楚。 – Prateek

+0

問題的標題是誤導性的,你可能想改變這個問題。 –

+0

Map nameToValueMap你有沒有聽說過? – SenthilPrabhu

回答

0

好吧,這是很多代碼,讓我們試着將它分解成更簡單的類。 首先是公寓。讓我們有一個公寓類,把所有的實用方法,如report()和search()放入它。

公寓類可以是這樣的:

import java.util.HashMap; 
import java.util.List; 
import java.util.Scanner; 



class Apartment{ 

    /** Integer: roomid, String StrudentId **/ 
    HashMap<Integer, String> rooms = new HashMap<Integer, String>(); 

    public Apartment(){ 

    } 

    public Integer sequentialSearch(String tpNumber){ 
    /** Returns null if student is absent **/ 
    for(Integer roomId : rooms.keySet()) 
    { 
     if(tpNumber.equals(rooms.get(roomId))) 
      return roomId; 
    } 

    return null; 
} 


public void report(){ 
    System.out.println("ROOM_NUM\tSTUDENT_ID"); 
    for(Integer roomId : rooms.keySet()) 
    { 
     System.out.println(roomId +"\t" + rooms.get(roomId)); 
    } 
} 
} 

現在,在主類Jachi您可以創建和使用的公寓,如:

Apartment apartA = new Apartment(); 
apartA.sequentialSearch("SOME_STUDENTID_HERE"); 
apartA.report(); 

Apartment apartB = new Apartment(); 
apartB.sequentialSearch("SOME_STUDENTID_HERE"); 
apartB.report(); 

您現在還可以創建公寓的陣列。休息是留給你去探索。

相關問題