好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现商品管理系统代码实例讲解

实现功能:商品查询,新增,更改价格,以及删除

首先是三个基本类的构建

商品类、账号类、品牌类

1、商品类

?

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

public class Goods {

  //商品信息:商品名称,商品价格,商品销量,商品种类,商品品牌对应编号

  private String goodsName;

  private double goodsPrice;

  private int goodsSales;

  private String goodsCategories;

  private int brandsNum;

  private String GoodsNum;

 

  public Goods(String goodsName, double goodsPrice, int goodsSales, String goodsCategories, int brandsNum, String goodsNum) {

  this .goodsName = goodsName;

  this .goodsPrice = goodsPrice;

  this .goodsSales = goodsSales;

  this .goodsCategories = goodsCategories;

  this .brandsNum = brandsNum;

  GoodsNum = goodsNum;

  }

 

  public int getBrandsNum() {

  return brandsNum;

  }

 

  public void setBrandsNum( int brandsNum) {

  this .brandsNum = brandsNum;

  }

 

  public Goods() {

  }

 

  public String getGoodsNum() {

  return GoodsNum;

  }

 

  public void setGoodsNum(String goodsNum) {

  GoodsNum = goodsNum;

  }

 

  public String getGoodsName() {

  return goodsName;

  }

 

  public void setGoodsName(String goodsName) {

  this .goodsName = goodsName;

  }

 

  public double getGoodsPrice() {

  return goodsPrice;

  }

 

  public void setGoodsPrice( double goodsPrice) {

  this .goodsPrice = goodsPrice;

  }

 

  public int getGoodsSales() {

  return goodsSales;

  }

 

  public void setGoodsSales( int goodsSales) {

  this .goodsSales = goodsSales;

  }

 

  public String getGoodsCategories() {

  return goodsCategories;

  }

 

  public void setGoodsCategories(String goodsCategories) {

  this .goodsCategories = goodsCategories;

  }

 

  public int getBrands() {

  return brandsNum;

  }

 

  public void setBrands( int brandsNum) {

  this .brandsNum = brandsNum;

  }

}

实现了其各个属性的get和set方法

账户类

?

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

public class Accounts {

  //账户信息:账户名称,账户密码

  private String userName;

  private String userPassword;

 

  public Accounts(String userName, String userPassword) {

  this .userName = userName;

  this .userPassword = userPassword;

  }

 

  public Accounts() {

  }

 

  public String getUserName() {

  return userName;

  }

 

  public void setUserName(String userName) {

  this .userName = userName;

  }

 

  public String getUserPassword() {

  return userPassword;

  }

 

  public void setUserPassword(String userPassword) {

  this .userPassword = userPassword;

  }

}

品牌类

?

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

public class Brands {

  //商品品牌信息:商品品牌名称,商品品牌对应编号

  private String brandsName;

  private int brandsNum;

 

  public Brands(String brandsName, int brandsNum) {

  this .brandsName = brandsName;

  this .brandsNum = brandsNum;

  }

 

  public String getBrandsName() {

  return brandsName;

  }

 

  public void setBrandsName(String brandsName) {

  this .brandsName = brandsName;

  }

 

  public int getBrandsNum() {

  return brandsNum;

  }

 

  public void setBrandsNum( int brandsNum) {

  this .brandsNum = brandsNum;

  }

}

之后是一个存储这三个基本类信息的数据类,通过定义List实现三个类的存储

数据类

?

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

import java.util.ArrayList;

import java.util.List;

import java.util.UUID;

 

public class Database {

  //存储用户信息

  private List<Accounts>accountsList= new ArrayList<>();

  //存储商品信息

  private List<Goods>goodsList= new ArrayList<>();

  //存储品牌信息

  private List<Brands>brandsList= new ArrayList<>();

  public Database(){

  //添加初始化信息

  此处自己添加商品,用户信息,品牌信息

  }

 

  public List<Accounts> getAccountsList() {

  return accountsList;

  }

 

  public void setAccountsList(List<Accounts> accountsList) {

  this .accountsList = accountsList;

  }

 

  public List<Goods> getGoodsList() {

  return goodsList;

  }

 

  public void setGoodsList(List<Goods> goodsList) {

  this .goodsList = goodsList;

  }

 

  public List<Brands> getBrandsList() {

  return brandsList;

  }

 

