package org.ntlab.nemophila.models.accounts; import org.ntlab.nemophila.models.shops.Shop; import org.ntlab.nemophila.models.shops.ShopManager; import java.util.HashMap; import java.util.UUID; public class AccountManager { private static AccountManager theInstance = null; private HashMap<String,Account> accountsMap = new HashMap<>(); private int newId = 1111; private AccountManager() { //ダミーのお店を作成 ShopManager sm = ShopManager.getInstance(); Shop shopA = sm.createShop("甲南カフェ", 135.26, 34.73); Shop shopB = sm.createShop("新田ラーメン", 133.11, 33.29); Shop shopC = sm.createShop("ナンボーナンカレー", 130.78, 34.50); Shop shopD = sm.createShop("食事処ネモフィラ", 140.23, 35.21); //ダミーアカウントの作成 this.createAccount("渡邊","a"); Account testAc1 = this.getAccount("1111"); testAc1.createPost(shopA, 5, "カフェ", "落ち着いた雰囲気で良かった", null, null, null); testAc1.createPost(shopB, 3, "ラーメン", "提供が速かった", null, null, null); testAc1.createPost(shopC, 5, "カレー", "とてもおいしかった", null, null, null); testAc1.createPost(shopD, 4, "和食", "サバがおいしかった", null, null, null); this.createAccount("彌永","a"); Account testAc2 = this.getAccount("1112"); testAc2.createPost(shopA, 2, "カフェ", "騒がしかった", null, null, null); this.createAccount("和田","a"); Account testAc3 = this.getAccount("1113"); testAc3.createPost(shopA, 3, "カフェ", "コーヒーがうまいぞ", null, null, null); this.createAccount("前原","a"); Account testAc4 = this.getAccount("1114"); testAc4.createPost(shopA, 5, "カフェ", "落ち着いた雰囲気ですごく良い", null, null, null); FriendManager friendManagerA = testAc1.getFriendManager(); FriendManager friendManagerB = testAc1.getFriendManager(); FriendManager friendManagerC = testAc3.getFriendManager(); FriendManager friendManagerD = testAc4.getFriendManager(); friendManagerA.addFriend(testAc2); friendManagerA.addFriend(testAc3); friendManagerA.addFriend(testAc4); friendManagerB.addFriend(testAc1); friendManagerB.addFriend(testAc3); friendManagerB.addFriend(testAc4); friendManagerC.addFriend(testAc1); friendManagerC.addFriend(testAc2); friendManagerC.addFriend(testAc4); friendManagerD.addFriend(testAc1); friendManagerD.addFriend(testAc2); friendManagerD.addFriend(testAc3); } //シングルトン化 public static AccountManager getInstance() { if(theInstance == null){ theInstance = new AccountManager(); } return theInstance; } //全てのアカウントの取得 public HashMap<String, Account> getAccountsMap() { return accountsMap; } //アカウントの取得 public Account getAccount(String id){ return accountsMap.get(id); } //新規アカウントの作成 public Account createAccount(String name, String pw){ //ランダムにトークンを作成 UUID uuid = UUID.randomUUID(); String token = uuid.toString(); Account ac = new Account(name, pw); ac.setToken(token); //IDの作成 String id = Integer.toString(newId); ac.setId(id); newId++; accountsMap.put(id, ac); return ac; } //再ログイン時に新しいトークンを更新する public Account updateAccount(String id) { Account ac = accountsMap.get(id); UUID uuid = UUID.randomUUID(); String token = uuid.toString(); ac.setToken(token); return ac; } //アカウント消去 public void deleteAccount(String id){ accountsMap.remove(id); } }