利用socket实现在线购买电脑零配件

客户端代码

package com.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;


import com.entity.Fuction;
public class Client{
private PrintWriter writer;
private BufferedReader reader;
private FileWriter fw;
private BufferedWriter bw;
Socket socket;
int totalMoney;
String cpustr,niecunstr,yinpanstr,qudongstr,pingmuSizestr,suremai;
Scanner input=new Scanner(System.in);
public void welcome(){
System.out.println("欢迎进入电脑选择系统");
cpustr="cpu选择: 1-高速,   2-中速,   3-低速";
niecunstr="内存选择: 1-8G,     2-4G,      3-2G";
yinpanstr="硬盘选择: 1-2T,  2-1T,   3-500G";
qudongstr="显卡驱动: 1-高级,   2-中级,   3-低级";
pingmuSizestr="屏幕大小: 1-16寸,   2-14寸,  3-12寸";
suremai="是否确定:1确定,2取消";
}
public void getServerMessage(){
try{
File file=new File("D:/Client","Client.txt");
String strfile="";
if(file.exists()){
file.delete();
}else{
file.createNewFile();
System.out.println("文件已创建成功!");
}
while(true){
String str=reader.readLine();
System.out.println("客户需要付费:\n"+str);
strfile=strfile+str+'\n';
   fw=new FileWriter(file);
bw=new BufferedWriter(fw);
bw.write(strfile);
bw.newLine();
bw.flush();
}
}catch(Exception e){
e.printStackTrace();
}
}
public boolean juage(int choice){
if(choice==1 || choice==2 || choice==3){
return true;
}else{
return false;
}
}
public void choice(){
System.out.println(cpustr);
writer.println(cpustr+"\n客户选择----");
int cpuchoice=input.nextInt();
if(juage(cpuchoice)){
writer.println("cpu"+cpuchoice);
writer.flush();
}else{
System.out.println("错误选择");
writer.println("error");
writer.flush();
System.exit(0);
}
System.out.println(niecunstr);
writer.println(niecunstr+"\n客户选择:");
int niecunchoice=input.nextInt();
if(juage(niecunchoice)){
writer.println("niecun"+niecunchoice);
writer.flush();
}else{
System.out.println("错误选择");
writer.println("error");
writer.flush();
System.exit(0);
}
System.out.println(yinpanstr);
writer.println(yinpanstr+"\n客户选择:");
int yinpanchoice=input.nextInt();
if(juage(yinpanchoice)){
writer.println("yinpan"+yinpanchoice);
writer.flush();
}else{
System.out.println("错误选择");
writer.println("error");
writer.flush();
System.exit(0);
}
System.out.println(qudongstr);
writer.println(qudongstr+"\n客户选择:");
int qudongchoice=input.nextInt();
if(juage(qudongchoice)){
writer.println("qudong"+qudongchoice);
writer.flush();
}else{
System.out.println("错误选择");
writer.println("error");
writer.flush();
System.exit(0);
}
System.out.println(pingmuSizestr);
writer.println(pingmuSizestr+"\n客户选择:");
int pingmuSizechoice=input.nextInt();
if(juage(pingmuSizechoice)){
writer.println("pingmu"+pingmuSizechoice);
writer.flush();
}else{
System.out.println("错误选择");
writer.println("error");
writer.flush();
System.exit(0);
}
System.out.println(suremai);
writer.println(suremai+"\n客户选择:");
int suremaichoice=input.nextInt();
writer.println("choice"+suremaichoice);
writer.flush();
System.out.println("请填写客户姓名:");
String name=input.next();
writer.println("name"+name);
writer.flush();
System.out.println("请填写收货地址:");
String adress=input.next();
writer.println("adress"+adress);
writer.flush();
System.out.println("请填写手机号码:");
String phone=input.next();
writer.println("phone"+phone);
writer.flush();
System.out.println("按2确定");
String sure=input.next();
writer.println("choice"+sure);
writer.flush();
System.out.println("按end退出本系统!");
}
private void connect(){
try{
socket=new Socket("localhost",6000);
writer=new PrintWriter(socket.getOutputStream(),true);
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer.println("连接成功!");
choice();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client=new Client();
client.welcome();
client.connect();
client.getServerMessage();
}
}

服务器代码

package com.server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private BufferedReader reader;
private PrintWriter writer;
private ServerSocket server;
private FileWriter fw;
private BufferedWriter bw;
private Socket socket;
String cpustr,niecunstr,yinpanstr,qudongstr,pingmuSizestr;
int cpuMoney,niecunMoney,yinpanMoney,qudongMoney,PingmuSizeMoney,totalMoney;
public int cpuFuction(String choice){
switch(choice){
case "cpu1":
cpuMoney=1000;
break;
case "cpu2":
cpuMoney=800;
break;
case "cpu3":
cpuMoney=500;
break;
default:
break;
}
return cpuMoney;
}
public int niecunFuction(String choice){
switch(choice){
case "niecun1":
niecunMoney=500;
break;
case "niecun2":
niecunMoney=300;
break;
case "niecun3":
niecunMoney=150;
break;
default:
break;
}
return niecunMoney;
}
public int yinpanFuction(String choice){
switch(choice){
case "yinpan1":
yinpanMoney=1000;
break;
case "yinpan2":
yinpanMoney=500;
break;
case "yinpan3":
yinpanMoney=300;
break;
default:
break;
}
return yinpanMoney;
}
public int qudongFuction(String choice){
switch(choice){
case "qudong1":
qudongMoney=2000;
break;
case "qudong2":
qudongMoney=1500;
break;
case "qudong3":
qudongMoney=1000;
break;
default:
break;
}
return qudongMoney;
}
public int pingmuSizeFuction(String choice){
switch(choice){
case "pingmu1":
PingmuSizeMoney=1000;
break;
case "pingmu2":
PingmuSizeMoney=500;
break;
case "pingmu3":
PingmuSizeMoney=200;
break;
default:
break;
}
return PingmuSizeMoney;
}
public int totalMoney(int cpuMoney,int niecunMoney,int yinpanMoney,int qudongMoney,int PingmuSizeMoney){
return totalMoney=cpuMoney+niecunMoney+yinpanMoney+qudongMoney+PingmuSizeMoney;
}
public void getServer(){
try{
server=new ServerSocket(6000);
System.out.println("服务器连接成功。");
while(true){
System.out.println("等待客户端的连接");
socket=server.accept();
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(),true);
getClientMessage();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void getClientMessage(){
try{
File file=new File("D:/Server","Server.txt");
String strfile="";
if(file.exists()){
file.delete();
}else{
file.createNewFile();
System.out.println("文件已创建成功!");
}
while(true){
String strchoice=reader.readLine();
if(strchoice.equals("cpu1") || strchoice.equals("cpu2") || strchoice.equals("cpu3")){
System.out.println("客户:"+strchoice);
cpuMoney=cpuFuction(strchoice);
System.out.println("价格为:"+cpuFuction(strchoice));
strfile=strfile+cpustr+"客户:"+strchoice+'\n';
}else if(strchoice.equals("niecun1") || strchoice.equals("niecun2") || strchoice.equals("niecun3")){
System.out.println("客户:"+strchoice);
niecunMoney=niecunFuction(strchoice);
System.out.println("价格为:"+niecunFuction(strchoice));
strfile=strfile+niecunstr+"客户:"+strchoice+'\n';
}else if(strchoice.equals("yinpan1") || strchoice.equals("yinpan2") || strchoice.equals("yinpan3")){
System.out.println("客户:"+strchoice);
yinpanMoney=yinpanFuction(strchoice);
System.out.println("价格为:"+yinpanFuction(strchoice));
strfile=strfile+yinpanstr+"客户:"+strchoice+'\n';
}else if(strchoice.equals("qudong1") || strchoice.equals("qudong2") || strchoice.equals("qudong3")){
System.out.println("客户:"+strchoice);
qudongMoney=qudongFuction(strchoice);
System.out.println("价格为:"+qudongFuction(strchoice));
strfile=strfile+qudongstr+"客户:"+strchoice+'\n';
}else if(strchoice.equals("pingmu1") || strchoice.equals("pingmu2") || strchoice.equals("pingmu3")){
System.out.println("客户:"+strchoice);
PingmuSizeMoney=pingmuSizeFuction(strchoice);
System.out.println("价格为:"+pingmuSizeFuction(strchoice));
strfile=strfile+pingmuSizestr+"客户:"+strchoice+'\n';
}else if(strchoice.equals("choice1")){
System.out.println("客户:"+strchoice);
int total=totalMoney(cpuMoney,niecunMoney,yinpanMoney,qudongMoney,PingmuSizeMoney);
writer.println("本台电脑cpu费为:\n"+cpuMoney+"\n本台电脑niecun费为:\n"+niecunMoney+"\n本台电脑yinpan费为:\n"+yinpanMoney+"\n本台电脑qudong费为:\n"+qudongMoney+"\n本台电脑pingmu为:\n"+PingmuSizeMoney+"\n本台电脑总费为:\n"+total);
strfile=strfile+"客户:"+strchoice+'\n'+"本台电脑cpu费为:\n"+cpuMoney+"\n本台电脑niecun费为:\n"+niecunMoney+
"\n本台电脑yinpan费为:\n"+yinpanMoney+"\n本台电脑qudong费为:\n"+qudongMoney+
"\n本台电脑pingmu为:\n"+PingmuSizeMoney+"\n本台电脑总费为:\n"+total;
writer.flush();
}else if(strchoice.equals("choice2")){
System.out.println("是否确定:");
System.out.println("客户:"+strchoice);
   fw=new FileWriter(file);
   bw=new BufferedWriter(fw);
bw.write(strfile);
bw.newLine();
bw.flush();
writer.flush();
}else if(strchoice.substring(0,4).equals("name")){
System.out.println("客户姓名:");
System.out.println("客户:"+strchoice);
strfile=strfile+"客户姓名:"+strchoice+'\n';
}else if(strchoice.substring(0,4).equals("adre")){
System.out.println("收货地址:");
System.out.println("客户:"+strchoice);
strfile=strfile+"收货地址:"+strchoice+'\n';
}else if(strchoice.substring(0,5).equals("phone")){
System.out.println("手机号码:");
System.out.println("客户:"+strchoice);
strfile=strfile+"手机号码:"+strchoice+'\n';
}else if(strchoice.substring(0,5).equals("error")){
fw.close();
bw.close();
reader.close();
writer.close();
socket.close();
}
else{
System.out.println("客户:"+strchoice);
}
}
}catch(Exception e){
e.printStackTrace();
}
try{
if(reader!=null){
reader.close();
}
if(socket!=null){
socket.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server=new Server();
server.getServer();
}
}

实体类代码

package com.entity;
public class Fuction {
int cpuMoney,niecunMoney,yinpanMoney,qudongMoney,PingmuSizeMoney,totalMoney;
public int cpuFuction(int choice){
switch(choice){
case 1:
cpuMoney=1000;
break;
case 2:
cpuMoney=800;
break;
case 3:
cpuMoney=500;
break;
default:
break;
}
return cpuMoney;
}
public int niecunFuction(int choice){
switch(choice){
case 1:
niecunMoney=500;
break;
case 2:
niecunMoney=300;
break;
case 3:
niecunMoney=150;
break;
default:
break;
}
return niecunMoney;
}
public int yinpanFuction(int choice){
switch(choice){
case 1:
yinpanMoney=1000;
break;
case 2:
yinpanMoney=500;
break;
case 3:
yinpanMoney=300;
break;
default:
break;
}
return yinpanMoney;
}
public int qudongFuction(int choice){
switch(choice){
case 1:
qudongMoney=2000;
break;
case 2:
qudongMoney=1500;
break;
case 3:
qudongMoney=1000;
break;
default:
break;
}
return qudongMoney;
}
public int pingmuSizeFuction(int choice){
switch(choice){
case 1:
PingmuSizeMoney=1000;
break;
case 2:
PingmuSizeMoney=500;
break;
case 3:
PingmuSizeMoney=200;
break;
default:
break;
}
return PingmuSizeMoney;
}
public int totalMoney(int cpuMoney,int niecunMoney,int yinpanMoney,int qudongMoney,int PingmuSizeMoney){
return totalMoney=cpuMoney+niecunMoney+yinpanMoney+qudongMoney+PingmuSizeMoney;
}
}

 

package com.entity;
import java.util.Scanner;
public class Menu {
int totalMoney;
String cpustr,niecunstr,yinpanstr,qudongstr,pingmuSizestr;
Fuction fc=new Fuction();
Scanner input=new Scanner(System.in);
public void welcome(){
System.out.println("欢迎进入电脑选择系统");
cpustr="cpu选择: 1-高速,   2-中速,   3-低速";
niecunstr="内存选择: 1-8G,     2-4G,      3-2G";
yinpanstr="硬盘选择: 1-2T,  2-1T,   3-500G";
qudongstr="显卡驱动: 1-高级,   2-中级,   3-低级";
pingmuSizestr="屏幕大小: 1-16寸,   2-14寸,  3-12寸";
}
public void choice(){
System.out.println("cpustr");
int cpuchoice=input.nextInt();
int cpuM=fc.cpuFuction(cpuchoice);
System.out.println(niecunstr);
int niecunchoice=input.nextInt();
int niecunM=fc.niecunFuction(niecunchoice);
System.out.println(yinpanstr);
int yinpanchoice=input.nextInt();
int yinpanM=fc.yinpanFuction(yinpanchoice);
System.out.println(qudongstr);
int qudongchoice=input.nextInt();
int qudongM=fc.qudongFuction(qudongchoice);
System.out.println(pingmuSizestr);
int pingmuSizechoice=input.nextInt();
int pingmuSizeM=fc.pingmuSizeFuction(pingmuSizechoice);
    totalMoney=fc.totalMoney(cpuM, niecunM, yinpanM, qudongM, pingmuSizeM);
}
public void result(){
System.out.println("总共要花的钱为:"+totalMoney);
}
}

上一篇:CF1373F. Network Coverage


下一篇:python 测试覆盖率