用java实现简易收银台,供大家参考,具体内容如下
简易收银台的实现并不难,主要实现这几个类:
- 商品类 goods (将商品根据编号,名称,价格存放)
- 商品中心类 goodscenter (存放商品,可以添加商品,下架商品,修改商品信息,判断商品是否存在或者货架已满,打印商品等功能)
- 订单类 order (处理订单,主要实现买单功能,下单,计算总价)
功能实现
初始化界面
商品上架
修改商品信息
下架商品
返回并进入买单功能
选择商品及数量进行买单
取消订单
查看订单
返回并退出
功能分析
代码展示
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
package com.bittech;
import java.time.localdate;
import java.util.scanner;
/**
* author:weiwei
* description:
* creat:2019/5/5
**/
public class checkstand {
public static scanner scanner = new scanner(system.in);
public static void helpinfo() {
system.out.println( "==============欢迎使用简易收银台=============" );
system.out.println( " [u]使用 [s]设置 [a]关于 [q]退出 " );
system.out.println( " 输入 u s a q 进行操作 " );
system.out.println( "============================================" );
}
public static void quit() {
system.out.println( "===========================================" );
system.out.println( " 欢迎下次使用 " );
system.out.println( "===========================================" );
system.exit( 0 );
}
public static void usageinfo() {
system.out.println( "================买单功能====================" );
system.out.println( " [s]查看 [a]下单 [d]取消 [l]浏览 [r]返回" );
system.out.println( " 输入 s a d l r 进行操作 " );
system.out.println( "===========================================" );
}
public static void about() {
system.out.println( "==================关于=====================" );
system.out.println( " 名称:简易收银台 " );
system.out.println( " 功能:基于字符界面的收银台操作 " );
system.out.println( " 作者:weiwei " );
system.out.println( " 版本:v0.0.1 " );
system.out.println( " 意见反馈:liusz0501@163.com " );
system.out.println( "==========================================" );
}
public static void settinginfo() {
system.out.println( "=================设置功能==================" );
system.out.println( " [s]查看 [a]上架 [d]下架 [u]修改 [r]返回 " );
system.out.println( " 输入 s a d u r 进行操作 " );
system.out.println( "===========================================" );
}
public static void usage() {
usageinfo();
goodscenter.printgoods();
order order = new order();
while ( true ){
string line = scanner.nextline();
switch (line.trim()){
case "s" :{
order.printorder();
break ;
}
case "a" :{
system.out.println( "请输入下单信息[编号][数量] (格式如:1 2 ):" );
string value = scanner.nextline();
string[] infoarray = value.split( " " );
if (infoarray != null && (infoarray.length == 2 )){
goods goods = goodscenter.getgoods(integer.parseint(infoarray[ 0 ]));
if (goods != null ){
order.add(goods,integer.parseint(infoarray[ 1 ]));
order.printorder();
break ;
}
}
system.out.println( "请按照格式要求输入信息" );
break ;
}
case "d" :{
system.out.println( "请输入取消信息[编号 数量](如下格式:1 2 ):" );
string value = scanner.nextline();
string[] infoarray = value.split( " " );
if (infoarray != null && (infoarray.length == 2 )) {
goods goods = goodscenter.getgoods(integer.parseint(infoarray[ 0 ]));
if (goods != null ) {
order.cance(goods, integer.parseint(infoarray[ 1 ]));
order.printorder();
break ;
}
}
system.out.println( "请按照格式要求输入信息" );
break ;
}
case "l" : {
goodscenter.printgoods();
break ;
}
case "r" : {
return ;
}
default : {
usageinfo();
}
}
}
}
public static void setting() {
settinginfo();
if (goodscenter.isfull()) {
system.out.println( "!当前商品货架已经满了,如果要进行添加请下降部分商品" );
}
while ( true ) {
string line = scanner.nextline();
switch (line.touppercase()) {
case "s" : {
goodscenter.printgoods();
break ;
}
case "a" : {
system.out.println( "请输入上架商品信息(如下格式:1 餐巾纸 1.4):" );
goods goods = readgoods();
if (goods == null ) {
system.out.println( "!请按照格式要求输入信息" );
break ;
}
if (goodscenter.isfull()) {
system.out.println( "!当前商品货架已经满了,如果要进行添加请下降部分商品" );
} else if (goodscenter.isexist(goods)) {
system.out.println( "!上架商品已经存在,注意编号不能重复" );
} else {
goodscenter.addgoods(goods);
goodscenter.printgoods();
}
break ;
}
case "d" : {
system.out.println( "请输入下架商品信息编号(如下格式:1 ):" );
goods goods = readgoods();
if (goods == null ) {
system.out.println( "请按照格式要求输入信息" );
break ;
}
if (goodscenter.isputaway(goods)) {
goodscenter.soldoutgoods(goods);
goodscenter.printgoods();
} else {
system.out.println( "请选择上架的商品编号,当前下架商品未设置" );
}
break ;
}
case "u" : {
system.out.println( "请输入修改商品信息(如下格式:1 餐巾纸 1.4 )" );
goods goods = readgoods();
if (goods == null ) {
system.out.println( "请按照格式要求输入信息" );
break ;
}
if (goodscenter.isputaway(goods)) {
goodscenter.modifygoods(goods);
goodscenter.printgoods();
} else {
system.out.println( "请选择上架的商品编号,当前修改商品未设置" );
}
break ;
}
case "r" : {
return ;
}
default : {
settinginfo();
}
}
}
}
public static goods readgoods() {
string value = scanner.nextline();
string[] infoarray = value.split( " " );
if (infoarray != null && (infoarray.length == 3 || infoarray.length == 1 )) {
if (infoarray.length == 3 ) {
goods goods = new goods(integer.parseint(infoarray[ 0 ]), infoarray[ 1 ], double .parsedouble(infoarray[ 2 ]));
return goods;
}
if (infoarray.length == 1 ) {
goods goods = new goods(integer.parseint(infoarray[ 0 ]), "" , 0 .0d);
return goods;
}
}
return null ;
}
public static void main(string[] args) {
helpinfo();
while ( true ) {
string line = scanner.nextline();
switch (line.trim().touppercase()) {
case "u" :
usage();
helpinfo();
break ;
case "s" :
setting();
helpinfo();
break ;
case "a" :
about();
break ;
case "q" :
quit();
break ;
default :
helpinfo();
}
}
}
}
|
goodscenter类
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
|
class goodscenter {
//商品占位符
private static string placeholder = "--" ;
//最大商品数量
private static int maxgoods = 10 ;
//商品容器
private static goods[] goodsarray;
//初始化商品容器
static {
goodsarray = new goods[maxgoods];
for ( int i = 0 ; i < goodsarray.length; i++) {
goodsarray[i] = new goods(i + 1 , "--" , 0 .0d);
}
}
private goodscenter() {
}
public static int getmaxgoods() {
return maxgoods;
}
//添加商品
public static void addgoods(goods goods) {
for ( int i = 0 ; i < goodsarray.length; i++) {
goods temp = goodsarray[i];
if (temp.getid() == goods.getid()) {
temp.setname(goods.getname());
temp.setprice(goods.getprice());
break ;
}
}
}
//下架商品
public static void soldoutgoods(goods goods) {
for ( int i = 0 ; i < goodsarray.length; i++) {
goods temp = goodsarray[i];
if (temp.getid() == goods.getid()) {
temp.setname(placeholder);
temp.setprice( 0 .0d);
break ;
}
}
}
//修改商品
public static void modifygoods(goods goods) {
for ( int i = 0 ; i < goodsarray.length; i++) {
goods temp = goodsarray[i];
if (temp.getid() == goods.getid()) {
temp.setname(goods.getname());
temp.setprice(goods.getprice());
break ;
}
}
}
//商品是否存在
public static boolean isexist(goods goods) {
for ( int i = 0 ; i < goodsarray.length; i++) {
goods temp = goodsarray[i];
if (temp.getid() == goods.getid() && temp.getname().equals(goods.getname())) {
return true ;
}
}
return false ;
}
//商品位是否存在商品
public static boolean isputaway(goods goods) {
for ( int i = 0 ; i < goodsarray.length; i++) {
goods temp = goodsarray[i];
if (temp.getid() == goods.getid() && !temp.getname().equals(placeholder)) {
return true ;
}
}
return false ;
}
//商品已满
public static boolean isfull(){
for ( int i = 0 ;i<goodsarray.length;i++){
if (goodsarray[i].getname().equals(placeholder)){
return false ;
}
}
return true ;
}
public static goods getgoods( int id){
for ( int i = 0 ;i<goodsarray.length;i++){
goods temp = goodsarray[i];
if (temp.getid() == id && !temp.getname().equals(placeholder)){
return goodsarray[i];
}
}
return null ;
}
//打印商品
public static void printgoods(){
system.out.println( "=============商品清单================" );
system.out.println( "\t" + "编号" + "\t" + "产品名称" + "\t" + "单价" );
for ( int i = 0 ;i<goodsarray.length;i++){
goods temp = goodsarray[i];
string name = temp.getname();
if (name.equals(placeholder)){
name = name + "[未上架]" ;
}
system.out.println( "\t" + temp.getid() + "\t" + temp.getname() + "\t" + temp.getprice());
}
system.out.println( "=========================================" );
}
}
|
goods类
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
|
class goods{
//商品编号
private int id;
//商品名称
private string name;
//商品价格
private double price;
public goods( int id,string name, double price){
this .id = id;
this .name = name;
this .price = price;
}
public int getid(){
return this .id;
}
public int getindex(){
return this .getid()- 1 ;
}
public string getname(){
return this .name;
}
public void setname(string name) {
this .name = name;
}
public double getprice(){
return this .price;
}
public void setprice( double price) {
this .price = price;
}
@override
public string tostring(){
return string.format( "[%2d] %s %.2f" , this .getid(), this .getname(), this .getprice());
}
}
|
order类
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
|
class order{
private static int orderid = 0 ;
private int id;
private goods[] items;
private int [] itmesnumber;
private int currentindex;
public order(){
this .id = ++orderid;
this .items = new goods[goodscenter.getmaxgoods()];
this .itmesnumber = new int [goodscenter.getmaxgoods()];
this .currentindex = - 1 ;
}
public void add(goods goods, int count){
int index = goods.getindex();
this .items[index] = goods;
this .itmesnumber[index] += count;
}
public void cance(goods goods, int count){
int index = goods.getindex();
int value = this .itmesnumber[index]-count;
if (value > 0 ){
this .itmesnumber[index] = value;
} else {
this .items[index] = null ;
this .itmesnumber[index] = 0 ;
}
}
public int getsize(){
return this .currentindex+ 1 ;
}
public double gettotalprice(){
double tatalprivce = 0 ;
for ( int i = 0 ;i< this .items.length;i++){
goods goods = this .items[i];
if (goods != null ){
tatalprivce += ( this .itmesnumber[goods.getindex()] * goods.getprice());
}
}
return tatalprivce;
}
public int getid(){
return this .id;
}
public void printorder(){
system.out.println( "========================" );
system.out.println( "编号" + this .getid() );
system.out.println( "打印时间" + localdate.now().tostring());
system.out.println( "========================" );
system.out.println( "编号 名称 数量 单价" );
for ( int i = 0 ;i< this .items.length;i++){
goods goods = this .items[i];
if (goods != null ){
int count = this .itmesnumber[goods.getindex()];
if (count <= 0 ){
continue ;
}
system.out.println(string.format( "%2d\t%s\t%d\t%.2f" ,goods.getid(),goods.getname(),count,goods.getprice() ));
}
}
system.out.println( "=========================" );
system.out.println(string.format( "总价:%2f" , this .gettotalprice()));
system.out.println( "=========================" );
}
}
|
项目总结
- 用常用string类,scanner类实现,代码量不多,简单易懂
- 有弊端存在,就是用数组存放商品,容易出现数组越界异常,而且如果商品多的话用数组存储也是极其不方便的
- 还有就是未使用到数据库,商品信息,订单信息的保存有很多不方便的地方,如果建立连接了数据库,这个问题就解决了
目前能力只能实现到这了,希望可以再努力一下,将数据库加入到项目中,让它的易用性再提升更多。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43224539/article/details/89855873