java新手的第一个小东西,或许小东西都算不上=。 =
import java.util.ArrayList;
import java.util.Scanner;
/**
* 系统类
*
* @author Administrator
*
*/
public class Systems {
private Scanner input = new Scanner(System.in);
private Database db = new Database();
//用户数据库
private ArrayList<Users> usersList = db.getUsersList();
//商品数据库
private ArrayList<Goods> goodsList = db.getGoodsList();
//登录用户
Users user = null;
//购物车信息
private ArrayList<Goods> shoppingCart = new Database().getShoppingCart();
/**
* 显示主菜单
*/
public int getMainMenu() {
System.out.println("****************** 欢迎使用大神超市管理系统 ******************");
System.out.println("****************** 【1】 用 户 登 录 ******************");
System.out.println("****************** 【2】 用 户 注 册 ******************");
System.out.println("****************** 【3】 退 出 系 统 ******************");
System.out.print("请输入编号选择功能: ");
return input.nextInt();
}
/**
*显示二级菜单
*/
public int getSecondMenu(){
System.out.println("****************** 【1】 查看购买商品 ******************");
System.out.println("****************** 【2】 查看购物车信息 ******************");
System.out.println("****************** 【3】 在线冲值 ******************");
System.out.println("****************** 【4】 添加商品 ******************");
System.out.println("****************** 【5】 返回上一级菜单 ******************");
System.out.print("请输入编号选择功能: ");
return input.nextInt();
}
/**
* 用户登录
*/
public boolean login(){
System.out.print("请输入帐号: ");
String name = input.next();
System.out.print("请输入密码: ");
String password = input.next();
//标记 false 表示不存在
boolean flag = false;
//遍历用户数据库 , 查找该用户是否存在
for(Users u : usersList){
if( u.getName().equals(name) && u.getPassword().equals(password) ){
this.user = u;
//修改标记
flag = true;
}
}
return flag;
}
/**
* 用户注册
*/
public void register(){
//输入新账号
System.out.print("请输入新帐号: ");
String name = input.next();
//标记
boolean flag = true;
for(Users i : usersList){
if(i.getName().equals(name)){
//修改标记
flag = false;
}
}
//判断
if(flag){
System.out.print("请输入密码: ");
String password = input.next();
usersList.add(new Users(name,password));
System.out.println("注册成功!"+name+"欢迎您!");
//this.user.setName(name);
//this.user.setPassword(password);
usersList.add(new Users(name,password));
this.mainLogic();
}else{
System.out.println("注册失败 !");
this.register();
}
}
/**
* 显示所有商品信息
*/
public void showAllGoods(){
System.out.println("商品ID****商品名****单价****库存******\n");
for(Goods g : goodsList){
System.out.println( g.getID()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getStock());
}
}
/**
* 购买
*/
public void buy(){
this.showAllGoods();
System.out.print("请输入需要购买的商品ID(输入 0 返回上级菜单):");
int goodsID = input.nextInt();
if(goodsID == 0){
this.secondLogic();
}
boolean flag = false;
//克隆
Goods good = null;
//现在购买的商品的信息
Goods gs = null;
for(Goods g : goodsList){
if( g.getID() == goodsID ){
flag = true;
gs = g;
good = new Goods(g.getID(),g.getName(),g.getPrice(),0);
}
}
if(flag){
System.out.print("请输入要购买的商品数量:");
int count = input.nextInt();
if(count > gs.getStock()){
System.out.println("库存不足,请重新购买!\n");
this.buy();
}else{
double total = gs.getPrice()*count;
if(total > user.getBalance()){
System.out.println("余额不足,请重新购买!\n");
this.buy();
}else{
System.out.println("购买成功,账户余额"+(user.getBalance()-total)+"元!"+"本次花费"+total+"元!");
//扣费
user.setBalance(user.getBalance()-total);
//更新商品列表
gs.setStock(gs.getStock()-count);
/**
* 将购买信息加入购物车
* 判断该次购买商品是否为第一次加入购物车,是则直接调用setStock(count)方法
* 否则 setStock(count+getStock())
*/
boolean flag2 = true;
for(Goods g : shoppingCart){
if(g.getID() == goodsID){
flag2 = false;
g.setStock(g.getStock()+count);
}
}
if(flag2){
good.setStock(count);
shoppingCart.add(good);
}
this.buy();
}
}
}else{
System.err.println("您输入的商品ID不存在,请重新输入!\n");
this.buy();
}
}
/**
* 查看购物车信息
*/
public void showShoppingCart(){
System.out.println("用户名***********余额*******");
System.out.println(user.getName()+"\t\t"+user.getBalance());
System.out.println("商品ID****商品名****单价****库存******\n");
for(Goods g : shoppingCart){
System.out.println(g.getID()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getStock());
}
System.out.print("输入任意键返回上级:");
String temp = input.next();
this.secondLogic();
}
/**
* 在线充值
*/
public void recharge(){
System.out.print("请输入充值金额:");
double m = input.nextDouble();
user.setBalance(m + user.getBalance());
System.out.print("充值成功,是否继续充值:(yes/no)");
String ys = input.next();
if(ys.equalsIgnoreCase("yes".trim())){
this.recharge();
}else{
this.secondLogic();
}
}
/**
* 添加商品
*/
public void addGoods(){
/**
* 选择添加商品类型:1.已存在商品(只添加数量)
* 2.不存在商品(添加ID,商品名,价格,数量)
*/
System.out.println("请选择要添加的商品类型:【1】已存在的商品。【2】不存在的商品");
int choose = input.nextInt();
switch(choose){
case 1:
//已存在的商品
this.existGoods();
break;
case 2:
//不存在数据库中的商品
this.unexistGoods();
break;
default:
System.err.println("输入错误!请重新输入");
this.addGoods();
}
}
/**
* 已存在的商品
*/
public void existGoods(){
System.out.println("请输入商品名:");
String goodName = input.next();
//判断商品名是否存在
boolean flag = true;
for(Goods g : goodsList){
if(goodName.equalsIgnoreCase(g.getName().trim())){
flag = false;
System.out.println("请输入添加商品数量:");
int count = input.nextInt();
g.setStock(g.getStock()+count);
}
}
if(flag){
System.out.print("商品不存在,请重新输入!");
this.existGoods();
}
System.out.print("商品添加成功,是否继续添加(yes/no):");
String temp = input.next();
if(temp.equalsIgnoreCase("yes".trim())){
this.addGoods();
}else{
this.secondLogic();
}
this.showAllGoods();
}
/**
* 不存在的商品
*/
public void unexistGoods(){
//新商品名
System.out.print("请输入商品名:");
String goodName = input.next();
//新商品价格
System.out.print("请输入商品价格:");
double goodPrice = input.nextDouble();
//新商品数量
System.out.print("请输入商品数量:");
int goodStock = input.nextInt();
int goodsID = goodsList.size()+1;
goodsList.add(new Goods(goodsID,goodName,goodPrice,goodStock));
System.out.println("添加成功,是否继续添加:(yes/no)");
String ys = input.next();
if(ys.equalsIgnoreCase("yes".trim())){
this.addGoods();
}else{
this.secondLogic();
}
}
/**
* 核心逻辑控制方法
*/
public void mainLogic() {
// 启动主菜单
int id = this.getMainMenu();
// 判断
switch (id) {
case 1:
//用户登录
if( this.login() ){
System.err.println("欢迎" + this.user.getName() + "登录成功 !");
//this.getSecondMenu();
this.secondLogic();
}else{
System.err.println("登录失败 !");
}
break;
case 2:
//用户注册
this.register();
break;
case 3:
System.err.println("退出系统 , 谢谢使用 !");
//退出系统
System.exit(0);
break;
default:
System.err.println("您输入的编号无效 , 请重新输入 !\n");
//开始递归
this.mainLogic();
break;
}
}
/**
* 二级菜单操作控制
*/
public void secondLogic () {
int id = this.getSecondMenu();
switch(id){
case 1:
this.buy();
break;
case 2:
this.showShoppingCart();
break;
case 3:
this.recharge();
break;
case 4:
this.addGoods();
break;
case 5:
this.mainLogic();
break;
default:
System.out.println("输入错误!");
this.secondLogic();
}
}
}
/**
* 用户对象
* @author Administrator
*/
public class Users {
//帐号
private String name;
//密码
private String password;
//用户账户余额
private double balance = 100;
//构造器
public Users() {
}
public Users(String name, String password) {
this.name = name;
this.password = password;
}
public Users(String name, String password, double balance) {
this.name = name;
this.password = password;
this.balance = balance;
}
//getter , setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
public class Goods {
//商品编号
private int ID;
//商品名
private String name;
//价格
private double price;
//存量
private int stock;
//构造器定义
public Goods() {
}
public Goods(int iD, String name, double price, int stock) {
ID = iD;
this.name = name;
this.price = price;
this.stock = stock;
}
//getter , settet;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
import java.util.ArrayList;
/**
* 数据库类
* @author Administrator
*/
public class Database {
//模拟用户数据库
private ArrayList<Users> usersList = new ArrayList<Users>();
//模拟商品数据库
private ArrayList<Goods> goodsList = new ArrayList<Goods>();
//购物车
private ArrayList<Goods> shoppingCart = new ArrayList<Goods>();
/**
* 构造器
* @return
*/
public Database(){
//添加一条默认用户数据
Users users = new Users("admin","admin");
usersList.add(users);
//添加一条默认商品数据
goodsList.add(new Goods(1,"苹果",5.0,1000));
goodsList.add(new Goods(2,"牙刷",3,100));
goodsList.add(new Goods(3,"梨子",4,1000));
goodsList.add(new Goods(4,"节操",99999,0));
goodsList.add(new Goods(5,"甲胺磷",24,20));
goodsList.add(new Goods(6,"肥皂",5,300));
goodsList.add(new Goods(7,"衬衫",100,20));
goodsList.add(new Goods(8,"酸奶",2.5,2000));
goodsList.add(new Goods(9,"iphone",5000,3));
}
public ArrayList<Users> getUsersList() {
return usersList;
}
public void setUsersList(ArrayList<Users> usersList) {
this.usersList = usersList;
}
public ArrayList<Goods> getGoodsList() {
return goodsList;
}
public void setGoodsList(ArrayList<Goods> goodsList) {
this.goodsList = goodsList;
}
public ArrayList<Goods> getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(ArrayList<Goods> shoppingCart) {
this.shoppingCart = shoppingCart;
}
}
public class SystemTest {
public static void main(String[] args) {
//启动系统
Systems system = new Systems();
system.mainLogic();
}
}
本文出自 “新手出门” 博客,谢绝转载!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。