服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java版水果管理系统源码

Java版水果管理系统源码

2021-03-22 13:50叁念 Java教程

这篇文章主要为大家详细介绍了Java版水果管理系统源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

Java版水果管理系统源码

主类 fruitsdemo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
 * 功能:
 * 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 类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * 水果类
 * @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 类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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;
 }
 
}

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

原文链接:http://blog.csdn.net/qq_36868342/article/details/76522356

延伸 · 阅读

精彩推荐