完成了商城项目,需要好好消化一下
import com.team1.util.ScannerUtil;
import java.sql.SQLOutput;
import java.util.Scanner;
public class HomePage {
public static void main(String[] args) throws Exception {
HomePage homePage = new HomePage();
homePage.loginView();
homePage.homeView();
}
public void homeView() {
System.out.println("欢迎登录商城,请选择业务:");
boolean flag = true;
System.out.println("1.购买商品 2.查看个人信息 3.查看购物车 4.付款 5.账户充值 6.退出商城");
while (true) {
String function = ScannerUtil.getInput();
switch (function) {
case ShopConstent.FUNCTION_SHOP:
Shop shop = new Shop();
shop.shopView();
break;
case ShopConstent.FUNCTION_PERSONAL:
User user = new User();
user.userView();
break;
case ShopConstent.FUNCTION_CART:
//System.out.println("购物车目前已有商品:"+ ShopConstent.LOGIN_USER.getCart().getCartMessage());
Cart cart = new Cart();
cart.cartView();
break;
case ShopConstent.FUNCTION_PAYMENT:
ShopConstent.LOGIN_USER.pay();
break;
case ShopConstent.FUNCTION_RECHARGE:
Recharge recharge = new Recharge();
recharge.rechargeView();
break;
case ShopConstent.FUNCTION_EXIT:
HomePage homePage = new HomePage();
homePage.loginView();
homePage.homeView();
break;
}
}
}
//展示注册登录页面
public void loginView(){
boolean flag = true;
while (flag){
System.out.println("请选择功能:"+ShopConstent.USER_LOGIN+"登录 "+ShopConstent.USER_REGIST+"注册");
Scanner scanner = new Scanner(System.in);
String function = scanner.next();
//User user = new User();
switch (function){
case ShopConstent.USER_LOGIN:
flag = !ShopConstent.LOGIN_USER.login();
break;
case ShopConstent.USER_REGIST:
ShopConstent.LOGIN_USER.regist();
}
}
}
}
import com.sun.org.apache.xpath.internal.operations.Or;
import com.team1.util.ScannerUtil;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class User {
private String username;
private String password;
private Cart cart;
public User(){
this.cart = new Cart();
}
public void userView(){
System.out.println("现在显示您的个人信息,请选择:1.修改 2.返回上层页面");
System.out.println("账户:"+ShopConstent.LOGIN_USER.getUsername()+" 密码:"+ShopConstent.LOGIN_USER.getPassword());
Scanner scanner = new Scanner(System.in);
String function = scanner.next();
if(function.equals("1")){
ShopConstent.LOGIN_USER.regist();
System.out.println("修改信息成功,请输入2返回上层页面。");
}else if(function.equals("2")){
HomePage homePage = new HomePage();
homePage.homeView();
}else{
System.out.println("输入非法,请重新输入。");
userView();
}
/*switch (function) {
case "1":
// User user = new User();
ShopConstent.LOGIN_USER.regist();
System.out.println("修改信息成功,请输入2返回上层页面。");
break;
case "2":
HomePage homePage = new HomePage();
homePage.homeView();
break;
default:
System.out.println("输入非法,请重新输入。");
userView();
break;
}*/
}
public boolean login() {
System.out.println("请输入用户名:");
//
String username = ScannerUtil.getInput();
System.out.println();
System.out.println("请输入密码:");
String password = ScannerUtil.getInput();
//String username = ScannerUtil.getInput();
User user = getUsersByName(username);
if(user == null){
System.out.println("用户名不存在,登录失败");
return false;
}
//如果拿到user 去比对密码
if(!password.equals(user.password)){
System.out.println("密码错误,登录失败");
return false;
}
//保存已登录的用户
ShopConstent.LOGIN_USER = getUsersByName(username);
System.out.println("登录成功");
return true;
}
public boolean regist(){
//System.out.println("这是注册方法");
System.out.println("请输入用户名:");
//
String username = ScannerUtil.getInput();
System.out.println();
System.out.println("请输入密码:");
String password = ScannerUtil.getInput();
User user = new User();
user.setUsername(username);
user.setPassword(password);
//保存用户
saveUser(user);
return true;
}
//传参传入 商品id 商品购买数量
public boolean addGoodsToCart(String id,int goodsCount){
//1.就应该查一下goods信息
//静态方法拿类名直接调
//Goods goods = Goods.getGoodsById(id);
//2.把商品加入购物车
cart.addGoods(id,goodsCount);
return true;
}
//获得所有的User
private List<User> getUsers(String url) {
List<User> users = new ArrayList<>();
//主动关闭流
try(FileReader fs = new FileReader(url);BufferedReader bf = new BufferedReader(fs);) {
String userStr = null;
while ((userStr = bf.readLine()) != null) {
//读一行封装一行
String[] str = userStr.split("_");
User user = new User();
user.setUsername(str[0]);
user.setPassword(str[1]);
users.add(user);
}
}catch (IOException ex){
ex.printStackTrace();
}
return users;
}
//获取一个user的方法
public User getUsersByName(String name){
getUsers(ShopConstent.USER_PATH);
List<User> users = getUsers(ShopConstent.USER_PATH);
for (User user : users){
if(user.getUsername().equals(name)){
return user;
}
}
return null;
}
public boolean saveUser(User user) {
FileWriter fw = null;
BufferedWriter bw =null;
try{
fw = new FileWriter(ShopConstent.USER_PATH,true);
bw = new BufferedWriter(fw);
bw.newLine();
bw.append(user.getUsername()).append("_").append(user.getPassword());
}catch (IOException ex){
ex.printStackTrace();
}finally {
try {
if(bw != null){
bw.close();
}
if(fw != null){
fw.close();
}
}catch (IOException ex){
ex.printStackTrace();
}
}
return true;
}
public void pay(){
//拿到购物车总价
List<Order> cartMessage = cart.getCartMessage();
double cartTotalMoney = 0;
for (Order order : cartMessage) {
//把每一条信息加一起获得总价
cartTotalMoney += order.getTotalMoney();
}
if(cartTotalMoney>ShopConstent.USER_RECHARG){
System.out.println("您的余额不足,请充值");
}else {
ShopConstent.USER_RECHARG = ShopConstent.USER_RECHARG - cartTotalMoney;
System.out.println("购买成功,您本次消费为:"+cartTotalMoney+" 您的账户余额为:"+ShopConstent.USER_RECHARG);
cart.clear();
}
HomePage homePage = new HomePage();
homePage.homeView();
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", cart=" + cart +
'}';
}
}
import com.team1.util.ScannerUtil;
import java.util.List;
import java.util.Scanner;
public class Shop {
/* public static void main(String[] args) throws Exception {
//面向对象
Shop shop = new Shop();
//shop.loginView();
//shop.homeView();
shop.shopView();
//使用静态对象
//loginView();
}*/
/* private void homeView() {
System.out.println("欢迎登录商城,请选择业务:");
boolean flag = true;
System.out.println("1.购买商品 2.查看个人信息 3.查看购物车 4.付款 5.账户充值 6.退出商城");
while (true){
String function = ScannerUtil.getInput();
switch (function){
case ShopConstent.FUNCTION_SHOP:
//Shop shop = new Shop();
Shop.shopView();
break;
case ShopConstent.SHOP_TYPE_CRUET:
showGoodsMessage(ShopConstent.SHOP_TYPE_CRUET);
break;
case ShopConstent.SHOP_TYPE_NECESSITIES:
showGoodsMessage(ShopConstent.SHOP_TYPE_NECESSITIES);
break;
}*/
//展示购物页面
void shopView() {
System.out.println("开始购物");
boolean flag = true;
System.out.println("1.食品 2.调味品 3.日用品 0.返回上层页面");
while (true){
String function = ScannerUtil.getInput();
switch (function){
case ShopConstent.SHOP_TYPE_FOOD:
showGoodsMessage(ShopConstent.SHOP_TYPE_FOOD);
break;
case ShopConstent.SHOP_TYPE_CRUET:
showGoodsMessage(ShopConstent.SHOP_TYPE_CRUET);
break;
case ShopConstent.SHOP_TYPE_NECESSITIES:
showGoodsMessage(ShopConstent.SHOP_TYPE_NECESSITIES);
break;
case ShopConstent.SHOP_TYPE_EXIT:
HomePage homePage = new HomePage();
homePage.homeView();
break;
}
}
}
//展示商品信息
private void showGoodsMessage(String type){
List<Goods> goodsByType = Goods.getGoodsByType(type);
String str = "商品编号:商品名称 商品价格"+"\n";
for (Goods goods : goodsByType) {
str = str + goods.getId()+":"+goods.getName()+" "+goods.getPrice()+" \n";
}
System.out.println(str);
//User user = new User();
System.out.println("请输入购买商品id和数量:");
String id = ScannerUtil.getInput();
int goodsCount = Integer.parseInt(ScannerUtil.getInput());
ShopConstent.LOGIN_USER.addGoodsToCart(id,goodsCount);
//System.out.println("您购买了"+ShopConstent.LOGIN_USER.getCart().getCartMessage());
shopView();
}
//展示注册登录页面
/*public void loginView(){
boolean flag = true;
while (flag){
System.out.println("请选择功能:"+ShopConstent.USER_LOGIN+"登录 "+ShopConstent.USER_REGIST+"注册");
Scanner scanner = new Scanner(System.in);
String function = scanner.next();
User user = new User();
switch (function){
case ShopConstent.USER_LOGIN:
flag = !user.login();
break;
case ShopConstent.USER_REGIST:
user.regist();
}
}
}*/
}
import com.sun.org.apache.xpath.internal.operations.Or;
//商品信息 商品数量
public class Order {
private String goodsId;
private String goodsName;
private int goodsCount;
private double goodsPrice;
private double totalMoney;
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public int getGoodsCount() {
return this.goodsCount;
}
public void setGoodsCount(int goodsCount) {
this.goodsCount = goodsCount;
}
public double getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(double goodsPrice) {
this.goodsPrice = goodsPrice;
}
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
@Override
public String toString() {
return "Order{" +
"goodsId='" + goodsId + '\'' +
", goodsName='" + goodsName + '\'' +
", goodsCount=" + goodsCount +
", goodsPrice=" + goodsPrice +
", totalMoney=" + totalMoney +
'}';
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Goods {
private String type;
private String id;
private String name;
private double price;
/*public static void main(String[] args){
List<Goods> allGoods = Goods.getGoodsByType(ShopConstent.SHOP_TYPE_FOOD);
for(Goods allGood : allGoods){
System.out.println(allGood);
}
System.out.println(Goods.getGoodsById("1"));
}*/
/* public static Goods getGoodsById(String id) {
//使用流去文件夹找,返回LIST类型 遍历 根据id返回数据
*//* List<Goods> goodsList =new ArrayList<>();
for (int i = 0; i < goodsList.size(); i++) {
if(goodsList.get(i).getName().equals(id)){
return goodsList.get(i);
}
}*//*
Goods goods = new Goods();
goods.setName("棒棒糖");
goods.setPrice(3.5);
goods.setType("食品");
return goods;
}*/
public static List<Goods> getAllGoods(String path){
List<Goods> goodsList = new ArrayList<>();
//主动关闭流
try(FileReader fs = new FileReader(path); BufferedReader bf = new BufferedReader(fs);) {
String userStr = null;
while ((userStr = bf.readLine()) != null) {
//读一行封装一行
String[] str = userStr.split("_");
Goods goods = new Goods();
goods.setId(str[0]);
goods.setType(str[1]);
goods.setName(str[2]);
//Double的静态方法 将字符串转化为Double
goods.setPrice(Double.parseDouble(str[3]));
goodsList.add(goods);
}
}catch (IOException ex){
ex.printStackTrace();
}
return goodsList;
}
//根据id找到一个商品
public static Goods getGoodsById(String id){
List<Goods> allGoods = getAllGoods(ShopConstent.SHOP_PATH);
Goods targetGoods = null;
for (Goods goods : allGoods){
if(goods.getId().equals(id)){
//if(goods.getType().equals(id)){
targetGoods = goods;
break;
}
}
return targetGoods;
}
//查找方式 根据类查找
public static List<Goods> getGoodsByType(String type){
List<Goods> allGoods = getAllGoods(ShopConstent.SHOP_PATH);
List<Goods> goodsType = new ArrayList<>();
/*for (int i = 0; i < allGoods.size(); i++) {
if(allGoods.get(i).getType().equals(type)){
}
}*/
for (Goods goods : allGoods){
if(goods.getType().equals(type)){
goodsType.add(goods);
}
}
return goodsType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Goods{" +
"type='" + type + '\'' +
", id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
import com.team1.util.ScannerUtil;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class Cart {
public static List<Order> orderList = new ArrayList<>();
public void cartView() {
//List<Order> cartMessage = char.get
System.out.println("购物车已有商品:"+ShopConstent.LOGIN_USER.getCart().getCartMessage().toString());
System.out.println("请输入0返回上一层");
String str = ScannerUtil.getInput();
if (str.equals("0")){
HomePage homePage = new HomePage();
homePage.homeView();
}else{
System.out.println("非法输入,请输入0返回上一层");
cartView();
}
}
//订单封装
public void addGoods(String id,int goodsCount){
Goods goods = Goods.getGoodsById(id);
Order order = new Order();
order.setGoodsId(goods.getId());
order.setGoodsName(goods.getName());
order.setGoodsCount(goodsCount);
order.setGoodsPrice(goods.getPrice());
order.setTotalMoney(goodsCount * goods.getPrice());
orderList.add(order);
System.out.println("您添加了: "+order.getGoodsCount() + "个" + order.getGoodsName() +" ,到购物车,共计 "
+ order.getTotalMoney() + "元。");
}
//返回购物车信息
public List<Order> getCartMessage(){
if(orderList.size() != 0){
return orderList;
}
System.out.println("您还未添加商品,请先添加商品再购买");
HomePage homePage = new HomePage();
homePage.homeView();
return null;
}
/* public void getOrder(){
for (int i = 0; i < orderList.size(); i++) {
return orderList.get(name);
}
}*/
//清除购物车
public boolean clear(){
orderList.clear();
return true;
}
public static List<Order> getOrderList() {
return orderList;
}
public static void setOrderList(List<Order> orderList) {
Cart.orderList = orderList;
}
}
今日总结:创建类后要初始化,不然会报错。
看教程一定要认真,不能中间落下一集,不然看的很迷糊。
基础一定要掌握牢固,关于集合,和面向对象要再认真看一遍。