2011년 5월 20일 금요일

WebView Demo Code

package com.google.android.web;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class WebActivity extends Activity {
WebView web ; Button yahoo , msn ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.web) ;
web.setWebViewClient(new WebViewClient()) ;
web.loadUrl("http://www.yahoo.com");
yahoo = (Button) findViewById(R.id.yahoo);
msn = (Button) findViewById(R.id.msn) ;
yahoo.setOnClickListener(listener);
msn.setOnClickListener(listener) ;
}
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.yahoo : web.loadUrl("http://www.yahoo.com");
break ;
case R.id.msn : web.loadUrl("http://www.msn.com") ;
break ;
}
}
} ;
}

2011년 4월 15일 금요일

콜렉션과 파일입출력

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Person implements Serializable {
String name ;
int ssn ;
String phone ;

public Person(String name, int ssn, String phone) {
super();
this.name = name;
this.ssn = ssn;
this.phone = phone;
}
public String toString() {
return name + "/" + ssn + "/" + phone ;
}

public static void writePersons(List list , String file) {
Iterator iter = list.iterator() ;
ObjectOutputStream out = null ;
try {
out = new ObjectOutputStream(new FileOutputStream(file)) ;
while(iter.hasNext()) {
Person p = iter.next() ;
out.writeObject(p);
}
System.out.println("Data were written") ;
} catch (IOException e) {
e.printStackTrace();
} finally {
try { out.close() ; } catch(Exception e) {}
}
}
public static List readPersons(String file) {
ArrayList list = new ArrayList() ;
ObjectInputStream in = null ;
try {
in = new ObjectInputStream(new FileInputStream(file)) ;
while(true) list.add((Person) in.readObject()) ;
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} finally { try { in.close(); } catch(Exception e) {} }
return list;
}
public static void printPersons(List list) {
System.out.println("######### Persons ##########") ;
for(Person p : list) {
System.out.println(p);
}
System.out.println("########End of Persons ##########") ;
}

public static void main(String[] args) {
List persons = new ArrayList() ;
BufferedInputStream in = new BufferedInputStream(System.in) ;
boolean isRunning = true ;
while(isRunning) {
System.out.println("1.데이터추가 2.저장 3.읽어오기 4.나가") ;
try {
char data = (char) in.read() ;
switch(data) {
case '1': Person p = getPerson();
if(p != null) persons.add(p) ; break ;
case '2': writePersons(persons , "person.data") ; break;
case '3': persons = readPersons("person.data") ; printPersons(persons) ; break ;
case '4': isRunning = false ; break;
default: System.out.println("Please Select menu!") ; break ;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try { in.close(); } catch (IOException e) { }
}
public static Person getPerson() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) ;
String name , phone ; int ssn ;
Person p = null ;
try {
System.out.print("Name:") ; name = reader.readLine() ;
System.out.print("Phone:"); phone = reader.readLine() ;
System.out.print("SSN:"); ssn = Integer.parseInt(reader.readLine()) ;
p = new Person(name , ssn , phone) ;
} catch (IOException e) { }
return p ;
}
}

2011년 4월 8일 금요일

자바 프로그래밍 과제

1. 엑셀로 주소록을 csv로 만든 후 이것을 읽어들여 콘솔에 출력하는 코드를 작성하시오.
2. 직원정보를 담고 있는 Employee 클래스를 작성하고 이 클래스의 객체를
콘솔로부터 입력받아 생성한 뒤 피일로 출력하고 읽는 코드를 작성하시오. 이때 ObjectStream을
사용해서 할것

객체 입출력

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Person implements Serializable {
String name ;
int ssn ;
String phone ;

public Person(String name, int ssn, String phone) {
super();
this.name = name;
this.ssn = ssn;
this.phone = phone;
}
public String toString() {
return name + "/" + ssn + "/" + phone ;
}
public static void main(String[] args) {
Person p = new Person("Kathy" , 22334 , "010-234-5674") ;
ObjectOutputStream out = null ;
try {
out = new ObjectOutputStream(new FileOutputStream("person.data")) ;
out.writeObject(p);
System.out.println("Person:" + p + " was written") ;
} catch (IOException e) {
e.printStackTrace();
} finally {
try { out.close() ; } catch(Exception e) {}
}
Person p2 = null ;
ObjectInputStream in = null ;
try {
in = new ObjectInputStream(new FileInputStream("person.data")) ;
p2 = (Person) in.readObject() ;
System.out.println("Person:" + p2 + " was read") ;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally { try { in.close(); } catch(Exception e) {} }
}
}

Java 수업코드 입출력 1

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class BufferedIODemo {
public static void main(String[] args) {
FileWriter writer = null ;
try {
writer = new FileWriter("mydata.txt");
BufferedWriter bw = new BufferedWriter(writer) ;
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in)) ;
boolean bwrite = true ;
while(bwrite) {
System.out.print("Please Type Message:") ;
String line = reader.readLine() ;
if(line.equals("exit")) {
bwrite = false ;
continue ;
}
bw.write(line + "\n") ;
bw.flush() ;
}
System.out.println("File Written");
} catch (IOException e) {
e.printStackTrace();
} finally {
try { writer.close(); } catch(Exception e) { }
}
BufferedReader br = null ;
try {
br = new BufferedReader(new FileReader("mydata.txt"));
StringBuffer buffer = new StringBuffer() ;
String line = null ;
while((line = br.readLine()) != null) {
buffer.append(line + "\n") ;
}
System.out.println(buffer.toString()) ;
} catch (IOException e) {
} finally {
try { br.close() ;} catch (Exception e) { e.printStackTrace(); }
}
}
}

