一个简易的网上书店吧

package com.coding.study12;

import java.util.Scanner;

public class Demo01 {
	public static void main(String[] args) {
		while (true) {

			Scanner scanner = new Scanner(System.in);
			System.out.println("----欢迎使用在线购书平台----");
			System.out.println("首次登录请按" + "#" + "注册");
			String account = scanner.nextLine();
			if (account.equals("#")) {
				System.out.println("请输入注册用户属性boss/customer:");
				String type = scanner.nextLine();
				if (type.equals("boss")) {
					System.out.println("请输入新的账号:");
					String account1 = scanner.nextLine();
					System.out.println("请输入新的密码:");
					String passwd1 = scanner.nextLine();
					System.out.println("请输入姓名:");
					String name1 = scanner.nextLine();
					Operation.regist(account1, passwd1, name1, type);

				} else if (type.equals("customer")) {
					System.out.println("请输入新的账号:");
					String account2 = scanner.nextLine();
					System.out.println("请输入新的密码:");
					String passwd2 = scanner.nextLine();
					System.out.println("请输入姓名:");
					String name2 = scanner.nextLine();
					Operation.regist(account2, passwd2, name2, type);
				}

			} else {
				System.out.println("请输入账户密码:");
				String passwd = scanner.nextLine();
				System.out.println("请输入用户类型boss/customer:");
				String type = scanner.nextLine();
				Operation.logine(account, passwd, type);
				// Scanner scanner2 = new Scanner(System.in);
				// Scanner scanner3 = new Scanner(System.in);
				if (type.equals("boss")) {
					operateBoss();
					continue;
				} else if (type.equals("customer")) {
					operateCustomer();
					continue;
				}
			}
		}
	}

	public static void operateBoss() {
		Scanner scanner = new Scanner(System.in);
		Scanner scanner1 = new Scanner(System.in);
		Scanner scanner2 = new Scanner(System.in);
		while (true) {
			System.out.println("请输入你要执行的操作:");
			System.out.println("1.添加书籍");
			System.out.println("2.查看库存");
			System.out.println("3.退出系统");
			int n = scanner2.nextInt();
			switch (n) {
			case 1:
				System.out.println("请输入书籍名:");
				String name = scanner.nextLine();
				System.out.println("请输入书籍作者:");
				String author = scanner.nextLine();
				System.out.println("请输入出版社:");
				String press = scanner.nextLine();
				System.out.println("请输入书籍价格:");
				double price = scanner1.nextDouble();
				System.out.println("请输入书籍数量:");
				int amount = scanner2.nextInt();
				Operation.add(name, author, press, price, amount);

				break;
			case 2:
				Operation.check();
				break;
			case 3:
				return;
			default:
				System.out.println("输入错误请重新输入");
				break;
			}
		}

	}

	public static void operateCustomer() {
		Scanner scanner = new Scanner(System.in);
		Scanner scanner1 = new Scanner(System.in);
		Scanner scanner2 = new Scanner(System.in);
		while (true) {
			System.out.println("请输入你要执行的操作:");
			System.out.println("1.添加购物车");
			System.out.println("2.查看店铺书籍");
			System.out.println("3.查看订单");
			System.out.println("4.退出");
			int n = scanner2.nextInt();
			switch (n) {
			case 1:
				System.out.println("请输入要购买的书籍:");
				String name=scanner.nextLine();
				Operation.addGoods(name);
				break;
			case 2:
				System.out.println("店铺书籍有:");
				Operation.check();
				break;
			case 3:
				System.out.println("您的订单详情:");
				Operation.query();
				break;
			case 4:
				return;
			default:
				System.out.println("输入错误请重新输入");
				break;
			}
		}
	}
}
package com.coding.study12;

public class Boss {
	String account;
	String passwd;
	String name;