  public void setBrandsList(List<Brands> brandsList) {

  this .brandsList = brandsList;

  }

}

之后是三个基本类各个增删改查方法的实现,这个根据程序需求写

商品交互类

?

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

import java.util.ArrayList;

import java.util.List;

 

 

public class GoodsDAO {

  //调用数据库

  private Database database;

  //初始化商品DAO类,引入数据库

  public GoodsDAO(Database database){

  this .database=database;

  }

  //返回销量最高的商品

  public List<Goods>hotSell(){

  List<Goods> temporList=database.getGoodsList();

  for ( int i= 0 ;i<temporList.size();i++){

  for ( int j= 0 ;j<temporList.size()- 1 -i;j++){

  if (temporList.get(j).getGoodsSales()<temporList.get(j+ 1 ).getGoodsSales())

  {

   Goods e=temporList.get(j+ 1 );

   temporList.set(j+ 1 ,temporList.get(j));

   temporList.set(j,e);

  }

  }

  }

  return temporList;

  }

  //根据商品序号查询商品

  public Goods searchById( int id){

  Goods goods;

  goods=database.getGoodsList().get(id- 1 );

  return goods;

  }

  //根据品牌编号查询商品

  public List<Goods>searchByBranchId( int brandsId){

  List<Goods>temporList= new ArrayList<>();

  for (Goods each:database.getGoodsList()){

  if (each.getBrandsNum()==brandsId)

  temporList.add(each);

  }

  if (temporList.isEmpty())

  temporList= null ;

  return temporList;

  }

  //根据价格区间查询商品

  public List<Goods>searchByPrice( double minPrice, double maxPrice){

  List<Goods>temporList= new ArrayList<>();

  for (Goods each:database.getGoodsList()){

  if (each.getGoodsPrice()>=minPrice&&each.getGoodsPrice()<=maxPrice) {

  temporList.add(each);

  }

  }

  if (temporList.isEmpty())

  temporList= null ;

  return temporList;

  }

  //新增商品

  public void addGoods(Goods goods){

  database.getGoodsList().add(goods);

  }

  //修改商品价格

  public void modify(String goodsId, double price){

  for (Goods each:database.getGoodsList()){

  if (each.getGoodsNum().equals(goodsId))

  each.setGoodsPrice(price);

  }

  }

  //删除商品

  public void delete(String goodsId){

  Goods goods= null ;

  for (Goods each:database.getGoodsList()){

  if (each.getGoodsNum().equals(goodsId))

  goods=each;

  }

  database.getGoodsList().remove(goods);

  //迭代操作时不允许ArrayList被改变,因此重新定义一个对象指向他,迭代完后进行操作。

  }

  //更改商品品牌编号

  public void changeByBranchId( int brandsId){

  for (Goods each:database.getGoodsList()){

  if (each.getBrandsNum()==brandsId)

  each.setBrandsNum(brandsId- 1 );

  }

  }

}

账户交互类实现注册,登录的需求

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class AccountDAO {

  //调用数据库

  private Database database;

  //初始化账户DAO类,引入数据库

  public AccountDAO(Database database){

  this .database=database;

  }

  //根据账户名返回账户类

  public Accounts searchAccounts(String userName){

  Accounts accounts= null ;

  for (Accounts each:database.getAccountsList()){

  if (each.getUserName().equals(userName))

  accounts=each;

  }

  return accounts;

  }

  //添加账户类

  public void addAccount(Accounts accounts){

  database.getAccountsList().add(accounts);

  }

}

品牌类

?

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

public class BrandsDAO {

  //调用数据库

  private Database database;

  //初始化品牌DAO类,引入数据库

  public BrandsDAO(Database database){

  this .database=database;

  }

  //根据品牌名称查询

  public Brands findByName(String brandName){

  Brands brands= null ;

  for (Brands each:database.getBrandsList()){

  if (each.getBrandsName().equals(brandName)) {

  brands = each;

  break ;

  }

  }

  return brands;

  }

  //查询所有品牌

  public void findAll(){

  System.out.println( "现有品牌:" );

  for (Brands each:database.getBrandsList()){

  System.out.println(each.getBrandsNum()+ "\t" +each.getBrandsName());

  }

  }

  //根据品牌编号查询品牌名称

