java - Control Statements: Why is my 'New Balance' change when I 'charge to credit' then 'pay credit' twice? -


i tried reading start of java book , of exercises summer vacation. came problem: "(credit limit calculator) develop java application determines whether of several department-store customers has exceeded credit limit on charge account.....for customers credit limit exceeded, program should display message "credit limit exceeded"."

my code works if use charge credit once. if use charge credit again, new balance value change. example:

* acc # = 123 * beginning balance = 20000 * credit limit = 25000 * select transaction = 1 (charge credit) * amount charge = 4000 * select transaction = 2 (pay credit) * amount pay = 2000 [my balance should 22000) * select transaction = 1 (charge credit) * amount charge = 10000 [this shud exceed credit limit] * select transaction = 3 (balance inquiry) * new balance 18000 

this problem now. new balance should still 22000 since credit limit has exceeded. 18000. don't know went wrong need help, please.

so here's code:

customer.java

import java.util.scanner;  public class customer {  scanner input = new scanner (system.in);  int accnum,     beginbal,     creditlimit;  int itemscharged,     creditspaid,     newbalance;  int transaction; string action;  public void start (){     system.out.print("\naccount number: ");     accnum = input.nextint();      system.out.print("beginning balance: ");     beginbal = input.nextint();      system.out.print("credit limit: ");     creditlimit = input.nextint();      transaction(); }  public void transaction (){      boolean loop = true;     while (loop){         system.out.println("\n[1] charge credit \n[2] pay credit \n[3] balance inquiry/exit");         system.out.print("select transaction #: ");          transaction = input.nextint();          switch (transaction){         case 1:             system.out.print("\n--charge credit-- \nenter amount: ");             itemscharged = input.nextint();              newbalance = beginbal + itemscharged - creditspaid;             if (newbalance > creditlimit){                 system.err.println("credit limit exceeded!");                 newbalance -= itemscharged;             } else {                 system.out.println("amount charged.");             }             break;         case 2:             system.out.print("\n--pay credit-- \nenter amount: ");             creditspaid = input.nextint();              newbalance = beginbal + itemscharged - creditspaid;             if (creditspaid > newbalance){                 system.err.println("invalid amount!");                 newbalance -= creditspaid;             } else {                 system.out.println("payment posted!");                 newbalance = beginbal + itemscharged - creditspaid;             }             break;         case 3:             system.out.println("\n--balance inquiry-- \nnew balance: " + newbalance);             restart();             break;         default:             system.err.println("invalid number!");             transaction();             break;         }     } }  public void restart (){      system.out.println("\ndo have transaction? [y/n]");     system.out.print("select action: ");      boolean loop = true;     while (loop){          action = input.nextline();          switch (action){         case "y":             start();             break;         case "n":             system.err.println("terminated.");             system.exit(0);             break;         }     } }  } // end class 

creditlimitcal.java

public class creditlimitcal { public static void main (string[] args){      customer cus = new customer();      cus.start();  }  } 

this line:

newbalance = beginbal + itemscharged - creditspaid; 

doesn't need subtract creditspayed, you've done when user paid off part of balance.

newbalance should modified 1 thing each time, case 1:

newbalance = newbalance + itemscharged; 

case 2:

newbalance = newbalance - creditspaid; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -