我正在编写一个应该像ATM机一样的程序。 Java程序运行并向用户询问帐户的ID(1-10)。输入有效ID后,它会将用户带到一个菜单,其中包含可能的选项列表:
用户输入与他们想要执行的操作相对应的数字。数据存储在.csv文件中,其文件如下:
ID,余额
1100
2100
3100
4100
等
基本上我正在尝试让程序查看我输入的ID,将其与.csv文件中相同数字的行匹配,然后相应地操纵余额。我正在尝试使用扫描仪,但我不知道从哪里开始?我的代码列在下面。
import java.io.File;
import java.util.Date;
import java.util.Scanner;
public class AccountProg {
public static void main(String[] args) {
File accounts = new File("accounts.csv");
Scanner input = new Scanner(System.in);
//Ask for valid ID and check if valid
System.out.println("Enter an ID (1-10): ");
int id = input.nextInt();
while(id < 1 || id > 10) {
System.out.println("Invalid ID. Please input a valid ID: ");
id = input.nextInt();
}
//Create menu
System.out.println("Main Menu");
System.out.println("1: Check Balance");
System.out.println("2: Withdraw");
System.out.println("3: Deposit");
System.out.println("4: Exit");
System.out.println("\nPlease enter a choice: ");
//Ask for valid selection and check if valid
int selection = input.nextInt();
while(selection < 1 || selection > 4) {
System.out.println("Invalid selection. Please input a valid selection: ");
selection = input.nextInt();
}
//Load CSV file into
Scanner inputStream = new Scanner(accounts);
inputStream.useDelimiter(",");
//switch statement for selection
switch (selection) {
case 1:
case 2:
case 3:
case 4:
System.out.println("Exiting...");
break;
default:
break;
}
}
}
class Account {
//I will declare variables for the Account class
private int id = 0;
private double balance = 0;
private Date dateCreated;
//I will create a no arg constructor for a default Account
Account(){
}
//I will pass the values from the main method to the Account class
Account(int id, double balance) {
this.id = id;
this.balance = balance;
dateCreated = new Date();
}
//I will create the accessor methods for id, balance, annualInterestRate, and dateCreated
public int getid() {
return id;
}
public double getBalance() {
return balance;
}
public Date getDateCreated() {
return dateCreated;
}
//I will create the mutator method for id, balance, and annualInterestRate
public void setid(int newId) {
newId = id;
}
public void setBalance(double newBalance) {
newBalance = balance;
}
//I will write a method named withdraw
public void withdraw(double w) {
balance = balance - w;
}
//I will write a method named deposit
public void deposit(double d) {
balance = balance + d;
}
}
答案 0 :(得分:0)
我会使用split
来读取字符串的每个部分而不是另一个Scanner类。
您将使用扫描仪读取文件的每一行,将其拆分并将其保存在ID和余额变量中。
Scanner fileScanner = new Scanner(new File("file.csv"));
fileScanner.nextLine(); // skip the ID,Balance line
while (fileScanner.hasNextLine())
{
String[] content = fileScanner.nextLine().split(",");
if (content.length != 2)
{
System.out.println("Invalid line.");
continue;
}
int ID = Integer.parseInt(content[0]);
int balance = Integer.parseInt(content[1]);
System.out.println("ID: " + ID + " Balance: " + balance);
}
第一个fileScanner.nextLine();
确保您不会解析ID,Balance
行(如果我不在最终文件中,请删除此行。)
使用此输入
ID,Balance
1,100
2,100
3,100
4,100
每个文件行都会得到ID: 1 Balance: 100
。
在ID
和balance
中,您将获得readed值,因此请在需要的地方使用。
这个系统可以用于简单的事情,对于更精细的CSV,您应该使用库(在线搜索Java CSV)
如果要获取用户插入的ID的余额,请将已读ID(ID)与插入的ID(insertedID)进行比较,如果等于你想要的,如果不是继续读取。