  public String findNameById( int id){

  String brandName;

  Brands brands= null ;

  for (Brands each:database.getBrandsList()){

  if (each.getBrandsNum()==id){

  brands=each;

  }

  }

  return brands.getBrandsName();

  }

  //查询品牌是否拥有

  public boolean findBranch(String brandsName){

  boolean choose= false ;

  for (Brands each:database.getBrandsList())

  {

  if (each.getBrandsName().equals(brandsName))

  choose= true ;

  }

  return choose;

  }

  //新建一个品牌

  public void addBranch(Brands brands){

  database.getBrandsList().add(brands);

  }

  //查找当前品牌的的数量

  public int brandsNumbers(){

  int num=database.getBrandsList().size();

  return num;

  }

  //查找要删除的品牌

  public void deleteBrands( int num){

  Brands brands= null ;

  for (Brands each:database.getBrandsList()){

  if (each.getBrandsNum()==num)

  brands=each;

  }

  database.getBrandsList().remove(brands);

  }

  //移动编号

  public void moveBrandsNum( int brandsNum){

  int index;

  for ( int i= 0 ;i<database.getBrandsList().size();i++){

  if (database.getBrandsList().get(i).getBrandsNum()>brandsNum)

  database.getBrandsList().get(i).setBrandsNum(database.getBrandsList().get(i).getBrandsNum()- 1 );

  }

  }

}

服务类实现项目需要的功能,构建交互界面

?

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

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

public class GoodsSystem {

  Scanner scanner= new Scanner(System.in);

  Random random= new Random();

  AccountDAO accountDAO;

  BrandsDAO brandsDAO;

  GoodsDAO goodsDAO;

  //初始化DAO

  public GoodsSystem(Database database){

  accountDAO= new AccountDAO(database);

  brandsDAO= new BrandsDAO(database);

  goodsDAO= new GoodsDAO(database);

  }

  public void start(){

// 1.登陆

  System.out.println( "----------------商品系统---------------" );

  System.out.println( "1.登录" );

// 2.注册

  System.out.println( "2.注册" );

// 3.退出系统

  System.out.println( "3.退出系统" );

  System.out.println( "请选择:" );

  String choose=scanner.next();

  switch (choose){

  //登录

  case "1" :

  login();

  break ;

  //注册

  case "2" :

  regist();

  break ;

  //退出系统

  case "3" :

  System.out.println( "系统已经退出" );

  break ;

  default :

  System.out.println( "输入错误,请重新输入:" );

  start();

  }

  }

  //登录

  public void login(){

  System.out.println( "请输入用户名:" );

  String userName=scanner.next();

  System.out.println( "请输入密码:" );

  String passWord=scanner.next();

  Accounts accounts=accountDAO.searchAccounts(userName);

  String testNum= "" ;

  if (accounts!= null ){

  if (accounts.getUserPassword().equals(passWord)){

  for ( int i= 0 ;i< 4 ;i++){

   int num=random.nextInt( 10 );

   testNum=testNum+num;

  }

  System.out.println( "验证码:" +testNum);

  System.out.println( "请输入验证码:" );

  String testNumInput=scanner.next();

  if (testNumInput.equals(testNum)) {

   System.out.println( "登录成功" );

   mainMenu();

  }

  else {

   System.out.println( "验证码错误,请重新登录" );

   login();

  }

  }

  else {

  System.out.println( "密码错误,请重新登录" );

  System.out.println( "输入任意键执行操作,或输入0返回上一层目录" );

  String choose=scanner.next();

  if (choose.equals( "0" ))

   start();

  else

   login();

  }

  }

  else {

  System.out.println( "该账户不存在,请重新输入:" );

  System.out.println( "输入任意键执行操作,或输入0返回上一层目录" );

  String choose=scanner.next();

  if (choose.equals( "0" ))

  start();

  else

  login();

  }

  }

  //注册

