/* * Karp.java * * Created on neljapäev, 4. Oktoober 2007. a, 12:16 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package hoiukassa; import java.io.*; /** * * @author haldur */ public class Karp { private String failinimi; /** Creates a new instance of Karp */ public Karp(String failinimi) { this.failinimi=failinimi; } public int kysiSumma() throws IOException{ BufferedReader failist=new BufferedReader( new FileReader(failinimi)); int vastus=Integer.parseInt(failist.readLine()); failist.close(); return vastus; } private void kirjutaSumma(int summa) throws IOException{ FileWriter faili=new FileWriter(failinimi); faili.write(String.valueOf(summa)); faili.close(); } public void lisaSumma(int summa) throws IOException{ if(summa<0){ throw new ArithmeticException("Negatiivne summa"); } kirjutaSumma(kysiSumma()+summa); } public void eemaldaSumma(int summa) throws IOException, KarbiException{ if(kysiSumma()-summa<0){ throw new KarbiException("Karbis pole piisavalt raha"); } kirjutaSumma(kysiSumma()-summa); } }