基于面向对象的方式编程
根据商城菜单分析如下的对象的内容:
对象 | 属性 | 行为 |
用户 | 用户名和密码 | 注册,登录,购买,查看 |
管理员 | 用户名和密码 | 添加,修改,删除 |
商城 | 当前登录用户 管理员 商品集合 用户集合 扫描器 |
显示菜单的方法 获取用户输入的菜单 判断用户输入的菜单 |
商品 | 编号,名称,价格,数量 |
User类:
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Scanner;
/**
* User
* 这个been类实现了Serializable这个接口(实现文件读写(序列化))
* 所具有的属性:用户名,密码
* 所具有的行为:注册,登录,购买,查看
* @author
*
*/
public class User implements Serializable{
String username;
String userpwd;
Goods goods = new Goods();
//验证是否登录
private boolean isLogin;
public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
/*
* 校验用户名合法性
*/
private boolean checkUsername(String username) {
//定义一个为false的bool值
boolean res = false;
//用户名长度不满6位,则重新输入
if (username.length() != 6) {
System.out.println("用户名长度必须为6位");
return res;
} else if (Character.isDigit(username.charAt(0))) {//用户名不能以数字开头,则重新输入
System.out.println("用户名不能以数字开头");
return res;
} else {
//成功,返回true,继续下一步操作
return res = true;
}
}
/*
* 购买商品的方法
*/
public void buy() {
while (true) {
System.out.println("请选择您需要购买商品的编号:");
int id = Shop.sc.nextInt();
System.out.println("您将要的购买的商品信息如下:");
Goods shopGoods = this.findGoodsById(id);
System.out.println(shopGoods);
System.out.println("请输入您需要购买商品的数量:");
int num = Shop.sc.nextInt();
Goods myGoods = new Goods();
// myGoods.setId(shopGoods.getId());
// myGoods.setName(shopGoods.getName());
// myGoods.setPrice(shopGoods.getPrice());
//通过clone()方法,就省去了以上备注释的内容
myGoods = shopGoods.clone();
myGoods.setNum(num);
//将已选择的商品添加到我的商品集合中
Shop.myGoodsList.add(myGoods);
System.out.println("是否继续Y/N");
String choice = Shop.sc.next();
choice = choice.toUpperCase();
if (choice.equals("N")) {
break;
}
}
this.showMyGoodsList();
}
/*
* 展示已购买的商品信息
*/
private void showMyGoodsList() {
System.out.println("******您购买的商品列表如下******");
BigDecimal total = new BigDecimal("0");
for (Goods myGoods : Shop.myGoodsList) {
System.out.println(myGoods);
//将在buy()方法中储存的信息提取出来,获取商品的价格
BigDecimal price = myGoods.getPrice();
//获取商品的数量
int num = myGoods.getNum();
//计算
total = total.add(price.multiply(new BigDecimal(num)));
//total = total.add(myGoods.getPrice().multiply(new BigDecimal(myGoods.getNum())));
}
System.out.println("总价格为:" + total);
}
/*
* 注册方法
*/
public void registUser() {
//校验用户名合法性的标识
boolean isCheck = true;
while (true) {
System.out.println("欢迎注册");
System.out.println("请输入用户名");
Shop.sc = new Scanner(System.in);
String rusername = Shop.sc.next();
//校验结果的返回值
isCheck = this.checkUsername(rusername);
System.out.println(isCheck);
if (isCheck == true) {
System.out.println("请输入密码");
String ruserpwd = Shop.sc.next();
System.out.println("请再次输入密码");
String reuserpwd = Shop.sc.next();
if (ruserpwd.equals(reuserpwd)) {
User user = new User();
user.setUsername(rusername);
user.setUserpwd(ruserpwd);
Shop.userList.add(user);
System.out.println("注册成功");
//用户注册成功则将用户的注册信息保存到Userfile文件中
Shop.saveListToFile();
break;
} else {
System.out.println("密码不一致请重新输入");
}
}
}
}
/*
* 登录方法
*/
public void login() {
boolean loginResult = false;
int maxTime = 0;
while (true) {
if (maxTime != 3) {
maxTime++;
System.out.println("欢迎登录");
System.out.println("请输入用户名");
Shop.sc = new Scanner(System.in);
String username = Shop.sc.next();
System.out.println("请输入密码");
String userpwd = Shop.sc.next();
for (User user : Shop.userList) {
if (username.equals(user.username) && userpwd.equals(user.userpwd)) {
System.out.println("登录成功");
this.setLogin(true);
loginResult = true;
break;
}
}
if (loginResult == true) {
break;
} else {
if (maxTime != 3) {
System.out.println("登录失败账号或密码有误,请重新登录");
}
}
} else {
System.out.println(maxTime + "次登录失败,系统退出");
System.exit(0);
}
}
}
/*
* 展示商品信息
*/
public void showGoodsList() {
System.out.println("******商品列表如下******");
// Shop.readGoods2File();
// for (Goods goods : Shop.goodsList) {
// System.out.println(goods);
// }
goods.initGoodsList();
}
/*
* 查找商品的方法
*/
public Goods findGoodsById(int id) {
Goods returnGoods = null;
for (Goods goods : Shop.goodsList) {
if (goods.getId() == id) {
returnGoods = goods;
break;
}
}
return returnGoods;
}
public User() {
super();
}
public User(String username, String userpwd) {
super();
this.username = username;
this.userpwd = userpwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
Admin类:
import java.math.BigDecimal;
/**
* Admin
* 这个been类继承了User(Admin为User的子类)
* 所具有的属性:用户名和密码
* 所具有的行为:添加,修改,删除
* @author
*
*/
public class Admin extends User {
/*
* 管理员登录
*/
public void adminLogin() {
System.out.println("欢迎管理员登录");
System.out.println("请输入管理员账号");
String admin = Shop.sc.next();
while (true) {
System.out.println("请输入管理员密码");
String password = Shop.sc.next();
if (admin.equals("admin") && password.equals("123456")) {
System.out.println("管理员登录成功");
while (true) {
int choice = this.showAdminMenu();
if (choice == 5) {
break;
}
this.choiceAdminMenu(choice);
}
break;
} else {
System.out.println("登录失败");
}
}
}
/*
* 展示管理员菜单
*/
public int showAdminMenu() {
System.out.println("*****************欢迎进入管理员菜单**************");
System.out.println("\t1.添加商品");
System.out.println("\t2.修改商品");
System.out.println("\t3.删除商品");
System.out.println("\t4.查看商品列表");
System.out.println("\t5.退出");
System.out.println("******************************************");
System.out.print("请选择菜单:");
int choice = Shop.sc.nextInt();
return choice;
}
/*
* 判断是否继续的方法
*/
public String isGo_on() {
System.out.println("您是否继续:Y/N");
String go_on = Shop.sc.next();
return go_on.toUpperCase();
}
/*
* 菜单选择方法
*/
public boolean choiceAdminMenu(int choice) {
boolean result = true;
String go_on = "Y";
switch (choice) {
case 1:
System.out.println("你选择的是添加商品");
//当go_on为Y时,则继续执行添加商品操作
while (go_on.equals("Y")) {
this.addGoods();
go_on = this.isGo_on();
}
break;
case 2:
System.out.println("你选择的是修改商品");
this.updateGoods();
break;
case 3:
System.out.println("你选择的是删除商品");
this.deleteGoods();
break;
case 4:
System.out.println("你选择的是查看商品列表");
super.showGoodsList();
break;
case 5:
System.out.println("退出");
result = false;
break;
default:
System.out.println("输入有误");
break;
}
return false;
}
/*
* 删除商品方法
*/
public void deleteGoods() {
System.out.println("****开始修改商品信息****");
System.out.println("请输入要修改的商品编号:");
int id = Shop.sc.nextInt();
Goods goods = super.findGoodsById(id);
Shop.goodsList.remove(goods);
System.out.println("商品删除成功");
Shop.saveGoods2File();
}
/*
* 修改商品方法
*/
public void updateGoods() {
System.out.println("****开始修改商品信息****");
System.out.println("请输入要修改的商品编号:");
int id = Shop.sc.nextInt();
Goods goods = super.findGoodsById(id);
if (goods == null) {
System.out.println("未找到该商品");
} else {
System.out.println("商品信息如下:");
System.out.println("商品编号\t商品名称\t商品价格\t商品数量\t");
System.out.println(
goods.getId() + "\t" + goods.getName() + "\t" + goods.getPrice() + "\t" + goods.getNum() + "\t");
}
System.out.println("请输入修改后的商品名称:");
String name = Shop.sc.next();
goods.setName(name);
System.out.println("请输入修改后的商品价格:");
double price = Shop.sc.nextDouble();
goods.setPrice(new BigDecimal(price));
System.out.println("请输入修改后的商品数量:");
int num = Shop.sc.nextInt();
goods.setNum(num);
Shop.saveGoods2File();
}
/*
* 添加商品方法
*/
public void addGoods() {
System.out.println("****开始添加商品****");
System.out.println("请输入商品编号:");
int id = Shop.sc.nextInt();
System.out.println("请输入商品名称:");
String name = Shop.sc.next();
System.out.println("请输入商品价格:");
String price = Shop.sc.next();
System.out.println("请输入商品数量:");
int num = Shop.sc.nextInt();
Goods goods = new Goods();
goods.setId(id);
goods.setName(name);
goods.setPrice(new BigDecimal(price));
goods.setNum(num);
Shop.goodsList.add(goods);
System.out.println("商品添加成功");
Shop.saveGoods2File();
}
}
Shop类:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 商城类
* 所具有的属性:当前登录用户,管理员,商品集合,用户集合,扫描器
* 所具有的行为:显示菜单的方法,获取用户输入的菜单,判断用户输入的菜单
* @author
*
*/
public class Shop {
//定义一个全局变量的扫描i去,用于用户输出
static Scanner sc = new Scanner(System.in);
//定义一个goodsList容器存放商品信息
static List goodsList = new ArrayList();
//定义一个myGoodsList容器存购买的放商品信息
static List myGoodsList = new ArrayList();
//定义一个userList容器存放用户信息
static List userList = new ArrayList();
static File userFile = new File("E:\\workspace2\\ConsolShop1.8\\src\\com\\test\\shop\\Userfile");
static File goodsFile = new File("E:\\workspace2\\ConsolShop1.8\\src\\com\\test\\shop\\Goodsfile");
User user = new User();
Admin admin = new Admin();
Goods goods = new Goods();
public static void saveGoods2File(){
try {
FileOutputStream fos = new FileOutputStream(goodsFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(goodsList);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readGoods2File(){
try {
FileInputStream fis = new FileInputStream(goodsFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
goodsList = (List) obj;
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void saveListToFile(){
try {
FileOutputStream fos = new FileOutputStream(userFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readListFromFile(){
try {
FileInputStream fis = new FileInputStream(userFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
userList = (List) obj;
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Shop shop = new Shop();
shop.run();
}
private void run() {
this.readListFromFile();
//设定一个是否退出的条件
boolean isBreak = true;
while (isBreak) {
int choice = this.showMenu();
//当choice选择为6事,返回的值为false,同时将值赋给了isBreak,并退出菜单
isBreak = this.chooseMenu(choice);
}
}
/*
* 菜单展示
*/
private int showMenu() {
System.out.println("*****************欢迎进入电子商城**************");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.查看商城");
System.out.println("\t4.查看我购买的商品(我的购物车)");
System.out.println("\t5.管理员登录");
System.out.println("\t6.退出系统");
System.out.println("******************************************");
System.out.print("请选择菜单:");
int choice = sc.nextInt();
return choice;
}
/*
* 菜单选择
*/
private boolean chooseMenu(int choice) {
boolean result = true;
switch (choice) {
case 1:
System.out.println("你选择的菜单是:注册");
user.registUser();
break;
case 2:
System.out.println("你选择的菜单是:登录");
user.login();
break;
case 3:
System.out.println("你选择的菜单是:查看商城");
goods.initGoodsList();
//判断用户是否登录
if(user.isLogin() == true){//成功登录,则可以进行购买操作
user.buy();
}else{
System.out.println("你还未登录,请先登录,在购买商品");
}
break;
case 4:
System.out.println("你选择的菜单是:查看我购买的商品(我的购物车)");
break;
case 5:
System.out.println("管理员登录");
admin.adminLogin();
break;
case 6:
System.out.println("谢谢使用");
result = false;
break;
default:
System.out.println("输入有误");
break;
}
return result;
}
}
Goods类:
import java.io.Serializable;
import java.math.BigDecimal;
/**
* Goods
* 这个been类需要先实现Comparable, Cloneable, Serializable这三个接口
* 分别意思为:实现排序方法,实现clone对象方法,实现文件读写(序列化)
* 所具有的属性:编号,名称,价格,数量
* @author
*
*/
public class Goods implements Comparable, Cloneable, Serializable {
private int id;
private String name;
private BigDecimal price;
private int num;
/**
* 重写clone方法
*/
public Goods clone(){
Goods g1 = new Goods();
try {
g1 = (Goods) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return g1;
}
/*
* 初始化商品
*/
public void initGoodsList() {
Goods goods1 = new Goods(1, "iPhone7Plus", new BigDecimal("4999"), 5);
Goods goods2 = new Goods(2, "iPhone8Plus", new BigDecimal("6999"), 10);
Goods goods3 = new Goods(3, "iPhoneXMax", new BigDecimal("11999"), 25);
//将商品信息添加到商品集合中
Shop.goodsList.add(goods1);
Shop.goodsList.add(goods2);
Shop.goodsList.add(goods3);
//循环遍历显示商品信息
for (Goods goods : Shop.goodsList) {
System.out.println(goods);
}
//读取Goodsfile中的商品信息到控制台
Shop.readGoods2File();
}
public Goods() {
super();
}
public Goods(int id, String name, BigDecimal price, int num) {
super();
this.id = id;
this.name = name;
this.price = price;
this.num = num;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[id = " + id + ",name = " + name + ",price = " + price + ",num = " + num + "]";
}
// 商品的排序
@Override
public int compareTo(Object o) {
Goods goods = (Goods) o;
// if(this.num < goods.num){
// //小于
// return -1;
// } else if(this.num == goods.num){
// //等于
// return 0;
// } else{
// //大于
// return 1;
// }
return this.compareTo(goods.price);
}
}