英文:
Exception in thread "main" java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class crewcut
问题
按照标题中所述,
当我继续进行编码中的下一个预定部分时,RMI系统没有根据我的输入显示动作。我希望在系统识别输入为"Customer"或"Barbershop"时,它能显示代码的下一个部分。
如何使它显示下一个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();
//实现代码
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;
}
英文:
As per stated in the title,
as I were to continue to next supposed section in the coding. The RMI system did not the display the action based on my input. I want it to display the next part of the coding when the system recognized the input as Customer / Barbershop.
How do i make it show the next case statement?
Client
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();
}
}}
Server
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();
}
}
}
Implementation
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;
}
}
Interface
import java.rmi.RemoteException;
public interface crewcut {
public String check_client(String name, String num, String service, String day) throws
RemoteException;
}
答案1
得分: 1
接口必须有“extends Remote”,这应该可以解决你的第二个问题。
“Exception in thread “main” java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class...”
英文:
Interface must have "extends Remote" that should solve your 2nd problem
"Exception in thread "main" java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class..."
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论