  public void regist(){

  System.out.println( "----注册----" );

  //用户名

  System.out.println( "请输入用户名:" );

  String userName=scanner.next();

  Accounts accounts=accountDAO.searchAccounts(userName);

  while (accounts!= null ){

  System.out.println( "用户名已经被使用,请重新输入" );

  userName=scanner.next();

  accounts=accountDAO.searchAccounts(userName);

  }

  //密码

  System.out.println( "请输入密码:" );

  String userPassWord=scanner.next();

  System.out.println( "确认密码:" );

  String testPassWord=scanner.next();

  while (!userPassWord.equals(testPassWord)){

  System.out.println( "确认密码与注册密码不相符,请重新输入密码" );

  System.out.println( "请输入密码:" );

  userPassWord=scanner.next();

  System.out.println( "确认密码:" );

  testPassWord=scanner.next();

  }

  //验证码

  String testNum= "" ;

  for ( int i= 0 ;i< 4 ;i++){

  int num=random.nextInt( 10 );

  testNum=testNum+num;

  }

  System.out.println( "验证码:" +testNum);

  System.out.println( "请输入验证码:" );

  String testNumInput=scanner.next();

  if (testNumInput.equals(testNum)) {

  accountDAO.addAccount( new Accounts(userName,userPassWord));

  System.out.println( "注册成功" );

  start();

  }

  else {

  System.out.println( "验证码错误,请重新注册" );

  System.out.println( "输入任意键执行操作,或输入0返回上一层目录" );

  String choose=scanner.next();

  if (choose.equals( "0" ))

  start();

  else

  regist();

  }

  }

  //主菜单

  public void mainMenu(){

  System.out.println( "-------------主菜单------------" );

// 1.热门商品

  System.out.println( "1.热门商品" );

// 2.商品查询

  System.out.println( "2.商品查询" );

// 3.后台管理

  System.out.println( "3.后台管理" );

  System.out.println( "4.退出系统" );

  System.out.println( "请输入你的选择:" );

  String choose=scanner.next();

  switch (choose){

  case "1" : //热门商品

  hotSell();

  break ;

  case "2" : //商品查询

  searchGoods();

  break ;

  case "3" : //后台管理

  controlGoods();

  break ;

  case "4" : //退出系统

  System.out.println( "系统已经退出" );

  break ;

  default :

  mainMenu();

  }

  }

  //热卖商品

  public void hotSell(){

  System.out.println( "------------热销商品------------" );

  List <Goods>tempor=goodsDAO.hotSell();

  System.out.println( "序号\t\t" + "商品" + "\t\t\t\t\t销量\t" );

  int num= 10 ;

  if (tempor.size()< 10 )

  num=tempor.size();

  for ( int i= 0 ;i<num;i++){

  System.out.println((i+ 1 )+ "\t\t" +tempor.get(i).getGoodsName()+ "\t\t\t\t\t" +tempor.get(i).getGoodsSales()+ "\t" );

  }

  System.out.println( "请输入要查询的商品ID或输入0返回上一层菜单。" );

  String chooseEnd=scanner.next();

  if (isNumeric(chooseEnd)== true )

  {

  int choose=Integer.parseInt(chooseEnd);

  if (choose== 0 ){

  mainMenu();

  }

  else

  {

  if (choose<=num&&choose>= 0 ) {

   Goods goods = goodsDAO.searchById(choose);

   System.out.println( "商品名称\t\t品牌\t\t价格\t\t类型" );

   System.out.println(goods.getGoodsName() + "\t\t\t" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + goods.getGoodsPrice() + "元\t" + goods.getGoodsCategories());

   mainMenu();

  }

  else

  {

   System.out.println( "查询的序号超过限定,请重新输入" );

   hotSell();

  }

  }

  }

  else

  {

  System.out.println( "输入错误符号,请重新选择" );

  hotSell();

  }

  }

  //商品查询

  public void searchGoods(){

  System.out.println( "---------商品查询----------" );

// 1.品牌查询

  System.out.println( "1.品牌查询" );

// 2.价格查询

  System.out.println( "2.价格查询" );

  System.out.println( "3.返回上一层目录" );

  System.out.println( "请输入你的选择:" );

  String choose=scanner.next();

  switch (choose){

  case "1" : //品牌查询

  searchByBranch();

  break ;

  case "2" : //价格查询

  searchByPrice();

  break ;

  case "3" :

  mainMenu();

  break ;

  default :

  searchGoods();

  }

  }

  //后台管理

  public void controlGoods(){

  System.out.println( "--------后台管理--------" );

// 1.新增商品

  System.out.println( "1.新增商品" );

// 2.修改价格

  System.out.println( "2.修改价格" );

// 3.删除商品

  System.out.println( "3.删除商品" );

  System.out.println( "4.返回主菜单" );

  System.out.println( "请输入选择:" );

  String choose=scanner.next();

  switch (choose){

  case "1" : //新增商品

  addGoods();

  break ;

  case "2" : //修改价格

  changePrice();

  break ;

  case "3" : //删除商品

  deleteGoods();

  break ;

  case "4" : //返回主菜单

  mainMenu();

  break ;

  default :

  controlGoods();

  }

  }

