简易的超市管理系统
主要功能:
1.管理员登录
2.管理员注册
3.商品的增删查改
实现步骤:
创建一个商品类
下面展示一些 内联代码片
。
下面展示一些 `内联代码片`。
package goods;
public class Goods1 {
private int id;//商品编号
private String name ;//商品名称
private double price;//商品价格
Goods1 []good;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Goods1(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
创建一个商品业务类
下面展示一些 内联代码片
。
package goods;
import java.util.ArrayList;
import java.util.Scanner;
public class Business {
//创建一个集合存放商品
static ArrayList<Goods1> arrayList=new ArrayList();
static int id;//商品编号
static String name ;//商品名称
static double price;//商品价格
Scanner sc=new Scanner(System.in);
//查看商品信息
void list(){
System.out.println("------------货物清单-----------------");
System.out.println("编号\t名称\t单价");
//1 System.out.println(good.length);
if(arrayList.size()!=0){
for ( Goods1 goods1:arrayList) {
System.out.println(goods1.getId()+"\t\t"+goods1.getName()+"\t\t"+goods1.getPrice());
}
}else{
System.out.println("商品信息为空!");
}
}
//添加商品
void add(){
System.out.println("-------------增加货物-----------------");
System.out.println("请输入您要添加的货物编号:");
id=sc.nextInt();
System.out.println("请输入您要添加的货物名称:");
name=sc.next();
System.out.println("请输入您要添加的货物价格:");
price=sc.nextDouble();
arrayList.add(new Goods1(id,name,price));
System.out.println("添加成功!");
System.out.println("编号\t名称\t单价");
System.out.print(id+"\t\t"+ name+"\t\t"+ price);
System.out.println();
}
//删除商品
void remove(){
System.out.println("-------------删除货物-----------------");
System.out.println("请选择您要删除的商品编号:");
int id2=sc.nextInt();
int index=0;
int i=0;
for (Goods1 g1:arrayList) {
if(g1.getId()==id2){
index=i;
}
i++;
}
arrayList.remove(index);
System.out.println("删除成功!");
}
//修改商品
void set(){
boolean flag=false;
System.out.println("-------------修改货物-----------------");
System.out.println("请选择您要修改的商品编号:");
int id2=sc.nextInt();
int index=0;
int i=0;
for (Goods1 g1:arrayList) {
if(g1.getId()==id2){
index=i;
}
i++;
}
if(flag==false){
System.out.println("编号错误请重新输入:");
}
System.out.println("请输入新的商品编号:");
int newid=sc.nextInt();
System.out.println("请输入新的商品名称:");
String newName=sc.next();
System.out.println("请输入新的商品单价:");
double newPrice=sc.nextDouble();
arrayList.set(index,new Goods1(newid,newName,newPrice));
}
//登录
void login(){
while (true){
System.out.println("----------------超市管理系统!--------------------------");
System.out.println("1.货物清单");
System.out.println("2.增加货物");
System.out.println("3.删除货物");
System.out.println("4.修改货物");
System.out.println("5.退出");
System.out.println("请输入您选择的菜单(请从1-5中选择):");
int menue=sc.nextInt();
if(menue==1){//查看列表
list();
}else if(menue==2){
add();//添加商品
}
else if(menue==3){
remove();//删除商品
}else if(menue==4){
set();//修改商品
}else if(menue==5){
test.login();//退出商品系统
break;
}else{
System.out.println("选择错误请重新选择:");
}
}
}
}
创建一个用户类:
下面展示一些 内联代码片
。
package goods;
import java.util.HashMap;
import java.util.Scanner;
//管理员登录
public class User {
private static String user;
private static String pwd;
static Business b = new Business();
static HashMap hashMap=new HashMap();
public User(String user, String pwd) {
this.user = user;
this.pwd = pwd;
}
//管理员注册
static void register() {
Scanner sc = new Scanner(System.in);
System.out.println("------------用户注册------------------");
System.out.println("用户名和密码必须由字母和数字,长度为5-16");
while(true){
System.out.println("请输入您的用户名:");
user = sc.next();
System.out.println("请输入您的密码:");
pwd = sc.next();
if(user.matches("[a-zA-Z0-9]{5,16}")&&pwd.matches("[a-zA-Z0-9]{5,12}")){
hashMap.put(user,pwd);
System.out.println("注册成功");
break;
}else{
System.out.println("用户名和密码必须由字母和数字,长度为5-16");
}
}
}
//管理员登陆
static void userlogin() {
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("---------------用户登陆--------------------------");
System.out.println("请输入您的用户名:");
String user1 = sc.next();
System.out.println("请输入您的密码:");
String pwd1 = sc.next();
if(hashMap.containsKey(user1)&&hashMap.containsValue(pwd1)){
System.out.println("登陆成功!");
b.login();
break;
}else{
System.out.println("用户名和密码不正确,请重新输入:");
break;
}
}
}
}
创建一个测试类
下面展示一些 内联代码片
。
package goods;
import java.util.Scanner;
public class test {
static void login() {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("----------------超市管理系统!--------------------------");
System.out.println("请输入编号:1.登录,2.注册 其他键退出");
try {//异常捕获
int menue = sc.nextInt();
if (menue == 1) {
User.userlogin();//登录
} else if (menue == 2) {
User.register();//注册
} else {
System.out.println("谢谢使用!");
break;
}
} catch (Exception e) {
System.out.println("输入错误!");
}
}
}
public static void main(String[] args) {
test.login();
}
}
功能效果图:
注册
登录
增删查改