티스토리 뷰

IT/Java

[Java] class, ArrayList 연습

DongChul 2014. 5. 23. 14:40

이전에 포스팅한 creditcard4 클래스를 참고하였습니다.

ArrayList의 간략한 메소드들은 아래 포스팅을 참고하시길 바랍니다.

http://slowlywalk1993.tistory.com/entry/Java-ArrayList%EB%9E%80



package ks.example.creditcard;


import java.util.ArrayList;

import java.util.Scanner;


public class CreditCardtest2 {

public static void main(String[] args){

ArrayList<CreditCard4> CardList = new ArrayList<CreditCard4>(); //ArrayList객체생성

boolean isStop = false; //while루프가 돌기위해 선언

Scanner scan = new Scanner(System.in);

do{

System.out.println("다음 메뉴중 하나를 선택하세요.");

System.out.println("1.카드신청");

System.out.println("2.카드해지");

System.out.println("3.정보수정");

System.out.println("4.카드조회");

System.out.println("5.결재요청");

System.out.println("6.종      료");

String command = scan.next();

switch (command) {

case "1" : 

{

System.out.println("<카드신청>");

System.out.print("card number : ");

long number = scan.nextLong();

System.out.print("name : ");

String owner = scan.next();

CardList.add(new CreditCard4(owner,number)); //ArrayList에 객체추가

} break;

case "2" : 

{

System.out.println("<카드해지>");

System.out.print("card number : ");

long number = scan.nextLong();

for(CreditCard4 c : CardList){ // for - each 문사용

if(c.getNumber() == number) CardList.remove(c); // ArrayList에저장된 객체 삭제

break;

}

} break;

case "3" : 

{

System.out.println("<정보수정>");

System.out.print("card number : ");

long number = scan.nextLong();

for(CreditCard4 c : CardList){

if(c.getNumber() == number){

System.out.println("name : " + c.getOwner() + " =>");

c.setOwner(scan.next());

break;

}

}

} break;

case "4" : 

{

System.out.println("<카드조회>");

System.out.print("card number(all = 0) : ");

long number = scan.nextLong();

if(number == 0) {

for(CreditCard4 c : CardList){

System.out.println(c.getNumber() + " / " +c.getOwner() + " / " + c.getPoint() + "(포인트)" );

}

}

else

for(CreditCard4 c : CardList){

if(c.getNumber() == number)

System.out.println(c.getNumber() + " / " +c.getOwner() + " / " + c.getPoint() + "(포인트)" );

break;

}

} break;

case "5" : 

{

System.out.println("<카드결재>");

System.out.print("card number : ");

long number = scan.nextLong();

System.out.print("결재 금액 :");

int amount = scan.nextInt();

for(CreditCard4 c : CardList){

if(number == c.getNumber()){

c.payBill(amount);

break;

}

}

} break;

case "6" : 

{

System.out.println("프로그램을 종료합니다 .");

isStop = true;

} break;

default : System.out.print("잘못된 입력입니다.");

}

}while(!isStop);

scan.close();

}

}


실행결과는 아래와 같습니다.





'IT > Java' 카테고리의 다른 글

[Java] 한글 인코딩테스트 한번에 하기  (0) 2015.07.16
[Java] URL 한글인식 , 한글 인코딩  (0) 2014.11.14
[Java] 상속예제3  (0) 2014.05.20
[Java] Calendar클래스를 사용한 달력예제  (0) 2014.05.20
[Java] 상속예제2  (0) 2014.05.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함