我正在尝试将顺序搜索功能连接到菜单,以便它接受用户输入并搜索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;
}
}
答案 0 :(得分: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));
}
}
}
现在,在您的Main类Jachi中,您可以创建和使用以下公寓:
Apartment apartA = new Apartment();
apartA.sequentialSearch("SOME_STUDENTID_HERE");
apartA.report();
Apartment apartB = new Apartment();
apartB.sequentialSearch("SOME_STUDENTID_HERE");
apartB.report();
您现在还可以创建公寓阵列。休息留给你去探索。