  //根据品牌查询

  public void searchByBranch(){

  brandsDAO.findAll();

  System.out.println( "请输入要查询的品牌编号:" );

  String brandsNum1=scanner.next();

  if (isNumeric(brandsNum1)) {

  int brandsNum=Integer.parseInt(brandsNum1);

  List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);

  if (temporlist == null ) {

  System.out.println( "没有该品牌的商品" );

  searchGoods();

  }

  for ( int i = 0 ; i < temporlist.size(); i++) {

  System.out.println( "序号" + (i + 1 ) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName());

  }

  System.out.println( "请输入要查询的商品ID或输入0返回上一层菜单。" );

  String choose1 = scanner.next();

  if (isNumeric(choose1)) {

  int choose=Integer.parseInt(choose1);

  if (choose == 0 ) {

   searchGoods();

  } else {

   if (choose>= 1 &&choose<=temporlist.size()){

   Goods goods = temporlist.get(choose - 1 );

   System.out.println( "商品名称:" + goods.getGoodsName() + "\t" + "品牌:" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "价格:" + goods.getGoodsPrice() + "元\t" + "类型:" + goods.getGoodsCategories());

   searchGoods();

   }

   else {

   System.out.println( "输入序号超过边界范围,请重新输入" );

   searchByBranch();

   }

  }

  }

  else {

  System.out.println( "输入错误符号,请重新选择" );

  searchByBranch();

  }

  }