2011년 4월 1일 금요일

인터페이스 이해를 위한 예제코드

public class Account implements Transferable{
String id ;
String name ;
int balance ;
static long grandTotal = 0L ;
Account() { }
Account(String id , String name , int balance) {
this.id = id ; this.name = name ; this.balance = balance ;
grandTotal += balance ;
}
Account(String id , String name) {
this(id,name,0);
}
final public void deposit(int value) {
balance += value ; grandTotal += value ;
}
public int withdraw(int value) throws Exception {
if( value > balance) throw new Exception("잔액없음") ;
balance -= value ; grandTotal -= value;
return balance ;
}
public String toString() {
return "id:" + id + ",name:" + name + ",balance:" + balance;
}
public static void main(String[] args) {
Account acc = new Account("111-222-333" , "홍길동" , 500000) ;
Account acc2 = new Account("1234" , "오진우") ; acc2.deposit(8500);
System.out.println(acc2) ;
System.out.println(acc) ;
acc.deposit(50000) ;
try {
acc.withdraw(4500) ;
} catch (Exception e) {
System.out.println(e.getMessage()) ;
}
System.out.println(acc) ;
System.out.println("Grand Total:" + Account.grandTotal) ;
}

@Override
public int calculate(int amount) {
return balance ;
}
@Override
public void transferIn(int value) {
deposit(value) ;
}
@Override
public void transferOut(int value) throws Exception {
withdraw(value) ;
}

}

public class Fund implements Transferable{
private String name ;
private int price ;
private int balance ;
private int cash = 0 ;
public Fund(String name , int price , int balance) {
this.name = name ;
this.price = price ;
this.balance = balance ;
}
public int buy(int amount) {
balance += amount ;
return price * amount ;
}
public int sell(int amount) throws Exception {
if( amount > balance) throw new Exception("보유주식 부족") ;
balance -= amount ;
return amount * price ;
}
public String toString() {
return name + ":" + price + ":" + balance + ":" + (price*balance) ;
}
@Override
public int calculate(int amount) {
return price * amount ;
}
@Override
public void transferIn(int value) throws Exception {
int buyamount = (value+ cash) / price ;
if( buyamount < 0 ) throw new Exception("1주도 살수 없음") ;
cash = (value+cash) % price ;
int total = buy(buyamount) ;
System.out.print("주식 현재 금액:" + total + "보유") ;
}
@Override
public void transferOut(int value) throws Exception {
if( value > getGrandTotal()) throw new Exception("주식가격 미달") ;
int amount = value / price ;
if( value % price != 0 ) amount ++ ;
int total = sell(amount) ;
cash += total - value ;
}
private int getGrandTotal() {
return price * balance + cash ;
}
}

public interface Transferable {
void transferIn(int value) throws Exception ;
void transferOut(int value) throws Exception ;
int calculate(int amount) ;
}

public class ATMMachine {
public static void main(String[] args) {
Fund fund = new Fund("11-22" , 50000 , 1000) ;
Account acc = new Account("111-333" , "KIM" , 45000000);
System.out.println(fund);
System.out.println(acc) ;
try {
transfer(fund , acc , 500000) ;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(fund);
System.out.println(acc) ;
}

static void transfer(Transferable out , Transferable in , int amount)
throws Exception {
out.transferOut(amount) ;
in.transferIn(amount);
}

}