TJETC Java实训03:Java模拟购物商城


实训第三天,接着写Java模拟实现的一个购物商城的功能,由于只是简单的模拟,故数据库也省去了,干脆也模拟了。整体包括4个功能模块:

书籍(书名,ISBN,价格,作者,出版社,……)

用户(用户名,密码,……)

购物车(书籍,数量,小计,……)

结算(用户,总额,……)

老师家的宝贝狗狗挂了,故昨天老师木有来上课,晚上才到了,偶们整了一天这堆代码……下面是简单的代码实现,eclipse下写的,命令行模式。

package com.chinasofti.etc.view;

package com.chinasofti.etc.view;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import com.chinasofti.etc.biz.BookBiz;
import com.chinasofti.etc.biz.GoodBiz;
import com.chinasofti.etc.biz.UserBiz;
import com.chinasofti.etc.biz.seimpl.BookBizImpl;
import com.chinasofti.etc.biz.seimpl.GoodBizImpl;
import com.chinasofti.etc.biz.seimpl.UserBizImpl;
import com.chinasofti.etc.database.Database;
import com.chinasofti.etc.po.Book;
import com.chinasofti.etc.po.Good;

public class BookShopView {

	public Scanner input = new Scanner(System.in);
	public int answer;
	public int bookIdSelect;
	public String user = null;

	private BookBiz bookBiz = new BookBizImpl();
	private UserBiz userBiz = new UserBizImpl();
	private GoodBiz goodBiz = new GoodBizImpl();

	/**
	 * 用户登录
	 */

	public boolean userLogin() {
		System.out.println("请输入用户名和密码:");
		String userName = input.next();
		String userPassword = input.next();
		if (userBiz.isValidateUser(userName, userPassword)) {
			user = userName;// 暂存登录用户名
			return true;
		}
		return false;// 此方法返回判断结果
	}

	/**
	 * 显示书籍列表
	 */
	public void showBookList() {
		List<Book> books = bookBiz.findAllBooks();
		System.out.println("所有书籍如下:");
		System.out.println("序号\tISBN\t书名");
		// 遍历列表书籍
		Iterator<Book> iterator = books.iterator();
		while (iterator.hasNext()) {
			Book book = iterator.next();
			System.out.println(book.getBookId() + "\t" + book.getBookIsbn() + "\t" + book.getBookName());
		}
		System.out.println("请输入:(1-" + books.size() + ":选择):");// 选择对应的书的序号
	}

	/**
	 * 显示书籍详细信息
	 * 
	 * @param bookId
	 */
	public void showBookDetails(int bookId) {
		Book book = bookBiz.findBookByBookId(bookId);
		System.out.println("书籍详细信息:");
		System.out.println("序号\tISBN\t书名\t价格\t作  者\t\t出版社");
		System.out.println(book.getBookId() + "\t" + book.getBookIsbn() + "\t" + book.getBookName() + "\t" + book.getBookPrice() + "\t" + book.getBookAuthor() + "\t" + book.getBookPulisher());
	}

	/**
	 * 购物车
	 */
	public void GouWuChe(int bookId, int bookNum) {
		Good good = goodBiz.findGoodByBookIdNum(bookId, bookNum);
		showGoodList();
	}

	public void showGoodList() {
		List<Good> goods = goodBiz.findAllGoods();
		Book book;
		System.out.println("购物车列表如下:");
		// 遍历购物车书籍
		Iterator<Good> iterator = goods.iterator();
		while (iterator.hasNext()) {
			Good good = iterator.next();
			showBookDetails(good.getBookId());
			System.out.print("购买数量:" + good.getBookNum());
			book = bookBiz.findBookByBookId(good.getBookId());
			System.out.println("\t\t总价:" + book.getBookPrice() * good.getBookNum());
		}
	}

