JavaSE商品管理员对商品的增、删、改、查

JavaSE商品管理员对商品的:增、删、改、查

Commodity商品类:
public class Commodity {
private double price; //商品价格
private String name; //商品名称
private String sort; //商品分类
private int count; //商品数量
public Commodity(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
//提供get、set方法给商品对象赋值和取值
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
//提供无参构造
public Commodity() {
}
//满参构造
public Commodity(double price, String name, String sort) {
this.price = price;
this.name = name;
this.sort = sort;
}
}
Administrators管理员类:
public class Administrators {
private String name; //管理员姓名
private String password;
//提供get和set方法来给对象赋值和取值
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//提供无参构造
public Administrators() {
}
//提供满参构造
public Administrators(String name, String password) {
this.name = name;
this.password = password;
}
}
AdminView用户操作界面测试类:
import java.util.ArrayList;
import java.util.Scanner;

public class AdminView { //类名
public static void main(String[] args) {//主方法:程序入口
//将商品保存到ArrayLis
ArrayList list = new ArrayList<>();
//将对象信息封装成私有,外界无法改变:管理员用户名:admin 管理员密码:123456
Administrators amt3 = new Administrators(“admin”, “123456”);
while(true){
boolean falg = false;
//管理员有三次登录机会
for (int i = 0; i < 3; i++) {
//创建键盘输入对象Scanner
Scanner sc = new Scanner(System.in);
System.out.println(“管理员页面”);
System.out.println(“请输入您的名字:”);
String name = sc.nextLine();
System.out.println(“请输入您的密码:”);
String password = sc.nextLine();
//判断输入的用户名和密码是否正确
if (name.equals(amt3.getName()) && password.equals(amt3.getPassword())) {
System.out.println(“登录成功!”);
falg = true; //登录成功给flag赋值为true
break; // 结束循环
} else {
System.out.println(“您输入的用户名或者用户密码不正确,请重新输入”);
}
if (i <= 2) {
System.out.println(“您还有” + (2 - i) + “次机会”);
break;
} else {
System.out.println(“您输入错误!”);
}
}
//用if语句判断用户登录成功以后传来的true值
if (falg) {
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(“请选择以上功能”);
Scanner sc = new Scanner(System.in);
String core = sc.next();
//用循环语句来让管理员选择功能
switch (core) {
case “1”:
addCommoddity(list);
break;
case “2”:
reviseCommodity(list);
break;
case “3”:
insertCommodity(list);
break;
case “4”:
deleteCommodity(list);
break;
case “5”:
System.out.println(“下面是管理员主页哦!!!”);
break;
default:
System.out.println(“输入的选项不正确,请重新输入!”);
System.exit(0);
}
}
}
}
//增加商品
private static void addCommoddity(ArrayList list) {
//判断是否输入上商品
for (int i = 0; i < list.size(); i++) {
if(list.size()==0){
System.out.println(“商品不存在,请重新输入商品”);
break;
}
}
Scanner sc = new Scanner(System.in);
String name;
//判断为无限循环
while (true){
System.out.println(“请输入商品名称:”);
name = sc.nextLine();
//调用封装好的方法来判断
boolean flag = getSum(list,name);
if(flag){
System.out.println("*****************************");
}else{
break;
}
}
//创建输入对象Scanner
String sname = sc.next();
System.out.println(“请输入商品的价格:”);
double price = sc.nextDouble();
System.out.println(“请输入商品的类别:”);
String sort = sc.next();
System.out.println(“请输入商品的数量:”);
int count = sc.nextInt();
//创建商品对象
Commodity cty = new Commodity();
//给商品赋值
cty.setName(sname);
cty.setPrice(price);
cty.setSort(sort);
cty.setCount(count);
//将商品的信息地址传入集合中
list.add(cty);
//友情提示
System.out.println(“商品添加成功!”);
}
//修改商品信息
private static void reviseCommodity(ArrayList list) {
//创建商品对象
Commodity cty = new Commodity();
Scanner sc = new Scanner(System.in);
System.out.println(“请输入商品名称”);
String name = sc.nextLine();
System.out.println(“请输入新的商品名字:”);
String sname = sc.nextLine();
System.out.println(“请输入新的商品价格:”);
double price = sc.nextDouble();
System.out.println(“请输入商品新的归类:”);
String sort = sc.next();
System.out.println(“请输入商品的数量:”);
int count = sc.nextInt();
cty.setPrice(price);
cty.setName(name);
cty.setSort(sort);
cty.setCount(count);
for (int i = 0; i < list.size(); i++) {
Commodity com = list.get(i);
//如果输入的商品名称和已经有的商品名称一致
if(name.equals(com.getName())){
//修改商品信息
list.set(i,cty);
}
}
System.out.println(“商品信息修改完毕!”);
}
//查询商品
private static void insertCommodity(ArrayList list) {
//商品简单查询
System.out.println(“商品名称\t\t商品价格\t\t商品分类\t\t\t数量”);
for (int i = 0; i < list.size(); i++) {
Commodity som = list.get(i);
System.out.println(som.getName() + “\t\t” + som.getPrice() + “\t\t\t” + som.getSort()+ “\t\t\t”+som.getCount());
}
}
//删除商品
private static void deleteCommodity(ArrayList list) {

    Scanner sc = new Scanner(System.in);
    //根据商品名称来找到索引位置
    System.out.println("请输入商品名称:");
    String name = sc.nextLine();
    //定义初始化索引位置为-1
    int index = -1;
    //用for遍历对象信息,找到对象索引位置,然后索引走if判断语句
    for (int i = 0; i < list.size(); i++) {
        Commodity commodity = list.get(i);
        if(commodity.getName().equals(name)){
            index = i;
            break;
        }
    }
    //接收上面传来的i值,然后运用if判断语句
    if(index==-1){
        System.out.println("该商品不存在,请从新输入");
    }else{
        list.remove(index);
    }
}
//判断是否已经有商品
public static boolean getSum(ArrayList list,String name){
    //定义布尔值来判断
    boolean flag = false;
    //如果商品存在返回true,否则返回false
    for (int i = 0; i < list.size(); i++) {
        Commodity con = list.get(i);
        if(name.equals(con.getName())){
            System.out.println("商品已经存在,请输入其他商品");
            flag = true;
            break;
        }
    }
    return flag;
}

}

你可能感兴趣的:(笔记)