간단한 가상 증권 거래소 코드(JAVA)

LeeHyunSu·2022년 9월 21일

java

목록 보기
1/2

👉 코드

Transation.java

package transation;

public class Transation {
	private String name;

	public Transation(String name) {
		this.name = name;
	}

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

	@Override
	public String toString() {
		return "[거래 = "+ name + "]";
	}
	
}

Coins.java

package transation;

public class Coins extends Transation {
	private String address;		
	private long buy;
	private long price;	
	private int number;					
	
	public Coins(String name, String address, long buy, long price, int number) {
		super(name);
		this.address = address;
		this.buy = buy;
		this.price = price;
		this.number = number;
	}
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public long getBuy() {
		return buy;
	}
	public void setBuy(long buy) {
		this.buy = buy;
	}
	public long getPrice() {
		return price;
	}
	public void setPrice(long price) {
		this.price = price;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	
	public long getCoin() {
		return buy / price;
	}	

	@Override
	public String toString() {
		return super.toString() + "주소: " + getAddress() + ", " + "보유량: " + getCoin() + "개" ;
	}
	
}

stocks.java

package transation;

public class stocks extends Transation {
	private String owner;		
	private long Amount;
	private long StockPrice;
	private int num;				
	
	public stocks(String name, String owner, long Amount, long StockPrice, int num) {
		super(name);
		this.owner = owner;
		this.Amount = Amount;
		this.StockPrice = StockPrice; 
		this.num = num;
	}
	
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public long getAmount() {
		return Amount;
	}
	public void setAmount(long Amount) {
		this.Amount = Amount;
	}
	public long getStockPrice() {
		return StockPrice;
	}
	public void setStockPrice(long stockPrice) {
		StockPrice = stockPrice;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	

	public long getStock() {
		return  (Amount / StockPrice);
	}
	
	public double getTax() {
		return (Amount * 0.3 / 100);
	}

	@Override
	public String toString() {
		return super.toString() + "자산가 성함: " + getOwner() + ", 보유량: " + getStock() + "개" + ", " + "수수료: " + getTax() + "원 (주식은 세금 0.3%를 지불합니다.)";
		
	}
			
}

Test.java

package transation;
import java.util.*;

public class Test {

	public static void main(String[] args) {
		ArrayList<Transation> pList = new ArrayList<Transation>();
		Scanner sc = new Scanner(System.in);
		boolean isStop = false;
		String menu, name;
		
		do
		{
			System.out.println(" *** Java 거래소 *** ");
			System.out.println(" 메뉴번호 입력: ");
			System.out.println(" 1.거래 시작 ");
			System.out.println(" 2.거래 수정 ");
			System.out.println(" 3.자산 팔기 ");
			System.out.println(" 4.자산 검색 ");
			System.out.println(" 9.프로그램 종료 ");
			menu = sc.next();
			switch(menu)
			{
			case "1":
			{
				System.out.println(" *** Buy *** ");
				System.out.println(" 1.코인 2.주식"); int type = sc.nextInt();
				System.out.print("무엇을 매수하실건가요? : "); name = sc.next(); 
				
				switch(type) {
				case 1:
				{
					System.out.print("코인 주소(16진수): "); String address = sc.next();
					System.out.print("투자할 금액: "); long buy = sc.nextLong();
					System.out.print("현재 " + name + " 코인 가격: "); long price = sc.nextLong();
					System.out.print("자동매수 동의하시면 1번을 눌러주세요: "); int number = sc.nextInt();
					
					pList.add(new Coins(name, address, buy, price, number));
				} break;
				case 2:
				{
					System.out.print("당신의 성함: "); String owner = sc.next();
					System.out.print(name + "에 투자할 금액을 입력해주세요: "); long Amount = sc.nextLong();
					System.out.print("현재 " + name + "가격: "); long Stockprice = sc.nextLong();
					System.out.print("자동매수 동의하시면 1번을 눌러주세요: "); int num = sc.nextInt();
					
					pList.add(new stocks(name, owner, Amount, Stockprice, num));
				} break;
				}
				System.out.println("자동매수 되었습니다.");
			} break;
			case "2":
			{
				System.out.println(" *** 거래 수정 *** ");
				System.out.print("1.코인거래소로 이동 2.주식거래소로 이동: "); int type = sc.nextInt();
				
				switch(type) {
				case 1:
				{
					System.out.print("수정할 코인이름: "); name = sc.next();
					for(Transation p:pList) {
						if(!(p instanceof Coins)) continue;
						Coins c = (Coins)p;
						if(name.equals(p.getName())) {
							System.out.print("주소 변경: " + c.getAddress() + "=> ");
							c.setAddress(sc.next());
							
							System.out.print("투자할 금액: " + c.getBuy() + "=> ");
							c.setBuy(sc.nextLong());
							
							System.out.print(p.getName() + " 현재가격: " + c.getPrice() + "=> ");
							c.setPrice(sc.nextLong());
					
							System.out.print("자동매수 동의하시면 1번을 눌러주세요: ");
							c.setNumber(sc.nextInt());
							System.out.println("자동매수 되었습니다.");
						}
					}
				} break;
				case 2:
				{
					System.out.print("수정할 주식이름: "); name = sc.next();
					for(Transation p:pList) {
						if(!(p instanceof stocks)) continue;
						stocks s = (stocks)p;
						if(name.equals(p.getName())) {
							System.out.print("성함 변경: " + s.getOwner() + "=> ");
							s.setOwner(sc.next());
							
							System.out.print("수정할 투자 금액: " + s.getAmount() + "=> ");
							s.setAmount(sc.nextLong());
							
							System.out.print(p.getName() + " 현재가격: " + s.getStockPrice() + "=> ");
							s.setStockPrice(sc.nextLong());
							
							System.out.print("자동매수 동의하시면 1번을 눌러주세요: ");
							s.setNum(sc.nextInt());
							System.out.println("자동매수 되었습니다.");
						}
					}
					} break;
				}
			} break;
			case "3":
			{
				System.out.println(" *** Sell ***");
				System.out.print("판매할 자산: "); name = sc.next();
				int result = 1;
				
				for(Transation p:pList) {
					if (name.equals(p.getName()))
					{
						pList.remove(p);
						System.out.println(name + "이(가) 모두 판매되었습니다.");
						result = 0;
						break;
					}
				}
			} break;
			case "4":
			{
				System.out.println(" *** 자산 검색 *** ");
				for (Transation p:pList) {
					System.out.println(p);
				}
				System.out.println("--------------------------");
 			} break;
			case "9":
			{
				System.out.println("종료합니다.");
				isStop=true;
			} break;
			}
		} while (!isStop);
	}
}

👉 실행결과

profile
데이터 엔지니어

0개의 댓글