java宠物商店项目代码,我需要若干个宠物的类如Cat,Dog用来输入宠物的颜色,年龄,名字和出售价格。还需要一个Pet的类用来修改宠物信息并且拥有删除功能,要有一个PetShop的类用来查看已有的宠物。还需要用java swing来实现用户界面,这个界面中用下拉列表以选择不同的宠物类后会跳转相应到的界面和PetShop类的查询功能。在跳转的界面中可以实现的类的功能和Pet类的功能
时间: 2024-03-19 18:43:44 浏览: 50
好的,我会根据您的要求提供一个简单的宠物商店项目代码,其中包括宠物类,Pet类,PetShop类和用户界面。但是,需要注意的是,这个代码只是一个简单的示例,需要根据实际需求进行修改和完善。
首先是宠物类,包括Cat和Dog类:
```java
public class Cat {
private String color;
private int age;
private String name;
private double price;
public Cat(String color, int age, String name, double price) {
this.color = color;
this.age = age;
this.name = name;
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class Dog {
private String color;
private int age;
private String name;
private double price;
public Dog(String color, int age, String name, double price) {
this.color = color;
this.age = age;
this.name = name;
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
接下来是Pet类,用来修改宠物信息和实现删除功能:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Pet {
private List<Cat> cats = new ArrayList<>();
private List<Dog> dogs = new ArrayList<>();
public void addCat(Cat cat) {
cats.add(cat);
}
public void addDog(Dog dog) {
dogs.add(dog);
}
public void deleteCat(String name) {
Iterator<Cat> iterator = cats.iterator();
while (iterator.hasNext()) {
Cat cat = iterator.next();
if (cat.getName().equals(name)) {
iterator.remove();
break;
}
}
}
public void deleteDog(String name) {
Iterator<Dog> iterator = dogs.iterator();
while (iterator.hasNext()) {
Dog dog = iterator.next();
if (dog.getName().equals(name)) {
iterator.remove();
break;
}
}
}
public void modifyCat(String name, String color, int age, double price) {
for (Cat cat : cats) {
if (cat.getName().equals(name)) {
cat.setColor(color);
cat.setAge(age);
cat.setPrice(price);
break;
}
}
}
public void modifyDog(String name, String color, int age, double price) {
for (Dog dog : dogs) {
if (dog.getName().equals(name)) {
dog.setColor(color);
dog.setAge(age);
dog.setPrice(price);
break;
}
}
}
public List<Cat> getCats() {
return cats;
}
public List<Dog> getDogs() {
return dogs;
}
}
```
然后是PetShop类,用来查看已有的宠物:
```java
import java.util.List;
public class PetShop {
private Pet pet = new Pet();
public PetShop() {
// 初始化宠物数据
pet.addCat(new Cat("黄色", 1, "小黄", 100));
pet.addCat(new Cat("白色", 2, "小白", 200));
pet.addDog(new Dog("黑色", 3, "小黑", 300));
pet.addDog(new Dog("棕色", 4, "小棕", 400));
}
public List<Cat> getCats() {
return pet.getCats();
}
public List<Dog> getDogs() {
return pet.getDogs();
}
}
```
最后是用户界面,使用了Java Swing:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class PetStoreGUI {
private PetShop petShop = new PetShop();
private JFrame frame = new JFrame("宠物商店");
private JComboBox<String> petComboBox = new JComboBox<>();
private JTextArea petTextArea = new JTextArea();
private JLabel nameLabel = new JLabel("名字:");
private JTextField nameField = new JTextField();
private JLabel colorLabel = new JLabel("颜色:");
private JTextField colorField = new JTextField();
private JLabel ageLabel = new JLabel("年龄:");
private JTextField ageField = new JTextField();
private JLabel priceLabel = new JLabel("价格:");
private JTextField priceField = new JTextField();
private JButton addButton = new JButton("添加");
private JButton modifyButton = new JButton("修改");
private JButton deleteButton = new JButton("删除");
public PetStoreGUI() {
// 设置宠物下拉列表
petComboBox.addItem("猫");
petComboBox.addItem("狗");
// 设置宠物信息文本框
petTextArea.setEditable(false);
petTextArea.setPreferredSize(new Dimension(400, 200));
// 设置添加、修改、删除按钮
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String color = colorField.getText();
int age = Integer.parseInt(ageField.getText());
double price = Double.parseDouble(priceField.getText());
if (petComboBox.getSelectedItem().equals("猫")) {
petShop.getCats().add(new Cat(color, age, name, price));
} else {
petShop.getDogs().add(new Dog(color, age, name, price));
}
updatePetTextArea();
}
});
modifyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String color = colorField.getText();
int age = Integer.parseInt(ageField.getText());
double price = Double.parseDouble(priceField.getText());
if (petComboBox.getSelectedItem().equals("猫")) {
petShop.modifyCat(name, color, age, price);
} else {
petShop.modifyDog(name, color, age, price);
}
updatePetTextArea();
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
if (petComboBox.getSelectedItem().equals("猫")) {
petShop.deleteCat(name);
} else {
petShop.deleteDog(name);
}
updatePetTextArea();
}
});
// 设置界面布局
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("请选择宠物:"));
topPanel.add(petComboBox);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(5, 2));
leftPanel.add(nameLabel);
leftPanel.add(nameField);
leftPanel.add(colorLabel);
leftPanel.add(colorField);
leftPanel.add(ageLabel);
leftPanel.add(ageField);
leftPanel.add(priceLabel);
leftPanel.add(priceField);
leftPanel.add(addButton);
leftPanel.add(modifyButton);
leftPanel.add(deleteButton);
JPanel rightPanel = new JPanel();
rightPanel.add(petTextArea);
frame.setLayout(new BorderLayout());
frame.add(topPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(rightPanel, BorderLayout.CENTER);
// 更新宠物信息文本框
updatePetTextArea();
}
public void show() {
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void updatePetTextArea() {
StringBuilder sb = new StringBuilder();
sb.append("猫:\n");
List<Cat> cats = petShop.getCats();
for (Cat cat : cats) {
sb.append(cat.getName() + "," + cat.getColor() + "," + cat.getAge() + "岁,售价:" + cat.getPrice() + "\n");
}
sb.append("\n狗:\n");
List<Dog> dogs = petShop.getDogs();
for (Dog dog : dogs) {
sb.append(dog.getName() + "," + dog.getColor() + "," + dog.getAge() + "岁,售价:" + dog.getPrice() + "\n");
}
petTextArea.setText(sb.toString());
}
public static void main(String[] args) {
PetStoreGUI gui = new PetStoreGUI();
gui.show();
}
}
```
这个代码实现了您提出的要求,并且使用了Java Swing来实现用户界面。希望对您有所帮助。
阅读全文