Eclipse+Java+Swing实现宠物商店管理系统

举报
水坚石青 发表于 2021/10/29 22:43:36 2021/10/29
【摘要】 Java+Swing实现宠物商店 一、系统介绍二、系统展示1.主界面2.增加宠物3.删除宠物4.修改宠物5.查询宠物6.模块查询 三、系统实现Cat.javaDog.javaMouse.jav...

一、系统介绍

本系统通过文件系统实现实现宠物的增删改查。

二、系统展示

1.主界面

在这里插入图片描述

2.增加宠物

在这里插入图片描述

3.删除宠物

在这里插入图片描述

4.修改宠物

在这里插入图片描述

5.查询宠物

在这里插入图片描述

6.模块查询

在这里插入图片描述

三、系统实现

Cat.java

package com.sjsq;

public class Cat implements Pet {
	String age;
	String name;
	String price;
	String color;
	String pic_routh;
	static String kind = "猫";

	Cat(String name, String age, String color, String price, String routh) {
		this.name = name;
		this.age = age;
		this.color = color;
		this.price = price;
		this.pic_routh = routh;
	}

	public String getKind() {
		return kind;
	}

	public String getName() {
		return name;
	}

	public String getColor() {
		return color;
	}

	public String getAge() {
		return age;
	}

	public String getPrice() {
		return price;
	}

	public String getPic_routh() {
		return pic_routh;
	}

	public void Change(Pet pet) {
		if (!pet.getName().equals(""))
			name = pet.getName();
		if (!pet.getAge().equals(""))
			age = pet.getAge();
		if (!pet.getColor().equals(""))
			color = pet.getColor();
		if (!pet.getPrice().equals(""))
			price = pet.getPrice();
		if (!pet.getPic_routh().equals(""))
			pic_routh = pet.getPic_routh();
	}
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

Dog.java

package com.sjsq;

public class Dog implements Pet {
	String age;
	String name;
	String price;
	String color;
	String pic_routh;
	static String kind = "狗";

	Dog(String name, String age, String color, String price, String routh) {
		this.name = name;
		this.color = color;
		this.age = age;
		this.price = price;
		this.pic_routh = routh;
	}

	public String getKind() {
		return kind;
	}

	public String getName() {
		return name;
	}

	public String getColor() {
		return color;
	}

	public String getAge() {
		return age;
	}

	public String getPrice() {
		return price;
	}

	public String getPic_routh() {
		return pic_routh;
	}

	public void Change(Pet pet) {
		if (!pet.getName().equals(""))
			name = pet.getName();
		if (!pet.getAge().equals(""))
			age = pet.getAge();
		if (!pet.getColor().equals(""))
			color = pet.getColor();
		if (!pet.getPrice().equals(""))
			price = pet.getPrice();
		if (!pet.getPic_routh().equals(""))
			pic_routh = pet.getPic_routh();
	}
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

Mouse.java

package com.sjsq;

public class Mouse implements Pet {
	String age;
	String name;
	String price;
	String color;
	String pic_routh;
	static String kind = "鼠";

	Mouse(String name, String age, String color, String price, String routh) {
		this.name = name;
		this.color = color;
		this.age = age;
		this.price = price;
		this.pic_routh = routh;
	}

	public String getKind() {
		return kind;
	}

	public String getName() {
		return name;
	}

	public String getColor() {
		return color;
	}

	public String getAge() {
		return age;
	}

	public String getPrice() {
		return price;
	}

	public String getPic_routh() {
		return pic_routh;
	}

