加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java版水果管理系统源码

发布时间:2020-12-14 06:06:59 所属栏目:Java 来源:网络整理
导读:水果管理系统Java版分享给大家。 主类 FruitsDemo /** * 功能: * 1. 查看所有的水果 * 2. 添加新的水果(添加的时候判断水果名是否有重复) * 3. 对所有的水果进行排序(价格排序、库存排序) * 4. 删除指定的水果 * 5. 退出系统 * * 注意: * 1. 每种水果

水果管理系统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请输入下列序号选择相应功能:nn 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,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,double price)
 * 
 * @param name
 *  水果名
 * @param nums
 *  水果数量
 * @param price
 *  水果价格
 * @return
 */
 public static boolean cals(String name,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,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;
 }

}

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读