|
Page 1 of 1 |
|
Posted: Wed, 25th May 2011 13:40 Post subject: Java: Swing banking account |
|
 |
Heya.
I have taken on a project for myself and I've made a banking account in swing which lets me withdraw a certain amount of money ( without going below 0 ) and deposit whichever amount I see fit.
However, I want to improve my program a little bit, I have made a function that allows me to see the current amount of money on the bank account, and now I want the program to write an external file with a value ( the amount of money on the account ) that I will load whenever I want to launch the program.
Is there an easy way to do this?
Spoiler: | import javax.swing.*;
public class Bank
{
public static void main(String[] arg)
{
boolean fortsätt = true;
double uttag = 0, investering = 1;
double saldo = 11000;
String in;
int val;
do
{
in = JOptionPane.showInputDialog("Välkommen till BRKBank \n" +
"1. Uttag\n" +
"2. Insättning\n" +
"3. Saldo\n" +
"4. Avsluta\n" +
"Vad vill du göra?");
val = Integer.parseInt(in);
switch(val)
{
case 1:
in = JOptionPane.showInputDialog("Hur mycket pengar vill du ta ut?");
uttag = Double.parseDouble(in);
if (saldo <= uttag ) {
JOptionPane.showMessageDialog(null, "Du får inte ta ut så här mycket pengar");
break;}
saldo = saldo-uttag;
break;
case 2:
in = JOptionPane.showInputDialog("Ange längd");
investering = Double.parseDouble(in);
saldo = saldo+investering;
break;
case 3:
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
break;
case 4:
fortsätt = false;
break;
default:
JOptionPane.showMessageDialog(null, "Försök igen.");
}
if(val == 1 || val == 2 )
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
}while(fortsätt);
System.exit(0);
}
}
|
|
|
Back to top |
|
 |
garus
VIP Member
Posts: 34200
|
Posted: Wed, 25th May 2011 13:55 Post subject: |
|
 |
snip
Last edited by garus on Tue, 27th Aug 2024 21:54; edited 1 time in total
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
garus
VIP Member
Posts: 34200
|
Posted: Wed, 25th May 2011 14:19 Post subject: |
|
 |
snip
Last edited by garus on Tue, 27th Aug 2024 21:54; edited 1 time in total
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
|
Posted: Fri, 27th May 2011 02:12 Post subject: |
|
 |
I got most of it working right now, I can write a textfile by executing the program and all but I cant seem to find a way to grab the value of "saldo" and put it into the textfile, because to get it all to work I have to throw away the exceptions :s
Code: | import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class BRKBank
{
public static void main(String args[]){
boolean fortsätt = true;
double pengar = 1;
double saldo = 11000;
String in;
int val;
do
{
in = JOptionPane.showInputDialog("Välkommen till BRKBank \n" +
"1. Uttag\n" +
"2. Insättning\n" +
"3. Saldo\n" +
"4. Avsluta\n" +
"Vad vill du göra?");
val = Integer.parseInt(in);
switch(val)
{
case 1:
in = JOptionPane.showInputDialog("Hur mycket pengar vill du ta ut?");
pengar = Double.parseDouble(in);
FileInputStream fis = new FileInputStream("saldo.txt");
DataInputStream dis = new DataInputStream(fis);
saldo = dis.readInt();
dis.close();
if (saldo <= pengar ) {
JOptionPane.showMessageDialog(null, "Du får inte ta ut så här mycket pengar");
break;}
saldo = saldo-pengar;
break;
case 2:
in = JOptionPane.showInputDialog("Hur mycket pengar vill du sätta in?");
pengar = Double.parseDouble(in);
saldo = saldo+pengar;
break;
case 3:
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
break;
case 4:
fortsätt = false;
break;
default:
JOptionPane.showMessageDialog(null, "Försök igen.");
}
if(val == 1 || val == 2 )
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
}while(fortsätt);
FileOutputStream fos = new FileOutputStream("saldo.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeDouble(saldo);
dos.close();
System.exit(0);
}
}
|
Halp, pretty plx.
|
|
Back to top |
|
 |
|
Posted: Sun, 29th May 2011 18:26 Post subject: |
|
 |
Bufferedreader/writer is apparently very much needed for this kind of program, since the File***put reader can only apparently type down bytes.
|
|
Back to top |
|
 |
|
Posted: Mon, 30th May 2011 13:24 Post subject: |
|
 |
Code: | import javax.swing.*;
import java.io.*;
public class BRKBank
{
public static void main(String args[]) throws java.io.IOException
{
{
}
boolean fortsätt = true;
double pengar = 1;
double saldo = 11000;
String in;
String saldoString;
int val;
saldoString = Double.toString(saldo);
do
{
in = JOptionPane.showInputDialog("Välkommen till BRKBank \n" +
"1. Uttag\n" +
"2. Insättning\n" +
"3. Saldo\n" +
"4. Avsluta\n" +
"Vad vill du göra?");
val = Integer.parseInt(in);
switch(val)
{
case 1:
in = JOptionPane.showInputDialog("Hur mycket pengar vill du ta ut?");
pengar = Double.parseDouble(in);
try {
File file = new File("saldo.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while(( saldoString = br.readLine()) != null)
{
saldo = String.parseDouble(in);
}
} catch(IOException e) {
System.out.println("bad !");
}
if (saldo <= pengar ) {
JOptionPane.showMessageDialog(null, "Du får inte ta ut så här mycket pengar");
break;}
saldo = saldo - pengar;
break;
case 2:
in = JOptionPane.showInputDialog("Hur mycket pengar vill du sätta in?");
pengar = Double.parseDouble(in);
saldo = saldo + pengar;
break;
case 3:
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
break;
case 4:
fortsätt = false;
break;
default:
JOptionPane.showMessageDialog(null, "Försök igen.");
}
if(val == 1 || val == 2 )
JOptionPane.showMessageDialog(null, "Du har nu: " + saldo + " kr på ditt konto");
}while(fortsätt);
saldoString = Double.toString(saldo);
File file =new File("saldo.txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getName());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(saldoString);
bw.close();
System.exit(0);
}
} |
I have managed to make the program write out the exact amount of money that was in the last execution of the program, but I am unable to make the program read the .txt in a proper fashion, hoping that someone can help me with it, I refuse to give up!
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|