我用的是mysql数据库,Idea编译器,jdk1.7;

源码:在这

点击打开源码

首先建立一个DB包,新建一个DB class 用来连接自己的数据库;

package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DB {
    private static final String URL="jdbc:mysql://localhost:3306/card_meal?characterEncoding=utf8&useSSL=true";
    private static final String USER="root";//
    private static final String PASSWORD="root";
    //上面分别定义了URL(统一资源定位器),USER(用户名),PASSWORD(密码)

    private static Connection conn=null;   //(定义一个空的Connection)
    static {
        //使用try-catch语句,抛出错误
        try {
            Class.forName("com.mysql.jdbc.Driver");
             conn= DriverManager.getConnection(URL, USER, PASSWORD);
             //使用你在头部定义的三个变量,分别确定连接数据库的位置,用户名,密码
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        return conn;
    }
    //无参的构造方法
}

 

然后新建一个Card包,新建一个Card class 表示饭卡,里面的属性主要有

ID(学号),name(姓名),password(密码),class-name(班级名称),money(余额),numOfBankCard(饭卡对应的银行卡的钱数)

以及各种get,set方法

package Card;


public class Meal_Card {
    private String id;
    private String name;
    private String class_name;
    private double money;
    private String password;
    private double numOfBankCard;
    public void setNumOfBankCard(double numOfBankCard) {
        this.numOfBankCard = numOfBankCard;
    }

    public double getNumOfBankCard() {
        return numOfBankCard;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setClass_name(String class_name) {
        this.class_name = class_name;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getId() {
        return id;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public String getClass_name() {
        return class_name;
    }

    public double getMoney() {
        return money;
    }

}

PS:我是对着自己的饭卡的属性来写的,所以没啥说的

接下来开始写model-view-controller层(我抄慕课网上的老师的方法,我是不太理解,也就知道它们分的比较散,便于修改)

view包里面一个action类

package model.view;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import action.Card_action;
import Card.Meal_Card;

public class action {
	public static void main(String[] args) throws InterruptedException, SQLException {
		Scanner in = new Scanner(System.in);
		System.out.println("----------------------欢迎进入郑州轻工业饭卡业务系统---------------------");
		Integer power = 0; // 权限,power为 10 时为管理员权限,power为 0 时为用户权限
		Loop: while (true) {
			System.out.println(" 1.登录界面,请按 1 \n 2.注册界面,请按 2 \n 0.退出系统,请按 0 ");
			if (power == 10) {
				System.out.println(" 9.退出管理员身份,请输入 9 ");
			}
			String flag = in.nextLine(); // 通过判断输入的值,来确定用户接下来的操作
			power = admin(flag, power);// 管理员权限登录及回收
			if (flag.equals("1")) { // 输入“2”,首先先确定用户的卡号,然后,再进行其他操作。
				Meal_Card card = null; // 顶替一张新卡,看能否指向
				while (true) {
					if (power != 10) {
						System.out.print("请输入你的学号:");// 通过学号查询,调用CardDao的findCard方法
					} else {
						System.out.print("请输入您要操作的目标学号:");
					}
					String id0 = in.nextLine().trim();
					if (id0.equals("0")) { // 若输入的是0,则退出
						System.out.println("退回主界面\n");
						break Loop;
					}
					Card_action ca = new Card_action();
					card = ca.find(id0);// 通过输入的id得到一个旧卡的属性,
					// 如果id不合法,则令旧卡id=“-1”,使其报错
					if (card.getId().equals("-1")) {
						System.out.println("----------------------您输入的用户不存在----------------------");
						System.out.println("请重新输入用户名,或输入 0 退出主界面");
					} else {
						break;
						// //成功查找到该用户,进入密码验证
					}
				}
				while (true) {
					if (power == 0) {
						System.out.print("请输入您的密码: ");
					} else {
						System.out.println("管理员输入回车即可强制登录。");
					}
					String t = in.nextLine().trim();
					if (power == 10) { // 判断是否是管理员权限
						System.out.println("管理员强制登录中。。。。");
						Thread.sleep(1000);
						System.out.println("----------------------管理员强制登录成功----------------------------");
						break;
					} else if (card.getPassword().equals(t)) { // 判断用户输入的密码是否正确,正确则进入下一步
						break;
					} else { // 既不是管理员,密码输入又错误,那么需要重新输入
						System.out.println("您的密码输入错误,请重新输入.\n");
					}
				}
				System.out.println("---------------------------欢迎使用饭卡系统-----------------------");
				while (true) {
					if (power == 10) {
						System.out.println("0.注销饭卡 请按 0 (仅管理员可见)");
					}
					System.out.println("1.充值饭卡. 请按 1 ");
					System.out.println("2.查询余额. 请按 2");
					System.out.println("3.查询信息. 请按 3 ");
					System.out.println("4.饭卡消费. 请按 4");
					System.out.println("5.查询银行卡上余额 请按 5 ");
					System.out.println("6.修改信息. 请按 6 ");
					System.out.println("7.退出系统. 请按 7 ");
					String k = in.nextLine();// 判断用户输入的值
					Card_action cdan = new Card_action();// 新new一个Card_action对象,便于调用其内部方法
					if (power == 10 && k.equals("0")) {
						cdan.deleteCard(card); // 调用删除用户方法,进行删除
					} else if (k.equals("1")) {
						cdan.addMoney01(card); // 调用充钱方法
					} else if (k.equals("2")) {
						cdan.queryCardMoney(card);
					} else if (k.equals("3")) {
						cdan.queryCard(card);
					} else if (k.equals("4")) {
						cdan.costCard(card);
					} else if (k.equals("5")) {
						cdan.queryBankCard(card);
					} else if (k.equals("6")) {
						cdan.alterMessage(card);
					} else if (k.equals("7")) {
						System.out.println("退回主界面\n");
						break;
					} else {
						prinError();
					}
				}
			} else if (flag.equals("2")) { // 输入“1”,所以办理一张饭卡,调用Card_action的addCard1方法新增card
				Card_action cm = new Card_action();
				cm.addCard1();
				System.out.println("您办理饭卡余额为100.0元\n");
			} else if (flag.equals("0")) {// 输入“0”,退出系统。
				prinEnd();
				break;
			} else if (!flag.equals("admin")) { // 输入错误,提醒用户重新输入
				prinError();
			}
		}
	}

	public static void prinError() {
		System.out.println("您的输入不合法,请重新输入.");
	}

	public static void prinEnd() {
		System.out.println("--------期待您的下次光临!--------");
	}

	public static int admin(String flag, int power) throws InterruptedException {
		if (flag.equals("admin")) {
			Scanner in = new Scanner(System.in);
			System.out.print("请输入管理员密码:");
			String pass = in.nextLine();
			if (pass.equals("000000")) {
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss\n");// 设置日期格式
				System.out.println("管理员,您好,现在是" + df.format(new Date()));
				return 10;
			} else {
				System.out.println("密码错误,未得到管理员权限!!");
			}
		} else if (flag.equals("9")) {
			Thread.sleep(1000);
			System.out.println("-------您已成功退出管理员系统---------");
			return 0;
		}
		return power;
	}
}

几乎所有除了输出以外要实现的功能都调用的是model层的model包里面的Card_action

Card_action class里面的内容

package action;

import dao.CardDao;
import Card.Meal_Card;
import javax.smartcardio.Card;
import java.sql.SQLException;
import java.util.Scanner;

public class Card_action {
	public static void addCard1() throws SQLException {
		Scanner input = new Scanner(System.in);
		System.out.println("郑州轻工业饭卡欢迎您的使用!!!");
		Meal_Card card = new Meal_Card();
		while (true) {
			System.out.print("请输入您的学号:");
			String id = input.nextLine();
			CardDao cd = new CardDao();
			Meal_Card tempcard = cd.findCard(id);
			if (id.equals("0")) {
				System.out.println("输入不合法,请重新输入");
			} else if (tempcard.getId().equals("-1")) {
				card.setId(id);
				break;
			} else {
				System.out.println("用户学号已存在,请重新输入");
			}
		}
		System.out.print("请输入您的姓名:");
		card.setName(input.nextLine());
		System.out.print("请输入您的班级名称:");
		card.setClass_name(input.nextLine());
		System.out.print("请输入您的密码(---重要----):");
		card.setPassword(input.nextLine());
		CardDao cd = new CardDao();
		cd.addCard2(card);
		System.out.println();
	}

	public static void addMoney01(Meal_Card card) throws SQLException {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入您要充值的金额");
		int numOfMoney = in.nextInt();
		CardDao cd = new CardDao();
		cd.addMoney(card, numOfMoney);
	}

	public static void queryCardMoney(Meal_Card card) {
		System.out.println("饭卡余额:" + card.getMoney());
	}

	public static void queryCard(Meal_Card card) {
		System.out.println("您饭卡的完整信息为:");
		System.out.println("班级:" + card.getClass_name());
		System.out.println("学号:" + card.getId());
		System.out.println("姓名:" + card.getName());
	}

	public static Meal_Card find(String id) throws SQLException {
		CardDao cd = new CardDao();
		return cd.findCard(id);
	}

	public static void deleteCard(Meal_Card card) throws SQLException {
		CardDao cd = new CardDao();
		System.out.println("您真的要删除这个饭卡吗?\n 输入 1 确定,其他退出修改");
		Scanner in = new Scanner(System.in);
		if (in.nextLine().equals("1")) {
			System.out.println("已经成功删除学号为:" + card.getId() + "的用户。");
			cd.deleCard(card);
		} else {
			System.out.println("未删除" + card.getId() + " " + card.getName());
		}
	}

	public static void costCard(Meal_Card card) throws SQLException {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入您要花费的金额:");
		double cost = in.nextDouble();
		CardDao cd = new CardDao();
		cd.costCard(card, cost);
	}

	public static void queryBankCard(Meal_Card card) {
		System.out.println("银行卡余额为:" + card.getNumOfBankCard());
	}

	public static void alterMessage(Meal_Card card) throws SQLException {
		System.out.println("您当前的信息为:");
		queryCard(card);
		System.out.println("您确定要修改信息吗? 按 1 确认,其他退出修改");
		Scanner in = new Scanner(System.in);
		if (in.nextLine().equals("1")) {
			System.out.println("您已确认修改!");
			CardDao cd = new CardDao();
			cd.alter(card);
		} else {
			System.out.println("您放弃了修改个人信息。");
		}
	}
}

但是对于数据库的操作我又传给了dao包下的CardDao class 用来专门实现想要对数据库的修改

package dao;

import Card.Meal_Card;
import DB.DB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class CardDao {
	public static void addCard2(Meal_Card mc) throws SQLException {
		Connection conn = DB.getConnection();
		String sql = "" + "insert into card" + "(id, name, class_name,password)" + "values(" + "?,?,?,?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);

		ptmt.setString(1, mc.getId());
		ptmt.setString(2, mc.getName());
		ptmt.setString(3, mc.getClass_name());
		ptmt.setString(4, mc.getPassword());
		ptmt.execute();

	}

	public static Meal_Card findCard(String id) throws SQLException {// 通过传入的id进行查询是否存在饭卡,并将存在的饭卡作为返回值
		Connection conn = DB.getConnection();
		String sql = "" + "select * from card where id=? ";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, id);
		ResultSet rs = ptmt.executeQuery();
		Meal_Card card = new Meal_Card();
		if (rs.next()) {
			card.setId(rs.getString("id"));
			card.setName(rs.getString("name"));
			card.setClass_name(rs.getString("class_name"));
			card.setMoney(rs.getFloat("money"));
			card.setPassword(rs.getString("password"));
			card.setNumOfBankCard(rs.getDouble("numOfBankCard"));
		} else {
			card.setId("-1");
		}
		return card;
	}

	public static double addMoney(Meal_Card card, Integer add) throws SQLException {
		Connection conn = DB.getConnection();
		if (card.getNumOfBankCard() < add) {
			System.out.println("银行卡余额已不足,请及时到银行充值!!");
		} else {
			card.setMoney(card.getMoney() + (double) add); // 饭卡余额
			card.setNumOfBankCard(card.getNumOfBankCard() - (double) add); // 银行卡余额
			String sql = "" + "update card " + "set money=? , numOfBankCard=? " + "where id= ? ";
			PreparedStatement ptmt = conn.prepareStatement(sql);

			ptmt.setFloat(1, (float) card.getMoney());
			ptmt.setFloat(2, (float) card.getNumOfBankCard());
			ptmt.setString(3, card.getId());
			ptmt.execute();
			System.out.println("充值成功,您卡上的余额为:" + card.getMoney());
		}
		return card.getMoney();
	}

	public static void deleCard(Meal_Card card) throws SQLException {
		Connection conn = DB.getConnection();
		String sql = "" + "delete from card " + "where id=?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, card.getId());
		ptmt.execute();

	}

	public static void costCard(Meal_Card card, double cost) throws SQLException {
		Connection conn = DB.getConnection();
		if (card.getMoney() < cost) {
			System.out.println("饭卡余额已不足,请及时充值!!");
		} else {
			card.setMoney(card.getMoney() - cost); // 饭卡余额
			String sql = "" + "update card " + "set money=? " + "where id= ? ";
			PreparedStatement ptmt = conn.prepareStatement(sql);
			ptmt.setFloat(1, (float) card.getMoney());
			ptmt.setString(2, card.getId());
			ptmt.execute();
			System.out.println("你已消费" + cost + "元,卡上余额为" + card.getMoney());
		}
	}

	public static void alter(Meal_Card card) throws SQLException {
		System.out.println("请重新输入个人信息:");
		String temp = card.getId();
		Scanner in = new Scanner(System.in);
		System.out.println("输入您新的学号");
		card.setId(in.nextLine());
		System.out.println("输入您新的用户名");
		card.setName(in.nextLine());
		System.out.println("输入您新的密码");
		card.setPassword(in.nextLine());
		System.out.println("输入您新的班级名称");
		card.setClass_name(in.nextLine());
		Connection conn = DB.getConnection();
		String sql = "" + "UPDATE card set id=?, name=?,class_name=?,password=?" + "where id=?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, card.getId());
		ptmt.setString(2, card.getName());
		ptmt.setString(3, card.getClass_name());
		ptmt.setString(4, card.getPassword());
		ptmt.setString(5, temp);
		ptmt.execute();
	}
}

然后就可以实现我暂时知道的我自己的饭卡的功能了

 

 

 

 

 

 

 

 

 

 

 

Logo

更多推荐