欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

运用Socket+IO+HashMap-实现小区快递管理系统

程序员文章站 2022-03-10 10:53:55
需求基于异常处理快递管理任务 , 加入网络编程+io处理。要求:快递数据永久存储在文件中 , 每次程序打 开加载数据. 客户端不存储任何数据, 全部传输到服务器来存储.Service服务端源码:package pers.sheng.demo;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.DataOutputStream;im...

需求

基于异常处理快递管理任务 , 加入网络编程+io处理。要求:快递数据永久存储在文件中 , 每次程序打 开加载数据. 客户端不存储任何数据, 全部传输到服务器来存储.
运用Socket+IO+HashMap-实现小区快递管理系统

Service服务端源码:

package pers.sheng.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class ServiceSystem {

	public static void main(String[] args) {
		try {
			Service();
		} catch (IOException e) {
			System.out.println("最后一台客户机退出!");
		}

	}
	static void Service() throws IOException {
		System.out.println("--服务器已启动--");
		ServerSocket s = new ServerSocket(9999);
		Socket client = s.accept();
		Scanner input = new Scanner(System.in);
		
		DataInputStream dis = new DataInputStream(client.getInputStream());
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
		System.out.println("--服务器已与客户机建立连接--");
		String company = null;
		String orderNumber = null;
		while(true) {
			String context = null;
			Parcel p2 = new Parcel();
			 Map map = new HashMap();
			context = dis.readUTF();
			switch(
					context.substring(0,context.indexOf(":")).equals("1.1")?1:
						(context.substring(0,context.indexOf(":")).equals("1.2")?2:
							(context.substring(0,context.indexOf(":")).equals("1.3")?3:
								(context.substring(0,context.indexOf(":")).equals("2.1")?4:
									((context.substring(0,context.indexOf(":")).equals("3.1")?5:
										((context.substring(0,context.indexOf(":")).equals("0")?6:0)))))))
					) {
					case 1://快递录入
						company = context.substring(context.indexOf(":")+1, context.length());
						System.out.println("客户输入的快递公司:"+company);
						break;
					case 2://快递录入
						orderNumber = context.substring(context.indexOf(":")+1, context.length());
						System.out.println("客户输入的快递单号:"+orderNumber);
						break;
					case 3://快递录入
						p2.setPhoneNumber(context.substring(context.indexOf(":")+1, context.length()));
						System.out.println("客户输入的收件人手机号码:"+p2.getPhoneNumber());
						Random r = new Random();
						p2.setCode(String.valueOf(r.nextInt(900000)+100000));
						p2.setCompany(company);
						p2.setOrderNumber(orderNumber);
						map.put(p2.getCode(), p2);
						dos.writeUTF("快递录入成功,取件码:"+p2.getCode());
						BufferedWriter bw1 = new BufferedWriter(new FileWriter("快递柜.txt",true));
						bw1.write(map.toString());
						bw1.newLine();
						bw1.close();
						System.out.println("已成功存入一条客户快递信息:"+map);
						break;
					case 4://快递取出
						System.out.println("客户输入的取件码:"+context.substring(context.indexOf(":")+1, context.length()));
						String key = context.substring(context.indexOf(":")+1,context.length());
						BufferedReader br = new BufferedReader(new FileReader("快递柜.txt"));
						String context2;
						while((context2=br.readLine())!=null) {
							Parcel p1 = new Parcel();
							p1.setCode(context2.substring(1,context2.indexOf("=")));
							p1.setCompany(context2.substring(context2.indexOf("y")+2,context2.indexOf(",")));
							String context3 = context2.substring(context2.indexOf(",")+1,context2.length());
							p1.setOrderNumber(context3.substring(context3.indexOf("=")+1,context3.indexOf(",")));
							context3 = context3.substring(context3.indexOf(",")+1,context3.length());
							p1.setPhoneNumber(context3.substring(context3.indexOf("=")+1,context3.indexOf(",")));
							map.put(p1.getCode(), p1);
						}
						if(map.containsKey(key)) {
							String context1 = map.get(key).toString();
							String company1 = context1.substring(context1.indexOf("=")+1,context1.indexOf(","));
							context1 = context1.substring(context1.indexOf(",")+1,context1.length());
							String orderNumber1 = context1.substring(context1.indexOf("=")+1,context1.indexOf(","));
							context1 = context1.substring(context1.indexOf(",")+1,context1.length());
							String phoneNumber = context1.substring(context1.indexOf("=")+1,context1.indexOf(","));
							context1 = context1.substring(context1.indexOf(",")+1,context1.length());
							String Code = context1.substring(context1.indexOf("=")+1,context1.indexOf("]"));
							dos.writeUTF("【取件成功】\n快递公司:"+company1+",快递单号:"+orderNumber1+",收件人手机号码:"+phoneNumber+",取件码:"+Code);
							map.remove(key);
							BufferedWriter bw2 = new BufferedWriter(new FileWriter("快递柜.txt"));
							bw2.write(map.toString());
							bw2.newLine();
							bw2.close();
						}else {
							dos.writeUTF("【您输入取件码不存在,如您有任何问题,请咨询快递员】\n");
						}
						break;
					case 5://查看所有快递
						BufferedReader br1 = new BufferedReader(new FileReader("快递柜.txt"));
						String context21;
						while((context21=br1.readLine())!=null) {
							Parcel p1 = new Parcel();
							p1.setCode(context21.substring(1,context21.indexOf("=")));
							p1.setCompany(context21.substring(context21.indexOf("y")+2,context21.indexOf(",")));
							String context3 = context21.substring(context21.indexOf(",")+1,context21.length());
							p1.setOrderNumber(context3.substring(context3.indexOf("=")+1,context3.indexOf(",")));
							context3 = context3.substring(context3.indexOf(",")+1,context3.length());
							p1.setPhoneNumber(context3.substring(context3.indexOf("=")+1,context3.indexOf(",")));
							map.put(p1.getCode(), p1);
						}
						dos.writeUTF(map.toString().equals("{}")?"【目前快递柜为空,系统已通知快递员】":"\n"+map.toString()+"\n");
						break;
					case 6://打印客户机退出信息
						System.out.println("有一台客户机退出!");
						break;
					default:
						break;
						
			}
		}
	}
}
	
