大家好我是Dale,我还是java的新手。我有需要完成的项目,但我的代码有问题。
描述:我想创建一个java程序,要求用户从菜单中进行选择。 1.对于E-load 2.用于搜索加载记录和4.传输和删除。 3应该与E-load一起创建一个记录菜单。换句话说,在更新时创建。在1中,我想要求用户从3个网络服务提供商中进行选择,然后输入他想要最后要求付款的金额负载。无论用户选择的网络服务提供商是什么,用户输入的金额 - 负载都应该存储在文本文件中。例如,如果他选择Globe telecom,它将存储到salesGlobe.ex文本文件内部
环球/ 100
智能/ 0
太阳/ 0
总销售额/ 100
接下来,如果你做了另一个交易,并从智能50加载了一个金额,它应该像这个Globe / 100下一行Smart / 50下一行Sun / 0下一行总销售额/ 150
1.电子负载
2.搜索记录
3.删除并转移
1
输入您选择的网络:0
“你选择了全球电信”
输入11手机号码:091787898787
输入装载量:100
“负载100将加载到您的号码091787898787”
输入付款:101.0
你的变化是1.0
conitue? ñ
还请帮我介绍如何在整个系统处于JOptionPane
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
public class CRUD { //---------class opening-------------//
String strInputMsg="", strInputNum, strInputNetwork, strInputAge, strNumber = " ", strLoad = " ";
double change, amount, load;
int intInputMsg, choice;
int intExist=0;
String network[] = {"Globe", "Smart", "Sun"};
public static void main(String[] args) throws IOException
{
CRUD method = new CRUD();
char ans;
method.menuRecord();
}//-----------close main-------------------//
//-------------------------user define method-------------------------------------------//
public void menuRecord() throws IOException{
strInputMsg = JOptionPane.showInputDialog(null, "1-add"
+"\n2-search"
+"\n3-update"
+"\n4-delete"
+"\n5-exit");
intInputMsg = Integer.parseInt(strInputMsg);
switch(intInputMsg){
case 1: this.addRecord(); break; //----accessing attributes------//
case 2: this.searchRecord(); break;
case 3: this.updateRecord(); break;
case 4: this.deleteRecord(); break;
case 5: JOptionPane.showMessageDialog(null, "exit"); break;
default: JOptionPane.showMessageDialog(null, "out of the list");
}
}//close menu
//---------------------------------------------add record--------------------------------//
//no returning value
//===========================================CREATE==============================================
public void addRecord() throws IOException{
FileWriter fwriter = new FileWriter ("D:\\emprec.txt", true);
BufferedWriter outFile = new BufferedWriter(fwriter);
File orig_outfile = new File("D:\\emprec.txt");
BufferedReader infile = new BufferedReader (new FileReader(orig_outfile));
String line=null;
choice = Integer.parseInt(JOptionPane.showInputDialog("Input choice"));
switch(choice)
{
case 0:
JOptionPane.showMessageDialog(null, "You chose " + network[0]);
strInputNum = JOptionPane.showInputDialog("Enter the 11 - digit number");
load = Double.parseDouble(JOptionPane.showInputDialog("Input the load you want"));
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " will be loaded to your mobile number " + strInputNum);
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
while(true)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
if(amount < load)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
}
else
{
change = amount - load;
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " has been loaded to your mobile number " + strInputNum + "\nYour change is " + String.format("%.2f", change) );
break;
}
}
break;
case 1:
JOptionPane.showMessageDialog(null, "You chose " + network[1]);
strInputNum = JOptionPane.showInputDialog("Enter the 11 - digit number");
load = Double.parseDouble(JOptionPane.showInputDialog("Input the load you want"));
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " will be loaded to your mobile number " + strInputNum);
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
while(true)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
if(amount < load)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
}
else
{
change = amount - load;
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " has been loaded to your mobile number " + strInputNum + "\nYour change is " + String.format("%.2f", change) );
break;
}
} break;
case 2:
JOptionPane.showMessageDialog(null, "You chose " + network[2]);
strInputNum = JOptionPane.showInputDialog("Enter the 11 - digit number");
load = Double.parseDouble(JOptionPane.showInputDialog("Input the load you want"));
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " will be loaded to your mobile number " + strInputNum);
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
while(true)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
if(amount < load)
{
JOptionPane.showMessageDialog(null, "Insufficient amount");
amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for payment"));
}
else
{
change = amount - load;
JOptionPane.showMessageDialog(null, "The amount " + String.format("%.2f", load) + " has been loaded to your mobile number " + strInputNum + "\nYour change is " + String.format("%.2f", change) );
break;
}
}
break;
}
outFile.append(network[choice] +"/" +strInputNum +"/" + load);
outFile.newLine();
outFile.close();
} //---------------close add method-------------------//
//===========================================SEARCH===========================================
public void searchRecord() throws IOException
{
choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Search mobile network: "));
File orig_outfile = new File("D:\\emprec.txt");
BufferedReader infile = new BufferedReader (new FileReader(orig_outfile));
String line = null;
while((line=infile.readLine())!=null)
{
String strNetworkinfile[] = line.split("/");
if(strNetworkinfile[0].matches(network[choice])){
JOptionPane.showMessageDialog(null, strNetworkinfile[1] + " " + strNetworkinfile[2]);
intExist = 1;
infile.close();
break;
}//close if
else {
intExist=0;
}//close else
}//close while
if (intExist==0)
{
JOptionPane.showMessageDialog(null, "No record found.");
}// close if
}//close search method
//======================================UPDATE=============================================
public void updateRecord() throws IOException{
File temp = new File("D:\\temprec.txt");
PrintWriter temp_outfile = new PrintWriter(new PrintWriter(temp));
choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Search mobile network: "));
File orig_outfile = new File("D:\\emprec.txt");
BufferedReader infile = new BufferedReader (new FileReader(orig_outfile));
String line = null;
while((line=infile.readLine())!=null)
{
String strNetworkinfile[] = line.split("/");
if(strNetworkinfile[0].matches(network[choice]))
{
JOptionPane.showMessageDialog(null, strNetworkinfile[1] + " " + strNetworkinfile[2]);
strNumber = JOptionPane.showInputDialog(null,"Input name to edit: ");
strLoad = JOptionPane.showInputDialog(null,"Input age to edit: ");
temp_outfile.println(strNetworkinfile[0] + "/" + strNumber + "/" + strLoad);
temp_outfile.flush();
JOptionPane.showMessageDialog(null, "Successfuly updated!");
intExist = 1;
infile.close();
break;
}//close if
else {
intExist=0;
}//close else
}//close while
if (intExist==0){
temp_outfile.append(line);
temp_outfile.flush();
JOptionPane.showMessageDialog(null, "No record found.");
}// close if
temp_outfile.close();
orig_outfile.delete();
temp.renameTo(orig_outfile);
}//close update
public void deleteRecord()throws IOException
{
File temp = new File ("D:\\temprec.txt");
PrintWriter temp_outfile=new PrintWriter(new PrintWriter(temp));
choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Search mobile network: "));
File orig_outfile=new File ("D:\\emprec.txt");//open the original file
BufferedReader inFile= new BufferedReader (new FileReader(orig_outfile));
String Line=null;
while ((Line=inFile.readLine())!=null){//Alol 2-3. Search and Retrieve record to emprec.txt
String strNetworkinFile[]=Line.split("/");//pasahan sa gi chop nga information
if(strNetworkinFile[0].matches(network[choice])){//Algol 4. Determine
}//if Close
else {
temp_outfile.println(Line); }//Close Else
}//While Close
inFile.close();
temp_outfile.close();
orig_outfile.delete();//Dlete the original file emprec.txt
temp.renameTo(orig_outfile);//Rename temprec.txt to emprec.txt
JOptionPane.showMessageDialog(null,strInputAge+ "Sucessfully Deleted ...");
}//close Delete Method
}
//---------------------close class-----------------------//