我正在尝试创建一个程序,以便于注册客户资料和银行账户,但存款和取款。
我现在的问题是,当索引变量获取错误值时,我无法实现存款。它没有找到代码搜索的值,因此返回-1。
我的问题是,是否有人可以帮助我,告诉我错误在哪里。 我非常擅长使用Java进行编程,因为不希望收到消息会发出嘶哑的评论。我问,因为我想了解更多。所以请提供提示和建议以及解释。
提前致谢=)
Menu.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
import java.util.Scanner;
public class Menu {
int val = 0;
int user = 0;
Scanner user_input = new Scanner(System.in);
public void printMenu() {
while (val != 10) {
System.out.println("----------------------------");
System.out.println("1. Lägg till kund");
System.out.println("2. Redigera kunduppgifter");
System.out.println("3. Ta bort kund");
System.out.println("----------------------------");
System.out.println("4. Nytt konto(befintlig kund)");
System.out.println("5. Ta bort konto");
System.out.println("----------------------------");
System.out.println("6. Visa alla kunder");
System.out.println("----------------------------");
System.out.println("7. Insättning");
System.out.println("8. Uttag");
System.out.println("");
System.out.print("Ditt val: ");
//Fångar upp användarens val
try{
val = user_input.nextInt();
if(val>10){ System.out.println("Meny finns inte"); }
}catch(java.util.InputMismatchException e){
System.out.println("Felaktig inmatning");
}
//Returnerar val
menuAction(val);
}
}
public void menuAction(int val){
BankLogic BL = new BankLogic();
//Switch som bytar metod beroende på val
switch (val) {
case 1:
System.out.print("Ange kundens förnamn: ");
String fName = user_input.next();
System.out.print("Ange kundens efternamn: ");
String lName = user_input.next();
System.out.print("Ange personnummer: ");
long perNR = user_input.nextLong();
System.out.print("Ange startkapital: ");
int deposit = user_input.nextInt();
String name = fName + " " + lName;
BL.addCustomer(name, perNR, deposit);
break;
case 2:
System.out.print("Ange personnummer: ");
long ID = user_input.nextLong();
BL.infoCustomer(ID);
break;
case 3:
System.out.print("Ange kundens personnummer: ");
ID = user_input.nextLong();
BL.removeCustomer(ID);
break;
case 4:
System.out.print("Ange kundens personnummer: ");
ID = user_input.nextLong();
BL.addSavingsAccount(ID);
break;
case 5:
System.out.print("Ange kundens personnummer: ");
ID = user_input.nextLong();
System.out.print("Ange det kontonr som skall tas bort: ");
int ACCNR = user_input.nextInt();
BL.closeAccount(ID, ACCNR);
break;
case 6:
BL.infoBank();
break;
case 7:
System.out.print("Ange kundens personnummer: ");
long pernr = user_input.nextLong();
System.out.print("Ange kundens kontonummer: ");
int acc = user_input.nextInt();
System.out.print("Ange kundens instättningsbelopp: ");
double amount = user_input.nextDouble();
BL.deposit(pernr, acc, amount);
break;
case 8:
System.out.print("Ange kundens personnummer: ");
ID = user_input.nextLong();
System.out.print("Ange kontonr: ");
ACCNR = user_input.nextInt();
System.out.print("Ange belopp: ");
amount = user_input.nextInt();
BL.withdraw(ID, ACCNR, amount);
break;
case 9:
System.out.println("Loggar ut!");
break;
}
}
}
Customer.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
import java.util.ArrayList;
//###### CUSTOMER ######
class Customer{
String name;
long ID;
int accounts;
ArrayList<String> list = new ArrayList<String>();
ArrayList<ArrayList<String>> fullList = new ArrayList<ArrayList<String>>();
public boolean addCustomer(String name, long ID, int deposit){
SavingsAccount SA = new SavingsAccount();
String ACCNR = String.valueOf(SA.generateAccountNr());
String id = String.valueOf(ID);
String Deposit = String.valueOf(deposit);
list.add(id);
list.add(name);
list.add(ACCNR);
list.add(Deposit);
fullList.add(list);
System.out.println("Kund lades till");
return true;
}
public boolean editCustomer(String newName, long ID){
int index = fullList.indexOf(ID);
fullList.get(index).set(3, newName);
return true;
}
public boolean deleteCustomer (long ID){
int index = fullList.indexOf(ID);
fullList.remove(index);
return true;
}
public String showCustomer(long ID){
int index = fullList.indexOf(ID);
System.out.println("Kontonummer \t Name \t Personnummer \t ");
for(ArrayList<String> r: fullList){
System.out.print(r.get(index) + "\t");
}
System.out.print("");
return null;
}
public String showCustomers(){
System.out.println("Kontonummer \t Name \t Personnummer \t ");
for(ArrayList<String> r : fullList){
for(int i = 0; i < r.size(); i++){
System.out.print(r.get(i) + "\t");
}
}
return null;
}
}
SavingsAccount.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
//###### SAVINGACCOUNT ######
class SavingsAccount {
int AccNr = 1000;
double saldo = 0;
int intrest = 2;
Customer CR = new Customer();
public int generateAccountNr(){
AccNr++;
return AccNr;
}
public void newSavingAcc(long ID){
int index = CR.fullList.indexOf(ID);
String ACCNR = String.valueOf(generateAccountNr());
CR.fullList.get(index).add(ACCNR);
}
public void deposit(long ID, int accNr, double amount){
//String id = String.valueOf(ID);
int index = CR.fullList.indexOf("ID");
System.out.print("Detta är INDEX: " + index);
saldo = saldo + amount;
String balance = String.valueOf(saldo);
CR.fullList.get(index).set(2, balance);
}
public void withdraw(long ID, int withdraw){
int index = CR.fullList.indexOf(ID);
String id = String.valueOf(index);
saldo = saldo - withdraw;
String balance = String.valueOf(saldo);
CR.fullList.get(index).set(2, balance);
}
public double intrest (long ID, int saldo){
int index = CR.fullList.indexOf(ID);
double decSaldo = saldo;
decSaldo = decSaldo * (intrest/100);
String stopBalance = String.valueOf(saldo);
CR.fullList.get(index).set(2, stopBalance);
return decSaldo;
}
}
BankLogic
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
//###### BankLogic ######
public class BankLogic {
Customer CR = new Customer();
SavingsAccount SA = new SavingsAccount();
public String infoBank(){
return null;
}
public boolean addCustomer(String name, long pNr, int deposit){
CR.addCustomer(name, pNr, deposit);
CR.showCustomers();
return true;
}
public String infoCustomer(long pNr){
CR.showCustomer(pNr);
return null;
}
public boolean changeCustomerName(String name, long pNr){
CR.editCustomer(name, pNr);
return true;
}
public String removeCustomer(long pNr){
CR.deleteCustomer(pNr);
return null;
}
public int addSavingsAccount(long pNr){
SA.newSavingAcc(pNr);
return 0;
}
public String infoAccount(long pNr, int accountId){
return null;
}
public boolean deposit(long pNr, int accountId, double amount){
SA.deposit(pNr, accountId, amount);
return true;
}
public boolean withdraw(long pNr, int accountId, double amount){
return true;
}
public String closeAccount(long pNr, int accountId){
return null;
}
}
答案 0 :(得分:2)
更改显示
的行int index = CR.fullList.indexOf("ID");
到
int index = CR.fullList.indexOf(ID);
否则,您正在寻找实际的字母“ID”作为标识符。