【发布时间】:2023-11-02 15:22:01
【问题描述】:
如标题所述, 因为我将继续编码中的下一个假设部分。 RMI 系统没有根据我的输入显示操作。当系统将输入识别为客户/理发店时,我希望它显示编码的下一部分。
如何让它显示下一个 case 语句?
客户
import java.rmi.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class crewcut_client {
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to CrewCut Service");
System.out.println("Are you a 'CUSTOMER' | 'BARBER' ? Type in below");
String resp = sc.next();
crewcut cc = (crewcut) Naming.lookup("rmi://localhost/CrewCut");
switch (resp){
case "CUSTOMER":
case "customer":
case "Customer":
{
System.out.println("Hello Customer !");
System.out.println("Welcome to our system");
System.out.println("Please enter your name :");
String name = sc.nextLine();
System.out.println("Please enter your contact num :");
String num = sc.next();
System.out.println("Here are our services :");
System.out.println("Hair Cut RM15");
System.out.println("Hair Wash RM20");
System.out.println("Hair Dye RM25");
System.out.println("Press '1' for Hair Cut");
System.out.println("Press '2' for Hair Wash");
System.out.println("Press '3' for Hair Dye");
String service = sc.next();
System.out.println("Your day of booking ? Monday | Tuesday | Wednesday | Thursday | Friday |Saturday | Sunday");
String day = sc.next();
//impl code
System.out.println(cc.check_client(name,num,service,day));
break;
}
case "BARBER":
case "barber":
case "Barber":
{
System.out.println("Hello Barber !");
System.out.println("Are you in for work today? Enter 'Y' for YES or 'N' for NO" );
String attend = sc.nextLine();
if (attend.equals("Y")){
System.out.println("Welcome to work ! ");
System.out.println("The shop shall be closed in 9pm");
}else if (attend.equals("N")){
System.out.println("Hope to see you soon !");
}else{
System.out.println("Invalid input");
}
}
}
}catch (RemoteException re){
re.printStackTrace();
}catch (NotBoundException nbe){
nbe.printStackTrace();
}catch(MalformedURLException mfe){
mfe.printStackTrace();
}
}}
服务器
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class crewcut_server {
public static void main(String[] args) {
try{
crewcutimpl ccl = new crewcutimpl();
Naming.rebind("rmi://localhost/CrewCut",ccl);
}catch (RemoteException re){
re.printStackTrace();
}catch (MalformedURLException mfe){
mfe.printStackTrace();
}
}
}
实施
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;
public class crewcutimpl extends UnicastRemoteObject implements crewcut{
public crewcutimpl() throws RemoteException{
super();
}
public String check_client (String name, String num, String service, String day){
String display;
int price = 0;
switch (service){
case "1":
price = 15;
break;
case "2" :
price = 20;
break;
case "3":
price = 25;
break;
}display =" Your booking has been entered into the system Mr/Mrs " + name + "\n We shall contact
you through this num : " + num + "\n Service wanted : "+ service + "Total price :" + price +"Day
of appointment :" + day;
return display;
}
}
界面
import java.rmi.RemoteException;
public interface crewcut {
public String check_client(String name, String num, String service, String day) throws
RemoteException;
}
【问题讨论】:
-
为什么在定义类时不遵循 Java 命名约定?
-
你能进一步解释一下吗?
-
类名和接口名应以大写字母开头,很少使用下划线。
-
哦,好的,谢谢您指出这一点。这是一个新问题:
-
线程“main”java.lang.ClassCastException 中的异常:com.sun.proxy.$Proxy0 类无法转换为 Crewcut 类(com.sun.proxy.$Proxy0 和 Crewcut 在未命名的模块中loader 'app') 它说问题出在客户端类中的 Naming.lookup 中
标签: java client-server rmi