栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java版水果管理系统源码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java版水果管理系统源码

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

主类 FruitsDemo



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 类


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;


public class Calculation {
 static linkedList list = new linkedList();
 static Scanner sc = new Scanner(System.in);
 static int id = 1;

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

 }

 
 public static void seeAllFruits() {
 if (list.size() == 0) {
  System.out.println("数据为空!!!");
 } else {
  Iterator 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());
  }
 }

 }

 
 public static void remove(String index) {
 Iterator it = list.iterator();
 while (it.hasNext()) {
  if (index.equals(it.next().getName())) {
  it.remove();
  System.out.println(index + "已删除");
  }
 }
 }

 
 public static boolean cals(String name, int nums, double price) {
 Iterator 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;
 }

 
 public static void Sort() {
 System.out.println("1.按照价格升序 2.按照库存升序");
 int n = inputIsInt();
 switch (n) {
 case 1:
  Collections.sort(list, new Comparator() {
  @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() {
  @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();
 }

 

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

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

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

}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号