基于服务器的快递管理系统
程序员文章站
2024-02-01 21:30:04
...
package com.cwd;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 客户端*/
public class Client {
public static void main(String[] args) throws IOException, ClassNotFoundException{
/* Socket socket = new Socket("127.0.0.1", 51334);
OutputStream os = socket.getOutputStream();*/
/*h1:
while (true) {
//IP和端口号
Socket socket = new Socket("127.0.0.1", 51334);
OutputStream os = socket.getOutputStream();
try{
System.out.println("0结束");
System.out.println("1管理员");
System.out.println("2普通用户");
Scanner input = new Scanner(System.in);
//输入选择哪个
int y = input.nextInt();
//将序号发给服务器,功能一一对应;
os.write(y);
switch (y){
//结束程序
case 0: {
PrintStream ps = new PrintStream(os);
ps.println("程序结束");
ps.close();
// break h1;//跳出while循环
}
case 1: {
System.out.println("0结束");
System.out.println("1录入快递");
System.out.println("2删除快递");
System.out.println("3查看所有快递");
//Scanner input = new Scanner(System.in);
int x = input.nextInt();
//将序号发给服务器,功能一一对应;
os.write(x);
switch (x) {
//结束程序
case 0: {
PrintStream ps = new PrintStream(os);
ps.println("程序结束");
ps.close();
// break h1;//跳出while循环
}
case 1: {
*//* ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new Manage().addExpress());//创建快递信息并将此信息发给服务器*//*
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new Manage().addExpress());//创建快递信息并将此信息发给服务器
oos.close();
break;
}
case 2: {
System.out.println("请输入快递单号");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将快递单号传给服务器
ps.close();
socket = new Socket("127.0.0.1", 51334);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;
}
case 3: {
os.close();
socket = new Socket("127.0.0.1", 51334);
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();//服务器将快递信息以集合的形式传过来;
ois.close();
//遍历
for (Express e : list) {
System.out.println(e.toString());
}
break;
}
default:
}
//注意因为这是管理员的所以需要break退出
break ;
}
case 2: {
System.out.println("请输入取件码--");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将取件码传给服务器
ps.close();
socket = new Socket("127.0.0.1", 51334);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;
}
}
} catch (Exception e) {
System.out.println("操作有误");
continue h1;
}
}*/
h1:
while (true) {
//IP和端口号
Socket socket = new Socket("127.0.0.1", 55354);
OutputStream os = socket.getOutputStream();
try {
System.out.println("0结束");
System.out.println("1录入快递");
System.out.println("2删除快递");
System.out.println("3查看所有快递");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
//将序号发给服务器,功能一一对应;
os.write(x);
switch (x) {
//结束程序
case 0: {
PrintStream ps = new PrintStream(os);
ps.println("程序结束");
ps.close();
break h1;//跳出while循环
}
case 1: {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new Manage().addExpress());//创建快递信息并将此信息发给服务器
oos.close();
break;
}
case 2: {
/*System.out.println("请输入取件码");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将取件码传给服务器
ps.close();
socket = new Socket("127.0.0.1", 55554);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;*/
System.out.println("请输入快递单号");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将快递单号传给服务器
ps.close();
socket = new Socket("127.0.0.1", 55354);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;
}
case 3: {
os.close();
socket = new Socket("127.0.0.1", 55354);
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();//服务器将快递信息以集合的形式传过来;
ois.close();
//遍历
for (Express e :
list) {
System.out.println(e.toString());
}
break;
}
}
} catch (Exception e) {
System.out.println("操作有误");
continue h1;
}
}
}
}
package com.cwd;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Client2 {
public static void main(String[] args) throws IOException {
h1:
while (true) {
//IP和端口号
Socket socket = new Socket("127.0.0.1", 53354);
OutputStream os = socket.getOutputStream();
try {
System.out.println("0结束");
//System.out.println("1录入快递");
System.out.println("1取快递");
//System.out.println("3查看所有快递");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
//将序号发给服务器,功能一一对应;
os.write(x);
switch (x) {
//结束程序
case 0: {
PrintStream ps = new PrintStream(os);
ps.println("程序结束");
ps.close();
break h1;//跳出while循环
}
case 1: {
/*ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(new Manage().addExpress());//创建快递信息并将此信息发给服务器
oos.close();
break;*/
System.out.println("请输入取件码");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将取件码传给服务器
ps.close();
socket = new Socket("127.0.0.1", 53354);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;
}
/*case 2: {
System.out.println("请输入取件码");
PrintStream ps = new PrintStream(os);
ps.println(input.nextInt());//将取件码传给服务器
ps.close();
socket = new Socket("127.0.0.1", 53354);
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());//读取由服务器传过来的快递信息
br.close();
break;
}
case 3: {
os.close();
socket = new Socket("127.0.0.1", 53354);
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();//服务器将快递信息以集合的形式传过来;
ois.close();
//遍历
for (Express e :
list) {
System.out.println(e.toString());
}
break;
}*/
}
} catch (Exception e) {
System.out.println("操作有误");
continue h1;
}
}
}
}
package com.cwd;
import java.io.Serializable;
import java.util.Objects;
public class Express implements Serializable {
private String company;//快递公司
private int number;//快递单号
private int code;//取件码
private String phone;//手机号码
//快递在快递柜的行数
private int y;
//快递在快递柜中的列数
private int x;
public Express() {
}
public Express(String company, int number, int code, String phone) {
this.company = company;
this.number = number;
this.code = code;
this.phone = phone;
}
public Express(String company,int number, int code, String phone,int x,int y) {
this.company = company;
this.number = number;
this.code = code;
this.phone = phone;
this.x = x;
this.y = y;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
@Override
public String toString() {
return "Express{" +
"company='" + company + '\'' +
", number='" + number + '\'' +
", code=" + code +
", phone='" + phone + '\'' +
", y=" + y +
", x=" + x +
'}';
}
}
package com.cwd;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**注意不同项目服务端相同 文件要不同,端口不同 不然报错*/
public class Manage extends Express{
static int x;
static int y;
// 设置取件码
//继承父类数据
public Manage(String company, int number, int code, String phone) {
super(company, number, code, phone);
}
public Manage() {
}
//设置取件码
public int addcode() throws IOException, ClassNotFoundException {
int code = 0;
Random random = new Random();
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
//当有快递信息时
if (file.length() != 0) {
//遍历文件中的快递信息 ,查询是否有相同的取件码;这里没有考虑所有取件码都被占用的情况
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
h1:
while (true) {
//随机六位取件码
code = random.nextInt(900000) + 100000;
for (Express e : list) {
if (e.getCode() == code) {
break h1;
}
}
addPosition();
return code;
}
}
//没有快递信息时
code = random.nextInt(900000) + 100000;
addPosition();
return code;
}
// 设置快递放在哪里
public void addPosition() throws IOException, ClassNotFoundException {
Random random = new Random();
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
// 如果文件不为空
if (file.length() != 0) {
h1:
while (true) {
x = random.nextInt(9) + 1;
y = random.nextInt(9) + 1;
// 加载本地数据,判断位置是否有快递
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
for (Express e : list) {
if (e.getX() == x && e.getY() == y) {
// 位置已有快递,重新产生
continue h1;
}
}
break h1;
}
}else {
// 没有快递信息
x = random.nextInt(9) + 1;
y = random.nextInt(9) + 1;
}
}
//录入快递
public ArrayList<Express> addExpress() throws IOException, ClassNotFoundException {
ArrayList<Express> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("输入快递公司");
String company = input.next();
System.out.println("输入快递单号");
int number = input.nextInt();
System.out.println("输入手机号");
String phone = input.next();
int code = addcode();//随机取件码
System.out.println(code);
//将所有信息录入集合
Express express = new Express(company, number, code, phone, x, y);
list.add(express);//将快递添加到集合
return list;//返回此集合;
}
//保存快递信息到本地
public void save(ArrayList<Express> list) throws IOException, ClassNotFoundException {
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
//如果文件中有快递信息
if (file.length() != 0) {
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list1 = (ArrayList<Express>) ois.readObject();
ois.close();
list1.addAll(list);//将新快递信息加入
//序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list1);
oos.close();
} else {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.close();
}
}
//快递员删除快递
public StringBuffer delete(int number) throws IOException, ClassNotFoundException {
StringBuffer text = new StringBuffer();
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
//根据取件码定位快递信息
for (Express e : list) {
//e.getNumber() == number
if (e.getNumber() == number) {
text.append("快递在柜子的第"+e.getY()+"行,第"+e.getX()+"列,快递信息"+e.toString());
list.remove(e);//将此快递删除
//序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.close();
return text;//返回此快递信息
}
}
return text.append("未找到");//如果没有则返回未找到
}
//普通用户取出快递
public StringBuffer take(int code) throws IOException, ClassNotFoundException {
StringBuffer text = new StringBuffer();
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
//根据取件码定位快递信息
for (Express e : list) {
if (e.getCode() == code) {
text.append("快递在柜子的第"+e.getY()+"行,第"+e.getX()+"列,快递信息"+e.toString());
list.remove(e);//将此快递删除
//序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.close();
return text;//返回此快递信息
}
}
return text.append("未找到");//如果没有则返回未找到
}
//查看所有快递
public ArrayList<Express> show() throws IOException, ClassNotFoundException {
File file = new File("D:\\文件练习\\Express8.txt");
file.createNewFile();
//反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
return list;//返回快递信息的集合
}
}
package com.cwd;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* 服务器*/
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/* //端口55554
ServerSocket serverSocket = new ServerSocket(51334);
System.out.println("服务器连接成功");
Manage m = new Manage();
h1:
while (true) {
//等待连接
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//接收客户端传过来的序号
int y = is.read();
switch (y){
case 0: {
//读取
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//服务器关闭
if (br.readLine().equals("程序结束")) {
//关闭流
br.close();
break h1;
}
}
case 1: {
int x = is.read();
//功能选择
switch (x) {
case 0: {
//读取
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//服务器关闭
if (br.readLine().equals("程序结束")) {
//关闭流
br.close();
break h1;
}
}
case 1: {
ObjectInputStream ois = new ObjectInputStream(is);
//接收到的一个集合形式的快递信息
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
//保存到本地文件
m.save(list);
break;
}
case 2: {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int number = Integer.valueOf(br.readLine());
//删除快递信息;
StringBuffer text = m.delete(number);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;
}
case 3: {
is.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
//将快递信息以集合形式发给客户端
oos.writeObject(m.show());
oos.close();
break;
}
}
}
case 2: {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int code = Integer.valueOf(br.readLine());
//取出快递信息;
StringBuffer text = m.take(code);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;
}
}
}*/
ServerSocket serverSocket = new ServerSocket(55354);
System.out.println("服务器连接成功");
Manage m = new Manage();
//h1退出循环的
h1:
while (true) {
//等待连接
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//接收客户端传过来的序号
int x = is.read();
//功能选择
switch (x) {
case 0: {
//读取
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//服务器关闭
if (br.readLine().equals("程序结束")) {
//关闭流
br.close();
break h1;
}
}
case 1: {
ObjectInputStream ois = new ObjectInputStream(is);
//接收到的一个集合形式的快递信息
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
//保存到本地文件
m.save(list);
break;
}
case 2: {
/*BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int code = Integer.valueOf(br.readLine());
//取出快递信息;
StringBuffer text = m.take(code);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;*/
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int number = Integer.valueOf(br.readLine());
//删除快递信息;
StringBuffer text = m.delete(number);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;
}
case 3: {
is.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
//将快递信息以集合形式发给客户端
oos.writeObject(m.show());
oos.close();
break;
}
}
}
}
}
package com.cwd;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(53354);
System.out.println("服务器连接成功");
Manage m = new Manage();
//h1退出循环的
h1:
while (true) {
//等待连接
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//接收客户端传过来的序号
int x = is.read();
//功能选择
switch (x) {
case 0: {
//读取
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//服务器关闭
if (br.readLine().equals("程序结束")) {
//关闭流
br.close();
break h1;
}
}
case 1: {
/* ObjectInputStream ois = new ObjectInputStream(is);
//接收到的一个集合形式的快递信息
ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
ois.close();
//保存到本地文件
m.save(list);
break;*/
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int code = Integer.valueOf(br.readLine());
//取出快递信息;
StringBuffer text = m.take(code);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;
}
/*case 2: {
*//* BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int number = Integer.valueOf(br.readLine());
//删除快递信息;
StringBuffer text = m.delete(number);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;*//*
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取返回的是String类型的取件码,读取其中的数字;
int code = Integer.valueOf(br.readLine());
//取出快递信息;
StringBuffer text = m.take(code);
br.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintStream ps = new PrintStream(os);
//将快递信息发给客户端
ps.println(text);
ps.close();
break;
}
case 3: {
is.close();
socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
//将快递信息以集合形式发给客户端
oos.writeObject(m.show());
oos.close();
break;
}*/
}
}
}
}
因为循环连接服务器时出现错误,所以写了两个服务器实现客户和快递员的操作