JAVA实现简单超市收银系统、控制台输入
程序员文章站
2022-04-24 10:17:05
...
主体
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import cc.lanou.supermarket.com.Commodity;
import cc.lanou.supermarket.com.CommodityImpl;
import cc.lanou.supermarket.test.Information;
public class Supermarket {
static List<Commodity> users = new ArrayList<Commodity>();
public static void main(String[] args) {
coles();
}
public static void coles() {
// 用scanner接受控制台的信息,最后关闭close
Scanner scanner = new Scanner(System.in);
// 设置是否有门店信息推出系统
// 获取时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String str3 = formatter.format(date);
// 订单编号
Calendar calendar = Calendar.getInstance();
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH);
int d = calendar.get(calendar.DATE);
// 订单
int a = 0;
String str1 = y + "" + m + "" + d + "0000" + a;
// 收款机编号
String str2 = "0371-123456789";
// System.out.println(str1);
CommodityImpl ip = new CommodityImpl();
boolean isLogin = false;
boolean isLoop = true;
// 门店信息
Information information = new Information();
// 创建门店信息类对象
// 1.设置门店信息
while (isLoop) {
System.out.println("-------欢迎来到美丽不打折超市系统-------");
System.out.println("1.设置门店信息");
System.out.println("2.顾客结账");
System.out.println("3.推出系统");
//System.out.println(" a" + a);
// 接收操作
int bian = scanner.nextInt();
switch (bian) {
case 1:
scanner.nextLine();
System.out.println("请输入门店名字");
String strName = scanner.nextLine();
System.out.println("请输入门店地址");
String strSite = scanner.nextLine();
System.out.println("请输入门店联系方式");
String strRelation = scanner.nextLine();
// Information information = new Information(strName, strSite, strRelation);
information.setName(strName);
information.setSite(strSite);
information.setRelation(strRelation);
System.out.println("你的信息录入成功");
System.out.println("门店名字" + information.getName());
System.out.println("门店地址" + information.getSite());
System.out.println("门店联系方式" + information.getRelation());
if (information.getName() != null) {
isLogin = true;
}
break;
case 2:
if (isLogin == false) {
System.out.println("请先设置门店信息");
bian = 1;
break;
}
//购买次数
int a1 = 0;
float f1 = 0;
float f2 = 0;
float f3 = 0;
String str5 = "";
//System.out.println("门店名字" + information.getName());
boolean bl = true;
boolean b2 = false;
while (bl) {
System.out.println("1.添加商品 " + "2.付款方式" + "3.收款金额" + "4.生成小票" + "5.回主菜单");
System.out.println("请按编号选择功能");
int i1 = scanner.nextInt();
switch (i1) {
case 1:
Commodity commodity = new Commodity();
b2 = true;
scanner.nextLine();
System.out.println("请输入商品名字");
String commodityName = scanner.nextLine();
System.out.println("请输入商品单价");
float commodityPrice = scanner.nextFloat();
System.out.println("请输入购买数量");
float commodityAmount = scanner.nextFloat();
commodity.setName(commodityName);
commodity.setPrice(commodityPrice);
commodity.setAmount(commodityAmount);
f1 += commodityPrice * commodityAmount;
commodity.setJin(f1);
commodity.setXiaoPiao(str5);
Supermarket.cun(commodity);
a1++;
break;
case 2:
if (b2 == false) {
System.out.println("请先购买商品");
break;
}
scanner.nextLine();
System.out.println("付款方式:" + "1(支付宝)" + "2(微信)" + "3(现金)");
String str = scanner.nextLine();
if (str.equals("1")) {
str5="支付宝";
} else if (str.equals("2")) {
str5="微信";
} else if (str.equals("3")) {
str5="现金";
}
break;
case 3:
if (b2 == false) {
System.out.println("请先购买商品");
break;
}
System.out.println("客户付款金额");
f2 = scanner.nextFloat();
f3 = f2 - f1;
break;
case 4:
if (b2 == false) {
System.out.println("请先购买商品");
break;
}
//System.out.println("门店名字" + information.getName());
System.out.println("订单编号:" + str1 + (a++));
System.out.println("---------------------");
System.out.println("欢迎光临" + " " + "TEL " + str2);
System.out.println("商品名 " + " " + "售价 " + " " + "数量 " + " " + "金额 ");
// System.out.println(commodity.getName() + commodity.getXiaoPiao());
for (Commodity i : users) {
System.out.println(i.getName() + " " + i.getAmount() + " " + i.getPrice() + " " + i.getJin());
}
System.out.println("共计" + a1 + "商品");
System.out.println("总金额" + f1);
System.out.println(str5+"实付" + f2);
System.out.println("找余:" + f3);
System.out.println("付款时间" + str3);
System.out.println("机器标号" + " " + str2);
break;
case 5:
bl = false;
default:
break;
}
}
break;
case 3:
isLoop = false;
break;
default:
break;
}
}
System.out.println("已退出");
}
public static void cun(Commodity object) {
users.add(object);
}
}
创建商品类
package cc.lanou.supermarket.com;
/**
* 商品类
* @author CC
*
*/
public class Commodity {
private String name;
private float price;
private float Amount;
private float jin;
private String xiaoPiao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public float getAmount() {
return Amount;
}
public void setAmount(float amount) {
Amount = amount;
}
public float getJin() {
return jin;
}
public void setJin(float jin) {
this.jin = jin;
}
public String getXiaoPiao() {
return xiaoPiao;
}
public void setXiaoPiao(String xiaoPiao) {
this.xiaoPiao = xiaoPiao;
}
public Commodity(String name, float price, float amount, float jin, String xiaoPiao) {
super();
this.name = name;
this.price = price;
Amount = amount;
this.jin = jin;
this.xiaoPiao = xiaoPiao;
}
public Commodity() {
}
}
创建门店信息类
public class Information {
/**
* 门店信息
*/
private String name;
private String site;
private String relation;
public Information() {
}
public Information(String name, String site, String relation) {
super();
this.name = name;
this.site = site;
this.relation = relation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public String getRelation() {
return relation;
}
public void setRelation(String relation) {
this.relation = relation;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getName();
}
}
下一篇: 晋阳失守,李世民为什么装病不出战?
推荐阅读