Java版水果管理系统源码

 更新时间:2018年01月15日 13:42:50   作者:叁念  
这篇文章主要为大家详细介绍了Java版水果管理系统源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

水果管理系统Java版分享给大家。

主类 FruitsDemo

/**
 * 功能: 
 * 1. 查看所有的水果 
 * 2. 添加新的水果(添加的时候判断水果名是否有重复) 
 * 3. 对所有的水果进行排序(价格排序、库存排序) 
 * 4. 删除指定的水果
 * 5. 退出系统
 * 
 * 注意: 
 * 1. 每种水果都必须有水果id,水果名,水果数量,水果价格 
 * 2. 添加水果时,要由用户输入水果名、数量和价格 
 * 3. 删除水果时要二次确认
 * 
 * 评分依据: 功能实现的情况,代码规范性(命名规范、格式规范),设计的合理性
 * @author yj
 *
 */

public class FruitsDemo {
 public static void main(String[] args) {

 int select = 0; // 主菜单功能选择
 boolean isStart = true;// 程序运行标志位

 while (isStart) {
  System.out.println("******************水果管理系统******************\n请输入下列序号选择相应功能:\n\n 1.查看所有的水果 \t2. 添加新的水果 \n 3.对所有的水果进行排序查看(价格排序、库存排序) \n 4.删除水果\t5. 退出系统");
  select = Calculation.inputIsInt();

  switch (select) {
  case 1://1.查看所有的水果
  Calculation.seeAllFruits();
  break;
  case 2://2. 添加新的水果
  Calculation.add();
  break;
  case 3://3.对所有的水果进行排序查看(价格排序、库存排序)
  Calculation.Sort();
  break;
  case 4:// 4.删除水果
  System.out.println("请输入你要删除的水果");
  String index = Calculation.inputIsString();
  System.out.println("二次确认!!!请再次输入你要删除的水果");
  String index1 = Calculation.inputIsString();
  if(index.equals(index1)){
   Calculation.remove(index);
  }else{
   System.out.println("两次输入不匹配,删除失败!!!");
  }

  break;
  case 5://5. 退出系统
  isStart = false;
  break;
  default:
  System.out.println("输入错误,请重新输入");
  break;
  }
 }
 System.out.println("程序已退出,欢迎使用!!!");

 }
}

Fruits 类

/**
 * 水果类
 * @author yj
 *
 */
public class Fruits {
 // 每种水果都必须有水果id,水果名,水果数量,水果价格
 private int id;//ID
 private int nums;//数量(库存)
 private String name;//水果名
 private double price;//水果价格

 public Fruits(int id, String name, int nums, double price) {
 super();
 this.id = id;
 this.nums = nums;
 this.name = name;
 this.price = price;
 }

 public int getId() {
 return id;
 }

 public void setId(int id) {
 this.id = id;
 }

 public int getNums() {
 return nums;
 }

 public void setNums(int nums) {
 this.nums = nums;
 }

 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;
 }


}

Calculation 类

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * 计算类,存放关于计算处理数据的函数
 * 
 * @author yj
 *
 */
public class Calculation {
 static LinkedList<Fruits> list = new LinkedList<Fruits>();
 static Scanner sc = new Scanner(System.in);
 static int id = 1;

 /**
 * 添加水果 get()
 */
 public static void add() {
 int nums;
 String name;
 double price;

 System.out.print("请输入你要添加的水果名、数量(单位:个)和价格(单位:元)\n");
 name = Calculation.inputIsString();
 nums = Calculation.inputIsInt();
 price = Calculation.inputIsDouble();

 if (cals(name, nums, price)) {
  list.add(new Fruits(id, name, nums, price));
  id++;
 }

 }

 /**
 * 查看所有水果 seeAllFruits()
 */
 public static void seeAllFruits() {
 if (list.size() == 0) {
  System.out.println("数据为空!!!");
 } else {
  Iterator<Fruits> it = list.iterator();
  while (it.hasNext()) {
  Fruits temp = it.next();
  System.out.println("ID -> " + temp.getId() + "\t水果名称 -> " + temp.getName() + "\t水果数量 -> "
   + temp.getNums() + "\t水果价格 -> " + temp.getPrice());
  }
 }

 }

 /**
 * 删除水果 remove(String index)
 * 
 * @param index
 *  你要删除的水果名
 */
 public static void remove(String index) {
 Iterator<Fruits> it = list.iterator();
 while (it.hasNext()) {
  if (index.equals(it.next().getName())) {
  it.remove();
  System.out.println(index + "已删除");
  }
 }
 }

 /**
 * 判断是否重复 cals(String name, int nums, double price)
 * 
 * @param name
 *  水果名
 * @param nums
 *  水果数量
 * @param price
 *  水果价格
 * @return
 */
 public static boolean cals(String name, int nums, double price) {
 Iterator<Fruits> it1 = list.iterator();
 while (it1.hasNext()) {
  Fruits temp = it1.next();
  if (name.equals(temp.getName())) {
  temp.setNums(nums + temp.getNums());
  temp.setPrice(price);
  System.out.println("水果——"+name+" 已存在,数量在原基础上加上 "+nums+",价格已更新为 "+price);
  return false;
  }
 }
 return true;
 }

