Я пытаюсь создать программу, облегчающую регистрацию профилей клиентов и банковского счета, но депозиты и снятие средств.объект ориентированный Java, arraylist
Моя проблема в том, что я не могу реализовать депозиты, когда индексная переменная получает неправильное значение. Он не находит значение, которое код ищет и возвращает -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;
}
}
В каком классе вы сталкиваетесь с ошибкой.4 классы, затрудняющие поиск проблемы – Kick
Извините, мой плохой. В SavingsAccount, метод deposit – user2734182
http://stackoverflow.com/help/mcve с акцентом на * минимальный * – alex