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();

}

}


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