宠物管理系统
为宠物商店开发的小型系统,能保存大量宠物的信息,并能对宠物信息进行增删改查。
宠物信息包含:编号、昵称、性别、年龄、颜色、品种、价格
系统功能包含:
- 打印系统菜单
- 添加宠物
- 打印所有宠物信息
- 宠物排序
- 修改宠物
- 删除宠物
- 查找宠物
打印系统菜单
输出让系统用户选择功能的菜单,用户输入数字(1~6)选择功能,执行完功能后回到该菜单,输入其他数字则退出系统
1、添加宠物 2、显示所有宠物 3、宠物排序 4、修改宠物 5、删除宠物 6、查找宠物
添加宠物
按提示输入宠物的各项信息,添加新的宠物对象到系统中
打印所有宠物
将所有宠物以原始顺序打印出来
宠物排序
提示有3种排序方式:1、编号排序 2、年龄排序 3、价格排序
选择其中一种后,按此顺序显示所有宠物信息
修改宠物
提示输入宠物的编号,通过编号找到对应宠物,输入除编号外的所有信息,将该宠物的信息修改为新的数据;如果没有该编号的宠物,给与提示
删除宠物
提示输入宠物的编号,通过编号找到对应宠物,从系统中删除宠物;如果没有该编号的宠物,给与提示
查找宠物
提示有3种查找方式:1、按编号查找 2、按昵称查找 3、按品种查找
用户选择后,输入对应的查询内容,查找宠物,找不到则给与提示
import java.math.BigDecimal;
public class Pet {
private String number;
private String name;
private String gender;
private int age;
private String colour;
private String breed;
private BigDecimal price;
public Pet() {
}
public Pet(String number, String name, String gender, int age, String colour, String breed, BigDecimal price) {
this.number = number;
this.name = name;
this.gender = gender;
this.age = age;
this.colour = colour;
this.breed = breed;
this.price = price;
}
/**
* 获取
* @return number
*/
public String getNumber() {
return number;
}
/**
* 设置
* @param number
*/
public void setNumber(String number) {
this.number = number;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return gender
*/
public String getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return colour
*/
public String getColour() {
return colour;
}
/**
* 设置
* @param colour
*/
public void setColour(String colour) {
this.colour = colour;
}
/**
* 获取
* @return breed
*/
public String getBreed() {
return breed;
}
/**
* 设置
* @param breed
*/
public void setBreed(String breed) {
this.breed = breed;
}
/**
* 获取
* @return price
*/
public BigDecimal getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(BigDecimal price) {
this.price = price;
}
public String toString() {
return "Pet{number = " + number + ", name = " + name + ", gender = " + gender + ", age = " + age + ", colour = " + colour + ", breed = " + breed + ", price = " + price + "}";
}
}
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PetTest {
public static ArrayList<Pet> list = new ArrayList<Pet>();
public static int count = 0;
static Scanner sc = new Scanner(System.in);
public static int a;
public static int o = 0;
public static int choose(int c){
while (!sc.hasNextInt()){
// sc.nextLine();
System.out.println("请输入整数");
sc.nextLine();
}
while (true){
a = sc.nextInt();
if (a < 1 || a >c){
System.out.println("请输入1-"+c+"的整数");
sc.nextLine();
}else
return a;
}
}
// // 冒泡排序
// public static int[] bubbleSort(int[] arr) {
// int len = arr.length;
// for (int i = 0; i < len - 1; i++) {
// for (int j = 0; j < len - 1 - i; j++) {
// if (arr[j] > arr[j+1]) { // 相邻元素两两对比
// int temp = arr[j+1]; // 元素交换
// arr[j+1] = arr[j];
// arr[j] = temp;
// }
// }
// }
// return arr;
// }
public static void menu(){
System.out.println("1、添加宠物 \n2、显示所有宠物 \n3、宠物排序 \n4、修改宠物 \n5、删除宠物 \n6、查找宠物 \n7、退出 \n请选择:");
switch (choose(7)){
case 1->add();
case 2->show();
case 3->sort();
case 4->edit();
case 5->delete();
case 6->LookUp();
case 7->{
o = 7;
}
}
}
public static void breedLookUp(){
int g = 0;
System.out.println("请输入品种");
String a = sc.next();
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
for (Pet pet : list) {
if (pet.getBreed().equals(a)) {
System.out.println(pet.getNumber() + "\t\t" + pet.getName() + "\t\t" + pet.getGender() + "\t\t" + pet.getAge() + "\t\t" + pet.getColour() + "\t\t" + pet.getBreed() + "\t\t" + pet.getPrice());
g++;
}
}
if (g == 0)
System.out.println("未查询到品种:"+a);
}
public static void numberLookUp(){
int g = 0;
System.out.println("请输入编号");
String a = sc.next();
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
for (Pet pet : list) {
if (pet.getNumber().equals(a)) {
System.out.println(pet.getNumber() + "\t\t" + pet.getName() + "\t\t" + pet.getGender() + "\t\t" + pet.getAge() + "\t\t" + pet.getColour() + "\t\t" + pet.getBreed() + "\t\t" + pet.getPrice());
g++;
}
}
if (g == 0)
System.out.println("未查询到编号:"+a);
}
public static void nameLookUp(){
int g = 0;
System.out.println("请输入昵称");
String a = sc.next();
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
for (Pet pet : list) {
if (pet.getName().equals(a)) {
System.out.println(pet.getNumber() + "\t\t" + pet.getName() + "\t\t" + pet.getGender() + "\t\t" + pet.getAge() + "\t\t" + pet.getColour() + "\t\t" + pet.getBreed() + "\t\t" + pet.getPrice());
g++;
}
}
if (g == 0)
System.out.println("未查询到昵称:"+a);
}
public static void LookUp(){
System.out.println("1、按编号查找 \n2、按昵称查找 \n3、按品种查找");
switch (choose(3)){
case 1->numberLookUp();
case 2->nameLookUp();
case 3->breedLookUp();
}
}
public static void delete(){
if (count == 0){
System.out.println("库内无信息");
return;
}
System.out.println("请输入要删除的宠物编号");
list.remove(choose(count) - 1);
System.out.println("删除成功");
count--;
show();
}
public static void edit(){
show();
System.out.println("请输入您需要修改的宠物编号");
int a = choose(count);
System.out.println("请输入宠物编号");
list.get(a - 1).setNumber(sc.next());
System.out.println("请输入宠物昵称");
list.get(a - 1).setName(sc.next());
System.out.println("请输入宠物性别");
list.get(a - 1).setGender(sc.next());
System.out.println("请输入宠物年龄");
list.get(a - 1).setAge(sc.nextInt());
System.out.println("请输入宠物颜色");
list.get(a - 1).setColour(sc.next());
System.out.println("请输入宠物品种");
list.get(a - 1).setBreed(sc.next());
System.out.println("请输入宠物价格");
list.get(a - 1).setPrice(sc.nextBigDecimal());
System.out.println("修改成功");
show();
}
public static void ageSort(){
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
// 按年龄从小到大排序 冒泡
for (int i = 0; i < list.size()-1; i++) {
for(int j = 0; j < list.size() - 1 - i; j++){
Pet u1=list.get(j);
Pet u2=list.get(j+1);
if(u1.getAge() > u2.getAge()){
list.set(j,u2);
list.set(j+1,u1);
}
}
}
// 遍历
for (Pet p : list) {
System.out.println(p.getNumber()+"\t\t"+p.getName()+"\t\t"+p.getGender()+"\t\t"+p.getAge()+"\t\t"+p.getColour()+"\t\t"+p.getBreed()+"\t\t"+p.getPrice());
}
}
public static void numberSort(){
show();
}
public static void priceSort(){
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
// 按价格从小到大排序 冒泡
for (int i = 0; i < list.size()-1; i++) {
for(int j = 0; j < list.size() - 1 - i; j++){
Pet u1=list.get(j);
Pet u2=list.get(j+1);
if(u1.getPrice().compareTo(u2.getPrice()) > 0){
list.set(j,u2);
list.set(j+1,u1);
}
}
}
// 遍历
for (Pet p : list) {
System.out.println(p.getNumber()+"\t\t"+p.getName()+"\t\t"+p.getGender()+"\t\t"+p.getAge()+"\t\t"+p.getColour()+"\t\t"+p.getBreed()+"\t\t"+p.getPrice());
}
}
public static void sort(){
System.out.println("1、编号排序 \n2、年龄排序 \n3、价格排序");
switch (choose(3)){
case 1->numberSort();
case 2->ageSort();
case 3->priceSort();
}
}
public static void show(){
if (list.isEmpty()){
System.out.println("库内无信息");
return;
}
System.out.println("编号\t\t昵称\t\t性别\t\t年龄\t\t颜色\t\t品种\t\t价格");
for (int i = 0; i < list.size()-1; i++) {
for(int j = 0; j < list.size() - 1 - i; j++){
Pet u1=list.get(j);
Pet u2=list.get(j+1);
if(u1.getNumber().compareTo(u2.getNumber()) > 0){
list.set(j,u2);
list.set(j+1,u1);
}
}
}
// 遍历
for (Pet p : list) {
System.out.println(p.getNumber()+"\t\t"+p.getName()+"\t\t"+p.getGender()+"\t\t"+p.getAge()+"\t\t"+p.getColour()+"\t\t"+p.getBreed()+"\t\t"+p.getPrice());
}
}
public static void add(){
list.add(new Pet());
System.out.println("请输入宠物编号");
list.get(count).setNumber(sc.next());
System.out.println("请输入宠物昵称");
list.get(count).setName(sc.next());
System.out.println("请输入宠物性别");
list.get(count).setGender(sc.next());
System.out.println("请输入宠物年龄");
list.get(count).setAge(sc.nextInt());
System.out.println("请输入宠物颜色");
list.get(count).setColour(sc.next());
System.out.println("请输入宠物品种");
list.get(count).setBreed(sc.next());
System.out.println("请输入宠物价格");
list.get(count).setPrice(sc.nextBigDecimal());
count++;
}
public static void main(String[] args) {
System.out.println("欢迎使用宠物管理系统");
while(true){
menu();
if (o == 7)
return;
}
}
}