	/**
	 * 支付中心
	 */
	public void payCenter() {
		double totalPay = 0;
		List<Good> goods = goodBiz.findAllGoods();
		Book book;
		Iterator<Good> iterator = goods.iterator();
		while (iterator.hasNext()) {
			Good good = iterator.next();
			book = bookBiz.findBookByBookId(good.getBookId());
			totalPay += book.getBookPrice() * good.getBookNum();
		}
		System.out.println("用户名称:" + user);
		System.out.println("支付金额:" + totalPay);
		goodBiz.deleteGoodList();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean flag = true;
		Database.genDatabase();// 数据库初始化

		BookShopView bsv;
		bsv = new BookShopView();

		while (bsv.userLogin() != true) {// 用户登录循环判断
			System.out.println("Login Failed!");
		}

		while (flag) {
			System.out.println("---------------------------");
			// 功能A:显示书籍列表
			bsv.showBookList();
			bsv.answer = bsv.input.nextInt();

			bsv.bookIdSelect = bsv.answer;// 暂存书籍序号
			System.out.println("---------------------------");

			// 功能B:显示书籍详细信息
			bsv.showBookDetails(bsv.answer);
			System.out.println("请输入:(0:返回    1:购买)");// 选择:买或者返回
			bsv.answer = bsv.input.nextInt();

			System.out.println("---------------------------");
			if (bsv.answer == 0) {// 返回列表了
				continue;
			} else {// 输入购买数量
				System.out.println("请输入购买数量:(0:返回)");
				bsv.answer = bsv.input.nextInt();

				if (bsv.answer == 0) {
					continue;
				} else {
					System.out.println("---------------------------");

					// 功能C:购物车
					bsv.GouWuChe(bsv.bookIdSelect, bsv.answer);
					System.out.println("---------------------------");

					// 功能D:结算
					System.out.println("请输入:(0:继续购物/1:结算)");
					bsv.answer = bsv.input.nextInt();
					if (bsv.answer == 0) {
						continue;
					} else {
						System.out.println("---------------------------");
						bsv.payCenter();
						System.out.println("请输入:(0:继续)");
						bsv.answer = bsv.input.nextInt();
						if (bsv.answer == 0) {
							continue;
						} else {
							System.out.println("----退出----");
							break;
						}
					}
				}
			}
		}
	}
}

package com.chinasofti.etc.po;

package com.chinasofti.etc.po;

public class Book {
	private int bookId;
	private String bookIsbn;
	private String bookName;
	private double bookPrice;
	private String bookAuthor;
	private String bookPulisher;
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookIsbn() {
		return bookIsbn;
	}
	public void setBookIsbn(String bookIsbn) {
		this.bookIsbn = bookIsbn;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public double getBookPrice() {
		return bookPrice;
	}
	public void setBookPrice(double bookPrice) {
		this.bookPrice = bookPrice;
	}
	public String getBookAuthor() {
		return bookAuthor;
	}
	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}
	public String getBookPulisher() {
		return bookPulisher;
	}
	public void setBookPulisher(String bookPulisher) {
		this.bookPulisher = bookPulisher;
	}
	public Book(int bookId, String bookIsbn, String bookName, double bookPrice,
			String bookAuthor, String bookPulisher) {
		super();
		this.bookId = bookId;
		this.bookIsbn = bookIsbn;
		this.bookName = bookName;
		this.bookPrice = bookPrice;
		this.bookAuthor = bookAuthor;
		this.bookPulisher = bookPulisher;
	}
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public Book clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		Book book = new Book(this.bookId, this.bookIsbn, this.bookName, this.bookPrice, this.bookAuthor, this.bookPulisher);
		return book;
	}
	
	

}

package com.chinasofti.etc.po;

public class Good {
	private int bookId;
	private int bookNum;
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public int getBookNum() {
		return bookNum;
	}
	public void setBookNum(int bookNum) {
		this.bookNum = bookNum;
	}
	public Good(int bookId, int bookNum) {
		super();
		this.bookId = bookId;
		this.bookNum = bookNum;
	}
	public Good() {
		super();
		// TODO Auto-generated constructor stub
	}	
}

package com.chinasofti.etc.po;

