[英]Send multiple server objects from client to server
I have this code where there are two types of Object ObjectA
and ObjectB
on server. 我在服务器上有两种类型的对象
ObjectA
和ObjectB
代码。 I send these data to client on some action done on client side. 我在客户端执行某些操作后将这些数据发送给客户端。 After I update these objects on client side I need to send it back to sever to reflect the new values.
在客户端上更新这些对象后,我需要将其发送回服务器以反映新值。 I tried getting multiple objects using same
ObjectInputStream
and ObjectOutputStream
object from server but it throws ClassCastException
for reading objects from client. 我尝试使用相同的
ObjectInputStream
和ObjectOutputStream
对象从服务器获取多个对象,但是它抛出ClassCastException
以便从客户端读取对象。 How can I approach this. 我该如何处理。 My code is as shown below.
我的代码如下所示。
Server Class 服务器等级
private static class CustomerServer implements Runnable {
private Socket socket;
private int clientNumber;
public CustomerServer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
}
@SuppressWarnings("unchecked")
public void run() {
try {
ObjectInputStream oin = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream sOut = new ObjectOutputStream(socket.getOutputStream());
while (true) {
sOut.writeObject(items);
Object s = oin.readObject();
if(s instanceof CustomerAccount){
CustomerAccount cust = (CustomerAccount) s; // logged in customer, received from client login action
cust.accNumber = ++clientNumber;
customers.add(cust);
CustomerAccount delCust = (CustomerAccount)s; //customer who logged out, received from client logout action
//remove logged out customers from logged in customers list
if(customers.contains(delCust)){
customers.remove(delCust);
}
}else if(s instanceof ArrayList<?>){
if(((ArrayList<?>)s).get(0) instanceof ItemsForSale){
items = (ArrayList<ItemsForSale>) s;
}
}
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
}
}
Client Class 客户类别
public static void main(String[] args) throws IOException // main method
{
String serverName = "127.0.0.1"; // server IP address
int port = 4456; // port to get connected to server. This should match
// ServerSocket port in PatilVivekStoreServer
EventQueue.invokeLater(new Runnable() {
public void run() // define run method
{
try // try block
{
System.out.println("Connecting to " + serverName + " on port " + port);
Socket socket = new Socket(serverName, port);
System.out.println("Just connected to " + socket.getRemoteSocketAddress());
oout = new ObjectOutputStream(socket.getOutputStream()); // initialize
// new
// ObjectOutputStream
cIn = new ObjectInputStream(socket.getInputStream());
//dOut = new DataOutputStream(socket.getOutputStream());
//dIn = new DataInputStream(socket.getInputStream());
NewUser frame = new NewUser(); // new customer
// set NewUser frame visible
frame.setVisible(true);
} catch (Exception e) // catch block
{
e.printStackTrace();
}
}
});
}
NewUser Class NewUser类别
public static class NewUser extends JFrame// create class NewUser
{
private JPanel contentPane; // declare variable
private JButton btnSignup;
CustomerAccount customer;
ArrayList<ItemsForSale> items;
/**
* Create the frame.
*
* @throws IOException
*/
public NewUser() throws IOException // create constructor
{
customer = new CustomerAccount();
// set title
setTitle("CLIENT");
// set close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set bounds of frame
setBounds(10, 100, 1000, 550);
// create object of JPanel
contentPane = new JPanel();
// set contentPane border
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// set ContentPane with new object
setContentPane(contentPane);
// set contentPane layout is null
contentPane.setLayout(null);
// create text field for user
customer.setEmailAddress(new JTextField());
// set bounds for text fields
customer.getEmailAddress().setBounds(188, 106, 99, 20);
// in contentPane add text field
contentPane.add(customer.getEmailAddress());
// set column for text field
customer.getEmailAddress().setColumns(10);
// create text field for password
customer.setPassword(new JPasswordField());
// set bound for password field
customer.getPassword().setBounds(188, 161, 99, 20);
// add text field on contentPane
contentPane.add(customer.getPassword());
// set column for password text field
customer.getPassword().setColumns(10);
// create text field for password
customer.setCustomerName(new JTextField());
// set bound for password field
customer.getCustomerName().setBounds(188, 51, 99, 20);
// add text field on contentPane
contentPane.add(customer.getCustomerName());
// set column for password text field
customer.getCustomerName().setColumns(10);
// create text field for password
customer.setAddress(new JTextField());
// set bound for password field
customer.getAddress().setBounds(188, 216, 99, 20);
// add text field on contentPane
contentPane.add(customer.getAddress());
// set column for password text field
customer.getAddress().setColumns(10);
// label the text field
JLabel lblUserName = new JLabel("Email Address");
// set bounds for label
lblUserName.setBounds(70, 109, 86, 14);
// add into contentPane
contentPane.add(lblUserName);
// label the text field
JLabel lblPassword = new JLabel("Password");
// set bounds for label
lblPassword.setBounds(70, 164, 86, 14);
// add into contentPane
contentPane.add(lblPassword);
// label the text field
JLabel lblName = new JLabel("Name");
// set bounds for label
lblName.setBounds(70, 54, 86, 14);
// add into contentPane
contentPane.add(lblName);
// label the text field
JLabel lblAddress = new JLabel("Address");
// set bounds for label
lblAddress.setBounds(70, 219, 86, 14);
// add into contentPane
contentPane.add(lblAddress);
// create button signup
btnSignup = new JButton("Register");
JSeparator vSeparator = new JSeparator();
vSeparator.setOrientation(SwingConstants.VERTICAL);
vSeparator.setBounds(333, 16, 2, 307);
contentPane.add(vSeparator);
JLabel lblItem1 = new JLabel(" ");
lblItem1.setBounds(378, 57, 100, 20);
contentPane.add(lblItem1);
JLabel lblItem2 = new JLabel(" ");
lblItem2.setBounds(378, 82, 100, 20);
contentPane.add(lblItem2);
JLabel lblItem3 = new JLabel(" ");
lblItem3.setBounds(378, 109, 100, 20);
contentPane.add(lblItem3);
JLabel lblItem4 = new JLabel(" ");
lblItem4.setBounds(378, 135, 100, 20);
contentPane.add(lblItem4);
JLabel lblItem5 = new JLabel(" ");
lblItem5.setBounds(378, 161, 100, 20);
contentPane.add(lblItem5);
JLabel lblItem6 = new JLabel(" ");
lblItem6.setBounds(378, 186, 100, 20);
contentPane.add(lblItem6);
JLabel lblItem7 = new JLabel(" ");
lblItem7.setBounds(378, 211, 100, 20);
contentPane.add(lblItem7);
JLabel lblItem8 = new JLabel(" ");
lblItem8.setBounds(378, 238, 100, 20);
contentPane.add(lblItem8);
JLabel lblItem9 = new JLabel(" ");
lblItem9.setBounds(378, 264, 100, 20);
contentPane.add(lblItem9);
JLabel lblItem10 = new JLabel(" ");
lblItem10.setBounds(378, 292, 100, 20);
contentPane.add(lblItem10);
// add event handler on SignUp button
btnSignup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try // try block
{
// declare variables
String username = "";
String pwd = "";
String addr = "";
String name = "";
// get values using getText() method
username = customer.getEmailAddress().getText().trim();
pwd = new String(customer.getPassword().getPassword());
addr = customer.getAddress().getText().trim();
name = customer.getCustomerName().getText().trim();
// check condition if field equals to blank throw error
// message
if (username.equals("") || pwd.equals("")) {
JOptionPane.showMessageDialog(null, " name or password or Role is wrong", "Error",
JOptionPane.ERROR_MESSAGE);
} else // else register customer
{
accountNumber++;
String[] cust = new String[5];
cust[0] = Integer.toString(accountNumber);
cust[1] = name;
cust[2] = username;
cust[3] = pwd;
cust[4] = addr;
customers.add(cust);
System.out.println("In this methid register\n" + customers);
String SMessage = "Record added for " + name;
// create dialog ox which is print message
JOptionPane.showMessageDialog(null, SMessage, "Message", JOptionPane.PLAIN_MESSAGE);
}
} catch (Exception a) // catch block
{
a.printStackTrace();
}
}
});
// set bound for SignUp button
btnSignup.setBounds(70, 270, 115, 26);
// add button into contentPane
contentPane.add(btnSignup);
JLabel lblCustomerDetails = new JLabel("Customer Details:");
lblCustomerDetails.setBounds(439, 344, 136, 20);
contentPane.add(lblCustomerDetails);
JLabel dispName = new JLabel("");
dispName.setBounds(466, 51, 69, 20);
contentPane.add(dispName);
JLabel dispEmail = new JLabel("");
dispEmail.setBounds(466, 106, 69, 20);
contentPane.add(dispEmail);
JLabel dispAddress = new JLabel("");
dispAddress.setBounds(466, 161, 69, 20);
contentPane.add(dispAddress);
JSeparator separator = new JSeparator();
separator.setBounds(15, 328, 798, 14);
contentPane.add(separator);
JLabel lblLoginForm = new JLabel("Login Form");
lblLoginForm.setBounds(70, 344, 86, 20);
contentPane.add(lblLoginForm);
JLabel lblUsernameemail = new JLabel("Username/Email:");
lblUsernameemail.setBounds(70, 380, 122, 20);
contentPane.add(lblUsernameemail);
loginEmail = new JTextField();
loginEmail.setBounds(224, 377, 146, 26);
contentPane.add(loginEmail);
loginEmail.setColumns(10);
JLabel lblPassword_1 = new JLabel("Password:");
lblPassword_1.setBounds(70, 416, 86, 20);
contentPane.add(lblPassword_1);
loginPassword = new JPasswordField();
loginPassword.setBounds(224, 413, 146, 26);
contentPane.add(loginPassword);
loginPassword.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(70, 449, 115, 29);
contentPane.add(btnLogin);
JLabel lblName_1 = new JLabel("Name:");
lblName_1.setBounds(435, 380, 69, 20);
contentPane.add(lblName_1);
JLabel lblAddress_1 = new JLabel("Address:");
lblAddress_1.setBounds(435, 429, 69, 20);
contentPane.add(lblAddress_1);
JLabel detailName = new JLabel("");
detailName.setBounds(519, 380, 69, 20);
contentPane.add(detailName);
JLabel detailAddress = new JLabel("");
detailAddress.setBounds(519, 429, 160, 20);
contentPane.add(detailAddress);
JButton btnLogout = new JButton("Logout");
btnLogout.setBounds(234, 449, 115, 29);
contentPane.add(btnLogout);
JButton btnAddToCart_0 = new JButton("Add to Cart");
JButton btnAddToCart_1 = new JButton("Add to Cart");
JButton btnAddToCart_2 = new JButton("Add to Cart");
JButton btnAddToCart_3 = new JButton("Add to Cart");
JButton btnAddToCart_4 = new JButton("Add to Cart");
JButton btnAddToCart_5 = new JButton("Add to Cart");
JButton btnAddToCart_6 = new JButton("Add to Cart");
JButton btnAddToCart_7 = new JButton("Add to Cart");
JButton btnAddToCart_8 = new JButton("Add to Cart");
JButton btnAddToCart_9 = new JButton("Add to Cart");
JButton btnRemove_0 = new JButton("Remove");
JButton btnRemove_1 = new JButton("Remove");
JButton btnRemove_2 = new JButton("Remove");
JButton btnRemove_3 = new JButton("Remove");
JButton btnRemove_4 = new JButton("Remove");
JButton btnRemove_5 = new JButton("Remove");
JButton btnRemove_6 = new JButton("Remove");
JButton btnRemove_7 = new JButton("Remove");
JButton btnRemove_8 = new JButton("Remove");
JButton btnRemove_9 = new JButton("Remove");
JLabel lblInventory = new JLabel("Inventory");
JLabel lblQuantity = new JLabel("Quantity");
textField = new JTextField();
textField_1 = new JTextField();
textField_2 = new JTextField();
textField_3 = new JTextField();
textField_4 = new JTextField();
textField_5 = new JTextField();
textField_6 = new JTextField();
textField_7 = new JTextField();
textField_8 = new JTextField();
textField_9 = new JTextField();
textField.setBounds(502, 62, 79, 20);
contentPane.add(textField);
textField.setColumns(10);
textField_1.setBounds(502, 86, 79, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2.setBounds(502, 113, 79, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3.setColumns(10);
textField_3.setBounds(502, 140, 79, 20);
contentPane.add(textField_3);
textField_4.setColumns(10);
textField_4.setBounds(502, 166, 79, 20);
contentPane.add(textField_4);
textField_5.setColumns(10);
textField_5.setBounds(502, 191, 79, 20);
contentPane.add(textField_5);
textField_6.setColumns(10);
textField_6.setBounds(502, 217, 79, 20);
contentPane.add(textField_6);
textField_7.setColumns(10);
textField_7.setBounds(502, 243, 79, 20);
contentPane.add(textField_7);
textField_8.setColumns(10);
textField_8.setBounds(502, 267, 79, 20);
contentPane.add(textField_8);
textField_9.setColumns(10);
textField_9.setBounds(502, 292, 79, 20);
contentPane.add(textField_9);
JButton btnCartContents = new JButton("Cart Contents");
btnCartContents.setBounds(872, 71, 115, 29);
contentPane.add(btnCartContents);
JButton btnRefreshInventory = new JButton("Refresh Inventory");
btnRefreshInventory.addActionListener(new ActionListener() {
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
//items = (ArrayList<ItemsForSale>) cIn.readObject();
lblItem1.setText(
items.get(0).getItemName() + " " + items.get(0).getItemsInStock());
lblItem2.setText(
items.get(1).getItemName() + " " + items.get(1).getItemsInStock());
lblItem3.setText(
items.get(2).getItemName() + " " + items.get(2).getItemsInStock());
lblItem4.setText(
items.get(3).getItemName() + " " + items.get(3).getItemsInStock());
lblItem5.setText(
items.get(4).getItemName() + " " + items.get(4).getItemsInStock());
lblItem6.setText(
items.get(5).getItemName() + " " + items.get(5).getItemsInStock());
lblItem7.setText(
items.get(6).getItemName() + " " + items.get(6).getItemsInStock());
lblItem8.setText(
items.get(7).getItemName() + " " + items.get(7).getItemsInStock());
lblItem9.setText(
items.get(8).getItemName() + " " + items.get(8).getItemsInStock());
lblItem10
.setText(items.get(9).getItemName() + " " + items.get(9).getItemsInStock());
}
});
btnRefreshInventory.setBounds(872, 31, 115, 29);
contentPane.add(btnRefreshInventory);
lblInventory.setBounds(436, 41, 69, 20);
contentPane.add(lblInventory);
lblQuantity.setBounds(515, 41, 69, 20);
contentPane.add(lblQuantity);
btnAddToCart_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_0";
textFieldValidation(textField,'a',s);
}
});
btnAddToCart_0.setBounds(603, 62, 115, 20);
contentPane.add(btnAddToCart_0);
btnAddToCart_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_1";
textFieldValidation(textField_1,'a',s);
}
});
btnAddToCart_1.setBounds(603, 86, 115, 20);
contentPane.add(btnAddToCart_1);
btnAddToCart_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_2";
textFieldValidation(textField_2,'a',s);
}
});
btnAddToCart_2.setBounds(603, 113, 115, 20);
contentPane.add(btnAddToCart_2);
btnAddToCart_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_3";
textFieldValidation(textField_3,'a',s);
}
});
btnAddToCart_3.setBounds(603, 140, 115, 20);
contentPane.add(btnAddToCart_3);
btnAddToCart_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_4";
textFieldValidation(textField_4,'a',s);
}
});
btnAddToCart_4.setBounds(603, 166, 115, 20);
contentPane.add(btnAddToCart_4);
btnAddToCart_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_5";
textFieldValidation(textField_5,'a',s);
}
});
btnAddToCart_5.setBounds(603, 191, 115, 20);
contentPane.add(btnAddToCart_5);
btnAddToCart_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_6";
textFieldValidation(textField_6,'a',s);
}
});
btnAddToCart_6.setBounds(603, 217, 115, 20);
contentPane.add(btnAddToCart_6);
btnAddToCart_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_7";
textFieldValidation(textField_7,'a',s);
}
});
btnAddToCart_7.setBounds(603, 243, 115, 20);
contentPane.add(btnAddToCart_7);
btnAddToCart_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_8";
textFieldValidation(textField_8,'a',s);
}
});
btnAddToCart_8.setBounds(603, 267, 115, 20);
contentPane.add(btnAddToCart_8);
btnAddToCart_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnAddToCart_9";
textFieldValidation(textField_9,'a',s);
}
});
btnAddToCart_9.setBounds(603, 292, 115, 20);
contentPane.add(btnAddToCart_9);
btnRemove_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_0";
textFieldValidation(textField,'r',s);
}
});
btnRemove_0.setBounds(743, 62, 115, 20);
contentPane.add(btnRemove_0);
btnRemove_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_1";
textFieldValidation(textField_1,'r',s);
}
});
btnRemove_1.setBounds(743, 86, 115, 20);
contentPane.add(btnRemove_1);
btnRemove_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_2";
textFieldValidation(textField_2,'r',s);
}
});
btnRemove_2.setBounds(743, 113, 115, 20);
contentPane.add(btnRemove_2);
btnRemove_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_3";
textFieldValidation(textField_3,'r',s);
}
});
btnRemove_3.setBounds(743, 140, 115, 20);
contentPane.add(btnRemove_3);
btnRemove_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_4";
textFieldValidation(textField_4,'r',s);
}
});
btnRemove_4.setBounds(743, 166, 115, 20);
contentPane.add(btnRemove_4);
btnRemove_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_5";
textFieldValidation(textField_5,'r',s);
}
});
btnRemove_5.setBounds(743, 191, 115, 20);
contentPane.add(btnRemove_5);
btnRemove_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_6";
textFieldValidation(textField_6,'r',s);
}
});
btnRemove_6.setBounds(743, 217, 115, 20);
contentPane.add(btnRemove_6);
btnRemove_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_7";
textFieldValidation(textField_7,'r',s);
}
});
btnRemove_7.setBounds(743, 243, 115, 20);
contentPane.add(btnRemove_7);
btnRemove_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_8";
textFieldValidation(textField_8,'r',s);
}
});
btnRemove_8.setBounds(743, 267, 115, 20);
contentPane.add(btnRemove_8);
btnRemove_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "btnRemove_9";
textFieldValidation(textField_9,'r',s);
}
});
btnRemove_9.setBounds(743, 292, 115, 20);
contentPane.add(btnRemove_9);
btnRemove_0.setEnabled(false);
btnRemove_1.setEnabled(false);
btnRemove_2.setEnabled(false);
btnRemove_3.setEnabled(false);
btnRemove_4.setEnabled(false);
btnRemove_5.setEnabled(false);
btnRemove_6.setEnabled(false);
btnRemove_7.setEnabled(false);
btnRemove_8.setEnabled(false);
btnRemove_9.setEnabled(false);
btnAddToCart_0.setEnabled(false);
btnAddToCart_9.setEnabled(false);
btnAddToCart_1.setEnabled(false);
btnAddToCart_2.setEnabled(false);
btnAddToCart_3.setEnabled(false);
btnAddToCart_4.setEnabled(false);
btnAddToCart_5.setEnabled(false);
btnAddToCart_6.setEnabled(false);
btnAddToCart_7.setEnabled(false);
btnAddToCart_8.setEnabled(false);
btnRefreshInventory.setEnabled(false);
btnCartContents.setEnabled(false);
btnLogin.addActionListener(new ActionListener() {
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try // try block
{
// declare variables
String username = "";
String pwd = "";
// get values using getText() method
username = loginEmail.getText().trim();
pwd = new String(loginPassword.getPassword());
// check condition it field equals to blank throw error
// message
if (username.equals("") || pwd.equals("")) {
JOptionPane.showMessageDialog(null, " name or password or Role is wrong", "Error",
JOptionPane.ERROR_MESSAGE);
} else if (detailName.getText().length() != 0 || detailAddress.getText().length() != 0) {
JOptionPane.showMessageDialog(null, "Logout from previous user to login", "Error",
JOptionPane.ERROR_MESSAGE);
} else // else insert query is run properly
{
String accnum = null;
// get logged in customer details
for (String[] x : customers) {
if (x[2].compareTo(username) == 0 && x[3].compareTo(pwd) == 0) {
detailName.setText(x[1]);
detailAddress.setText(x[4]);
}
}
// send customer details to server
oout.writeChar('n');
oout.writeObject(customer);
items = (ArrayList<ItemsForSale>) cIn.readObject();
lblItem1.setText(
items.get(0).getItemName() + " " + items.get(0).getItemsInStock());
lblItem2.setText(
items.get(1).getItemName() + " " + items.get(1).getItemsInStock());
lblItem3.setText(
items.get(2).getItemName() + " " + items.get(2).getItemsInStock());
lblItem4.setText(
items.get(3).getItemName() + " " + items.get(3).getItemsInStock());
lblItem5.setText(
items.get(4).getItemName() + " " + items.get(4).getItemsInStock());
lblItem6.setText(
items.get(5).getItemName() + " " + items.get(5).getItemsInStock());
lblItem7.setText(
items.get(6).getItemName() + " " + items.get(6).getItemsInStock());
lblItem8.setText(
items.get(7).getItemName() + " " + items.get(7).getItemsInStock());
lblItem9.setText(
items.get(8).getItemName() + " " + items.get(8).getItemsInStock());
lblItem10
.setText(items.get(9).getItemName() + " " + items.get(9).getItemsInStock());
btnRemove_0.setEnabled(true);
btnRemove_1.setEnabled(true);
btnRemove_2.setEnabled(true);
btnRemove_3.setEnabled(true);
btnRemove_4.setEnabled(true);
btnRemove_5.setEnabled(true);
btnRemove_6.setEnabled(true);
btnRemove_7.setEnabled(true);
btnRemove_8.setEnabled(true);
btnRemove_9.setEnabled(true);
btnAddToCart_0.setEnabled(true);
btnAddToCart_9.setEnabled(true);
btnAddToCart_1.setEnabled(true);
btnAddToCart_2.setEnabled(true);
btnAddToCart_3.setEnabled(true);
btnAddToCart_4.setEnabled(true);
btnAddToCart_5.setEnabled(true);
btnAddToCart_6.setEnabled(true);
btnAddToCart_7.setEnabled(true);
btnAddToCart_8.setEnabled(true);
btnRefreshInventory.setEnabled(true);
btnCartContents.setEnabled(true);
}
} catch (Exception se) {
// handle errors for JDBC
se.printStackTrace();
}
}
});
btnLogout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String username = "";
String pwd = "";
// get values using getText() method
username = loginEmail.getText().trim();
pwd = new String(loginPassword.getPassword());
// check condition if field equals to blank throw error
// message
if (username.equals("") || pwd.equals("")) {
JOptionPane.showMessageDialog(null, " Not logged in", "Error", JOptionPane.ERROR_MESSAGE);
} else {
detailName.setText("");
detailAddress.setText("");
oout.writeObject(customer);
btnRemove_0.setEnabled(false);
btnRemove_1.setEnabled(false);
btnRemove_2.setEnabled(false);
btnRemove_3.setEnabled(false);
btnRemove_4.setEnabled(false);
btnRemove_5.setEnabled(false);
btnRemove_6.setEnabled(false);
btnRemove_7.setEnabled(false);
btnRemove_8.setEnabled(false);
btnRemove_9.setEnabled(false);
btnAddToCart_0.setEnabled(false);
btnAddToCart_9.setEnabled(false);
btnAddToCart_1.setEnabled(false);
btnAddToCart_2.setEnabled(false);
btnAddToCart_3.setEnabled(false);
btnAddToCart_4.setEnabled(false);
btnAddToCart_5.setEnabled(false);
btnAddToCart_6.setEnabled(false);
btnAddToCart_7.setEnabled(false);
btnAddToCart_8.setEnabled(false);
btnRefreshInventory.setEnabled(false);
btnCartContents.setEnabled(false);
}
} catch (Exception e2) {
// TODO: handle exception
}
}
});
}
public void textFieldValidation(JTextField t, char a, String s){
int i = Integer.parseInt(t.getText());
s = s.substring(s.length()-1);
int n = Integer.parseInt(s);
int iStock = items.get(n).getItemsInStock();
try {
if ((i>0)&&(i<7)) {
if(a == 'a'){
iStock = iStock - i;
}else if(a == 'r'){
iStock = iStock + i;
}
items.get(n).setItemsInStock(iStock);
try {
oout.writeObject(items);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (NumberFormatException n1) {
JOptionPane.showMessageDialog(null, " Please input valid quantity", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
A sample code of sending multiple objects from different client threads to server will be helpful 从不同的客户端线程向服务器发送多个对象的示例代码将很有帮助
If you're going to use this system of sending a char before an object to tell you what it is, you need to send it before every object. 如果要使用此系统在对象之前发送char来告诉您它是什么,则需要在每个对象之前发送它。 Otherwise when you read the char you're throwing away part of the object if it isn't the
'n'
case. 否则,当您读取字符时,如果不是
'n'
情况,则会丢弃该对象的一部分。
As for your ClassCastException,
you've provided zero information, but the message that comes with it tells you what the desired and actual classes were. 至于您的
ClassCastException,
您提供了零信息,但随附的消息告诉您所需的类和实际的类。
EDIT 编辑
Object s = oin.readObject();
if(s instanceof CustomerAccount){
CustomerAccount cust = (CustomerAccount) oin.readObject();
This is nonsense. 这是无稽之谈。 It should be
它应该是
CustomerAccount cust = (CustomerAccount)s;
Obviously. 明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.