首先将用户名密码封装到一个类中,另一个类保存图书信息。
package com.jereh4;
import java.util.ArrayList;
import java.util.List;
class Book{
private List bookList ;
public List getList(){
bookList = new ArrayList();
bookList.add(10001);
bookList.add("天空");
bookList.add("张三");
bookList.add("2012-06");
bookList.add(23.5);
bookList.add(30);
bookList.add(10002);
bookList.add("购物");
bookList.add("李四");
bookList.add("2013-11");
bookList.add(33.7);
bookList.add(28);
bookList.add(10003);
bookList.add("美女");
bookList.add("王五");
bookList.add("2014-03");
bookList.add(63.2);
bookList.add(48);
bookList.add(10004);
bookList.add("交流");
bookList.add("赵六");
bookList.add("2015-09");
bookList.add(16.4);
bookList.add(52);
return bookList;
}
}
public class Information {
private String username = "user";
private int password = 123;
private String admin = "admin";
private int pwd = 888 ;
public String getUsername(){
return username;
}
public int getPassword(){
return password;
}
public String getAdmin(){
return admin;
}
public int getPwd(){
return pwd;
}
}
然后主代码实现(假设用户都是善良的用户,不去刻意找BUG):
package com.jereh4;
import java.util.*;
public class Demo_bing {
public static void main(String[] args){
System.out.println("*******************欢迎使用图书书店*******************");
// Information类中保存用户名和密码,Book类中保存现有图书,定义List容器保存图书信息.
Scanner scn = new Scanner(System.in);
Information message = new Information();
Book book = new Book();
List list = new ArrayList();
List bookList = book.getList();
// 获取Information类中用户名和密码.
String user = message.getUsername();
int password = message.getPassword();
String admin = message.getAdmin();
int pwd = message.getPwd();
/*
* 定义passInput表示输入的密码,count判断是否输出容器元素,chooseNum表示选择操作,bookId表示图书号,bookNum表示图书数量,firstPosition表示
* 每行的第一个位置,lastPosition表示每行的最后一个位置,jadge判断进行了哪种操作,bookPrice表示图书价格,chr判断是否继续操作,userInput表示
* 输入的用户名,bookName表示输入的图书书名,date表示出版日期,bookActor表示图书作者.
*
*/
int passInput = 0 , count = -1 ,chooseNum = 0 , bookId = 0 , bookNum = 0 , firstPosition = 0 , lastPosition = 0 , jadge = 0;
double bookPrice = 0 ;
char chr = ‘y‘;
String userInput = null , bookName = null , date = null , bookActor = null ;
while(true){
System.out.println("请输入用户名:");
userInput = scn.next();
System.out.println("请输入密码:");
passInput = scn.nextInt();
// if-else判断输入的信息,进入不同的界面.
if(userInput.equals(user) && passInput == password){
System.out.println("登录成功,尊敬的用户您好!\n");
while(true){
System.out.println("\t\t\t*******************欢迎使用网上购书系统*******************\n");
System.out.println("\t\t书号\t\t书名\t\t作者\t\t发布日期\t\t价格\t\t库存");
// 用for循环来输出容器中图书的信息.
for(int j = 0 , m = 1; j < bookList.size();j++,m++){
if(firstPosition == 0){
System.out.print("\t\t"+bookList.get(j));
if(m%6==0&&m>0)
System.out.println();
}
}
System.out.print("\n");
System.out.println("*******************请选择进行的操作:\t1.查看图书\t2.购买图书\t3.退出系统\t*******************");
chooseNum = scn.nextInt();
switch(chooseNum){
case 1:
System.out.println("\t\t\t*********您选择了查看图书的操作,以下是本店的图书*********");
System.out.print("\n");
System.out.println("\t\t书号\t\t书名\t\t作者\t\t发布日期\t\t价格\t\t库存");
// 用for循环来输出容器中图书的信息.
for(int j = 0 , m = 1; j < bookList.size();j++,m++){
if(firstPosition == 0){
System.out.print("\t\t"+bookList.get(j));
if(m%6==0&&m>0)
System.out.println();
}
}
System.out.print("\n");
System.out.println("是否继续操作?(y/n)");
chr = scn.next().charAt(0);
if(chr == ‘y‘){
break;
}else{
System.out.println("谢谢您的使用,欢迎再来!");
System.exit(0);
}
break;
case 2:
System.out.println("\t\t\t*********您选择了购买图书的操作,以上是本店的图书*********");
System.out.print("\n");
System.out.println("***请输入您要购买的图书信息***");
System.out.print("\n");
System.out.print("请输入图书ID:");
bookId = scn.nextInt();
System.out.print("请输入购买数量:");
bookNum = scn.nextInt();
// 用for循环来查找输入的书号,并取出此行中其他信息.
for(int i = 0; i <= 18; i+=6 ){
if(bookId == (Integer)bookList.get(i)){
bookName = (String) bookList.get(i+1);
bookPrice = (Double) bookList.get(i+4);
}
}
System.out.print("\n");
System.out.println("您选择了《"+bookName+"》,单价¥:"+bookPrice+"元");
System.out.println("您选择的购买数量为:"+bookNum+"本");
System.out.println("总共消费:"+bookNum*bookPrice+"元");
System.out.print("\n");
System.out.println("是否继续操作?(y/n)");
chr = scn.next().charAt(0);
if(chr == ‘y‘){
break;
}else{
System.out.println("谢谢您的使用,欢迎再来!");
System.exit(0);
}
break;
case 3:
System.out.println("谢谢您的使用,欢迎再来!");
System.exit(0);
break;
default:
System.out.println("***输入有误,请重新输入***");
break;
}
}
}else if(userInput.equals(admin) && passInput == pwd){
System.out.println("登录成功,尊敬的管理员您好,欢迎来到系统后台!\n");
while(true){
System.out.println("\t\t\t*******************欢迎使用图书管理系统*******************\n");
System.out.print("\n");
System.out.println("\t\t书号\t\t书名\t\t作者\t\t发布日期\t\t价格\t\t库存");
count = 0;
while(count != -1){
// 用for循环来输出容器中图书的信息.
for(int j = 0 , m = 1; j < bookList.size();j++,m++){
System.out.print("\t\t"+bookList.get(j));
if(m%6==0&&m>0)
System.out.println();
}
/*
* 用for循环来输出新添加到容器中图书的信息,firstPosition值若不为0则表示已经过入库或出口操作,用jadge判断进行入库或出库操作,
* 更新对应的数据.
*
*/
for(int j = 0 , m = 1; j < list.size();j++,m++){
if(firstPosition == 0){
System.out.print("\t\t"+list.get(j));
if(m%6==0&&m>0)
System.out.println();
}else if(jadge==1){
for(int q = 0; q <= list.size(); q+=6){
if(firstPosition == (Integer)list.get(q)){
int num = lastPosition+(Integer)list.get(q+5);
list.remove(q+5);
list.add(q+5 , num);
firstPosition = 0;
j--;
m--;
break;
}
}
}else if(jadge==2){
for(int q = 0; q <= list.size(); q+=6){
if(firstPosition == (Integer)list.get(q)){
int num = (Integer)list.get(q+5) - lastPosition;
list.remove(q+5);
list.add(q+5 , num);
firstPosition = 0;
j--;
m--;
break;
}
}
}
}
break;
}
System.out.print("\n");
System.out.println("***************请选择进行的操作:\t1.图书入库\t2.图书出库\t3.新增图书\t4.退出系统\t***************");
chooseNum = scn.nextInt();
switch(chooseNum){
case 1:
System.out.println("\t\t\t*********您选择了图书入库的操作,以上是本店的图书*********");
System.out.print("\n");
System.out.println("***请输入您要入库的图书信息***");
System.out.print("\n");
System.out.print("请输入您要入库的图书ID:");
bookId = scn.nextInt();
System.out.print("请输入您要入库的图书数量:");
bookNum = scn.nextInt();
jadge = 1;
firstPosition = bookId ;
lastPosition = bookNum;
// 用for循环来查找输入的书号,并取出此行中其他信息.
for(int i = 0; i <= 18; i+=6 ){
if(firstPosition == (Integer)bookList.get(i)){
int num = (Integer)bookList.get(i+5) + lastPosition;
bookList.remove(i+5);
bookList.add(i+5 , num);
}
}
System.out.print("\n");
System.out.println("是否确定入库?(y/n)");
chr = scn.next().charAt(0);
if(chr == ‘y‘){
break;
}else{
System.out.println("谢谢您的使用,欢迎再来!");
System.exit(0);
}
break;
case 2:
System.out.println("\t\t\t*********您选择了图书出库的操作,以上是本店的图书*********");
System.out.print("\n");
System.out.println("***请输入您要出库的图书信息***");
System.out.print("\n");
System.out.print("请输入您要出库的图书ID:");
bookId = scn.nextInt();
System.out.print("请输入您要出库的图书数量:");
bookNum = scn.nextInt();
jadge = 2;
firstPosition = bookId ;
lastPosition = bookNum;
// 用for循环来查找输入的书号,并取出此行中其他信息.
for(int i = 0; i <= 18; i+=6 ){
if(firstPosition == (Integer)bookList.get(i)){
int num = (Integer)bookList.get(i+5) - lastPosition;
bookList.remove(i+5);
bookList.add(i+5 , num);
}
}
System.out.print("\n");
System.out.println("是否确定出库?(y/n)");
chr = scn.next().charAt(0);
if(chr == ‘y‘){
break;
}else{
System.out.println("谢谢您的使用,欢迎再来!");
System.exit(0);
}
break;
case 3:
System.out.println("\t\t\t*********您选择了新增图书的操作,请输入图书信息*********");
System.out.print("\n");
System.out.println("***请输入您要新增的图书信息***");
System.out.print("\n");
System.out.print("请输入图书ID:");
bookId = scn.nextInt();
System.out.print("请输入图书书名:");
bookName = scn.next();
System.out.print("请输入图书作者:");
bookActor = scn.next();
System.out.print("请输入出版时间:");
date = scn.next();
System.out.print("请输入图书价格:");
bookPrice = scn.nextDouble();
System.out.print("请输入图书数量:");
bookNum = scn.nextInt();
// 用while循环添加图书信息到容器中.
while(true){
list.add(bookId);
count ++;
list.add(bookName);
count ++;
list.add(bookActor);
count ++;
list.add(date);
count ++;
list.add(bookPrice);
count ++;
list.add(bookNum);
count ++;
break;
}
System.out.print("\n");
System.out.println("是否确定新增?(y/n)");
chr = scn.next().charAt(0);
if(chr == ‘y‘){
break;
}else{
System.out.println("谢谢您的使用!");
System.exit(0);
}
break;
case 4:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
default:
System.out.println("***输入有误,请重新输入***");
break;
}
}
}else{
System.out.println("帐号或密码错误,请重新输入!");
System.out.print("\n");
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-15 08:14:15