class Parcel{
	private String company,orderNumber,phoneNumber,Code;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((Code == null) ? 0 : Code.hashCode());
		result = prime * result + ((company == null) ? 0 : company.hashCode());
		result = prime * result + ((orderNumber == null) ? 0 : orderNumber.hashCode());
		result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
		return result;
	}
	
	public Parcel(String company, String orderNumber, String phoneNumber, String code) {
		super();
		this.company = company;
		this.orderNumber = orderNumber;
		this.phoneNumber = phoneNumber;
		Code = code;
	}

	public Parcel() {
		super();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Parcel other = (Parcel) obj;
		if (Code == null) {
			if (other.Code != null)
				return false;
		} else if (!Code.equals(other.Code))
			return false;
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		if (orderNumber == null) {
			if (other.orderNumber != null)
				return false;
		} else if (!orderNumber.equals(other.orderNumber))
			return false;
		if (phoneNumber == null) {
			if (other.phoneNumber != null)
				return false;
		} else if (!phoneNumber.equals(other.phoneNumber))
			return false;
		return true;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getCode() {
		return Code;
	}
	public void setCode(String code) {
		Code = code;
	}
	@Override
	public String toString() {
		return "Parcel [company=" + company + ", orderNumber=" + orderNumber + ", phoneNumber=" + phoneNumber
				+ ", Code=" + Code + "]";
	}
}

Customer客户端源码:

package pers.sheng.demo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class CustomerInterface {

	public static void main(String[] args) throws IOException {
		Customer c1 = new Customer();
		Customer c2 = new Customer();
		c1.enterSystem();
		c2.enterSystem();
	}

}

class Customer {
	void enterSystem() throws UnknownHostException, IOException {
		Socket s = new Socket("127.0.0.1",9999);
		DataOutputStream dos = new DataOutputStream(s.getOutputStream());
		DataInputStream dis = new DataInputStream(s.getInputStream());
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎使用小区快递管理系统");
		boolean isExit = false;
		while(!isExit) {
			System.out.println("--请根据提示,输入功能序号--\n1.快递录入\n2.快递取出\n3.查看所有\n0.退出程序");
			switch(input.nextInt()) {
				case 1:
					System.out.println("【请根据提示进行快递录入】");
					System.out.print("请输入快递公司:");
					dos.writeUTF("1.1:"+input.next());
					System.out.print("请输入快递单号:");
					dos.writeUTF("1.2:"+input.next());
					System.out.print("请输入收件人手机号码:");
					dos.writeUTF("1.3:"+input.next());
					System.out.println(dis.readUTF());
					break;
				case 2:
					System.out.print("请输入您的六位数字取件码:");
					dos.writeUTF("2.1:"+input.next());
					System.out.println(dis.readUTF());
					break;
				case 3:
					dos.writeUTF("3.1:查看所有快递");
					System.out.println(dis.readUTF());
					break;
				case 0:
					dos.writeUTF("0:客户机已离线");
					dos.close();
					isExit = true;
					break;
			}
		}
	}
}

运行结果:

客户端
运用Socket+IO+HashMap-实现小区快递管理系统
服务端
运用Socket+IO+HashMap-实现小区快递管理系统

本文地址:https://blog.csdn.net/qq_43759656/article/details/107671904

相关标签: java