	public void Change(Pet pet) {
		if (!pet.getName().equals(""))
			name = pet.getName();
		if (!pet.getAge().equals(""))
			age = pet.getAge();
		if (!pet.getColor().equals(""))
			color = pet.getColor();
		if (!pet.getPrice().equals(""))
			price = pet.getPrice();
		if (!pet.getPic_routh().equals(""))
			pic_routh = pet.getPic_routh();
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

MainScreen.java

package com.sjsq;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.io.*;

public class MainScreen extends JFrame {
	PetShop pet_shop;
	JButton dog, cat, mouse, otherpets;
	// JLabel dog_lab,cat_lab,mouse_lab,otherpets_lab;
	ImageIcon dog_icon, cat_icon, mouse_icon, otherpets_icon;
	JButton Add, Delete, Change, Search;
	Container con = getContentPane();
	FileReader filereader = null;
	BufferedReader bufferedreader = null;
	FileWriter fileWritter = null;
	PetInput pet_input;
	PetInterface petinterface;

	public MainScreen() {

		super("宠物商店");
		setLayout(null);
		setSize(650, 450);
		setVisible(true);
		//setLocation(450, 50);
		setLocationRelativeTo(null);
		pet_shop = new PetShop(0);
		dog = new JButton("狗");
		cat = new JButton("猫");
		mouse = new JButton("鼠");
		otherpets = new JButton("其他");
		Add = new JButton("增加宠物");
		Delete = new JButton("删除宠物");
		Change = new JButton("更改宠物");
		Search = new JButton("查找宠物");
		dog.setBounds(120, 120, 100, 30);
		con.add(dog);
		cat.setBounds(120, 160, 100, 30);
		con.add(cat);
		mouse.setBounds(120, 200, 100, 30);
		con.add(mouse);
		otherpets.setBounds(120, 240, 100, 30);
		con.add(otherpets);
		Add.setBounds(420, 120, 100, 30);
		con.add(Add);
		Delete.setBounds(420, 160, 100, 30);
		con.add(Delete);
		Change.setBounds(420, 200, 100, 30);
		con.add(Change);
		Search.setBounds(420, 240, 100, 30);
		con.add(Search);

		ImageIcon icon = new ImageIcon("src/PictureSource/主界面背景图.jpg");
		JLabel picture = new JLabel(icon);
		con.add(picture);
		picture.setBounds(0, 0, 700, 400);
		init_button_listener();
		file_read();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	class ImagePanel extends JPanel {
		private Image backgroundImage;

		ImagePanel() {
			backgroundImage = new ImageIcon("src/PictureSource/主界面背景图.jpg").getImage();
		}

		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);

			if (backgroundImage != null) {
				g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
			}
		}
	}

	void file_read() {
		try {
			filereader = new FileReader("src/Pet information.txt");
			bufferedreader = new BufferedReader(filereader);
			String s = null;
			Pet p;
			while ((s = bufferedreader.readLine()) != null) {
				String[] ss = new String[6];
				ss = s.split(" ");
				if (ss[0].equals("狗"))
					p = new Dog(ss[1], ss[2], ss[3], ss[4], ss[5]);
				else if (ss[0].equals("猫"))
					p = new Cat(ss[1], ss[2], ss[3], ss[4], ss[5]);
				else if (ss[0].equals("鼠"))
					p = new Mouse(ss[1], ss[2], ss[3], ss[4], ss[5]);
				else
					p = new OtherPets(ss[1], ss[2], ss[3], ss[4], ss[5]);
				pet_shop.add(p);
			}
			bufferedreader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void init_button_listener() {
		dog.addMouseListener(new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
				int[] pos = new int[110];
				int num = 0;
				Pet[] p = pet_shop.pets;
				for (int i = 0; i < pet_shop.num; i++)
					if (p[i].getKind().equals("狗"))
						pos[num++] = i;
				petinterface = new PetInterface(MainScreen.this, pos, num, p);
				petinterface.setVisible(true);
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
		cat.addMouseListener(new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
				int[] pos = new int[110];
				int num = 0;
				Pet[] p = pet_shop.pets;
				for (int i = 0; i < pet_shop.num; i++)
					if (p[i].getKind().equals("猫"))
						pos[num++] = i;
				petinterface = new PetInterface(MainScreen.this, pos, num, p);
				petinterface.setVisible(true);
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
		mouse.addMouseListener(new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
				int[] pos = new int[110];
				int num = 0;
				Pet[] p = pet_shop.pets;
				for (int i = 0; i < pet_shop.num; i++)
					if (p[i].getKind().equals("鼠"))
						pos[num++] = i;
				petinterface = new PetInterface(MainScreen.this, pos, num, p);
				petinterface.setVisible(true);
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
		otherpets.addMouseListener(new MouseListener() {
			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
				int[] pos = new int[110];
				int num = 0;
				Pet[] p = pet_shop.pets;
				for (int i = 0; i < pet_shop.num; i++)
					if (p[i].getKind().equals("其他"))
						pos[num++] = i;
				petinterface = new PetInterface(MainScreen.this, pos, num, p);
				petinterface.setVisible(true);
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
		Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pet_input = new PetInput(MainScreen.this, true);
				pet_input.setVisible(true);
				if (pet_shop.add(pet_input)) {
					try {
						fileWritter = new FileWriter("src/Pet information.txt");
						Pet[] p = pet_shop.pets;
						for (int i = 0; i < pet_shop.num; i++) {
							fileWritter.write(p[i].getKind() + " ");
							fileWritter.write(p[i].getName() + " ");
							fileWritter.write(p[i].getAge() + " ");
							fileWritter.write(p[i].getColor() + " ");
							fileWritter.write(p[i].getPrice() + " ");
							fileWritter.write(p[i].getPic_routh() + "\r\n");
						}
						fileWritter.close();
					} catch (Exception event) {
						event.printStackTrace();
					}
				}

			}
		});
		Delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (pet_shop.delete()) {
					try {
						fileWritter = new FileWriter("src/Pet information.txt");
						Pet[] p = pet_shop.pets;
						for (int i = 0; i < pet_shop.num; i++) {
							fileWritter.write(p[i].getKind() + " ");
							fileWritter.write(p[i].getName() + " ");
							fileWritter.write(p[i].getAge() + " ");
							fileWritter.write(p[i].getColor() + " ");
							fileWritter.write(p[i].getPrice() + " ");
							fileWritter.write(p[i].getPic_routh() + "\r\n");
						}
						fileWritter.close();
					} catch (Exception event) {
						event.printStackTrace();
					}
				}
			}
		});
		Change.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int pos = pet_shop.change();
				if (pos != -1) {
					pet_input = new PetInput(MainScreen.this, true);
					pet_input.setVisible(true);
					if (pet_input.flag == 1) {
						pet_shop.pets[pos].Change(pet_input.pet);
						JOptionPane.showMessageDialog(null, "宠物修改成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
						try {
							fileWritter = new FileWriter("src/Pet information.txt");
							Pet[] p = pet_shop.pets;
							for (int i = 0; i < pet_shop.num; i++) {
								fileWritter.write(p[i].getKind() + " ");
								fileWritter.write(p[i].getName() + " ");
								fileWritter.write(p[i].getAge() + " ");
								fileWritter.write(p[i].getColor() + " ");
								fileWritter.write(p[i].getPrice() + " ");
								fileWritter.write(p[i].getPic_routh() + "\r\n");
							}
							fileWritter.close();
						} catch (Exception event) {
							event.printStackTrace();
						}
					} else
						JOptionPane.showMessageDialog(null, "宠物修改失败!", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		Search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] pos = new int[110];
				int num = pet_shop.search(pos);
				Pet[] p = pet_shop.pets;
				if (num > 0) {
					petinterface = new PetInterface(MainScreen.this, pos, num, p);
					petinterface.setVisible(true);
				}
			}
		});
	}

	public static void main(String[] args) {
		new MainScreen();
	}
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303

PetShop.java

package com.sjsq;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PetShop {
	Pet[] pets;
	int num;
	PetInput pet_input;

	PetShop(int len) {
		num = len;
		pets = new Pet[110];
	}

	void add(Pet pet) {
		pets[num++] = pet;
	}

	boolean add(PetInput pet_input) {
		if (pet_input.flag == 1) {
			pets[num++] = pet_input.pet;
			JOptionPane.showMessageDialog(null, "宠物添加成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return true;
		} else {
			JOptionPane.showMessageDialog(null, "宠物添加失败!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
	}

	boolean delete() {
		String keyWord = JOptionPane.showInputDialog(null, "请输入要删除的宠物信息:", "名字");
		int flag = -1;
		for (int i = 0; i < num; i++)
			if (pets[i].getName().equals(keyWord)) {
				flag = i;
				break;
			}
		if (flag == -1) {
			JOptionPane.showMessageDialog(null, "未查找到该宠物!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return false;
		} else {
			for (int i = flag; i < num - 1; i++)
				pets[i] = pets[i + 1];
			num--;
			JOptionPane.showMessageDialog(null, "已成功删除该宠物!", "提示", JOptionPane.INFORMATION_MESSAGE);
			return true;
		}
	}

	int change() {
		String keyWord = JOptionPane.showInputDialog(null, "请输入要更改的宠物信息:", "名字");
		int pos = -1;
		for (int i = 0; i < num; i++)
			if (pets[i].getName().equals(keyWord)) {
				pos = i;
				break;
			}
		if (pos == -1) {
			JOptionPane.showMessageDialog(null, "未查找到该宠物!", "提示", JOptionPane.INFORMATION_MESSAGE);
			JOptionPane.showMessageDialog(null, "宠物修改失败!", "提示", JOptionPane.INFORMATION_MESSAGE);
		}
		return pos;
	}

	int search(int[] pos) {
		String keyWord = JOptionPane.showInputDialog(null, "请输入要查找的宠物信息:", "名字");
		int n = 0;
		for (int i = 0; i < num; i++) {
			System.out.println(pets[i].getName());
			if (pets[i].getName().equals(keyWord))
				pos[n++] = i;
		}
		if (n == 0)
			JOptionPane.showMessageDialog(null, "未查找到该宠物!", "提示", JOptionPane.INFORMATION_MESSAGE);
		return n;
	}
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

四、其他

1.其他系统实现

JavaWeb系统系列实现

Java+JSP实现学生图书管理系统
Java+JSP实现学生信息管理系统
Java+Servlet+JSP实现学生成绩管理系统
Java+Servlet+JSP实现宠物诊所管理系统
Java+SSM+Easyui实现网上考试系统
Java+Springboot+H-ui实现营销管理系统
Java+Springboot+Mybatis+Bootstrap实现网上商城系统

JavaSwing系统系列实现

Java+Swing实现斗地主游戏
Java+Swing实现图书管理系统
Java+Swing实现医院管理系统
Java+Swing实现仓库管理系统
Java+Swing实现考试管理系统
Java+Swing实现通讯录管理系统
Java+Swing实现学生信息管理系统
Java+Swing实现学生宿舍管理系统
Java+Swing实现学生选课管理系统
Java+Swing实现学生成绩管理系统
Java+Swing实现学校教材管理系统
Java+Swing实现学校教务管理系统
Java+Swing实现企业人事管理系统
Java+Swing实现电子相册管理系统
Java+Swing实现自助取款机(ATM)系统
Java+Swing实现超市管理系统-TXT存储信息
Java+Swing实现宠物商店管理系统-TXT存储信息

2.获取源码

点击以下链接获取源码
Java+Swing+Txt实现宠物管理系统

3.备注

如有侵权请联系我删除。

文章来源: blog.csdn.net,作者:水坚石青,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/helongqiang/article/details/109500916

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。