java宠物商店项目代码,我需要若干个宠物的类如Cat,Dog用来输入宠物的颜色,年龄,名字和出售价格。还需要一个Pet的类用来修改宠物信息并且拥有删除功能,要有一个PetShop的类用来查看已有的宠物。还需要用java swing来实现用户界面,这个界面中用下拉列表以选择不同的宠物类后会跳转相应到的界面和PetShop类的查询功能。在跳转的界面中可以实现的类的功能

时间: 2024-03-19 19:45:32 浏览: 27
好的,根据您的需求,我为您编写了一个简单的Java宠物商店项目代码,包含Cat、Dog、Pet和PetShop类,并使用了Java Swing实现了用户界面。以下是代码: Cat类: ```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; } } ``` Dog类: ```java 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; public class Pet { private ArrayList<Cat> cats; private ArrayList<Dog> dogs; public Pet() { this.cats = new ArrayList<>(); this.dogs = new ArrayList<>(); } public ArrayList<Cat> getCats() { return cats; } public ArrayList<Dog> getDogs() { return dogs; } public void addCat(Cat cat) { cats.add(cat); } public void addDog(Dog dog) { dogs.add(dog); } public void removeCat(int index) { cats.remove(index); } public void removeDog(int index) { dogs.remove(index); } public void updateCat(int index, String color, int age, String name, double price) { Cat cat = cats.get(index); cat.setColor(color); cat.setAge(age); cat.setName(name); cat.setPrice(price); } public void updateDog(int index, String color, int age, String name, double price) { Dog dog = dogs.get(index); dog.setColor(color); dog.setAge(age); dog.setName(name); dog.setPrice(price); } } ``` PetShop类: ```java import java.util.ArrayList; public class PetShop { private Pet pet; public PetShop() { this.pet = new Pet(); } public ArrayList<Cat> getCats() { return pet.getCats(); } public ArrayList<Dog> getDogs() { return pet.getDogs(); } public void addCat(Cat cat) { pet.addCat(cat); } public void addDog(Dog dog) { pet.addDog(dog); } public void removeCat(int index) { pet.removeCat(index); } public void removeDog(int index) { pet.removeDog(index); } public void updateCat(int index, String color, int age, String name, double price) { pet.updateCat(index, color, age, name, price); } public void updateDog(int index, String color, int age, String name, double price) { pet.updateDog(index, color, age, name, price); } } ``` PetShopUI类(用户界面): ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; public class PetShopUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JComboBox<String> petComboBox; private JLabel nameLabel, colorLabel, ageLabel, priceLabel; private JTextField nameField, colorField, ageField, priceField; private JButton addButton, removeButton, updateButton, queryButton; private JList<String> petList; private DefaultListModel<String> listModel; private PetShop petShop; public PetShopUI() { petShop = new PetShop(); setTitle("Pet Shop"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(new GridLayout(6, 2, 5, 5)); petComboBox = new JComboBox<>(new String[]{"Cat", "Dog"}); petComboBox.addActionListener(this); mainPanel.add(new JLabel("Pet Type:")); mainPanel.add(petComboBox); nameLabel = new JLabel("Name:"); mainPanel.add(nameLabel); nameField = new JTextField(); mainPanel.add(nameField); colorLabel = new JLabel("Color:"); mainPanel.add(colorLabel); colorField = new JTextField(); mainPanel.add(colorField); ageLabel = new JLabel("Age:"); mainPanel.add(ageLabel); ageField = new JTextField(); mainPanel.add(ageField); priceLabel = new JLabel("Price:"); mainPanel.add(priceLabel); priceField = new JTextField(); mainPanel.add(priceField); addButton = new JButton("Add"); addButton.addActionListener(this); mainPanel.add(addButton); removeButton = new JButton("Remove"); removeButton.addActionListener(this); mainPanel.add(removeButton); updateButton = new JButton("Update"); updateButton.addActionListener(this); mainPanel.add(updateButton); queryButton = new JButton("Query"); queryButton.addActionListener(this); mainPanel.add(queryButton); listModel = new DefaultListModel<>(); petList = new JList<>(listModel); JScrollPane scrollPane = new JScrollPane(petList); add(mainPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { if (e.getSource() == petComboBox) { String petType = (String) petComboBox.getSelectedItem(); if (petType.equals("Cat")) { nameLabel.setText("Cat Name:"); } else { nameLabel.setText("Dog Name:"); } } else if (e.getSource() == addButton) { String petType = (String) petComboBox.getSelectedItem(); String name = nameField.getText(); String color = colorField.getText(); String ageStr = ageField.getText(); String priceStr = priceField.getText(); if (name.isEmpty() || color.isEmpty() || ageStr.isEmpty() || priceStr.isEmpty()) { JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE); return; } int age = Integer.parseInt(ageStr); double price = Double.parseDouble(priceStr); if (petType.equals("Cat")) { Cat cat = new Cat(color, age, name, price); petShop.addCat(cat); } else { Dog dog = new Dog(color, age, name, price); petShop.addDog(dog); } listModel.clear(); ArrayList<Cat> cats = petShop.getCats(); for (Cat cat : cats) { listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice()); } ArrayList<Dog> dogs = petShop.getDogs(); for (Dog dog : dogs) { listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice()); } } else if (e.getSource() == removeButton) { int index = petList.getSelectedIndex(); if (index == -1) { JOptionPane.showMessageDialog(this, "Please select a pet to remove", "Error", JOptionPane.ERROR_MESSAGE); return; } String selectedPet = petList.getSelectedValue(); if (selectedPet.startsWith("Cat")) { petShop.removeCat(index); } else { petShop.removeDog(index); } listModel.remove(index); } else if (e.getSource() == updateButton) { int index = petList.getSelectedIndex(); if (index == -1) { JOptionPane.showMessageDialog(this, "Please select a pet to update", "Error", JOptionPane.ERROR_MESSAGE); return; } String petType = (String) petComboBox.getSelectedItem(); String name = nameField.getText(); String color = colorField.getText(); String ageStr = ageField.getText(); String priceStr = priceField.getText(); if (name.isEmpty() || color.isEmpty() || ageStr.isEmpty() || priceStr.isEmpty()) { JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE); return; } int age = Integer.parseInt(ageStr); double price = Double.parseDouble(priceStr); if (petType.equals("Cat")) { petShop.updateCat(index, color, age, name, price); } else { petShop.updateDog(index, color, age, name, price); } listModel.clear(); ArrayList<Cat> cats = petShop.getCats(); for (Cat cat : cats) { listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice()); } ArrayList<Dog> dogs = petShop.getDogs(); for (Dog dog : dogs) { listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice()); } } else if (e.getSource() == queryButton) { listModel.clear(); ArrayList<Cat> cats = petShop.getCats(); for (Cat cat : cats) { listModel.addElement("Cat: " + cat.getName() + " " + cat.getColor() + " " + cat.getAge() + " " + cat.getPrice()); } ArrayList<Dog> dogs = petShop.getDogs(); for (Dog dog : dogs) { listModel.addElement("Dog: " + dog.getName() + " " + dog.getColor() + " " + dog.getAge() + " " + dog.getPrice()); } } } public static void main(String[] args) { PetShopUI petShopUI = new PetShopUI(); petShopUI.setVisible(true); } } ``` 在这个用户界面中,您可以选择宠物类型,输入宠物的属性,使用按钮进行添加、删除和更新,以及查询已有宠物。当您添加、删除或更新宠物时,界面中的宠物列表将自动更新。

相关推荐

最新推荐

recommend-type

JAVA抽象类和抽象方法(abstract)实例分析

JAVA抽象类和抽象方法实例分析 JAVA抽象类和抽象方法是Java面向对象程序设计中非常重要的概念,它们是Java编程语言的基础组成部分。抽象类和抽象方法可以帮助开发者更好地组织和设计程序架构,提高代码的可读性和...
recommend-type

twincat添加路由和远程桌面

twincat添加路由和远程桌面的步骤和问题处理,对于添加路由不能通讯不能广播添加报错等可能出现的所有问题进行图文解说。对于实际工程中的应用进行分析和比较。
recommend-type

html+css购物网页设计.zip 点击右上角按钮可实现页面跳转,

html+css购物网页设计.zip 点击右上角按钮可实现页面跳转,及点击“今日推荐”里的图片可直接跳转到该官网,点击“…区”可呈现出相关按钮,style标签中时css部分,要求html与css分开显示可直接复制粘贴。
recommend-type

爬壁清洗机器人设计.doc

"爬壁清洗机器人设计" 爬壁清洗机器人是一种专为高层建筑外墙或屋顶清洁而设计的自动化设备。这种机器人能够有效地在垂直表面移动,完成高效且安全的清洗任务,减轻人工清洁的危险和劳动强度。在设计上,爬壁清洗机器人主要由两大部分构成:移动系统和吸附系统。 移动系统是机器人实现壁面自由移动的关键。它采用了十字框架结构,这种设计增加了机器人的稳定性,同时提高了其灵活性和避障能力。十字框架由两个呈十字型组合的无杆气缸构成,它们可以在X和Y两个相互垂直的方向上相互平移。这种设计使得机器人能够根据需要调整位置,适应不同的墙面条件。无杆气缸通过腿部支架与腿足结构相连,腿部结构包括拉杆气缸和真空吸盘,能够交替吸附在壁面上,实现机器人的前进、后退、转弯等动作。 吸附系统则由真空吸附结构组成,通常采用多组真空吸盘,以确保机器人在垂直壁面上的牢固吸附。文中提到的真空吸盘组以正三角形排列,这种方式提供了均匀的吸附力,增强了吸附稳定性。吸盘的开启和关闭由气动驱动,确保了吸附过程的快速响应和精确控制。 驱动方式是机器人移动的动力来源,由X方向和Y方向的双作用无杆气缸提供。这些气缸安置在中间的主体支架上,通过精确控制,实现机器人的精准移动。这种驱动方式既保证了力量,又确保了操作的精度。 控制系统作为爬壁清洗机器人的大脑,采用三菱公司的PLC-FX1N系列,负责管理机器人的各个功能,包括吸盘的脱离与吸附、主体的移动、清洗作业的执行等。PLC(可编程逻辑控制器)具有高可靠性,能根据预设程序自动执行指令,确保机器人的智能操作。 爬壁清洗机器人结合了机械结构、气动控制和智能电子技术,实现了在复杂环境下的自主清洁任务。其设计考虑了灵活性、稳定性和安全性,旨在提高高层建筑清洁工作的效率和安全性。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

Python并发编程:从新手到专家的进阶之路(多线程与多进程篇)

![Python并发编程:从新手到专家的进阶之路(多线程与多进程篇)](https://img-blog.csdnimg.cn/12b70559909c4535891adbdf96805846.png) # 1. Python并发编程基础** 并发编程是一种编程范式,它允许程序同时执行多个任务。在Python中,可以通过多线程和多进程来实现并发编程。 多线程是指在单个进程中创建多个线程,每个线程可以独立执行任务。多进程是指创建多个进程,每个进程都有自己的内存空间和资源。 选择多线程还是多进程取决于具体应用场景。一般来说,多线程适用于任务之间交互较少的情况,而多进程适用于任务之间交互较多或
recommend-type

matlab小程序代码

MATLAB是一款强大的数值计算和可视化工具,特别适合进行科学计算、工程分析和数据可视化。编写MATLAB小程序通常涉及使用其内置的数据类型、函数库以及面向对象编程特性。以下是一个简单的MATLAB代码示例,用于计算两个数的和: ```matlab % MATLAB程序:计算两个数的和 function sum = addTwoNumbers(num1, num2) % 定义函数 sum = num1 + num2; % 返回结果 disp(['The sum of ' num2str(num1) ' and ' num2str(num2) ' is ' nu
recommend-type

喷涂机器人.doc

"该文档详细介绍了喷涂机器人的设计与研发,包括其背景、现状、总体结构、机构设计、轴和螺钉的校核,并涉及到传感器选择等关键环节。" 喷涂机器人是一种结合了人类智能和机器优势的机电一体化设备,特别在自动化水平高的国家,其应用广泛程度是衡量自动化水平的重要指标。它们能够提升产品质量、增加产量,同时在保障人员安全、改善工作环境、减轻劳动强度、提高劳动生产率和节省原材料等方面具有显著优势。 第一章绪论深入探讨了喷涂机器人的研究背景和意义。课题研究的重点在于分析国内外研究现状,指出国内主要集中在基础理论和技术的应用,而国外则在技术创新和高级功能实现上取得更多进展。文章明确了本文的研究内容,旨在通过设计高效的喷涂机器人来推动相关技术的发展。 第二章详细阐述了喷涂机器人的总体结构设计,包括驱动系统的选择(如驱动件和自由度的确定),以及喷漆机器人的运动参数。各关节的结构形式和平衡方式也被详细讨论,如小臂、大臂和腰部的传动机构。 第三章主要关注喷漆机器人的机构设计,建立了数学模型进行分析,并对腕部、小臂和大臂进行了具体设计。这部分涵盖了电机的选择、铰链四杆机构设计、液压缸设计等内容,确保机器人的灵活性和精度。 第四章聚焦于轴和螺钉的设计与校核,以确保机器人的结构稳定性。大轴和小轴的结构设计与强度校核,以及回转底盘与腰部主轴连接螺钉的校核,都是为了保证机器人在运行过程中的可靠性和耐用性。 此外,文献综述和外文文献分析提供了更广泛的理论支持,开题报告则展示了整个研究项目的目标和计划。 这份文档全面地展示了喷涂机器人的设计过程,从概念到实际结构,再到部件的强度验证,为读者提供了深入理解喷涂机器人技术的宝贵资料。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

10个Python并发编程必知技巧:掌握多线程与多进程的精髓

![10个Python并发编程必知技巧:掌握多线程与多进程的精髓](https://img-blog.csdnimg.cn/20200424155054845.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lkcXN3dQ==,size_16,color_FFFFFF,t_70) # 1. Python并发编程概述 Python并发编程是一种编程范式,允许程序同时执行多个任务。它通过创建和管理多个线程或进程来实现,从而提高程序的性能