JAVA寵物商店,大一狗的練習

作爲一名新人,這是我這個廢柴大一狗大一狗的練習作業,要用什麼似懂非懂的DAO程序設計模式寫

要求過程是這樣的






過程大概是這樣的,講實話我看的是一臉懵逼,不過作業總是要交差的,只能硬着頭皮,寫一點是一點了

首先建表


然後我就頭一次破天荒一臉懵逼的分了五個包


大概就是一個連接數據庫用的,還有一個接口包,還有一個包是實體類,還有一個是用來寫具體的實現,最後一個就是測試了

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;


import com.cn.Test.Main;
import com.cn.Test.Manager;
import com.cn.dao.BaseDao;
import com.cn.entity.Account;
import com.cn.entity.Pet;
import com.cn.entity.PetOwner;
import com.cn.entity.PetStore;
import com.cn.impl.PetOwnerService;

public class PetOwnerServiceImpl extends BaseDao implements PetOwnerService{

	private String sname;
	public static int i;
	Scanner sc = new Scanner(System.in);
	private int sell;
	
//登錄驗證實現方法
	@Override
	public HashMap LoginCheck() throws Exception {
		BaseDao.selectPetOwnerServiceDao();
		System.out.println("請先登錄,請輸入主人名字:");
		 sname=sc.next();
		System.out.println("\n請輸入主人密碼:");
		String spassword=sc.next();
		Set keySet = hm.keySet();
		Iterator it = keySet.iterator();
		boolean falg = false;
		while (it.hasNext()) {
			Object user =  it.next();
			Object pw = hm.get(user);
			if (sname.equals(user)&&spassword.equals(pw)) {
				System.out.println("登錄成功");
				Manager.PetDeal();
				
				falg = true;
			}
			
		}
		if (falg==false) {
			System.out.println("用戶不存在,請重新輸入!!!");
			LoginCheck();
		}
		return hm;
	}

	@Override
//查看登錄的寵物主人實現方法	
	public ResultSet selectPetOwnerService(PetOwner po) throws Exception {
		String sql="select * from petowner where name='"+sname+"';";
		ResultSet i = this.selectPetOwnerServiceDao(sql);
		return i;
	}
//查看寵物庫存實現方法
	@Override
	public ResultSet InventoryPetOwnerService(Pet p) throws Exception {
		String sql="select * from pet;";
		ResultSet i = this.selectInventoryPetDao(sql);
		return i;
	}
//購買庫存寵物實現方法	
	@Override
	public int BuyInventoryPetOwner(Pet p, Account ac) throws Exception {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("---請選擇要購買哪一個寵物,並輸入序號---");
		int id=sc.nextInt();
		System.out.println("請輸入交易金額:");
		int money=sc.nextInt();
		String sql1="UPDATE pet SET owner_id ="+i+" WHERE id="+id+" ;";
		String sql4="UPDATE pet SET store_id ="+"null"+" WHERE id="+id+" ;";
		String sql2="insert account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values("+1+","+id+","+i+","+"(SELECT store_id FROM pet WHERE id="+id+"),"+money+","+"YEAR(NOW())"+");";
		String sql3="update petowner set money=money-"+money+" where id="+i+";";
		int i=this.BuyPet(sql1,sql2,sql3,sql4);
		return i;
	}
//查詢我的寵物
	@Override
	public ResultSet SellSelectPetOwner(Pet p, Account ac) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("---我的寵物列表---");
		String sql="select * from pet where id="+i+";";
		ResultSet i = this.OwnerselectPetDao(sql);
		return i;
		
	}
//選擇出售寵物	
	@Override
	public ResultSet SellPetOwner(Pet p) throws Exception {
		System.out.println("---請輸入要出售的寵物序號---");
		 sell=sc.nextInt();
		 String sql="select * from pet where owner_id="+i+";";
		 ResultSet i=this.SellSelectPetDao(sql);
		return i;
	}
	//出售寵物執行
	@Override
	public int SellPetOwnerExecute() throws Exception{
		
		int store=sc.nextInt();
		String sql1="update pet set store_id="+store+" where id="+sell+";";
		String sql4="update pet set owner_id="+"null"+" where id="+sell+";";
		System.out.println("請輸入交易金額:");
		int money=sc.nextInt();
		String sql2="update petowner set money=money+"+money+" where id="+i+";";
		String sql3="insert account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values("+2+","+sell+","+i+","+store+","+money+","+"YEAR(NOW())"+");";
		int i=this.BuyPet(sql1,sql2,sql3,sql4);
		return i;
		
	} 
	
}

具體的我就把主人登錄頁面寫完了,具體就一個登錄驗證,購買和出售的方法




大概就是這樣了

package com.cn.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.cn.dao.impl.PetOwnerServiceImpl;
import com.cn.entity.Pet;

public class BaseDao {

	private static String driver; // 驅動
	private static String url; // 鏈接地址
	private static String user; // 用戶名
	private static Connection conn; // 連接對象
	private static String password; // 密碼
	public static HashMap hm=new HashMap();
	private static String name;
//連接Mysql
	public static Connection getConnection() throws Exception {
		driver = "com.mysql.jdbc.Driver";
		Class.forName(driver);
		url = "jdbc:MYSQL://localhost:3306/ljt";
		user = "root";
		password = "root";
		conn = DriverManager.getConnection(url, user, password);
		return conn;
	}