public class User {
	private String userName;
	private String userPassword;
	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;
	}
	public User(String userName, String userPassword) {
		super();
		this.userName = userName;
		this.userPassword = userPassword;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

package com.chinasofti.etc.database;

package com.chinasofti.etc.database;

import java.util.ArrayList;
import java.util.List;

import com.chinasofti.etc.po.*;

public class Database {

	public static List<User> users = new ArrayList<User>();
	public static List<Book> books = new ArrayList<Book>();
	public static List<Good> goods = new ArrayList<Good>();

	public static void genDatabase() {
		User user1 = new User("zhangsan", "zhangsan");
		User user2 = new User("lisi", "lisi");
		User user3 = new User("wangwu", "wangwu");
		users.add(user1);
		users.add(user2);
		users.add(user3);

		Book book1 = new Book(1, "1111", "AAA", 10, "AAAAuthor", "AAAPulisher");
		Book book2 = new Book(2, "2222", "BBB", 20, "BBBAuthor", "BBBPulisher");
		Book book3 = new Book(3, "3333", "CCC", 30, "CCCAuthor", "CCCPulisher");
		books.add(book1);
		books.add(book2);
		books.add(book3);
	}
	
	//购物车
	public static void addGoodList(int bookId, int bookNum) {
		Good good = new Good(bookId, bookNum);
		goods.add(good);
	}
	
	public static void deleteGoodList(){
		goods.removeAll(goods);
		System.out.println("----购物车已清空----");
	}
	
}

package com.chinasofti.etc.dao.seimpl;

package com.chinasofti.etc.dao.seimpl;

import java.util.Iterator;
import java.util.List;

import com.chinasofti.etc.dao.BookDao;
import com.chinasofti.etc.database.Database;
import com.chinasofti.etc.po.Book;

public class BookDaoImpl implements BookDao {

	@Override
	public List<Book> findAllBooks() {
		// TODO Auto-generated method stub
		return Database.books;
	}

	@Override
	public Book findBookByBookId(int bookId) {
		// TODO Auto-generated method stub
		Book book = null;
		List<Book> books = this.findAllBooks();
		Iterator<Book> iterator = books.iterator();
		while (iterator.hasNext()) {
			Book b = iterator.next();
			if (b.getBookId() == bookId) {
				try {
					book = b.clone();
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		}
		return book;
	}

}

package com.chinasofti.etc.dao.seimpl;

import java.util.Iterator;
import java.util.List;

import com.chinasofti.etc.biz.GoodBiz;
import com.chinasofti.etc.biz.seimpl.GoodBizImpl;
import com.chinasofti.etc.dao.GoodDao;
import com.chinasofti.etc.database.Database;
import com.chinasofti.etc.po.Good;

public class GoodDaoImpl implements GoodDao {
	
	boolean flag = true;
	
	@Override
	public List<Good> findAllGoods() {
		// TODO Auto-generated method stub
		return Database.goods;
	}
	
	@Override
	public Good findGoodByBookIdNum(int bookId, int bookNum) {
		// TODO Auto-generated method stub
		Good good = null;
		GoodBiz goodBiz = new GoodBizImpl();
		List<Good> goods = this.findAllGoods();
		Iterator<Good> iterator = goods.iterator();
		while(iterator.hasNext()){
			Good g = iterator.next();
			if(g.getBookId() == bookId){//如果重复了,就只改变书的数量
				g.setBookNum(g.getBookNum() + bookNum);
				flag = false;
				break;
			}
		}
		if (flag) {//否则就添加一个新的进去
			good = goodBiz.addGoodByBookIdNum(bookId, bookNum);
		}
		return good;
	}

	@Override
	public Good addGoodByBookIdNum(int bookId, int bookNum) {
		// TODO Auto-generated method stub
		Database.addGoodList(bookId, bookNum);
		return null;
	}

	@Override
	public Good deleteGoodList() {
		// TODO Auto-generated method stub
		Database.deleteGoodList();
		return null;
	}

}

package com.chinasofti.etc.dao.seimpl;

import java.util.List;

import com.chinasofti.etc.dao.UserDao;
import com.chinasofti.etc.database.Database;
import com.chinasofti.etc.po.User;

public class UserDaoImpl implements UserDao {

	@Override
	public User findUserByUserName(String userName) {
		// TODO Auto-generated method stub
		User user = null;
		List<User> users = Database.users;
		
		for(User u : users){
			if(u.getUserName().equals(userName)){
				user = u;
				break;
			}
		}
		
		return user;
	}

}

package com.chinasofti.etc.dao;

package com.chinasofti.etc.dao;
import java.util.List;
import com.chinasofti.etc.po.*;

public interface BookDao {
	List<Book> findAllBooks();
	Book findBookByBookId(int bookId);

}

package com.chinasofti.etc.dao;

import java.util.List;

import com.chinasofti.etc.po.Book;
import com.chinasofti.etc.po.Good;

public interface GoodDao {
	List<Good> findAllGoods();
	Good findGoodByBookIdNum(int bookId,int bookNum);
	Good addGoodByBookIdNum(int bookId,int bookNum);
	Good deleteGoodList();
}

package com.chinasofti.etc.dao;

import com.chinasofti.etc.po.User;

public interface UserDao {
	User findUserByUserName(String userName);

}

package com.chinasofti.etc.biz.seimpl;

package com.chinasofti.etc.biz.seimpl;

import java.util.List;

import com.chinasofti.etc.biz.BookBiz;
import com.chinasofti.etc.dao.BookDao;
import com.chinasofti.etc.dao.seimpl.BookDaoImpl;
import com.chinasofti.etc.po.Book;

public class BookBizImpl implements BookBiz{
	private BookDao bookDao = new BookDaoImpl();

	@Override
	public List<Book> findAllBooks() {
		// TODO Auto-generated method stub
		return bookDao.findAllBooks();
	}

	@Override
	public Book findBookByBookId(int bookId) {
		// TODO Auto-generated method stub
		return bookDao.findBookByBookId(bookId);
	}

}

package com.chinasofti.etc.biz.seimpl;

import java.util.List;

import com.chinasofti.etc.biz.GoodBiz;
import com.chinasofti.etc.dao.GoodDao;
import com.chinasofti.etc.dao.seimpl.GoodDaoImpl;
import com.chinasofti.etc.po.Good;

public class GoodBizImpl implements GoodBiz {
	private GoodDao goodDao = new GoodDaoImpl();
	
	@Override
	public List<Good> findAllGoods(){
		return goodDao.findAllGoods();
	}
	
	@Override
	public Good findGoodByBookIdNum(int bookId, int bookNum) {
		// TODO Auto-generated method stub
		return goodDao.findGoodByBookIdNum(bookId, bookNum);
	}

	@Override
	public Good addGoodByBookIdNum(int bookId, int bookNum) {
		// TODO Auto-generated method stub
		return goodDao.addGoodByBookIdNum(bookId, bookNum);
	}

	@Override
	public Good deleteGoodList() {
		// TODO Auto-generated method stub
		return goodDao.deleteGoodList();
	}
}

package com.chinasofti.etc.biz.seimpl;

import com.chinasofti.etc.biz.UserBiz;
import com.chinasofti.etc.dao.UserDao;
import com.chinasofti.etc.dao.seimpl.UserDaoImpl;
import com.chinasofti.etc.po.User;

public class UserBizImpl implements UserBiz {
	private UserDao userDao = new UserDaoImpl();

	@Override
	public boolean isValidateUser(String userName, String userPassword) {
		// TODO Auto-generated method stub
		boolean result = false;

		User user = userDao.findUserByUserName(userName);

		if (user != null && user.getUserPassword().equals(userPassword))
			result = true;

		return result;
	}

}

package com.chinasofti.etc.biz;

package com.chinasofti.etc.biz;

import java.util.List;

import com.chinasofti.etc.po.*;

public interface BookBiz {
	List<Book> findAllBooks();

	Book findBookByBookId(int bookId);

}

package com.chinasofti.etc.biz;

import java.util.List;

import com.chinasofti.etc.po.Good;

public interface GoodBiz {
	List<Good> findAllGoods();
	Good findGoodByBookIdNum(int bookId,int bookNum);
	Good addGoodByBookIdNum(int bookId,int bookNum);
	Good deleteGoodList();
}

package com.chinasofti.etc.biz;

public interface UserBiz {
	boolean isValidateUser(String userName, String userPassword);

}

发表评论