  else {

  System.out.println( "输入错误符号,请重新选择" );

  searchByBranch();

  }

  }

  //根据价格查询

  public void searchByPrice(){

  System.out.println( "请输入要查询的价格区间:最小价格 最大价格" );

  double minPrice=scanner.nextDouble();

  double maxPrice=scanner.nextDouble();

  List<Goods>temporlist=goodsDAO.searchByPrice(minPrice,maxPrice);

  if (temporlist== null ){

  System.out.println( "没有该区间的商品" );

  searchGoods();

  }

  for ( int i= 0 ;i<temporlist.size();i++){

  System.out.println( "序号" +(i+ 1 )+ "\t" + "商品名字:" +temporlist.get(i).getGoodsName());

  }

  System.out.println( "请输入要查询的商品ID或输入0返回上一层菜单。" );

  String choose1 = scanner.next();

  if (isNumeric(choose1)) {

  int choose=Integer.parseInt(choose1);

  if (choose == 0 ) {

  searchGoods();

  }

  else {

  if (choose>= 1 &&choose<=temporlist.size()) {

   Goods goods = temporlist.get(choose - 1 );

   System.out.println( "商品名称:" + goods.getGoodsName() + "\t" + "品牌:" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "价格:" + goods.getGoodsPrice() + "元\t" + "类型:" + goods.getGoodsCategories());

   searchGoods();

  }

  else {

   System.out.println( "输入数字超过边界,请重新输入" );

   searchByPrice();

  }

  }

  }

  else {

  System.out.println( "输入错误符号,请重新选择" );

  searchByPrice();

  }

  }

  //新增商品

  public void addGoods(){

  System.out.println( "请输入商品名称:" );

  String goodsName=scanner.next();

  System.out.println( "请输入商品价格:" );

  double goodsPrice=scanner.nextDouble();

  System.out.println( "请输入品牌" );

  String brandsName=scanner.next();

  int brandsNum;

  if (brandsDAO.findBranch(brandsName)){

  brandsNum=brandsDAO.findByName(brandsName).getBrandsNum();

  }

  else {

  brandsNum=brandsDAO.brandsNumbers()+ 1 ;

  Brands brands= new Brands(brandsName,brandsNum);

  brandsDAO.addBranch(brands);

  }

  System.out.println( "请输入商品类型" );

  String goodsCate=scanner.next();

  Goods goods= new Goods(goodsName,goodsPrice, 0 ,goodsCate,brandsNum, UUID.randomUUID().toString());

  goodsDAO.addGoods(goods);

  System.out.println( "商品添加成功" );

  controlGoods();

  }

  //改变价格

  public void changePrice(){

  System.out.println( "--------修改价格------" );

  brandsDAO.findAll();

  System.out.println( "请输入要查询的品牌编号:" );

  String brandsNum1=scanner.next();

  if (isNumeric(brandsNum1)) {

  int brandsNum=Integer.parseInt(brandsNum1);

  List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);

  if (temporlist == null ) {

  System.out.println( "没有该品牌的商品" );

  changePrice();

  }

  for ( int i = 0 ; i < temporlist.size(); i++) {

  System.out.println( "序号" + (i + 1 ) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName() + "\t价格" + temporlist.get(i).getGoodsPrice() + "元" );

  }

  System.out.println( "请输入要修改价格的商品ID或输入0返回上一层菜单。" );

  String choose1 = scanner.next();

  if (isNumeric(choose1)) {

  int choose=Integer.parseInt(choose1);

  if (choose == 0 ) {

   controlGoods();

  }

  else if (choose<=temporlist.size())

  {

   System.out.println( "请输入要修改的价格:" );

   double price = scanner.nextDouble();

   System.out.println( "是否确定修改价格:是/否" );

   String endChoose = scanner.next();

   if (endChoose.equals( "是" )) {

   goodsDAO.modify(temporlist.get(choose - 1 ).getGoodsNum(), price);

   System.out.println( "价格修改完成" );

   controlGoods();

   } else {

   controlGoods();

   }

  }

  else {

   System.out.println( "输入序号超过边界,请重新输入" );

   changePrice();

  }

  }

  else {

  System.out.println( "输入错误符号,请重新输入" );

  changePrice();

  }

  }

  else {

  System.out.println( "输入错误符号,请重新输入" );

  changePrice();

  }

  }

  //删除商品

  public void deleteGoods(){

  System.out.println( "--------删除商品------" );

  brandsDAO.findAll();

  System.out.println( "请输入要查询的品牌编号:" );

  String brandsNum2=scanner.next();

  if (isNumeric(brandsNum2)) {

  int brandsNum=Integer.parseInt(brandsNum2);

  List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);

  if (temporlist == null ) {

  System.out.println( "没有该品牌的商品" );

  deleteGoods();

  }

  for ( int i = 0 ; i < temporlist.size(); i++) {

  System.out.println( "序号" + (i + 1 ) + "\t" + "商品名字:" + temporlist.get(i).getGoodsName());

  }

  System.out.println( "请输入要删除的商品ID或输入0返回上一层菜单。" );

  String choose1 = scanner.next();

  if (isNumeric(choose1)) {

  int choose=Integer.parseInt(choose1);

  if (choose == 0 ) {

   controlGoods();

  }

  else if (choose<=temporlist.size()){

   System.out.println( "是否确定删除:是/否" );

   String endChoose = scanner.next();

   if (endChoose.equals( "是" )) {

   int brandsNum1 = temporlist.get(choose - 1 ).getBrandsNum();

   goodsDAO.delete(temporlist.get(choose - 1 ).getGoodsNum());

   System.out.println( "删除成功" );

   if (goodsDAO.searchByBranchId(brandsNum1) == null ) {

   brandsDAO.deleteBrands(brandsNum1);

   brandsDAO.moveBrandsNum(brandsNum1);

   for ( int i = brandsNum1; i <= brandsDAO.brandsNumbers(); i++)

   goodsDAO.changeByBranchId(i + 1 );

   }

   controlGoods();

   } else {

   controlGoods();

   }

  }

  else {

   System.out.println( "输入序号超过边界,请重新输入" );

   deleteGoods();

  }

  }

  else {

  System.out.println( "输入错误符号,请重新输入" );

  deleteGoods();

  }

  }

  else {

  System.out.println( "输入错误符号,请重新输入" );

  deleteGoods();

  }

  }

  public boolean isNumeric(String str){

  for ( int i = str.length();--i>= 0 ;){

  if (!Character.isDigit(str.charAt(i))){

  return false ;

  }

  }

  return true ;

  }

}

注:以上代码部分内部类的导入未填写
还需要一个测试类运行
暂且略过

到此这篇关于Java实现商品管理系统代码实例讲解的文章就介绍到这了,更多相关Java实现商品管理系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_39528702/article/details/111635369

查看更多关于Java实现商品管理系统代码实例讲解的详细内容...

  阅读:19次