 /**
 * 排序输出 Sort()
 */
 public static void Sort() {
 System.out.println("1.按照价格升序 2.按照库存升序");
 int n = inputIsInt();
 switch (n) {
 case 1:
  Collections.sort(list, new Comparator<Fruits>() {
  @Override
  public int compare(Fruits o1, Fruits o2) {
   if (o1.getPrice() > o2.getPrice()) {
   return 1;
   } else if (o1.getPrice() < o2.getPrice()) {
   return -1;
   } else {
   return 0;
   }
   // return (int) (o1.getPrice() * 100 - o2.getPrice() * 100);
  }
  });
  break;
 case 2:
  Collections.sort(list, new Comparator<Fruits>() {
  @Override
  public int compare(Fruits o1, Fruits o2) {
   if (o1.getNums() > o2.getNums()) {
   return 1;
   } else if (o1.getNums() < o2.getNums()) {
   return -1;
   } else {
   return 0;
   }
//   return (int) (o1.getNums() - o2.getNums());
  }
  });
  break;
 default:
  System.out.println("输入指令错误!!!");
  break;
 }
 seeAllFruits();
 }

 /**
 * 输入是否是Int inputIsInt()
 * 
 * @return
 */

 public static int inputIsInt() {
 boolean isRight = true;
 int select = 0;
 do {
  try {
  select = sc.nextInt();
  isRight = true;
  } catch (Exception e) {
  System.out.println("输入类型不匹配,请输入一个整数(Int)");
  sc.nextLine();
  isRight = false;
  }
 } while (!isRight);
 return select;
 }

 /**
 * 输入是否是String inputIsString()
 * 
 * @return
 */
 public static String inputIsString() {
 boolean isRight = true;
 String select = null;
 do {
  try {
  select = sc.next();
  isRight = true;
  } catch (Exception e) {
  System.out.println("输入类型不匹配,请输入一个字符串(String)");
  sc.nextLine();
  isRight = false;
  }
 } while (!isRight);
 return select;
 }

 /**
 * 输入是否为Douuble
 * 
 * @return
 */
 public static Double inputIsDouble() {
 boolean isRight = true;
 Double select = null;
 do {
  try {
  select = sc.nextDouble();
  isRight = true;
  } catch (Exception e) {
  System.out.println("输入类型不匹配,请输入一个小数(Double)!!!");
  sc.nextLine();
  isRight = false;
  }
 } while (!isRight);
 return select;
 }

}

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

相关文章

  • springboot?ErrorPageFilter的实际应用详解

    springboot?ErrorPageFilter的实际应用详解

    这篇文章主要介绍了springboot?ErrorPageFilter的实际应用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 解决使用stream将list转map时,key重复导致报错的问题

    解决使用stream将list转map时,key重复导致报错的问题

    这篇文章主要介绍了解决使用stream将list转map时,key重复导致报错的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Spring Security中防护CSRF功能详解

    Spring Security中防护CSRF功能详解

    这篇文章主要介绍了Spring Security中防护CSRF功能,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • SpringBoot使用自定义json解析器的使用方法

    SpringBoot使用自定义json解析器的使用方法

    本篇文章主要介绍了SpringBoot使用自定义json解析器的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Java IO流和文件操作实现过程解析

    Java IO流和文件操作实现过程解析

    这篇文章主要介绍了Java IO流和文件操作实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java基于接口实现模拟动物声音代码实例

    Java基于接口实现模拟动物声音代码实例

    这篇文章主要介绍了Java基于接口实现模拟动物声音代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Sentinel整合Feign流程详细讲解

    Sentinel整合Feign流程详细讲解

    要想整合Feign,首先要了解Feign的使用以及执行过程,然后看?Sentinel如何整合进去,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • SpringSecurity实现动态加载权限信息的方法

    SpringSecurity实现动态加载权限信息的方法

    这篇文章主要介绍了SpringSecurity实现动态加载权限信息,本文给大家介绍的非常详细,对大家的学习或工作具有一定需要的朋友可以参考下
    2022-01-01
  • Java虚拟机运行时数据区域汇总

    Java虚拟机运行时数据区域汇总

    这篇文章主要给大家介绍了关于Java虚拟机运行时数据区域的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • java实现大文件导出的实现与优化

    java实现大文件导出的实现与优化

    这篇文章主要为大家详细介绍了java实现大文件导出的实现与优化的相关资料,文中的示例代码讲解详细,对我们深入了解java有一定的帮助,感兴趣的小伙伴可以了解下
    2023-11-11

最新评论

?


http://www.vxiaotou.com