需求分析:
1.用戶類:賬號(hào),密碼,購(gòu)物車
2.用戶管理類:
(1)注冊(cè)翠语,將注冊(cè)后的user對(duì)象存到一個(gè)集合然后存到文件里
(2)登陸,從文件中獲取集合民镜,遍歷集合啡专,如果賬號(hào)密碼相等則登陸成功
3.管理員類
4.管理員管理類
5.書類
6.商城類
(1)遍歷文件里的書集合
(2)管理員具備的權(quán)利 :添加商品
(3)2.管理具備的權(quán)利:下架商品
(4)利用分頁(yè)的方法顯示商品
(5)把書添加到用戶的購(gòu)物車
(6)查看購(gòu)物車
(7)改變購(gòu)物車
(8)搜索商品
用戶類
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String account;
private String password;
List<Book>list=new ArrayList<>();
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((account == null) ? 0 : account.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (account == null) {
if (other.account != null)
return false;
} else if (!account.equals(other.account))
return false;
return true;
}
}
用戶管理類
import java.io.*;
import java.util.*;
import java.util.*;
public class UsersManger {
//注冊(cè)
public void registUser(String account,String password) throws Exception {
User user=new User(account,password);
File file=new File("user.txt");
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
if(file.length()==0){
List<User> list=new ArrayList<>();
list.add(user);
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
System.out.println("注冊(cè)成功1");
}else{
ois=new ObjectInputStream(new FileInputStream(file));
List<User>list=(List<User>)ois.readObject();
for(User u:list){
if(u.getAccount().equals(account)){
System.out.println("你注冊(cè)的賬號(hào)已存在");
}else{
list.add(user);
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
System.out.println("注冊(cè)成功2");
}
}
}
//ois.close();
//oos.close();
}
//登陸
public boolean loginUser(String account,String password) throws Exception{
File file=new File("user.txt");
ObjectInputStream ois=null;
ois=new ObjectInputStream(new FileInputStream(file));
List<User>list=(List<User>)ois.readObject();
boolean b=false;
for(User u:list){
if(u.getAccount().equals(account)&&u.getPassword().equals(password)){
b=true;
}else{
b=false;
}
}
if(b){
System.out.println("登陸成功");
}else{
System.out.println("賬號(hào)或者密碼錯(cuò)誤再想一想");
}
ois.close();
return b;
}
}
管理員類
public class Administrator implements Serializable {
private static final long serialVersionUID = 1L;
private String account;
private String password;
public Administrator(String account, String password) {
super();
this.account = account;
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
管理員管理類
public class Administrator implements Serializable {
private static final long serialVersionUID = 1L;
private String account;
private String password;
public Administrator(String account, String password) {
super();
this.account = account;
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
書類
import java.io.Serializable;
public class Book implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String bookName;
private String bookAuther;
private int price;
private int amount;
public Book(String bookName, String bookAuther, int price, int amount) {
super();
this.bookName = bookName;
this.bookAuther = bookAuther;
this.price = price;
this.amount = amount;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuther() {
return bookAuther;
}
public void setBookAuther(String bookAuther) {
this.bookAuther = bookAuther;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String toString() {
return "書名:" + bookName + ", 作者:" + bookAuther + ", 價(jià)錢:" + price + ", 數(shù)量:" + amount;
}
}
商城類
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class Shop {
//列出商品,返回的是商城里的商品集合
public List<Book> listBook() throws FileNotFoundException, IOException, ClassNotFoundException{
File file=new File("book.txt");
List<Book> list=new ArrayList<Book>();
ObjectInputStream ois=null;
if(file.length()==0){
System.out.println("商城里沒(méi)有商品");
}else{
ois=new ObjectInputStream(new FileInputStream(file));
list=(List<Book>)ois.readObject();
}
ois.close();
return list;
}
//遍歷商城了里書的集合
public void bianliBook(List<Book> list){
System.out.print("書名\t");
System.out.print("作者\(yùn)t");
System.out.print("價(jià)錢\t");
System.out.print("數(shù)量\t");
System.out.println();
for(Book u:list){
System.out.print(u.getBookName()+"\t");
System.out.print(u.getBookAuther()+"\t");
System.out.print(u.getPrice()+"\t");
System.out.print(u.getAmount()+"\t");
System.out.println();
}
}
//1.管理員具備的權(quán)利 :添加商品
public void addBook(Book book) throws FileNotFoundException, IOException, ClassNotFoundException{
File file=new File("book.txt");
ObjectOutputStream oos=null;
if(file.length()==0){ //如果里面沒(méi)有書直接往里面加書
List<Book>list=new ArrayList<>();
list.add(book);
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
}else{ //獲取文件中集合,添加商品到集合制圈,然后重新存入
List<Book> list=listBook();
list.add(book);
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
}
System.out.println("添加成功");
//oos.close();
//ois.close();
}
//2.管理具備的權(quán)利:下架商品
public void removeBook(String bname) throws FileNotFoundException, IOException, ClassNotFoundException{
File file=new File("book.txt");
ObjectOutputStream oos=null;
if(file.length()==0){
System.out.println("沒(méi)有書了不能下架了");
}else{ //先找出文件中存商品的集合
List<Book> list=listBook();
Iterator<Book> it=list.iterator();
while(it.hasNext()){ //如果文件中商品是你想下架的商品就從集合刪除
if(it.next().getBookName().equals(bname)){
it.remove();
break;
}
} //把刪除后的集合重新存到文件中
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
}
}
//利用分頁(yè)的方法顯示商品
public void fenYe(List<Book> list){
Scanner in=new Scanner(System.in);
int i=0;
while(true){
System.out.println("1.首頁(yè)2.下一頁(yè)3.上一頁(yè)4.尾頁(yè)5.退出選擇商品");
int a=in.nextInt();
if(a==1){ //首頁(yè)
i=0;
for(;i<3;i++){
System.out.println((i+1)+"."+list.get(i).getBookName());
}
}else if(a==2){ //下一頁(yè)
int j=i+3;
if(j<=list.size()){
for(;i<j;i++){
System.out.println((i+1)+"."+list.get(i).getBookName());
}
}else{
System.out.println("不能再往后了");
continue;
}
}else if(a==3){//上一頁(yè)
int j=i-6;
if(j>=0){
i=i-3;
for(;j<i;j++){
System.out.println((j+1)+"."+list.get(j).getBookName());
}
}else{
System.out.println("不能再往前了");
continue;
}
}else if(a==4){//尾頁(yè)
int j=list.size()-3;
i=6;
for(;j<list.size();j++){
System.out.println((j+1)+"."+list.get(j).getBookName());
}
}else{
break;
}
}
}
public void listBook(User u) throws FileNotFoundException, ClassNotFoundException, IOException{
List<Book>list=listBook();
fenYe(list);
System.out.println("請(qǐng)輸入你選擇商品");
Scanner in=new Scanner(System.in);
int d=in.nextInt();
System.out.println(list.get(d-1));//查看選擇的商品詳情
System.out.println("是/否加入購(gòu)物車们童?");
in=new Scanner(System.in);
String buy=in.next();
if(buy.equals("是")){ //加入購(gòu)物車
addCar(u,list.get(d-1));
System.out.println("1返回上一級(jí)2.查看購(gòu)物車");
in=new Scanner(System.in);
int fan=in.nextInt();
if(fan==1){
listBook(u); //返回到商品分頁(yè)查看的地方
}else if(fan==2){
List<Book> lists=checkCar(u); //查看購(gòu)物車
changeCar(u,lists);
}else{
System.out.println("別瞎輸入好么");
}
}else if(buy.equals("否")){ //不加入購(gòu)物車
listBook(u); //返回listbook
}
}
//把書添加到用戶的購(gòu)物車
public void addCar(User u,Book book) throws FileNotFoundException, IOException, ClassNotFoundException{
u.list.add(book); //添加到用戶購(gòu)物車
File file=new File("user.txt");
ObjectOutputStream oos=null;
ObjectInputStream ois=null; //重新獲取這個(gè)用戶
ois=new ObjectInputStream(new FileInputStream(file));
List<User>list=(List<User>)ois.readObject();
for(User us:list){ //將當(dāng)前用戶與存到文件中的用戶交換數(shù)據(jù)
if(us.getAccount().equals(u.getAccount())){
us=u;
}
} //重新把用戶存進(jìn)去
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
}
//查看購(gòu)物車
public List<Book> checkCar(User u) throws FileNotFoundException, ClassNotFoundException, IOException{
int num=1;
List<Book>list=u.list;
System.out.print("ID\t");
System.out.print("書名\t");
System.out.print("單價(jià)\t");
System.out.print("購(gòu)買數(shù)量\t");
System.out.print("總價(jià)\t");
System.out.println();
for(int i=0;i<u.list.size();i++){
System.out.print(num+"\t");
System.out.print(u.list.get(i).getBookName()+"\t");
System.out.print(u.list.get(i).getPrice()+"\t");
System.out.print(u.list.get(i).getAmount()+"\t");
System.out.print(u.list.get(i).getAmount()*u.list.get(i).getPrice()+"\t");
System.out.println();
}
return list;
}
public void changeCar(User u,List<Book> list) throws FileNotFoundException, ClassNotFoundException, IOException{
System.out.println("請(qǐng)輸入你想修改的數(shù)量的書的名字");
Scanner in=new Scanner(System.in);
String name=in.next();
System.out.println("請(qǐng)輸入想買多少本");
in=new Scanner(System.in);
int shu=in.nextInt();
List<Book> l=listBook();
for(Book b:l){
if(b.getBookName().equals(name)){
if(b.getAmount()>shu){
System.out.println("修改成功");
int i=0;
for(;i<u.list.size();i++){
if(b.getBookName().equals(u.list.get(i).getBookName())){
break;
}
}
u.list.get(i).setAmount(shu);
//這有個(gè)小bug,數(shù)量改變沒(méi)有添加到文件中
//而且shop里的商品數(shù)量也還沒(méi)有減掉
checkCar(u);
System.exit(0);
}else{
System.out.println("沒(méi)有那么些書");
}
}
}
}
//搜索書
public Book searchBook(String name) throws FileNotFoundException, IOException, ClassNotFoundException{
File file=new File("book.txt");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
List<Book> list=(List)ois.readObject();
for(Book b:list){
if(b.getBookName().equals(name)){
System.out.println(b);
return b;
}
}
return null;
}
}
測(cè)試類
import java.util.*;
public class Demo1 {
public static void main(String[] args) throws Exception {
System.out.println("歡迎來(lái)到千鋒商城");
while(true){
System.out.println("選擇你想要進(jìn)行的操作1登陸2注冊(cè)");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
if(a==1){//登陸
System.out.println("1登陸管理員2登陸用戶");
in = new Scanner(System.in);
int b=in.nextInt();
if(b==1){ //登陸管理員
System.out.println("請(qǐng)輸入賬號(hào)");
in = new Scanner(System.in);
String account=in.next();
System.out.println("請(qǐng)輸入密碼");
in = new Scanner(System.in);
String password=in.next();
AdministratorManger am=new AdministratorManger();
boolean deng=am.loginAdministrator(account, password);
Administrator at=new Administrator(account,password);
if(deng){
Shop shop=new Shop();
while(true){
System.out.println("1添加商品2下架商品3退出");
in=new Scanner(System.in);
int ada=in.nextInt();
if(ada==1){
System.out.println("請(qǐng)輸入書名");
in=new Scanner(System.in);
String bname=in.next();
System.out.println("請(qǐng)輸入作者");
in=new Scanner(System.in);
String rname=in.next();
System.out.println("請(qǐng)輸入價(jià)錢");
in=new Scanner(System.in);
int price=in.nextInt();
System.out.println("請(qǐng)輸入數(shù)量");
in=new Scanner(System.in);
int ac=in.nextInt();
Book book=new Book(bname,rname,price,ac);
shop.addBook(book);
}else if(ada==2){
shop.bianliBook(shop.listBook());
System.out.println("請(qǐng)輸入書名");
in=new Scanner(System.in);
String bname=in.next();
shop.removeBook(bname);
System.out.println("下架成功");
}else if(ada==3){
System.exit(0);
}
}
}
}else if(b==2){ //登陸用戶
System.out.println("請(qǐng)輸入賬號(hào)");
in = new Scanner(System.in);
String account=in.next();
System.out.println("請(qǐng)輸入密碼");
in = new Scanner(System.in);
String password=in.next();
UsersManger um=new UsersManger();
boolean deng=um.loginUser(account, password);
User u=new User(account,password);
Shop shop=new Shop();
if(deng){
System.out.println("1.查看商品2.搜索商品3.退出");
in=new Scanner(System.in);
int c=in.nextInt();
if(c==1){ //查看商品
shop.listBook(u);
}else if(c==2){//搜索商品
System.out.println("請(qǐng)輸入你想搜索的書名");
in=new Scanner(System.in);
String name=in.next();
Book book=shop.searchBook(name);
shop.listBook(u);
}else if(c==3){//退出
System.exit(0);
}
}
}
}else if(a==2){ //注冊(cè)
System.out.println("1注冊(cè)管理員2注冊(cè)用戶");
in = new Scanner(System.in);
int c=in.nextInt();
if(c==1){ //注冊(cè)管理員
System.out.println("請(qǐng)輸入賬號(hào)");
in = new Scanner(System.in);
String account=in.next();
System.out.println("請(qǐng)輸入密碼");
in = new Scanner(System.in);
String password=in.next();
AdministratorManger am=new AdministratorManger();
am.registAdministrator(account, password);
}else if(c==2){ //注冊(cè)用戶
System.out.println("請(qǐng)輸入賬號(hào)");
in = new Scanner(System.in);
String account=in.next();
System.out.println("請(qǐng)輸入密碼");
in = new Scanner(System.in);
String password=in.next();
UsersManger um=new UsersManger();
um.registUser(account, password);
}else{
System.out.println("請(qǐng)按照規(guī)則輸入");
}
}else{
System.out.println("請(qǐng)按照規(guī)則輸入");
}
}
}
}