首先创建一个Java新的项目:
我在这里命名为Java_Supermarket_management
在项目中创建data和images文件夹用于存储数据,在data文件夹中建立data1和data2的txt文本。
这些都可在idea中直接操作,无需进入文件资源管理器创建。
前期工作做好开始创建和调用相应的项目类(这里我就不去赘述各个类的作用会在代码标头注释中写明):
这里我在src中创建了3个包共20个类(包大家自行创建类我会用代码块展现核心部分)
核心仓库类
package Warehouse;
import java.util.ArrayList;
//仓库类
public class Warehouse {
private String name;//仓库存储商品名
private int max;//仓库存储上限
private int number=0;//某商品在仓库数量
ArrayList<String> jilu=new ArrayList();//仓库交易记录存储
public Warehouse(String name, int max) {
this.name = name;
this.max=max;
}
public synchronized void input(){//进货函数
while (this.number>=this.max) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
number++;//先入库在声明
this.jilu.add("ID账户-"+Thread.currentThread().getName()+"正入库第"+number+"件"+this.name+"..."+"入库完毕");//进行记录
System.out.println("ID账户-"+Thread.currentThread().getName()+"正入库第"+number+"件"+this.name+"..."+"入库完毕");
notify();
}
public synchronized void output(){//出库方法
while (number<=0) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
this.jilu.add("ID账户-"+Thread.currentThread().getName()+"正出库第"+number+"件"+this.name+"..."+"出库完毕");//进行记录
System.out.println("ID账户-"+Thread.currentThread().getName()+"正出库第"+number+"件"+this.name+"..."+"出库完毕");
number--;//先声明在出库
notify();
}
}
核心消费者类
package Warehouse;
//消费者类
public class User_xf extends Thread{
String id;//生产商线程
Warehouse warehouse;//处理商品仓库
public User_xf(String id) {
super(id);
this.id = id;
}
@Override
public void run() {
this.warehouse.output();
}
public void setWarehouse(Warehouse warehouse) {
this.warehouse = warehouse;
}
}
核心生产者类
package Warehouse;
//生产者类
public class User_sc extends Thread{
String id;//生产商线程
Warehouse warehouse;//处理商品仓库
public User_sc(String id) {
super(id);
this.id = id;
}
@Override
public void run() {
this.warehouse.input();
}
public void setWarehouse(Warehouse warehouse) {
this.warehouse = warehouse;
}
}
超市类
package Supermarket_management;
import Warehouse.Warehouse;
//超市类
public class Jay_Data {//四个仓库
static Warehouse ke=new Warehouse("可乐",10);
static Warehouse bing=new Warehouse("冰淇淋",10);
static Warehouse bi=new Warehouse("笔记本",10);
static Warehouse yi=new Warehouse("衣服",10);
}
消费者页面
package Supermarket_management;
import Warehouse.User_sc;
import Warehouse.User_xf;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Page_xf extends JFrame {
private JPanel contentPane;
private Jay_Data J;
private String id;//消费者id
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Page_xf frame = new Page_xf("",new Jay_Data());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Page_xf(String id,Jay_Data J) {
setTitle("Jay");
this.id=id;
this.J=J;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(400, 100, 500, 510);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new Page_bj());//布局管理
JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\未标题-1.jpg"));//这里把对应商品图片放images文件中的绝对地址
lblNewLabel_1.setBounds(100, 65, 100, 100);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("New label");
lblNewLabel_1_1.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\冰淇淋.jpg"));
lblNewLabel_1_1.setBounds(300, 65, 100, 100);
contentPane.add(lblNewLabel_1_1);
JButton btnNewButton = new JButton("可乐");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_xf u = new User_xf(id);
u.setWarehouse(J.ke);
u.start();
}
});
btnNewButton.setBounds(100, 200, 100, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("冰淇淋");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_xf u = new User_xf(id);
u.setWarehouse(J.bing);
u.start();
}
});
btnNewButton_1.setBounds(300, 200, 93, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("笔记本");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_xf u = new User_xf(id);
u.setWarehouse(J.bi);
u.start();
}
});
btnNewButton_2.setBounds(100, 400, 100, 23);
contentPane.add(btnNewButton_2);
JLabel lblNewLabel_3 = new JLabel("消费者页面");
lblNewLabel_3.setFont(new Font("幼圆", Font.PLAIN, 20));
lblNewLabel_3.setBounds(185, 20, 114, 26);
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_1_2 = new JLabel("New label");
lblNewLabel_1_2.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\苹果.jpg"));
lblNewLabel_1_2.setBounds(100, 260, 100, 100);
contentPane.add(lblNewLabel_1_2);
JLabel lblNewLabel_1_3 = new JLabel("New label");
lblNewLabel_1_3.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\衣服.jpg"));
lblNewLabel_1_3.setBounds(300, 260, 100, 100);
contentPane.add(lblNewLabel_1_3);
JButton btnNewButton_1_1 = new JButton("衣服");
btnNewButton_1_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_xf u = new User_xf(id);
u.setWarehouse(J.yi);
u.start();
}
});
btnNewButton_1_1.setBounds(300, 400, 93, 23);
contentPane.add(btnNewButton_1_1);
JLabel lblNewLabel = new JLabel("点击按钮进行购买——购买商品将会从对应仓库中取出");
lblNewLabel.setForeground(new Color(255, 0, 128));
lblNewLabel.setBounds(100, 450, 300, 20);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblNewLabel);
}
}
生产者界面
package Supermarket_management;
//生产者界面
import Warehouse.User_sc;
import Warehouse.User_xf;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Page_sc extends JFrame {
private JPanel contentPane;
private Jay_Data J;//服务仓库
private String id;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Page_sc frame = new Page_sc("",new Jay_Data());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Page_sc(String id,Jay_Data J) {
setTitle("Jay");
this.id=id;
this.J=J;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(400, 100, 500, 510);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new Page_bj());//布局管理
JLabel lblNewLabel_1 = new JLabel("New label");
lblNewLabel_1.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\未标题-1.jpg"));
lblNewLabel_1.setBounds(100, 65, 100, 100);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("New label");
lblNewLabel_1_1.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\冰淇淋.jpg"));
lblNewLabel_1_1.setBounds(300, 65, 100, 100);
contentPane.add(lblNewLabel_1_1);
JButton btnNewButton = new JButton("可乐");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_sc u = new User_sc(id);
u.setWarehouse(J.ke);
u.start();
}
});
btnNewButton.setBounds(100, 200, 100, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("冰淇淋");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_sc u = new User_sc(id);
u.setWarehouse(J.bing);
u.start();
}
});
btnNewButton_1.setBounds(300, 200, 93, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("笔记本");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_sc u = new User_sc(id);
u.setWarehouse(J.bi);
u.start();
}
});
btnNewButton_2.setBounds(100, 400, 100, 23);
contentPane.add(btnNewButton_2);
JLabel lblNewLabel_3 = new JLabel("生产商页面");
lblNewLabel_3.setFont(new Font("幼圆", Font.PLAIN, 20));
lblNewLabel_3.setBounds(185, 20, 114, 26);
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_1_2 = new JLabel("New label");
lblNewLabel_1_2.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\苹果.jpg"));
lblNewLabel_1_2.setBounds(100, 260, 100, 100);
contentPane.add(lblNewLabel_1_2);
JLabel lblNewLabel_1_3 = new JLabel("New label");
lblNewLabel_1_3.setBounds(300, 260, 100, 100);
lblNewLabel_1_3.setIcon(new ImageIcon("...\\Java_Supermarket_management\\images\\衣服.jpg"));
contentPane.add(lblNewLabel_1_3);
JButton btnNewButton_1_1 = new JButton("衣服");
btnNewButton_1_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
User_sc u = new User_sc(id);
u.setWarehouse(J.yi);
u.start();
}
});
btnNewButton_1_1.setBounds(300, 400, 93, 23);
contentPane.add(btnNewButton_1_1);
JLabel lblNewLabel = new JLabel("点击按钮进行售卖——售卖商品将会存储在对应仓库");
lblNewLabel.setForeground(new Color(255, 0, 128));
lblNewLabel.setBounds(100, 450, 300, 20);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblNewLabel);
}
}
核心用户类
package Swing1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
//对象临时信息数据库处理类
public class User_Libraryx {
//存储文件夹通过对文件夹的使用调换实现对其中信息的修改
private static File file1=new File("data\\data1.txt");//id号、密码库文件
private static File file2=new File("data\\data2.txt");
private File file;//指定标准更新的信息数据库
public File getFile() {//保证更新数据库只对一个线程开放访问
this.lookFile();//在这之前确定更新数据库为哪一个
return this.file;
}
//相关字段
public static String T_ID_T_KAY="T_ID_T_KAY";
public static String T_ID_F_KAY="T_ID_F_KAY";
public static String F_ID="F_ID";
public void lookFile() {//找寻最近一次更新的文件,并将其赋值给file
if(file1.lastModified()-file2.lastModified()>=0) {
file=file1;
}else {
file=file2;
}
}
public File lookNfile() {
//寻找替换文件
File file=null;
if(file1.lastModified()-file2.lastModified()>=0) {
file=this.file2;
}else {
file=this.file1;
}
return file;
}
//存储操作将对象存储到文件
public void output(String id,String kay) {//初始化存储
FileOutputStream fos=null;//创建文件字节写入流
OutputStreamWriter osw=null;//创建字符到字节转义流
BufferedWriter bw=null;//创建缓冲区
try {
fos=new FileOutputStream(this.getFile(),true);
osw=new OutputStreamWriter(fos,"UTF-8");//以UTF-8方式转义
bw=new BufferedWriter(osw);
bw.write(this.s_u(new User(id,kay))+"\n");
bw.flush();
}catch(Exception e) {
e.printStackTrace();
}finally{
if(bw!=null) {
try {
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(osw!=null) {
try {
osw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
public boolean upData(User user) {//用于对账号信息的更改
//参数为更改后用户对象
boolean bool=false;
if(this.lookForId(user.getId())!=null) {
//存在该账号
//创建最近更新文件的读入流
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
//创建更新替换文件的写入流
FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
try {
fis=new FileInputStream(this.getFile());
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
fos=new FileOutputStream(this.lookNfile());
osw=new OutputStreamWriter(fos);
bw=new BufferedWriter(osw);
String s=null;
while((s=br.readLine())!=null) {
if(this.u_s(s).getId().equals(user.getId())) {
bw.write(this.s_u(user)+"\n");
}else {
bw.write(s+"\n");
}
bw.flush();
}
bool=true;
}catch(Exception e){
e.printStackTrace();
}finally {
if(br!=null) {
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(isr!=null) {
try {
isr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(bw!=null) {
try {
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(osw!=null) {
try {
osw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
return bool;
}
public boolean deleteData(User user) {//用于对账号信息的删除
//参数为更改后用户对象
boolean bool=false;
if(this.lookForId(user.getId())!=null) {
//存在该账号
//创建最近更新文件的读入流
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
//创建更新替换文件的写入流
FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
try {
fis=new FileInputStream(this.getFile());
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
fos=new FileOutputStream(this.lookNfile());
osw=new OutputStreamWriter(fos);
bw=new BufferedWriter(osw);
String s=null;
while((s=br.readLine())!=null) {
if(this.u_s(s).getId().equals(user.getId())!=true) {
bw.write(s+"\n");
}
bw.flush();
}
bool=true;
}catch(Exception e){
e.printStackTrace();
}finally {
if(br!=null) {
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(isr!=null) {
try {
isr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(bw!=null) {
try {
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(osw!=null) {
try {
osw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
return bool;
}
public User lookForId(String id) {//根据给出id在文件中寻找是否存在该用户
User user=null;
FileInputStream fis=null;//创建文件字节读入流
InputStreamReader isr=null;//创建字符到字节转义流
BufferedReader br=null;//创建缓冲区
try {
fis=new FileInputStream(this.getFile());
isr=new InputStreamReader(fis,"UTF-8");//以UTF-8方式转义
br=new BufferedReader(isr);
String s=null;
while((s=br.readLine())!=null) {
try {
if(this.u_s(s).getId().equals(id)) {
user=this.u_s(s);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(br!=null) {
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(isr!=null) {
try {
isr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
return user;
}
public User lookForIdKay(String id,String kay) {//根据给出id和kay在文件中寻找是否存在该用户
User user=null;
FileInputStream fis=null;//创建文件字节读入流
InputStreamReader isr=null;//创建字符到字节转义流
BufferedReader br=null;//创建缓冲区
try {
fis=new FileInputStream(this.getFile());
isr=new InputStreamReader(fis,"UTF-8");//以UTF-8方式转义
br=new BufferedReader(isr);
String s=null;
while((s=br.readLine())!=null) {
try {
if(this.u_s(s).getId().equals(id)&&this.u_s(s).getKay().equals(kay)) {
user=this.u_s(s);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(br!=null) {
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(isr!=null) {
try {
isr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
return user;
}
public String equUser(String id,String kay) {//登录比较判断,id正确kay错误返回T_ID_F_KAY,id正确kay正确返回T_ID_T_KAY,id错误返回F_ID
String s=null;
if(this.lookForIdKay(id,kay)!=null) {
s="T_ID_T_KAY";
}else if((this.lookForId(id)!=null)&&(this.lookForIdKay(id,kay)==null)) {
s="T_ID_F_KAY";
}else if(this.lookForId(id)==null) {
s="F_ID";
}
return s;
}
public static void main(String[] args) {//测试类
// TODO Auto-generated method stub
User_Libraryx u=new User_Libraryx();
User user=new User("叶湘伦","周杰伦");
System.out.println(u.upData(user));
}
public User u_s(String s) {//根据指定字符串信息解析成一个对象
//创建一个user对象
User user=new User();
//对属性逐个查看赋值
ArrayList<String> a=user.getArrayList();//获取属性容器
for(int i=0,j=1;i<a.size()-1;i++,j++) {
if(s.indexOf("@"+a.get(i)+":")==-1) {
new Exception("字符串解析异常");
}
user.setAttribute(a.get(i),s.substring(s.indexOf("@"+a.get(i)+":")+a.get(i).length()+2,s.indexOf("@"+a.get(j)+":")));
}
return user;
}
public String s_u(User user) {//根据对象信息将一个对象转换指定字符串
StringBuffer sb=new StringBuffer("");
for(String string:user.getArrayList()) {
sb.append("@"+string+":"+user.getHashMap().get(string));
}
return sb.toString();
}
}
效果展示
其他类我就不一一展示,我把项目文件放在下面,需要自行下载
注明:本系统因为不涉及数据库,相当于单机游戏,只适用于在本机或联机操作!