	public Boss(String account, String passwd, String name) {
		super();
		this.account = account;
		this.passwd = passwd;
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
package com.coding.study12;

public class Customer {
	String account;
	String passwd;
	String name;

	public Customer(String account, String passwd, String name) {
		super();
		this.account = account;
		this.passwd = passwd;
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.coding.study12;

public class Books {
	String name;
	String author;
	String press;
	double price;
	int amount;

	public Books(String name, String author, String press, double price, int amount) {
		super();
		this.name = name;
		this.author = author;
		this.press = press;
		this.price = price;
		this.amount = amount;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPress() {
		return press;
	}

	public void setPress(String press) {
		this.press = press;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

}
package com.coding.study12;


public class Operation {
	public static void regist(String account, String passwd, String name, String type) {
		Boss boss = new Boss(account, passwd, name);
		Customer customer = new Customer(account, passwd, name);
		if (type.equals("boss")) {
			for (int i = 0; i < Tools.bosses.length; i++) {
				if (Tools.bosses[i] == null) {
					Tools.bosses[i] = boss;
					System.out.println("boss注册成功!!!");
					break;
				}
			}
		} else if (type.equals("customer")) {
			for (int i = 0; i < Tools.customers.length; i++) {
				if (Tools.customers[i] == null) {
					Tools.customers[i] = customer;
					System.out.println("customer注册成功!!!");
					break;
				}
			}
		}
	}

	public static void logine(String account, String passwd, String type) {
		if (type.equals("boss")) {
			for (int i = 0; i < Tools.bosses.length; i++) {
				if (Tools.bosses[i] != null) {
					if (account.equals(Tools.bosses[i].getAccount()) && passwd.equals(Tools.bosses[i].getPasswd())) {
						System.out.println("欢迎boss:" + Tools.bosses[i].getName());
						// return true;
					}
				}
			}

		} else if (type.equals("customer")) {
			for (int i = 0; i < Tools.customers.length; i++) {
				if (Tools.customers[i] != null) {
					if (account.equals(Tools.customers[i].getAccount())
							&& passwd.equals(Tools.customers[i].getPasswd())) {
						System.out.println("欢迎customer:" + Tools.customers[i].getName() + "光临");
						// return true;
					}
				}
			}
		}
		// return false;
	}

	public static void add(String name, String author, String press, double price, int amount) {
		Books book = new Books(name, author, press, price, amount);
		for (int i = 0; i < Tools.books.length; i++) {
			if (Tools.books[i] == null) {
				Tools.books[i] = book;
				System.out.println("存储书籍信息成功");
				break;
			}
		}
	}

	public static void check() {
		for (int i = 0; i < Tools.books.length; i++) {
			if (Tools.books[i] != null) {
				System.out.println("书籍名:" + Tools.books[i].getName());
				System.out.println("作者:" + Tools.books[i].getAuthor());
				System.out.println("出版社:" + Tools.books[i].getPress());
				System.out.println("价格:" + Tools.books[i].getPrice());
				System.out.println("库存数:" + Tools.books[i].getAmount());
			}
		}
	}

	public static void addGoods(String name) {
		GoodsCar goods = new GoodsCar(name);
		for (int i = 0; i < Tools.goodsCar.length; i++) {
			if (Tools.goodsCar[i] == null) {
				Tools.goodsCar[i] = goods;
				System.out.println("添加购物车成功!");
				break;
			}
		}
	}

	public static void query() {
		double money = 0;
		int num = 0;
		for (int i = 0; i < Tools.goodsCar.length; i++) {
			if (Tools.goodsCar[i] != null) {
				System.out.println(Tools.goodsCar[i].getName());
				for (int j = 0; j < Tools.books.length; j++) {
					if (Tools.books[j]!=null) {
						if (Tools.goodsCar[i].getName().equals(Tools.books[j].getName())) {
							money = money + Tools.books[j].getPrice();
							num++;
							Tools.books[j].setAmount(Tools.books[j].getAmount()-1);
						}
					}
					
				}
			}
		}
		System.out.println("您共买了" + num + "本书,总价为¥:" + money);
	}
}
package com.coding.study12;

public class GoodsCar {
	String name;
	String price;
	public GoodsCar(String name) {
		super();
		this.name = name;
//		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
//	public String getPrice() {
//		return price;
//	}
//	public void setPrice(String price) {
//		this.price = price;
//	}
	
}
package com.coding.study12;

public class Tools {
	public static Books[] books=new Books[100];
	public static Boss[] bosses=new Boss[100];
	public static Customer[] customers=new Customer[100];
	public static GoodsCar[] goodsCar=new GoodsCar[100];
}

 

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
运行环境 操作系统:Windows XP。 Java平台:JDK 1.5。 Web服务器:Tomcat v 5.5.23,下载地址:http://tomcat.apache.org/。 数据库服务器:MySQL v 5.0.45,下载地址:http://www.mysql.com/。 开发平台:Eclipse SDK v 3.2.2,下载地址:http://www.eclipse.org/download/index.jsp。 Eclipse插件TomcatPlugins v 3.2.1,下载地址:http://www.eclipse-plugins.info/eclipse/index.jsp。 Eclipse插件ResourceBundleEditor v 0.7.7,下载地址:http://resourcebundleeditor.com/。 Eclipse插件MyEclipse v 5.5.1,下载地址:http://www.myeclipseide.com/ Spring 采用 2.0 版本 Hibernate 采用3.0版本 ====================================================== 请注意:如出现中文乱码,检查如下配置是否正确。 (1)MySql 数据库是否是utf-8 格式(在安装时选择支持多语言),数据是否正常。 (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 ====================================================== 数据库使用的是MySQL,其版本为5.0.45 版本。 数据库的用户名及密码均为root。 使用的时候,请参考附件数据库导入一节。或将需要用到的某章的数据库目录复制到“mysql安装根目录\data”文件夹下就可以了。 ★★★★★★★★★★★★★★★★★★★★★★★★★ 该实例来源下面这本书 《精通Java EE项目案例-基于Eclipse Spring Struts Hibernate》 本书下载链接,电驴,迅雷皆可 ed2k://|file|%E7%B2%BE%E9%80%9AJava.EE%E9%A1%B9%E7%9B%AE%E6%A1%88%E4%BE%8B-%E5%9F%BA%E4%BA%8EEclipse.Spring.Struts.Hibernate%E5%85%89%E7%9B%98%E6%BA%90%E7%A0%81.rar|70436209|475e7c3548acf955e89e378e760cf894|h=caud4x2auasf3pl2ln27e35fjw2jwxyb|/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值