今日工作:
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 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;
}
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 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 java.util.Scanner;
public class Shop {
public static void main(String[] args) throws Exception {
//面向对象
Shop shop = new Shop();
shop.loginView();
Shop.shopView();
System.out.println("开始购物");
//使用静态对象
//loginView();
}
private static void shopView() {
}
public void loginView(){
boolean flag = true;
while (flag){
System.out.println("请选择功能:"+ShopConstent.USER_LOGIN+"登录 "+ShopConstent.USER_REGIST+"注册 3.");
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();
}
}
}
}
总结:关于方法中返回值的作用。如果在if中遇见返回值,第一个作用是跳出方法,只要if满足无论后边有什么代码,直接结束方法。第二个作用才是返回对应的值进行后续操作。
由于昨天已经在AATest里面封装好了增删改查对应的方法。所以今天直接拿来用就可以了。