	/**
	 * 增,刪,該的操作方法
	 * */
	public int execute(String sql,Object[] parmar) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement st = conn.prepareStatement(sql);
		//遍歷
		for (int i = 0; i < parmar.length; i++) {
			//通過循環添加值
			st.setObject(i+1, parmar[i]);
		}
		//執行
		int i = st.executeUpdate();
		return i;
	}
	//購買寵物執行方法
	public int BuyPet(String sql,String sql2,String sql3,String sql4) throws Exception{
		conn = getConnection();
		//獲取st對象
		PreparedStatement st = conn.prepareStatement(sql);
		PreparedStatement st2 = conn.prepareStatement(sql2);
		PreparedStatement st3 = conn.prepareStatement(sql3);
		PreparedStatement st4= conn.prepareStatement(sql4);
		int i = st.executeUpdate();
		i=st2.executeUpdate();
		i=st3.executeUpdate();
		i=st4.executeUpdate();
		System.out.println("交易成功!!!");
		System.out.println("臺賬正確插入一條信息!!!");
		return i;
	}
	//查詢Account表
	public ResultSet  selectAccountDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		while (i.next()) {
			int id = i.getInt("id");
			int deal_type = i.getInt("deal_type");
			int pet_id = i.getInt("pet_id");
			int seller_id = i.getInt("seller_id");
			int buyer_id = i.getInt("buyer_id");
			int price = i.getInt("price");
			int deal_time = i.getInt("deal_time");
			System.out.println(id+"\t"+deal_type+"\t"+pet_id+"\t"+seller_id+"\t"+buyer_id+"\t"+price+"\t"+deal_time );
			
		}
		return i;
	}
	//查詢Pet表執行
	public ResultSet  selectPetDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"寵物名稱");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String type_name=i.getString("type_name");
			int health=i.getInt("health");
			int love=i.getInt("love");
			String birthday=i.getString("birthday");
			int owner_id=i.getInt("owner_id");
			int store_id=i.getInt("store_id");
			System.out.println(id+"\t"+name);
			
		}
		
		return i;
	}
	//選擇出售寵物信息
	public ResultSet  SellSelectPetDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"寵物名稱");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String type_name=i.getString("type_name");
			int health=i.getInt("health");
			int love=i.getInt("love");
			String birthday=i.getString("birthday");
			int owner_id=i.getInt("owner_id");
			int store_id=i.getInt("store_id");
			System.out.println("寵物名字叫:"+name+"\t"+"寵物類別是:"+type_name);
			
		}
		
		return i;
	}
	//查詢庫存寵物執行
	public ResultSet  selectInventoryPetDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"寵物名稱"+"\t"+"類型"+"\t"+"元寶數");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String type_name=i.getString("type_name");
			int health=i.getInt("health");
			int love=i.getInt("love");
			String birthday=i.getString("birthday");
			int owner_id=i.getInt("owner_id");
			int store_id=i.getInt("store_id");
			System.out.println(id+"\t"+name+"\t"+type_name);
		}
		
		return i;
	}
	//查詢PetStore表執行
	public ResultSet  selectPetStoreDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"寵物商店");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String password=i.getString("password");
			int balance=i.getInt("balance");
			System.out.println(id+"\t"+name);
			
		}
		
		return i;
	}
	//
	public static HashMap  selectPetOwnerServiceDao() throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		String sql="select * from petowner;";
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		while (i.next()) {
			
			int id = i.getInt("id");
			 name=i.getString("name");
			 password=i.getString("password");
			int money=i.getInt("money");
			hm.put(name, password);	
		}
		
		return hm;
	}
	public ResultSet  selectPetOwnerDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"主人姓名");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String password=i.getString("password");
			int money=i.getInt("money");
			System.out.println(id+"\t"+name);
			
		}
		
		return i;
	}
	public ResultSet  selectPetOwnerServiceDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		while (i.next()) {
			PetOwnerServiceImpl.i = i.getInt("id");
			String name=i.getString("name");
			String password=i.getString("password");
			int money=i.getInt("money");
			System.out.println("名字:"+name);
			System.out.println("元寶數:"+money);
			
		}
		
		return i;
	}
	//查詢主人擁有的寵物
	public ResultSet  OwnerselectPetDao(String sql) throws Exception{
		//獲取連接對象
		conn = getConnection();
		//獲取st對象
		PreparedStatement pt=conn.prepareStatement(sql);
		ResultSet i = pt.executeQuery();
		System.out.println("序號"+"\t"+"寵物名稱"+"\t"+"類型");
		while (i.next()) {
			
			int id = i.getInt("id");
			String name=i.getString("name");
			String type_name=i.getString("type_name");
			int health=i.getInt("health");
			int love=i.getInt("love");
			String birthday=i.getString("birthday");
			int owner_id=i.getInt("owner_id");
			int store_id=i.getInt("store_id");
			System.out.println(id+"\t"+name+"\t"+type_name);
			
		}
		
		return i;
	}
	
}

大部分也就這樣了,雖然全是bug,但勉強能把老師糊弄過去了,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章