From 44b2bf3f185014c36ae4f45405b83552e9cd3d1f Mon Sep 17 00:00:00 2001 From: Pourya Alizadeh <118482112+ThePral@users.noreply.github.com> Date: Sat, 1 Jul 2023 18:05:34 +0330 Subject: [PATCH 1/3] Add files via upload Phase 1 --- App.java | 12 +++++ AppTest.java | 38 ++++++++++++++++ Bank.java | 44 ++++++++++++++++++ BankAccount.java | 72 +++++++++++++++++++++++++++++ BankInterface.java | 4 ++ BankTurnover.java | 20 +++++++++ Character.java | 76 +++++++++++++++++++++++++++++++ CharacterInterface.java | 6 +++ City.java | 67 +++++++++++++++++++++++++++ CityInterface.java | 9 ++++ Database.java | 69 ++++++++++++++++++++++++++++ Employee.java | 47 +++++++++++++++++++ FastFoodShop.java | 17 +++++++ Food.java | 32 +++++++++++++ Game.java | 42 +++++++++++++++++ GameInterface.java | 15 +++++++ Gender.java | 5 +++ Industry.java | 65 +++++++++++++++++++++++++++ Job.java | 42 +++++++++++++++++ Life.java | 92 ++++++++++++++++++++++++++++++++++++++ Liquid.java | 16 +++++++ Manager.java | 8 ++++ Menu.java | 41 +++++++++++++++++ Municipality.java | 33 ++++++++++++++ MunicipalityInterface.java | 11 +++++ Property.java | 37 +++++++++++++++ StockMarket.java | 63 ++++++++++++++++++++++++++ User.java | 37 +++++++++++++++ 28 files changed, 1020 insertions(+) create mode 100644 App.java create mode 100644 AppTest.java create mode 100644 Bank.java create mode 100644 BankAccount.java create mode 100644 BankInterface.java create mode 100644 BankTurnover.java create mode 100644 Character.java create mode 100644 CharacterInterface.java create mode 100644 City.java create mode 100644 CityInterface.java create mode 100644 Database.java create mode 100644 Employee.java create mode 100644 FastFoodShop.java create mode 100644 Food.java create mode 100644 Game.java create mode 100644 GameInterface.java create mode 100644 Gender.java create mode 100644 Industry.java create mode 100644 Job.java create mode 100644 Life.java create mode 100644 Liquid.java create mode 100644 Manager.java create mode 100644 Menu.java create mode 100644 Municipality.java create mode 100644 MunicipalityInterface.java create mode 100644 Property.java create mode 100644 StockMarket.java create mode 100644 User.java diff --git a/App.java b/App.java new file mode 100644 index 0000000..0e05f09 --- /dev/null +++ b/App.java @@ -0,0 +1,12 @@ +package org.example; + +/** + * Hello world! + * + */ +public class App { + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/AppTest.java b/AppTest.java new file mode 100644 index 0000000..cf35e75 --- /dev/null +++ b/AppTest.java @@ -0,0 +1,38 @@ +package org.example; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} \ No newline at end of file diff --git a/Bank.java b/Bank.java new file mode 100644 index 0000000..200f31f --- /dev/null +++ b/Bank.java @@ -0,0 +1,44 @@ +package org.example.defualtSystem; + +import org.example.interfaces.BankInterface; +import org.example.models.*; +import org.example.models.Character; + +import java.util.ArrayList; +import java.util.Date; + +public class Bank extends Industry implements BankInterface { + + private static final int MAX_EMPLOYEE_COUNT = 5; + private static final float BASE_EMP_SALARY = 0.5f; + private ArrayList accounts = new ArrayList(); + + private Manager manager = null; + + public static BankTurnover turnover; + + public Bank(Property property,Character root) { + super("Bank",property,root,100.0f); + turnover = new BankTurnover(); + } + + public BankAccount newAccount(String username,String password){ + BankAccount bankAccount = new BankAccount(username,password,0,new Date()); + accounts.add(bankAccount); + return bankAccount; + } + public boolean registerAsEmp(Character character){ + if(employees.size() >= MAX_EMPLOYEE_COUNT)return false; + Employee employee = new Employee(character.getUserInfo().getUsername(),this,BASE_EMP_SALARY,character.getAccount()); + employees.add(employee); + return true; + } + + + public String bankDetail(Character character){ + if(character.getUserInfo().getUsername().equals(manager.getUsername())){ + return ""; + } + return "Only Manager can see Bank detail"; + } +} diff --git a/BankAccount.java b/BankAccount.java new file mode 100644 index 0000000..fc42329 --- /dev/null +++ b/BankAccount.java @@ -0,0 +1,72 @@ +package org.example.models; + +import org.example.defualtSystem.Bank; + +import java.util.Date; + +public class BankAccount { + private String owner; + private String password; + private float money; + private Date lastChange; + + private String logs = ""; + + public BankAccount(String owner, String password, float money, Date lastChange) { + this.owner = owner; + this.password = password; + this.money = money; + this.lastChange = lastChange; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public float getMoney() { + return money; + } + + + public Date getLastChange() { + return lastChange; + } + + public void setLastChange(Date lastChange) { + this.lastChange = lastChange; + } + + public boolean withdraw(Character character,float amount){ + if(character.getUserInfo().getUsername().equals(owner)){ + if(amount <= money){ + Bank.turnover.transfer(amount,-1); + money-= amount; + return true; + }else + return false; + } + return false; + } + public boolean deposit(Character character,float amount){ + if(amount >0){ + String log = String.format("User : %s deposit %f \n",character.getUserInfo().getUsername(),amount); + logs+=log; + Bank.turnover.transfer(amount,1); + money += amount; + return true; + } + return false; + } +} diff --git a/BankInterface.java b/BankInterface.java new file mode 100644 index 0000000..3ff6269 --- /dev/null +++ b/BankInterface.java @@ -0,0 +1,4 @@ +package org.example.interfaces; + +public interface BankInterface { +} diff --git a/BankTurnover.java b/BankTurnover.java new file mode 100644 index 0000000..1865c10 --- /dev/null +++ b/BankTurnover.java @@ -0,0 +1,20 @@ +package org.example.defualtSystem; + +public class BankTurnover { + private double withdraw = 0; + private double deposit = 0; + + /** + * @param amount : Money amount + * @param type : -1 for withdraw , +1 for deposit + * */ + public void transfer(float amount,int type){ + if(type == 1) + deposit=amount; + else if (type==-1) + withdraw+=amount; + } + public double calcTurnover(){ + return deposit-withdraw; + } +} diff --git a/Character.java b/Character.java new file mode 100644 index 0000000..f2d3a64 --- /dev/null +++ b/Character.java @@ -0,0 +1,76 @@ +package org.example.models; + +import org.example.defualtSystem.Life; +import org.example.interfaces.CharacterInterface; + +import java.util.ArrayList; + +public class Character implements CharacterInterface { + private User userInfo; + private BankAccount account; + private Life life; + + private Job job; + private ArrayList properties; + + private Property inPosition; + + public Character(User userInfo, BankAccount account, Life life, Job job, ArrayList properties, Property inPosition) { + this.userInfo = userInfo; + this.account = account; + this.life = life; + this.job = job; + this.properties = properties; + this.inPosition = inPosition; + } + + public User getUserInfo() { + return userInfo; + } + + public void setUserInfo(User userInfo) { + this.userInfo = userInfo; + } + + public BankAccount getAccount() { + return account; + } + + public void setAccount(BankAccount account) { + this.account = account; + } + + public Life getLife() { + return life; + } + + public void setLife(Life life) { + this.life = life; + } + + public Job getJob() { + return job; + } + + public void setJob(Job job) { + this.job = job; + } + + public void gotToLocation(Property destination){ + if(destination==null)return; + inPosition = destination; + } + + public ArrayList getProperties() { + return properties; + } + + public void setProperties(ArrayList properties) { + this.properties = properties; + } + + @Override + public void positionProcessing() { + + } +} diff --git a/CharacterInterface.java b/CharacterInterface.java new file mode 100644 index 0000000..f756469 --- /dev/null +++ b/CharacterInterface.java @@ -0,0 +1,6 @@ +package org.example.interfaces; + +public interface CharacterInterface { + void positionProcessing(); + +} diff --git a/City.java b/City.java new file mode 100644 index 0000000..30a25ce --- /dev/null +++ b/City.java @@ -0,0 +1,67 @@ +package org.example.models; + +import org.example.defualtSystem.Bank; +import org.example.defualtSystem.Life; +import org.example.defualtSystem.Municipality; +import org.example.defualtSystem.StockMarket; +import org.example.interfaces.CityInterface; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +public class City implements CityInterface { + private final ArrayList characters; + private final Bank bankSystem; + private final Municipality municipality; + + private final StockMarket stockMarket; + + private Character root; + + public City() { + characters = new ArrayList<>(); + municipality = new Municipality(); +// Get Bank Property from municipality + bankSystem = new Bank(new Property(new float[]{12, 32}, new float[]{42, 32}, root), root); + stockMarket = new StockMarket(); + stockMarket.startMarketSimulation(); + } + + @Override + public void joinCharacter(User userinfo) { + BankAccount newAccount = bankSystem.newAccount(userinfo.getUsername(), userinfo.getPassword()); + Character character = new Character(userinfo, newAccount, new Life(), null, null, null); + characters.add(character); + beginGame(character); + } + + @Override + public void getCityDetail() { + String players = Arrays.toString(characters.toArray()); + } + + + /** + * Begin Game function generate a new thread for each character , DO NOT CHANGE THIS FUNCTION STRUCTURE , + * + * */ + private void beginGame(Character character) { + Thread thread = new Thread(() -> { + try { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("Show Menu"); + switch (scanner.next()) { + case "2"-> System.out.println("Do Something"); + case "3"-> System.out.println("Do Something"); + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + thread.start(); + } +} diff --git a/CityInterface.java b/CityInterface.java new file mode 100644 index 0000000..b912da2 --- /dev/null +++ b/CityInterface.java @@ -0,0 +1,9 @@ +package org.example.interfaces; + +import org.example.models.Character; +import org.example.models.User; + +public interface CityInterface { + void joinCharacter(User character); + void getCityDetail(); +} diff --git a/Database.java b/Database.java new file mode 100644 index 0000000..6037f3f --- /dev/null +++ b/Database.java @@ -0,0 +1,69 @@ +package org.example; + +import org.example.models.User; + +import javax.xml.crypto.Data; +import java.sql.*; + +public class Database { + + static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; + static final String DB_URL = "jdbc:mysql://localhost/lightcity"; + + // Database credentials + static final String USER = "your_username"; + static final String PASS = "your_password"; + + + private Connection conn; + + public Database() { + try { + Class.forName(JDBC_DRIVER); + conn = DriverManager.getConnection(DB_URL, USER, PASS); + System.out.println("Connecting to database..."); + } catch (Exception exp) { + System.out.println("Database Exception : \n" + exp.toString()); + System.exit(0); + } + } + // Tables + + /** + * Users + */ + + private void createTables() { +// query example + String query = "CREATE TABLE IF NOT EXISTS Users (username varchar(255) primary key ,password varchar(255));" + + "CREATE TABLE IF NOT EXISTS ...."; + try { + Statement stmt = conn.createStatement(); + if(stmt.execute(query)){ + + }else + System.out.println("An error accord during operation"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + } + + public void loginGame(User user) { + try { + Statement stmt = conn.createStatement(); + String query = ""; + ResultSet res = stmt.executeQuery(query); + while (res.next()) { +// Check + } + } catch (Exception exception) { + } + + } + + public void registerGame(User user) { + } + +} \ No newline at end of file diff --git a/Employee.java b/Employee.java new file mode 100644 index 0000000..a8d76df --- /dev/null +++ b/Employee.java @@ -0,0 +1,47 @@ +package org.example.models; + +public class Employee { + private String username; + private float baseSalary ; + private int level; + + private BankAccount bankAccount; + + private Industry industry; + + public Employee(String username,Industry industry, float baseSalary,BankAccount bankAccount) { + this.username = username; + this.baseSalary = baseSalary; + this.industry = industry; + this.level = 1; + this.bankAccount = bankAccount; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public float getBaseSalary() { + return baseSalary; + } + + public void setBaseSalary(float baseSalary) { + this.baseSalary = baseSalary; + } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public void paySalary(){ + bankAccount.deposit(industry.getOwner(),level*baseSalary); + } +} diff --git a/FastFoodShop.java b/FastFoodShop.java new file mode 100644 index 0000000..092ef60 --- /dev/null +++ b/FastFoodShop.java @@ -0,0 +1,17 @@ +package org.example.defualtSystem; + +import org.example.models.Character; +import org.example.models.Industry; +import org.example.models.Property; + +public class FastFoodShop extends Industry { + + /** + * Industry type example (Business) + * */ + private static final float INCOME = 0.3f; + private static final float EMPLOYEE_INCOME = 0.02f; + public FastFoodShop(String title, Property property, Character character) { + super(title, property, character,EMPLOYEE_INCOME); + } +} diff --git a/Food.java b/Food.java new file mode 100644 index 0000000..137ec2f --- /dev/null +++ b/Food.java @@ -0,0 +1,32 @@ +package org.example.models; + +public class Food { + + private String title; + private final float water; + private final float food; + + public boolean available = true; + + public Food(String title,float food,float water) { + this.food = food; + this.water = water; + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public float getWater() { + return water; + } + + public float getFood() { + return food; + } +} diff --git a/Game.java b/Game.java new file mode 100644 index 0000000..17457b0 --- /dev/null +++ b/Game.java @@ -0,0 +1,42 @@ +package org.example; + +import org.example.interfaces.GameInterface; +import org.example.models.Character; +import org.example.models.City; +import org.example.models.User; + +import java.util.ArrayList; + +public class Game implements GameInterface { + + // Check Data from Database or file to see There is city or not + private City city; + @Override + public void continueGame(User user) { + + } + + /** Create new city and Generate new Character + * @param user : User information contain username, password + * */ + @Override + public void startGame(User user) { + generateNewCity(); + city.joinCharacter(user); + } + + /** + * @param ip Server ip address / example : 127.0.0.1 + * @param port Server open port for specific ip address + * */ + @Override + public void joinServer(String ip, int port) { + + } + + @Override + public void generateNewCity() { + city = new City(); + } + +} diff --git a/GameInterface.java b/GameInterface.java new file mode 100644 index 0000000..86e9086 --- /dev/null +++ b/GameInterface.java @@ -0,0 +1,15 @@ +package org.example.interfaces; + +import org.example.models.Character; +import org.example.models.User; + +public interface GameInterface { + void continueGame(User user); + void startGame(User user); + + void joinServer(String ip ,int port); + + void generateNewCity(); + + +} diff --git a/Gender.java b/Gender.java new file mode 100644 index 0000000..a8f0c9b --- /dev/null +++ b/Gender.java @@ -0,0 +1,5 @@ +package org.example.enumerations; + +public enum Gender { + Male,Female +} diff --git a/Industry.java b/Industry.java new file mode 100644 index 0000000..eaa658d --- /dev/null +++ b/Industry.java @@ -0,0 +1,65 @@ +package org.example.models; + +import java.util.ArrayList; +import java.util.List; + +public class Industry extends Property{ + private String title; + private float income; + + protected ArrayList employees = new ArrayList<>(); + + /** + * @param title : A Title for generate Industry @example : Bank extends Industry then title = "Bank" + * @param property : Industry owner should have a Property to create this industry on it + * @param character : Industry owner + * @param income : Each Business has a class like Bank and extends Industry , in super method Enter the desired monthly income amount + * */ + public Industry(String title,Property property,Character character,float income) { + super(property.getScales(),property.getCoordinate(),character); + this.title = title; + this.income = income; + startPaySalary(); + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public float getIncome() { + return income; + } + + public void setIncome(float income) { + this.income = income; + } + + + public ArrayList getEmployee() { + return employees; + } + + public void setEmployee(ArrayList employee) { + this.employees = employee; + } + + public void startPaySalary(){ + Thread thread = new Thread(()->{ + while (true){ + employees.stream().forEach((employee)->{ + employee.paySalary(); + }); + try { + Thread.sleep(10800000); // wait for 1 minute + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + thread.start(); + } +} diff --git a/Job.java b/Job.java new file mode 100644 index 0000000..039d8bf --- /dev/null +++ b/Job.java @@ -0,0 +1,42 @@ +package org.example.models; + +public class Job { + private String title; + private float income; + private String industryId; + + /** + * @param title : Industry title + * @param income : industry The monthly income of its employees + * @param industryId : industry id + * */ + public Job(String title, float income, String industryId) { + this.title = title; + this.income = income; + this.industryId = industryId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public float getIncome() { + return income; + } + + public void setIncome(float income) { + this.income = income; + } + + public String getIndustryId() { + return industryId; + } + + public void setIndustryId(String industryId) { + this.industryId = industryId; + } +} diff --git a/Life.java b/Life.java new file mode 100644 index 0000000..5f87547 --- /dev/null +++ b/Life.java @@ -0,0 +1,92 @@ +package org.example.defualtSystem; + +import org.example.models.Food; +import org.example.models.Liquid; + +public class Life { + private float food; + private float water; + private float sleep; + + public Life(){ + food = 100.0f; + water=100.0f; + sleep=100.0f; + } + + public Life(float food, float water, float sleep) { + this.food = food; + this.water = water; + this.sleep = sleep; + startConsuming(); + } + + public float getFood() { + return food; + } + + public float getWater() { + return water; + } + + public float getSleep() { + return sleep; + } + + + public void foodConsumption(Food product){ + if(product.available){ + water+=product.getWater(); + food +=product.getFood(); + } + } + + public void liquidConsumption(Liquid product){ + if(product.available){ + water+=product.getLiquid(); + } + } + + + public void startConsuming() { + Thread thread = new Thread(() -> { + while (true) { + consumeFood(0.4f); + consumeWater(0.8f); + consumeSleep(0.7f); + + try { + Thread.sleep(60000); // wait for 1 minute + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + + thread.start(); + } + + private synchronized void consumeFood(float amount) { + if (food >= amount) { + food -= amount; + } else { + System.out.println("Not enough food!"); + } + } + + private synchronized void consumeWater(float amount) { + if (water >= amount) { + water -= amount; + } else { + System.out.println("Not enough water!"); + } + } + + private synchronized void consumeSleep(float amount) { + if (sleep >= amount) { + sleep -= amount; + } else { + System.out.println("Not enough sleep!"); + } + } +} diff --git a/Liquid.java b/Liquid.java new file mode 100644 index 0000000..0d3735f --- /dev/null +++ b/Liquid.java @@ -0,0 +1,16 @@ +package org.example.models; + +public class Liquid { + private final float liquid; + + public boolean available =true; + + public Liquid(float liquid) { + this.liquid = liquid; + } + + public float getLiquid() { + return liquid; + } + +} diff --git a/Manager.java b/Manager.java new file mode 100644 index 0000000..46e4064 --- /dev/null +++ b/Manager.java @@ -0,0 +1,8 @@ +package org.example.models; + +public class Manager extends Employee{ + + public Manager(String username, Industry industry, float baseSalary, BankAccount bankAccount) { + super(username, industry, baseSalary, bankAccount); + } +} diff --git a/Menu.java b/Menu.java new file mode 100644 index 0000000..10c7659 --- /dev/null +++ b/Menu.java @@ -0,0 +1,41 @@ +package org.example; + +import org.example.models.User; + +import java.util.Scanner; + +public class Menu { + private static Game game = new Game(); + private static Scanner scanner = new Scanner(System.in); + public static void showMenu(){ + mainMenu(); + String next = scanner.next(); + if (next.equals("1")) { + game.continueGame(loginMenu()); + }else if(next.equals("2")){ + game.startGame(loginMenu()); + }else if (next.equals("3")){ + joinServer(); + }else if (next.equals("4")) + System.exit(0); + } + public static void mainMenu(){ +// show menu : sout () + } + + public static User loginMenu(){ +// get user info : username, password + return null; + } + + private static void joinServer(){ + System.out.print("Enter Server Ip Address :"); + String ip = scanner.next(); + System.out.print("Enter Server Port :"); + int port = scanner.nextInt(); + game.joinServer(ip,port); + } + public static void main(String[] args) { + showMenu(); + } +} \ No newline at end of file diff --git a/Municipality.java b/Municipality.java new file mode 100644 index 0000000..851c600 --- /dev/null +++ b/Municipality.java @@ -0,0 +1,33 @@ +package org.example.defualtSystem; + +import org.example.interfaces.MunicipalityInterface; +import org.example.models.Property; + +import java.util.ArrayList; + +public class Municipality implements MunicipalityInterface { + private ArrayList properties; + + public Municipality(){ + generateProperties(); + } + + private void generateProperties() { +// Create an algorithm for generating properties for city + } + + @Override + public Property buyProperty() { + return null; + } + + @Override + public void sellProperty(Property property) { + + } + + @Override + public void showProperties() { + + } +} diff --git a/MunicipalityInterface.java b/MunicipalityInterface.java new file mode 100644 index 0000000..5d8bb97 --- /dev/null +++ b/MunicipalityInterface.java @@ -0,0 +1,11 @@ +package org.example.interfaces; + +import org.example.models.Property; + +public interface MunicipalityInterface { + + // Buy and sell property + Property buyProperty(); + void sellProperty(Property property); + void showProperties(); +} diff --git a/Property.java b/Property.java new file mode 100644 index 0000000..6b18828 --- /dev/null +++ b/Property.java @@ -0,0 +1,37 @@ +package org.example.models; + +public class Property { + private float[] scales; + private float[] coordinate; + private Character owner ; + + public Property(float[] scales, float[] coordinate, Character owner) { + this.scales = scales; + this.coordinate = coordinate; + this.owner = owner; + } + + public float[] getScales() { + return scales; + } + + public void setScales(float[] scales) { + this.scales = scales; + } + + public Character getOwner() { + return owner; + } + + public void setOwner(Character owner) { + this.owner = owner; + } + + public float[] getCoordinate() { + return coordinate; + } + + public void setCoordinate(float[] coordinate) { + this.coordinate = coordinate; + } +} diff --git a/StockMarket.java b/StockMarket.java new file mode 100644 index 0000000..2aff358 --- /dev/null +++ b/StockMarket.java @@ -0,0 +1,63 @@ +package org.example.defualtSystem; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +public class StockMarket{ + private Map users; + + public StockMarket() { + users = new HashMap<>(); + } + + public synchronized void depositCapital(String userName, double amount) { + if (users.containsKey(userName)) { + double currentCapital = users.get(userName); + users.put(userName, currentCapital + amount); + } else { + users.put(userName, amount); + } + } + + public synchronized double getDetail(String userName) { + if (users.containsKey(userName)) { + return users.get(userName); + } else { + return 0.0; + } + } + + public synchronized double withdrawCapital(String userName) { + if (users.containsKey(userName)) { + double amount = users.get(userName); + users.put(userName, 0.0); + return amount; + } else { + return 0.0; + } + } + + public void startMarketSimulation() { + Thread marketThread = new Thread(() -> { + Random random = new Random(); + while (true) { + double change = random.nextDouble() * 0.06 - 0.03; + for (String userName : users.keySet()) { + double capital = users.get(userName); + double newCapital = capital + capital * change; + if (newCapital < 0) { + newCapital = 0; + } + users.put(userName, newCapital); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + marketThread.start(); + } +} diff --git a/User.java b/User.java new file mode 100644 index 0000000..da52b19 --- /dev/null +++ b/User.java @@ -0,0 +1,37 @@ +package org.example.models; + +public class User { + private String username; + private String password; + + public User() + { + + } + public User(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public void sign(String username,String password) + { + setPassword(password); + setUsername(username); + } +} From 051261fa3cfb2994a2b19126751a939daf5628e9 Mon Sep 17 00:00:00 2001 From: Pourya Alizadeh <118482112+ThePral@users.noreply.github.com> Date: Tue, 4 Jul 2023 10:08:14 +0330 Subject: [PATCH 2/3] changes in menu --- City.java | 3 +-- Database.java | 71 ++++++++++++++++++++++++++++++++++----------------- Game.java | 24 ++++++++++++++++- Menu.java | 21 ++++++++++++--- User.java | 4 --- 5 files changed, 89 insertions(+), 34 deletions(-) diff --git a/City.java b/City.java index 30a25ce..7e92611 100644 --- a/City.java +++ b/City.java @@ -54,8 +54,7 @@ private void beginGame(Character character) { while (true) { System.out.println("Show Menu"); switch (scanner.next()) { - case "2"-> System.out.println("Do Something"); - case "3"-> System.out.println("Do Something"); + case "2", "3" -> System.out.println("Do Something"); } } } catch (Exception e) { diff --git a/Database.java b/Database.java index 6037f3f..a9779ed 100644 --- a/Database.java +++ b/Database.java @@ -11,43 +11,55 @@ public class Database { static final String DB_URL = "jdbc:mysql://localhost/lightcity"; // Database credentials - static final String USER = "your_username"; - static final String PASS = "your_password"; + static final String USER = "root"; + static final String PASS = "2324LBJKB@pourya"; private Connection conn; - public Database() { - try { - Class.forName(JDBC_DRIVER); - conn = DriverManager.getConnection(DB_URL, USER, PASS); - System.out.println("Connecting to database..."); - } catch (Exception exp) { - System.out.println("Database Exception : \n" + exp.toString()); - System.exit(0); - } - } +// public Database() { +// try { +// Class.forName(JDBC_DRIVER); +// conn = DriverManager.getConnection(DB_URL, USER, PASS); +// System.out.println("Connecting to database..."); +// } catch (Exception exp) { +// System.out.println("Database Exception : \n" + exp.toString()); +// System.exit(0); +// } +// } // Tables /** * Users */ - private void createTables() { -// query example - String query = "CREATE TABLE IF NOT EXISTS Users (username varchar(255) primary key ,password varchar(255));" + - "CREATE TABLE IF NOT EXISTS ...."; + public void checkUserInfo(String username, String password) { + try { - Statement stmt = conn.createStatement(); - if(stmt.execute(query)){ + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT * FROM user"); + + boolean check = false; + + while (resultSet.next()){ + + if (username.equals(resultSet.getString("username")) && password. equals(resultSet.getString("password"))){ + check = true; + } + + } + + if (check){ - }else - System.out.println("An error accord during operation"); - } catch (SQLException e) { - throw new RuntimeException(e); - } + } + + }catch (Exception e){ + System.out.println(e); + } + } public void loginGame(User user) { @@ -64,6 +76,19 @@ public void loginGame(User user) { } public void registerGame(User user) { + + try { + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); + String sql = "INSERT INTO user (username, password) VALUES (?, ?)"; + + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + + preparedStatement.executeUpdate(); + } catch (Exception e) { + System.out.println(e); + } + } } \ No newline at end of file diff --git a/Game.java b/Game.java index 17457b0..2ed2cb5 100644 --- a/Game.java +++ b/Game.java @@ -6,6 +6,7 @@ import org.example.models.User; import java.util.ArrayList; +import java.util.Scanner; public class Game implements GameInterface { @@ -13,7 +14,17 @@ public class Game implements GameInterface { private City city; @Override public void continueGame(User user) { - + + Scanner sc = new Scanner(System.in); + + String username; + String pass; + + System.out.print("\nEnter your username: "); + username = sc.nextLine(); + + System.out.print("Enter your password: "); + pass = sc.nextLine(); } /** Create new city and Generate new Character @@ -23,6 +34,17 @@ public void continueGame(User user) { public void startGame(User user) { generateNewCity(); city.joinCharacter(user); + + Scanner sc = new Scanner(System.in); + + String username; + String pass; + + System.out.print("\nEnter Username: "); + username = sc.nextLine(); + + System.out.print("Enter Password: "); + pass = sc.nextLine(); } /** diff --git a/Menu.java b/Menu.java index 10c7659..48bf50e 100644 --- a/Menu.java +++ b/Menu.java @@ -5,10 +5,17 @@ import java.util.Scanner; public class Menu { + + private static Game game = new Game(); + private static Database database = new Database(); private static Scanner scanner = new Scanner(System.in); public static void showMenu(){ + mainMenu(); + + System.out.print("Input Number: "); + String next = scanner.next(); if (next.equals("1")) { game.continueGame(loginMenu()); @@ -20,18 +27,24 @@ public static void showMenu(){ System.exit(0); } public static void mainMenu(){ -// show menu : sout () + System.out.println("Please choose one of the options below:\n"); + System.out.println("1. Continue"); + System.out.println("2. Start New Game"); + System.out.println("3. Join Server"); + System.out.println("4. Exit\n"); } public static User loginMenu(){ -// get user info : username, password + + + return null; } private static void joinServer(){ - System.out.print("Enter Server Ip Address :"); + System.out.print("\nEnter Server IP Address: "); String ip = scanner.next(); - System.out.print("Enter Server Port :"); + System.out.print("Enter Server Port: "); int port = scanner.nextInt(); game.joinServer(ip,port); } diff --git a/User.java b/User.java index da52b19..3fc5ef0 100644 --- a/User.java +++ b/User.java @@ -4,10 +4,6 @@ public class User { private String username; private String password; - public User() - { - - } public User(String username, String password) { this.username = username; this.password = password; From bfd2d1ed909ec748144411419a683a2abe531be0 Mon Sep 17 00:00:00 2001 From: Pourya Alizadeh <118482112+ThePral@users.noreply.github.com> Date: Tue, 4 Jul 2023 20:26:48 +0330 Subject: [PATCH 3/3] Add files via upload Creating city, industry, login, and sign in --- Character.java | 2 - Database.java | 321 ++++++++++++++++++++++++++++++++++++++++++++++--- Game.java | 15 +-- Menu.java | 6 +- 4 files changed, 315 insertions(+), 29 deletions(-) diff --git a/Character.java b/Character.java index f2d3a64..2f52af8 100644 --- a/Character.java +++ b/Character.java @@ -9,10 +9,8 @@ public class Character implements CharacterInterface { private User userInfo; private BankAccount account; private Life life; - private Job job; private ArrayList properties; - private Property inPosition; public Character(User userInfo, BankAccount account, Life life, Job job, ArrayList properties, Property inPosition) { diff --git a/Database.java b/Database.java index a9779ed..5cd3293 100644 --- a/Database.java +++ b/Database.java @@ -3,10 +3,15 @@ import org.example.models.User; import javax.xml.crypto.Data; +import java.security.spec.ECField; import java.sql.*; +import java.util.Scanner; + +import java.security.*; public class Database { + int id; static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/lightcity"; @@ -17,16 +22,16 @@ public class Database { private Connection conn; -// public Database() { -// try { -// Class.forName(JDBC_DRIVER); -// conn = DriverManager.getConnection(DB_URL, USER, PASS); -// System.out.println("Connecting to database..."); -// } catch (Exception exp) { -// System.out.println("Database Exception : \n" + exp.toString()); -// System.exit(0); -// } -// } + public Database() { + try { + Class.forName(JDBC_DRIVER); + conn = DriverManager.getConnection(DB_URL, USER, PASS); + System.out.println("\nConnecting to database..."); + } catch (Exception exp) { + System.out.println("Database Exception : \n" + exp.toString()); + System.exit(0); + } + } // Tables /** @@ -36,15 +41,17 @@ public class Database { public void checkUserInfo(String username, String password) { try { - Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lightcity", "root", "2324LBJKB@pourya"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM user"); boolean check = false; + password = encryptPassword(password); + while (resultSet.next()){ - if (username.equals(resultSet.getString("username")) && password. equals(resultSet.getString("password"))){ + if (username.equals(resultSet.getString("username")) && password.equals(resultSet.getString("password"))){ check = true; } @@ -52,8 +59,62 @@ public void checkUserInfo(String username, String password) { if (check){ + System.out.println("\nWelcome!"); + System.out.println("\n1. Enter a city"); + System.out.println("2. Build a new city"); + + System.out.print("\nInput number: "); + + Scanner sc = new Scanner(System.in); + + int num; + num = sc.nextInt(); + + if (num == 1) { + cityList(username, password); + + } else if (num == 2) { + buildCity(username, password); + + } + + + } else { + + System.out.println("\nYou have not registered yet. Would you like to register now?"); + System.out.println("\n1. Yes"); + System.out.println("2. No"); + System.out.print("\nInput number: "); + + Scanner sc = new Scanner(System.in); + + int num; + num = sc.nextInt(); + + if (num == 1) { + Scanner str = new Scanner(System.in); + String userName; + String passWord; + + System.out.print("\nUsername: "); + userName = str.nextLine(); + + System.out.print("Password: "); + passWord = str.nextLine(); + + passWord = encryptPassword(passWord); + + User user = new User(userName, passWord); + + registerGame(user); + + System.out.println("\nYou have successfully registered.\n"); + + } else if (num == 2) { + Menu.showMenu(); + } } }catch (Exception e){ @@ -79,16 +140,248 @@ public void registerGame(User user) { try { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); - String sql = "INSERT INTO user (username, password) VALUES (?, ?)"; + String sql = "INSERT INTO user (id, username, password) VALUES (?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.setInt(1, id); + preparedStatement.setString(2, user.getUsername()); + preparedStatement.setString(3, user.getPassword()); + + preparedStatement.executeUpdate(); + + } catch (Exception e) { + System.out.println(e); + } + } + + public String encryptPassword(String password){ + + String encryptedPassword = null; + + try { + + MessageDigest m = MessageDigest.getInstance("MD5"); + + m.update(password.getBytes()); + + byte[] bytes = m.digest(); + + StringBuilder s = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + s.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + + encryptedPassword = s.toString(); + + } catch (NoSuchAlgorithmException e){ + e.printStackTrace(); + } + + return encryptedPassword; + + } + + public void cityList(String username, String password){ + + try { + + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lightcity", "root", "2324LBJKB@pourya"); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT * FROM city"); + + + + System.out.println("\nList of available cities:"); + + while (resultSet.next()){ + + int cityID; + String manager; + String cityName; + + + cityID = resultSet.getInt("id"); + manager = resultSet.getString("manager"); + cityName = resultSet.getString("name"); + + System.out.println("\nCity name: " + cityName); + System.out.println("Manager: " + manager); + System.out.println("City ID: " + cityID); + + System.out.println("\n------------------------------------------"); + + } + + Scanner sc = new Scanner(System.in); + + System.out.print("\nEnter the ID of the city you want to enter: "); + + int cityID; + cityID = sc.nextInt(); + + cityLogin(cityID); + + } catch (Exception e){ + + System.out.println("\nNo city has been created yet!"); + System.out.println("Do you want to build a new city?"); + + System.out.println("\n1. Yes"); + System.out.println("2. No"); + System.out.print("\nInput number: "); + + Scanner numSC = new Scanner(System.in); + + int num2; + num2 = numSC.nextInt(); + + if (num2 == 1) { + + buildCity(username, password); + + } else if (num2 == 2) { + Menu.showMenu(); + } + + } + + } + + public void buildCity(String username, String password){ + + Scanner sc = new Scanner(System.in); + + System.out.print("\nEnter the name of the city: "); + + String cityName; + cityName = sc.nextLine(); + + + + try { + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); + String sql = "INSERT INTO city (id, manager, name, password, industry, income, product) VALUES (?, ?, ?, ?, ?, ?, ?)"; + + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + preparedStatement.setLong(1, id); + preparedStatement.setString(2, username); + preparedStatement.setString(3, cityName); + preparedStatement.setString(4, password); + preparedStatement.setString(5, null); + preparedStatement.setString(6, null); + preparedStatement.setString(7, null); preparedStatement.executeUpdate(); } catch (Exception e) { System.out.println(e); } + System.out.println("\nCity has been built successfully!"); + + } + + public void cityLogin(int cityID){ + + try { + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lightcity", "root", "2324LBJKB@pourya"); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT * FROM city"); + + while (resultSet.next()){ + + if (cityID == resultSet.getInt("id")){ + cityMenu(resultSet.getString("name"), cityID); + } + } + + } catch (Exception e){ + System.out.println(e); + } + + } + + public void cityMenu(String cityName, int cityID){ + + System.out.println("\nCity name: " + cityName); + + System.out.println("\n1. Go to"); + System.out.println("2. Process Location"); + System.out.println("3. Dashboard"); + System.out.println("4. Build a new industry"); + System.out.println("5. Life"); + System.out.println("6. Exit"); + + System.out.print("Input number: "); + + Scanner sc = new Scanner(System.in); + + int num; + num = sc.nextInt(); + + switch (num){ + + case 1: + break; + + case 2: + break; + + case 3: + break; + + case 4: + buildIndustry(cityName, cityID); + break; + + case 5: + break; + + case 6: + System.exit(0); + break; + + } + } + + public void buildIndustry(String cityName, int cityID){ + + Scanner sc = new Scanner(System.in); + + String title; + String income; + String product; + + System.out.print("\nEnter the title of the industry: "); + title = sc.nextLine(); + + System.out.print("Product: "); + product = sc.nextLine(); + + System.out.print("Income: "); + income = sc.nextLine(); + + try { + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/lightcity", "root", "2324LBJKB@pourya"); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE city set industry = ?, income = ?, product = ? WHERE id = ?"); + + preparedStatement.setString(1, title); + preparedStatement.setString(2, income); + preparedStatement.setString(3, product); + preparedStatement.setInt(4, cityID); + + preparedStatement.executeUpdate(); + + }catch (Exception e){ + System.out.println(e); + } + + System.out.println("\nIndustry has been created successfully!"); + + cityMenu(cityName, cityID); + } -} \ No newline at end of file +} diff --git a/Game.java b/Game.java index 2ed2cb5..df7b098 100644 --- a/Game.java +++ b/Game.java @@ -25,6 +25,10 @@ public void continueGame(User user) { System.out.print("Enter your password: "); pass = sc.nextLine(); + + Database database = new Database(); + + database.checkUserInfo(username, pass); } /** Create new city and Generate new Character @@ -32,19 +36,10 @@ public void continueGame(User user) { * */ @Override public void startGame(User user) { + generateNewCity(); city.joinCharacter(user); - Scanner sc = new Scanner(System.in); - - String username; - String pass; - - System.out.print("\nEnter Username: "); - username = sc.nextLine(); - - System.out.print("Enter Password: "); - pass = sc.nextLine(); } /** diff --git a/Menu.java b/Menu.java index 48bf50e..e8a8ef3 100644 --- a/Menu.java +++ b/Menu.java @@ -10,11 +10,11 @@ public class Menu { private static Game game = new Game(); private static Database database = new Database(); private static Scanner scanner = new Scanner(System.in); - public static void showMenu(){ + public static void showMenu() { mainMenu(); - System.out.print("Input Number: "); + System.out.print("Input number: "); String next = scanner.next(); if (next.equals("1")) { @@ -27,7 +27,7 @@ public static void showMenu(){ System.exit(0); } public static void mainMenu(){ - System.out.println("Please choose one of the options below:\n"); + System.out.println("\nPlease choose one of the options below:\n"); System.out.println("1. Continue"); System.out.println("2. Start New Game"); System.out.println("3. Join Server");