基于io流实现的java超市管理系统

自写java超市管理系统,功能只有

1.用户注册,登录,购买

2.管理员对商品的增,删,改,查

3.管理员对用户信息(账号与密码)的查与删,你问我为什么不改?开玩笑,你改了用户怎么登?直接把他账号删了,他不就只能重新注册一个嘛!(坏笑)。

代码汇总,并无顺序可言,只是简单的将所有类粘贴过来,下面有分类

你问我为什么这样搞?不好意思,这没人看,我没有动力去给空气解释,希望以后能有混丝吧!

有了的话,我就能做出精良的博客了。

(这里不能贴表情吗?ε=(´ο`*)))唉)7fc71b5eca1942bc96b101a537927c6e.jpeg

哦哦,原来可以贴,。 

ps:

里面输出可能会有些问题,这绝对是idea的锅,明明在cmd里面可以正常对齐,idea就是犯病,所以有的地方我加了好几个\t用来对齐,不过还是商品名过长或者价格过长还是会出问题。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;
public class Administrators {
    private static File file = new File("users.txt");
    public Administrators() {
        System.out.println("您已成功进入管理员界面");
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您的指令");
        System.out.println("0退出 1查看现有商品名单 2增加商品 3修改商品 4删除商品 5查看已经注册的用户信息 6删除用户信息");
        String order = in.nextLine();
        while (true) {
            if (order.equals("0"))break;
            else if (order.equals("1")) {
                Goods goods = new Goods();
            }
            else if (order.equals("2")) {
                System.out.println("输入商品的信息!");
                System.out.println("编号 商品名 价格 存货数量");
                int number = in.nextInt();
                String name = in.next();
                double price = in.nextDouble();
                int count = in.nextInt();
                String xishou = in.nextLine();
                Goods goods = new Goods(number, name, price, count);
                System.out.println("商品添加成功!");
            }
            else if (order.equals("3")) {
                System.out.println("请输入您要修改商品的行数,以及修改内容(注意,输入价格无需单位,可输入整数或小数)");
                int count = in.nextInt();
                int number = in.nextInt();
                String name = in.next();
                double price = in.nextDouble();
                int _count = in.nextInt();
                String xishou = in.nextLine();
                changeGoods changegoods = new changeGoods(count, number, name, price, _count);
                System.out.println("修改成功!");
            }
            else if (order.equals("4")) {
                System.out.println("请输入要删除商品的行数!");
                int count = in.nextInt();
                String xiushou = in.nextLine();
                deleteGoods deletegoods = new deleteGoods(count);
            }
            else if (order.equals("5")) {
                try (FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr)) {
                    String line;
                    int sb = 0;
                    while ((line = br.readLine()) != null) {
                        String[] userArray = line.split(",");
                        sb++;
                        if (sb == 1)
                            System.out.println("用户名" + "\t" + '\t' + "密码" + "\t");
                        System.out.println(userArray[0] + "\t" + '\t' + userArray[1] + "\t");
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else if (order.equals("6")) {
                try (FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr)) {
                    String line;
                    int sb = 0;
                    while ((line = br.readLine()) != null) {
                        String[] userArray = line.split(",");
                        sb++;
                        if (sb == 1)
                            System.out.println("用户名" + "\t" + '\t' + "密码" + "\t");
                        System.out.println(userArray[0] + "\t" + '\t' + userArray[1] + "\t");
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("请输入您要删除的用户名");
                int xb = 0;
                String order1 = "1";
                while (true) {
                    if (order1.equals("exit"))break;
                    if (xb != 0)
                        System.out.println("请输入您要删除的用户名");
                    String username = in.nextLine();
                    deleteUsers deleteusers = new deleteUsers(username);
                    if (xb != 0) {
                        try (FileReader fr = new FileReader(file);
                        BufferedReader br = new BufferedReader(fr)) {
                            String line;
                            int sb = 0;
                            while ((line = br.readLine()) != null) {
                                String[] userArray = line.split(",");
                                sb++;
                                if (sb == 1)
                                    System.out.println("用户名" + "\t" + "密码" + "\t");
                                System.out.println(userArray[0] + "\t" + '\t' + userArray[1] + "\t");
                            }
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    xb++;
                    System.out.println("请输入您的指令,exit表示退出 输入其他表示继续删除");
                    order1 = in.nextLine();
                }
            }
            System.out.println();
            System.out.println("请输入您接下来的指令");
            System.out.println("0退出 1查看现有商品名单 2增加商品 3修改商品 4删除商品 5查看已经注册的用户信息 6删除用户信息 ");
            order = in.nextLine();
        }
    }
}
import java.io.*;
public class changeGoods {
    private static File file = new File("goods.txt");
    private static int number;
    private static String name;
    private  static double price;
    private  static int count;
    changeGoods(int count, int number, String name, double price, int _count) {
        int count1 = 1;
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter sbr = new BufferedWriter(fw);) {
            String line;
            //sbr.append("");
            while ((line = br.readLine()) != null) {
                if (count1 == count) {
                    line = (number + ",") + name + ("," + price) + ("," + _count);
                }
                sbr.append(line + '\n');
                count1++;
            }
            FileWriter fw1 = new FileWriter(file);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    changeGoods(int number, int count) {
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter sbr = new BufferedWriter(fw);) {
            String line;
            sbr.append("");
            while ((line = br.readLine()) != null) {
                String[] userArray = line.split(",");
                if (userArray[0].equals((number + ""))) {
                    this.price = Double.parseDouble(userArray[2]);
                    this.count = (Integer.parseInt(userArray[3]) - count);
                    if (this.count >= 0)
                        line = userArray[0] + "," + userArray[1] + "," + userArray[2] + ("," + this.count);
                    this.name = userArray[1];
                }
                sbr.append(line + '\n');
            }
            FileWriter fw1 = new FileWriter(file);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    public double  getprice() {
        return price;
    }
    public int getCount() {
        return count;
    }
    public String getName() { return name; }
}
import java.io.*;
public class deleteGoods {
    private static File file = new File("goods.txt");
    private static int number;
    private static String name;
    private  static double price;
    public  deleteGoods(int count) {
        int _count = 1;
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter sbr = new BufferedWriter(fw);) {
            String line;
            //sbr.append("");
            while ((line = br.readLine()) != null) {
                if (_count == count) {
                    line = "";
                    sbr.append(line);
                }
                else sbr.append(line + '\n');
                _count++;
            }
            System.out.println("删除成功!");
            try(FileWriter fw1 = new FileWriter(file);) {

            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import java.io.*;
public class deleteUsers {
    private static File file = new File("users.txt");
    public deleteUsers(String username) {
        int sb = 0;
        try (FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);) {
            String line;
            bw.append("");
            while ((line = br.readLine()) != null) {
                String args[] = line.split(",");
                if (args[0].equals(username)) {
                    line = "";
                    sb = 1;
                    bw.append(line);
                }
                else
                    bw.append(line + '\n');
            }
            try(FileWriter fw1 = new FileWriter(file)) {
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        if (sb == 0) System.out.println("该用户未创建用户名,请重新输入要删除的用户名!");
        else
            System.out.println("删除成功");
    }
}
import java.io.*;

public class Goods {
    private static File file = new File("goods.txt");
    static {
        if (!file.exists()) {
            try {
                file.createNewFile();
            }
            catch (IOException e) {
                System.out.println("创建文件失败...");
            }
        }
    }
    private static int number;
    private static String name;
    private static double price;
    private  static int count;
    public Goods(int number, String name, double price, int count) {
        this.number = number;
        this.name = name;
        this.price = price;
        try (FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw)) {
            out.println(number + "," + name + "," + price + "," + count);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Goods() {
        try (FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr)) {
            String line;
            int sb = 0;
            while ((line = br.readLine()) != null) {
                sb++;
                String[] userArray = line.split(",");
                if (sb == 1)
                    System.out.println("编号" + '\t' + "商品名" + '\t' + '\t' + '\t' + "价格" + '\t' + '\t' + '\t' + "存货数量" + '\t');
                this.count = Integer.parseInt(userArray[3]);
                System.out.println(userArray[0] + '\t' + '\t' + userArray[1] + '\t' + '\t' + '\t' + userArray[2] + "元" + '\t' + '\t' + '\t' + userArray[3] + '\t');
            }
            if (sb == 0) {
                System.out.println("超市内没有还任何物品呢!");
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  double getPrice() {
        return price;
    }
    public int getNumber() {
        return number;
    }
    public String getName() {
        return name;
    }
    public  void setName(String name) {
        this.name = name;
    }
    public  void setPrice(double price) {
        this.price = price;
    }
    public int getCount() {
        return count;
    }
    public  void setCount() {
        this.count = count;
    }
}
public class gotoMarket {
    public static void main(String[] args) {
        Market market = new Market();
    }
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Login {
    private static String username;
    private static File file = new File("users.txt");
    static{
        if (!file.exists()) {
            try {
                file.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Login() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您的用户名");
        String username = in.nextLine();
        while (judgment1(username)) {
            username = in.nextLine();
        }
        String password = in.nextLine();
        while (judgment2(password)) {
            password = in.nextLine();
        }
    }
    public boolean judgment1(String username) {
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr)) {
            String line;
            while ((line = br.readLine()) != null) {
                String args[] = line.split(",");
                if (args[0].equals(username)) {
                    System.out.println("请输入密码");
                    this.username = username;
                    return false;
                }
            }
            System.out.println("用户名不存在!请重新输入!");
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }
    public boolean judgment2(String password) {
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr)) {
            String line;
            while ((line = br.readLine()) != null) {
                String args[] = line.split(",");
                if (args[1].equals(password) && args[0].equals(this.username)) {
                    System.out.println("登陆成功!");
                    return false;
                }
            }
            System.out.println("密码错误!请重新输入!");
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Scanner;
public class Market {
    public Market() {
        System.out.println("欢迎来到真乌鱼超市系统,请输入您的命令");
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("0退出程序 1注册 2登录 6管理员界面");
            String order = in.nextLine();
            if (order.equals("0"))break;
            else if (order.equals("1")) {
                Register register = new Register();
            }
            else if (order.equals("2")) {
                Login login = new Login();
                sellGoods sellgoods = new sellGoods();
            }
            else if (order.equals("6")) {
                Administrators administrators = new Administrators();
            }
            else {
                System.out.println("您的输入有误,请重新输入!我乌鱼死了。");
            }
        }
        System.out.println("谢谢使用本系统,欢迎下次乌鱼!");
    }
    private void init() {
        Frame frame = new Frame("万商超市");
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            });
        frame.pack();
        frame.setVisible(true);
    }
}

import java.awt.*;
import java.io.*;
import java.util.Scanner;

public class Register {
    private String username;
    private String password;
    private static File file = new File("users.txt");
    static{
        if (!file.exists()) {
            try {
                file.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Register() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您要注册的用户名");
        String username = in.nextLine();
        while (judgment(username)) {
            username = in.nextLine();
        }
        String password = in.nextLine();
        Register r1 = new Register(username, password);
        r1.register();
    }
    public  Register(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public boolean judgment(String username) {
        try(FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);) {
            String line;
            while ((line = br.readLine()) != null) {
                String args[] = line.split(",");
                if (args[0].equals(username)) {
                    System.out.println("您输入的用户名已存在!请重新输入用户名!");
                    return true;
                }
            }
            System.out.println("请输入您要创建的密码");
            return false;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    public void register() {
        try(FileWriter fw = new FileWriter(file, true);
        BufferedWriter bwr = new BufferedWriter(fw);
        PrintWriter pwr = new PrintWriter(bwr);) {
            pwr.println(this.username + "," + this.password);
            System.out.println("账户创建成功!");
            System.out.println("您本次的创建的账户为:");
            System.out.println("用户名:" + "\t" + this.username);
            System.out.println("密码:" + "\t" + this.password);
            System.out.println("快去登录您的新账户吧!");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void init() {
        Frame frame = new Frame("注册界面");
        TextField tfusername = new TextField(100);
        TextField tfpassword = new TextField(100);
        Panel p1 = new Panel();
        p1.add(tfusername);
        p1.add(tfpassword);
        frame.add(p1);
        frame.pack();
        frame.setVisible(true);
    }
}

import java.util.Scanner;

public class sellGoods {
    private static double money;
    public sellGoods() {
        Goods goods = new Goods();
        double summoney = 0;
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("请选择您要购买商品的商品编号");
            System.out.println("购买完成后,可输入0表示退出购买");
            String goodsName = null;
            int buycount1 = 0;
            while (true) {
                int buyorder = in.nextInt();
                if (buyorder == 0)break;
                else {
                    int number = buyorder;
                    System.out.println("请输入您要购买的数量");
                    int buycount = in.nextInt();
                    buycount1 = buycount;
                    changeGoods buygoods = new changeGoods(number, buycount);
                    sellGoods sellgoods = new sellGoods(buygoods, buycount);
                    summoney = summoney + sellgoods.getMoney();
                    goodsName = buygoods.getName();
                }
                System.out.println("输入商品编号可继续购买,输入0即可退出购买!");

            }
            if (goodsName != null) {
                System.out.println("您本次购买的物品为:" + goodsName);
                System.out.println("数量为" + buycount1);
                System.out.println("您本次购买的金额为" + summoney + "元");
            }
            else
                System.out.println("您此次未购买任何物品");
            String xiushou = in.nextLine();
            System.out.println("是否退出系统");
            System.out.println("输入0即可退出系统!输入其他表示继续购买!");
            String n = in.nextLine();
            Goods g = new Goods();
            if (n.equals("0"))break;
        }
    }
    public sellGoods(changeGoods goods, int count) {
        if (goods.getCount() >= 0)
            this.money = goods.getprice() * count;
        else if (goods.getCount() < 0) {
            System.out.println("您买的商品数量超过现有存货!请重新购买!");
        }
        else System.out.println("该商品已售罄!");
    }
    public double getMoney() {
        return money;
    }
}

1.超市入口类(主类)

public class gotoMarket {
    public static void main(String[] args) {
        Market market=new Market();
    }
}

代码简洁明了,小盆友看了都说好,(^-^)V。

2.超市类

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Scanner;
public class Market {
    public Market() {
        System.out.println("欢迎来到真乌鱼超市系统,请输入您的命令");
        Scanner in=new Scanner(System.in);
        while(true){
            System.out.println("0退出程序 1注册 2登录 6管理员界面");
            String order=in.nextLine();
            if(order.equals("0"))break;
            else if(order.equals("1")){
                Register register=new Register();
            }else if(order.equals("2")){
                Login login=new Login();
                sellGoods sellgoods=new sellGoods();
            }else if(order.equals("6")){
                Administrators administrators=new Administrators();
            }else{
                System.out.println("您的输入有误,请重新输入!我乌鱼死了。");
            }
        }
        System.out.println("谢谢使用本系统,欢迎下次乌鱼!");
    }
    private void init(){
        Frame frame=new Frame("万商超市");
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}

我本来想把窗口写上的,可是要水博客了,所有就先发了,而且感觉写了没啥逼格,不对,应该叫意义不大。

3.注册类

import java.awt.*;
import java.io.*;
import java.util.Scanner;

public class Register {
    private String username;
    private String password;
    private static File file=new File("users.txt");
    static{
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   public Register(){
       Scanner in=new Scanner(System.in);
       System.out.println("请输入您要注册的用户名");
       String username=in.nextLine();
       while(judgment(username)){
           username=in.nextLine();
       }
       String password=in.nextLine();
       Register r1=new Register(username,password);
       r1.register();
   }
    public  Register(String username,String password){
       this.username=username;
       this.password=password;
   }
   public boolean judgment(String username){
       try(FileReader fr=new FileReader(file);
       BufferedReader br=new BufferedReader(fr);){
           String line;
           while((line=br.readLine())!=null){
               String args[]=line.split(",");
               if(args[0].equals(username)){
                   System.out.println("您输入的用户名已存在!请重新输入用户名!");
                   return true;
               }
           }
           System.out.println("请输入您要创建的密码");
           return false;
       }catch(IOException e){
           e.printStackTrace();
           return false;
       }
   }
  public void register(){
       try(FileWriter fw=new FileWriter(file,true);
       BufferedWriter bwr=new BufferedWriter(fw);
       PrintWriter pwr=new PrintWriter(bwr);){
           pwr.println(this.username+","+this.password);
           System.out.println("账户创建成功!");
           System.out.println("您本次的创建的账户为:");
           System.out.println("用户名:"+"\t"+this.username);
           System.out.println("密码:"+"\t"+this.password);
           System.out.println("快去登录您的新账户吧!");
       }catch(IOException e){
           e.printStackTrace();
      }
  }
  private void init(){
        Frame frame=new Frame("注册界面");
        TextField tfusername=new TextField(100);
        TextField tfpassword=new TextField(100);
        Panel p1=new Panel();
        p1.add(tfusername);
        p1.add(tfpassword);
        frame.add(p1);
        frame.pack();
        frame.setVisible(true);
  }
}

此类用于注册时调用,能够连接你存储用户信息的文件,并进行文件读取与判定。

4.登录类

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Login {
    private static String username;
    private static File file=new File("users.txt");
    static{
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Login(){
        Scanner in=new Scanner(System.in);
        System.out.println("请输入您的用户名");
        String username=in.nextLine();
        while(judgment1(username)){
            username=in.nextLine();
        }
        String password=in.nextLine();
        while(judgment2(password)){
            password=in.nextLine();
        }
    }
    public boolean judgment1(String username){
        try(FileReader fr=new FileReader(file);
            BufferedReader br=new BufferedReader(fr)){
            String line;
            while((line=br.readLine())!=null){
                String args[]=line.split(",");
                if(args[0].equals(username)){
                    System.out.println("请输入密码");
                    this.username=username;
                    return false;
                }
            }
            System.out.println("用户名不存在!请重新输入!");
            return true;
        }catch (IOException e){
            e.printStackTrace();
            return true;
        }
    }
    public boolean judgment2(String password){
        try(FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr)){
            String line;
            while((line=br.readLine())!=null){
                String args[]=line.split(",");
                if(args[1].equals(password)&&args[0].equals(this.username)){
                    System.out.println("登陆成功!");
                    return false;
                }
            }
            System.out.println("密码错误!请重新输入!");
            return true;
        }catch (IOException e){
            e.printStackTrace();
            return true;
        }
    }
}

5.商品类

import java.io.*;

public class Goods {
    private static File file = new File("goods.txt");
    static {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("创建文件失败...");
            }
        }
    }
    private static int number;
    private static String name;
    private static double price;
    private  static int count;
    public Goods(int number,String name,double price,int count){
        this.number=number;
        this.name=name;
        this.price=price;
        try (FileWriter fw = new FileWriter(file, true);
             BufferedWriter bw = new BufferedWriter(fw);
             PrintWriter out = new PrintWriter(bw)) {
            out.println(number + "," +name+","+price+","+count);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Goods(){
        try (FileReader fr = new FileReader(file);
             BufferedReader br = new BufferedReader(fr)) {
            String line;
            int sb=0;
            while ((line = br.readLine()) != null) {
                sb++;
                String[] userArray = line.split(",");
                if(sb==1)
                    System.out.println("编号"+'\t'+"商品名"+'\t'+'\t'+'\t'+"价格"+'\t'+'\t'+'\t'+"存货数量"+'\t');
                this.count=Integer.parseInt(userArray[3]);
                System.out.println(userArray[0]+'\t'+'\t'+userArray[1]+'\t'+'\t'+'\t'+userArray[2]+"元"+'\t'+'\t'+'\t'+userArray[3]+'\t');
            }
            if(sb==0){
                System.out.println("超市内没有还任何物品呢!");
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  double getPrice(){
        return price;
    }
    public int getNumber(){
        return number;
    }
    public String getName(){
        return name;
    }
    public  void setName(String name){
        this.name=name;
    }
    public  void setPrice(double price){
        this.price=price;
    }
    public int getCount(){
        return count;
    }
    public  void setCount(){
        this.count=count;
    }
}

6.管理员类

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;
public class Administrators {
    private static File file=new File("users.txt");
    public Administrators()  {
        System.out.println("您已成功进入管理员界面");
        Scanner in=new Scanner(System.in);
        System.out.println("请输入您的指令");
        System.out.println("0退出 1查看现有商品名单 2增加商品 3修改商品 4删除商品 5查看已经注册的用户信息 6删除用户信息");
        String order=in.nextLine();
        while(true){
            if(order.equals("0"))break;
            else if(order.equals("1")){
                Goods goods=new Goods();
            }else if(order.equals("2")){
                System.out.println("输入商品的信息!");
                System.out.println("编号 商品名 价格 存货数量");
                int number=in.nextInt();
                String name=in.next();
                double price=in.nextDouble();
                int count=in.nextInt();
                String xishou=in.nextLine();
                Goods goods=new Goods(number,name,price,count);
                System.out.println("商品添加成功!");
            }else if(order.equals("3")){
                System.out.println("请输入您要修改商品的行数,以及修改内容(注意,输入价格无需单位,可输入整数或小数)");
                int count=in.nextInt();
                int number=in.nextInt();
                String name=in.next();
                double price=in.nextDouble();
                int _count=in.nextInt();
                String xishou=in.nextLine();
                changeGoods changegoods=new changeGoods(count,number,name,price,_count);
                System.out.println("修改成功!");
            } else if(order.equals("4")){
                System.out.println("请输入要删除商品的行数!");
                int count =in.nextInt();
                String xiushou=in.nextLine();
                deleteGoods deletegoods=new deleteGoods(count);
            }else if(order.equals("5")){
                try (FileReader fr = new FileReader(file);
                     BufferedReader br = new BufferedReader(fr)) {
                    String line;
                    int sb=0;
                    while ((line = br.readLine()) != null) {
                        String[] userArray = line.split(",");
                        sb++;
                        if(sb==1)
                            System.out.println("用户名"+"\t"+'\t'+"密码"+"\t");
                        System.out.println(userArray[0]+"\t"+'\t'+userArray[1]+"\t");
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }else if(order.equals("6")){
                try (FileReader fr = new FileReader(file);
                     BufferedReader br = new BufferedReader(fr)) {
                    String line;
                    int sb=0;
                    while ((line = br.readLine()) != null) {
                        String[] userArray = line.split(",");
                        sb++;
                        if(sb==1)
                            System.out.println("用户名"+"\t"+'\t'+"密码"+"\t");
                        System.out.println(userArray[0]+"\t"+'\t'+userArray[1]+"\t");
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("请输入您要删除的用户名");
                int xb=0;
                String order1="1";
                while(true){
                    if(order1.equals("exit"))break;
                    if(xb!=0)
                    System.out.println("请输入您要删除的用户名");
                    String username=in.nextLine();
                    deleteUsers deleteusers=new deleteUsers(username);
                    if(xb!=0){
                        try (FileReader fr = new FileReader(file);
                             BufferedReader br = new BufferedReader(fr)) {
                            String line;
                            int sb=0;
                            while ((line = br.readLine()) != null) {
                                String[] userArray = line.split(",");
                                sb++;
                                if(sb==1)
                                    System.out.println("用户名"+"\t"+"密码"+"\t");
                                System.out.println(userArray[0]+"\t"+'\t'+userArray[1]+"\t");
                            }
                        }catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    xb++;
                    System.out.println("请输入您的指令,exit表示退出 输入其他表示继续删除");
                    order1=in.nextLine();
                }
            }
            System.out.println();
            System.out.println("请输入您接下来的指令");
            System.out.println("0退出 1查看现有商品名单 2增加商品 3修改商品 4删除商品 5查看已经注册的用户信息 6删除用户信息 ");
            order=in.nextLine();
        }
    }
}

7.删除商品类

import java.io.*;
public class deleteGoods {
    private static File file = new File("goods.txt");
    private static int number;
    private static String name;
    private  static double price;
     public  deleteGoods(int count) {
        int _count=1;
       try(FileReader fr=new FileReader(file);
           BufferedReader br = new BufferedReader(fr);
           FileWriter fw=new FileWriter(file,true);
           BufferedWriter sbr=new BufferedWriter(fw);) {
           String line;
           //sbr.append("");
           while((line=br.readLine())!=null){
               if(_count==count){
                   line="";
                   sbr.append(line);
               }
               else sbr.append(line+'\n');
               _count++;
           }
           System.out.println("删除成功!");
           try( FileWriter fw1 = new FileWriter(file);){

           }catch (IOException e){
               e.printStackTrace();
           }
       }catch (IOException e){
       e.printStackTrace();
    }
}
}

8.修改商品类

import java.io.*;
public class changeGoods {
    private static File file = new File("goods.txt");
    private static int number;
    private static String name;
    private  static double price;
    private  static int count;
    changeGoods(int count,int number,String name,double price,int _count)  {
        int count1=1;
        try(FileReader fr=new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            FileWriter fw=new FileWriter(file,true);
            BufferedWriter sbr=new BufferedWriter(fw);){
            String line;
            //sbr.append("");
            while((line=br.readLine())!=null){
                if(count1==count){
                    line=(number+",")+name+(","+price)+(","+_count);
                }
                sbr.append(line+'\n');
                count1++;
            }
            FileWriter fw1 = new FileWriter(file);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    changeGoods(int number,int count)  {
        try(FileReader fr=new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter sbr=new BufferedWriter(fw);){
            String line;
            sbr.append("");
            while((line=br.readLine())!=null){
                String[] userArray = line.split(",");
                if(userArray[0].equals((number+""))){
                    this.price=Double.parseDouble(userArray[2]);
                    this.count=(Integer.parseInt(userArray[3])-count);
                    if(this.count>=0)
                        line=userArray[0]+","+userArray[1]+","+userArray[2]+(","+this.count);
                    this.name=userArray[1];
                }
                sbr.append(line+'\n');
            }
            FileWriter fw1 = new FileWriter(file);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public double  getprice(){
        return price;
    }
    public int getCount(){
        return count;
    }
    public String getName(){return name;}
}

9.售卖商品类

import java.util.Scanner;

public class sellGoods {
    private static double money;
    public sellGoods(){
        Goods goods=new Goods();
        double summoney=0;
        Scanner in=new Scanner(System.in);
        while(true){
            System.out.println("请选择您要购买商品的商品编号");
            System.out.println("购买完成后,可输入0表示退出购买");
            String goodsName=null;
            int buycount1=0;
            while(true){
                int buyorder=in.nextInt();
                if(buyorder==0)break;
                else{
                    int number=buyorder;
                    System.out.println("请输入您要购买的数量");
                    int buycount=in.nextInt();
                    buycount1=buycount;
                    changeGoods buygoods=new changeGoods(number,buycount);
                    sellGoods sellgoods=new sellGoods(buygoods,buycount);
                    summoney=summoney+sellgoods.getMoney();
                    goodsName=buygoods.getName();
                }
                System.out.println("输入商品编号可继续购买,输入0即可退出购买!");

            }
            if(goodsName!=null){
                System.out.println("您本次购买的物品为:"+goodsName);
                System.out.println("数量为"+buycount1);
                System.out.println("您本次购买的金额为"+summoney+"元");
            }
            else
            System.out.println("您此次未购买任何物品");
            String xiushou=in.nextLine();
            System.out.println("是否退出系统");
            System.out.println("输入0即可退出系统!输入其他表示继续购买!");
            String n=in.nextLine();
            Goods g=new Goods();
            if(n.equals("0"))break;
        }
    }
    public sellGoods(changeGoods goods,int count){
        if(goods.getCount()>=0)
            this.money=goods.getprice()*count;
        else if(goods.getCount()<0){
            System.out.println("您买的商品数量超过现有存货!请重新购买!");
        }
        else System.out.println("该商品已售罄!");
    }
    public double getMoney(){
        return money;
    }
}

10.删除用户类

import java.io.*;
public class deleteUsers {
    private static File file=new File("users.txt");
    public deleteUsers(String username){
        int sb=0;
        try (FileReader fr=new FileReader(file);
        BufferedReader br=new BufferedReader(fr);
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw=new BufferedWriter(fw);){
            String line;
            bw.append("");
            while((line=br.readLine())!=null){
                String args[]=line.split(",");
                if(args[0].equals(username)){
                   line="";
                   sb=1;
                   bw.append(line);
                }else
                bw.append(line+'\n');
            }
            try(FileWriter fw1=new FileWriter(file)){
            }catch (IOException e){
                e.printStackTrace();
            }
        }catch (IOException e){
         e.printStackTrace();
        }
        if(sb==0) System.out.println("该用户未创建用户名,请重新输入要删除的用户名!");
        else
        System.out.println("删除成功");
    }
}

 

题外话,这代码就是托答辩,你们要是没有思路可以看看,但是不要和我这样写,我这是边学边写的,所以非常乱,并且一点也不美观,后期我会重写的,也不知道有没有时间重写,我感觉我已经触碰到“屎山”了。

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值