안드 입문한지 아직 일주일도 안됐거든...
지금 중간과제 같은걸로 은행 입출금 어플 간단한거 만들고있는데
좀 막히는 부분이 생김... 소스 별로 안길고 잘못된 부분 강조해놨어 좀 분석좀 부탁해 ㅠ
일단 만들면 이렇게 나옴
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
package com.example.omi;
import android.widget.EditText;
public class Account {
public EditText name;
int money = 0;
public Account(EditText name) {
this.name = name;
}
public void input(int a) {
money += a;
}
public void output(int b) {
money -= b;
}
public int jan() {
return money;
}
public EditText nameGet() {
return name;
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
package com.example.omi;
import java.util.Iterator;
import java.util.Vector;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
TextView result; // 신경 안써도됨
EditText edName, edDeposit, edWithdraw, txtConfirm; // 순서대로 계좌이름, 계좌에 돈넣기, 돈빼기, 잔액확인
Button create, deposit, withdraw, confirm; // 버튼 ㅇㅇ
Vector<Account> vec = new Vector<Account>();
int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
i = 0;
result = (TextView)findViewById(R.id.result);
edName = (EditText)findViewById(R.id.edName);
edDeposit = (EditText)findViewById(R.id.edDeposit);
edWithdraw = (EditText)findViewById(R.id.edWithdraw);
txtConfirm = (EditText)findViewById(R.id.txtConfirm);
create = (Button)findViewById(R.id.create);
deposit = (Button)findViewById(R.id.deposit);
withdraw = (Button)findViewById(R.id.withdraw);
confirm = (Button)findViewById(R.id.confirm);
create.setOnClickListener(this);
deposit.setOnClickListener(this);
withdraw.setOnClickListener(this);
confirm.setOnClickListener(this); // 여기까지는 그냥 xml에 있는 변수들 불러온거
}
public void onClick(View v) { // 이벤트 핸들러
Account ac = null; // 벡터의 각 요소를 참조하기 위한 참조변수 선언
if(v.getId() == R.id.create)
vec.add(new Account(edName)); // edName이 계좌 주인이름임. 이걸로 계좌를 찾음
else if(v.getId() == R.id.deposit) {
Iterator<Account> itr = vec.iterator();
int jjj = 0;
while(itr.hasNext()) {
ac = itr.next();
if(ac.nameGet().equals(R.id.edName)) // 각if문에서 뭔가오류가생김 아마 equals를 썻지만 같다고 인식을 안한거같음
ac.input(Integer.parseInt(edDeposit.getText().toString()));
}
}
else if(v.getId() == R.id.withdraw) {
Iterator<Account> itr1 = vec.iterator();
while(itr1.hasNext()) {
ac = itr1.next();
if(ac.nameGet().equals(R.id.edName))
ac.output(Integer.parseInt(edWithdraw.getText().toString()));
}
}
else if(v.getId() == R.id.confirm) {
Iterator<Account> itr2 = vec.iterator();
while(itr2.hasNext()) {
ac = itr2.next();
if(ac.nameGet().equals(R.id.edName))
confirm.setText(Integer.toString(ac.jan()));
}
}
}
}
분홍색 음영 씌어놓은거 보이지??
아마 거기서 뭔가 제대로 잘못읽는거 같음.
if문이 작동을 안해..ㅠㅠ
내가 생각한건 참조변수 ac를 이용해서 itr.next()로 벡터의 요소를 참조하고 거기에다가 값을 집어넣는거임.
소스길이도 별로 안길어서 딱히 뭐 메소드 여러개 안만들고 그냥 이벤트 메소드 하나에 우겨넣어놨거든
이게 문제가 되는걸까?
입금을 나타낼때 input이 아니라 deposit이라고 메소드 이름을 붙인다. 인출할때는 output이 아니라 withdraw라고 이름 붙인다. 잔액을 조회하는 메소드는 jan이 아니라 getBalance라고 이름 붙인다.
봐드렸습니다 • 엃
if문 안이 맞는건지 모르겠음... 자바할땐 저런식으로 했는데 안드로이드 오니까 작동을안해 ㅠㅠ
아 해결법 찾았다. 되도록이면 책 참고 안하려고 햇는데 ㅠㅠ
R.id.... 이 EditText값을 미리 String값으로 변환시킨후 비교를 해야하더라. 아마 저값 그대로 쓰면 equals로 비교를 못하나봐 ㅠ
뷰가 너무 많은걸 요구한다. 계좌이체 프로세스는 계좌가 존재한다는 전제로 성립되는데 이걸 가정하지 않다보니 \'계좌생성\'이라는 버튼이 들어가고 이로인해 계좌를 생성하는 프로세스를 위 화면에 우겨넣어야 함. 그러니 비즈니스 로직은 더 복잡해지고 가뜩이나 코딩도 익숙하지 않은데 원하는대로 안되면 어디서부터 손댈지 막막해지지.
쿠리쿠리쿠리// 미리 Account 클래스에서 EditView로 생성자에 받아들이는게 아니라 미리 메인 클래스에서 String으로 변환시킨후 매개변수를 전달해야한거임 ㅠㅠ
String name = edName.getText().toString(); 이런식으로
저 name을 Account 인스턴스에 전달하고 비교를 했어야했어. EditText로는 equals 메소드로 비교가 안되나봄
뷰는 그냥 책에있는거 그대로 따라한거야. 소스도 그대로 있긴한데 그거만 따라치면 싱거워서 내가 새로 소스를 짠건데 아무래도 자바랑 다른부분이 이건거같네.. 난 그냥 equals 메소드는 마음대로 썼는데 이건 타입까지 생각해야해 ㅠ
로직 자체에는 문제가 없었는데 안드로이드 타입이 익숙하지 않아서 벌어진 문제인듯
그게 나는 EditText 타입이 equals 메소드로 비교가 안되는줄 몰라서그랬음 ㅠㅠ 어차피 똑같은 변수를 참조할텐데 왜 안되는거지..
edittext.getText().toString();
그리구 굳이 벡터 타입으로 저장하는 이유는? 그냥 account 클래스만들어서 getter, setter만 사용하믄 될거같은데. 자료구조 먼저 공부 고고