Java programming... How can I use the same method with the same variables to calculate different sales in one transaction? I mean I made a method that ask for info and calculate the info to make one sale, but I want to use it again to make multiple sales... The thing is that the variable will be store just the last sale so the first sale wont appear in the print screen? What should I do?
Load the individual sales into an array local to that method, then return that array of sales to a print function at the end of the loop.
What exactly are you trying to make? Like a cash register?
yeah exactly..
Gimme a second.
Sorry, had to find an old program. //this function calculates the net sale public void CalcNetsale (int count, double tax[], double sale[], double net_sale[]) { int C; //loop control variable for(C = 0; C < count; C++) net_sale[C] = tax[C] + value[C]; } You could insert a print in the loop if you want.
this is part of the code... that's the code I want to reuse..
//* This class ask for info and calculate the transaction*/ import java.util.Scanner; public class Transaction { //* asking cashier for info*/ public void info(){ String title; double code , price, quantity, dsct, total; Scanner sc = new Scanner(System.in); System.out.println("Enter DVD TITLE: "); title = sc.nextLine(); System.out.println("Enter DVD ID CODE: ID#"); code = sc.nextDouble(); System.out.println("Enter DVD PRICE: $"); price = sc.nextDouble(); System.out.println("Enter DVD QUANTITY: x"); quantity = sc.nextDouble(); //* Finding the Total*/ total = (price * quantity); if ((quantity >= 5 )&(quantity <= 9)){ dsct = (total * 12)/100 ;} else if ((quantity >= 10 )&(quantity <= 14)){ dsct = (total * 17)/100 ;} else if (quantity >= 15 ){dsct = (total * 25)/100 ;} else { dsct =0;} total = total - dsct; System.out.println("TOTAL $" + total); } }
So what do you want to add to it?
Ugh. This site needs [code] tags
I just want to reuse it to make multiple transactions..
Do you know what an array is?
yeah kinda.. ive never used it but I know is a multiple string or something like that
Ok. I assume this is for school. Are you allowed to use one at this point? Some professors take points off if they feel you are trying to "skip ahead."
Well Actually I am studying by my own...
String [] products; products = new String [2]: products[0]= first Sale;
I dont know how to use it in this case...
Ok. Check this out. Everything your doing in that program up there is just allocating some computer memory so you can write stuff down, and then letting the computer do just that. All an array is, is like a group of that memory allocation in a line. You play with fireworks as a kid? You know the string of black-cats? Think of an array like that, only filled with information instead of gunpowder.
You have created this application that calculates a sale. Now instead of dealing with one sale at a time you want it to deal with several, correct?
yeah
Ok. So what we'll do is we will create an array of sales.
What exactly do you want to keep track of? The variable total?
go to the group chat please
//**This is a program for a Case Study on Facebook about a DVD Store Cashier*/ //** Pogram Version 1.0.0*/ //** Jose R. Camilo 7/2/2011*/ //** This method makes a Salutation*/ import java.util.Date; import java.util.Scanner; public class MainSales { public static void main(String args[]){ java.util.Date CDate = new Date(); //Fecha actual del sistema //* Salutation */ System.out.println("Andora Video"); System.out.println(CDate); System.out.println("Welcome"); //*calling "Transaction" class*/ int a = 0; int b= 0; while (a != 1) { System.out.println("New Transcation?"); System.out.println("Yes press 1"); Scanner sc = new Scanner(System.in); a = sc.nextInt(); } if (a == 1){ Transaction TransactionObject = new Transaction(); TransactionObject.info(); System.out.println("Press 1 to add more sales. Press 2 to continue."); Scanner sc = new Scanner(System.in); b = sc.nextInt(); if (b == 1){ // call transaction method again } } } }
import java.util.Scanner; class sales { public static void main(String arg[]) { char ch; String title; double code,price,quantity; do { Scanner sc=new Scanner(System.in); System.out.println("Enter DVD TITLE: "); // taking input info about the DVD title = sc.nextLine(); System.out.println("Enter DVD ID CODE: ID#"); code = sc.nextDouble(); System.out.println("Enter DVD PRICE: $"); price = sc.nextDouble(); System.out.println("Enter DVD QUANTITY: x"); quantity = sc.nextDouble(); salescalc(title,code,price,quantity); // calling the method to calculate the total System.out.print("Do you want more sales Y or N"); // this line asks you if you want to make more sales transaction String str=sc.next();//Inputs String // stores your answer ch=str.charAt(0); // it takes that character you entered } while(ch=='y'); // if your answer is y then it again starts the process inside the do{} stuff } public static void salescalc(String title, double code, double price,double quantity) // method to calculate your total on basis of discount { double total,dsct; //* Finding the Total*/ total = (price * quantity); if (quantity >= 5 &&quantity <= 9) { dsct = (total * 12)/100 ; } else if (quantity >= 10 && quantity <= 14) { dsct = (total * 17)/100 ; } else if (quantity >= 15 ) { dsct = (total * 25)/100 ; } else { dsct =0; } total = total - dsct; System.out.println("TOTAL $" + total+"\n\n\n"); } } // the whole program is running on two methods one is the main() method which calls the salescalc method to calculate your discounted price of your DVD.
i have checked the above code its working and it asks you after every transaction if you want to continue or not
tuhinrawat90 What about the variables? they store all the sales? or just the last one?
they store the last one ,, do you want to store all the sales transaction
Yeah that's my question :)
Alright. The only difference between this program and what you want is this reads from a file instead of being user input. //import decimal formatter import java.text.DecimalFormat; //import scanner object import java.util.Scanner; //import all file processing methods import java.io.*; public class prog4 { public static void main(String[] args) throws IOException { final int DAR = 10; //constant size of array int number[], //the sale order count, //number of sales C; //constant for data array double sale[], //raw value of sale tax[], //tax amount net_sale[], //sale value plus tax change[], // money returned to the customer payment[], //money received from customer total_tax, //total value of tax collected total_net; //total value of net sales String flag[]; //tax status of item sold //create arrays of correct type with constant size flag = new String[DAR]; number = new int[DAR]; sale = new double[DAR]; payment = new double[DAR]; tax = new double[DAR]; net_sale = new double[DAR]; change = new double[DAR]; Trans pos; //a trans object at pos //create decimal format object DecimalFormat formatter = new DecimalFormat("#0.00"); //create a scanner object for file input File input = new File("NCR.data"); //open file for reading Scanner NCR = new Scanner(input); //populate arrays with info from file count = 0; while(NCR.hasNext()) { flag[count] = NCR.next(); number[count] = NCR.nextInt(); sale[count] = NCR.nextDouble(); payment[count] = NCR.nextDouble(); count = count + 1; } //close the file NCR.close(); //pass information to created class pos = new Trans(count,flag,sale,payment); //print heading function PrintHeadings(); //calculatation methods from created class pos.CalcTax(count,flag,sale,tax); pos.CalcNetsale(count,tax,sale,net_sale); pos.CalcChange(count,net_sale,payment,change); //print the information to the screen PrintTable(count,number,sale,tax,net_sale,change); //print a line PrintLine(); //calculate and print the total net sales and total tax total_net = pos.CalcNet(count,net_sale); System.out.println("TOTAL SALES: " + formatter.format(total_net)); total_tax = pos.CalcTaxt(count,tax); System.out.println("TOTAL TAX: " + formatter.format(total_tax)); } //print heading funtion public static void PrintHeadings() { System.out.println(" POS CALC ALGORITHM TEST RUN"); PrintLine(); System.out.print("SALE# NET SALE SALES TAX"); System.out.println(" SALE TOTAL CHANGE"); PrintLine(); } //This function prints the table data. Input parameters are the //count, sale number, net sale, tax, sale total and change. public static void PrintTable(int count, int number[], double sale[], double tax[], double net_sale[], double change[]) { int C; //loop control variable for(C = 0; C < count; C++) { System.out.printf("%3d", number[C]); System.out.printf("%13.2f", sale[C]); System.out.printf("%11.2f", tax[C]); System.out.printf("%14.2f", net_sale[C]); System.out.printf("%12.2f\n", change[C]); } } //This function prints a line public static void PrintLine() { System.out.print("----------------------------"); System.out.println("---------------------------"); } }
is there any limit to the number of transactions you want to store
//This class holds the pos information. //It also calculates tax and net sale. public class Trans { final int DAR = 10; //set private variables of the class private String[] status = new String[DAR]; private int[] num = new int[DAR]; private double[] value = new double[DAR]; private double[] receive = new double[DAR]; //constructor sets private variables to inital value public Trans(int count) { int C; //loop control variable for(C = 0; C < count; C++) { num[C] = 0; value[C] = 0.0; receive[C] = 0.0; } } //constructor assigns the data from the main into //class variables public Trans(int count, String flag[], double sale[], double payment[]) { int C; //loop control variable for(C = 0; C < count; C++) { status[C] = flag[C]; value[C] = sale[C]; receive[C] = payment[C]; } } // calculates the pos information, i.e. tax and net sales public void CalcTax (int count, String flag[], double sale[], double tax[]) { int C; //loop control variable for(C = 0; C < count; C++) if (!status[C].equals ("N")) { tax[C] = value[C]*.09; } else { tax[C] = 0.0; } } //this function calculates the net sale public void CalcNetsale (int count, double tax[], double sale[], double net_sale[]) { int C; //loop control variable for(C = 0; C < count; C++) net_sale[C] = tax[C] + value[C]; } //this function calculates the change returned public void CalcChange (int count, double net_sale[], double payment[], double change[]) { int C; //loop control variable for(C = 0; C < count; C++) change[C] = receive[C]-net_sale[C]; } //calculates the total net sales public double CalcNet(int count, double net_sale[]) { int C; //loop control variable double tot_sale; tot_sale = 0.0; for(C = 0; C < count; C++) tot_sale = tot_sale + net_sale[C]; return tot_sale; } //calculates the total of taxes collected public double CalcTaxt(int count, double tax[]) { int C; //loop control variable double tot_tax; tot_tax = 0.0; for(C = 0; C < count; C++) tot_tax = tot_tax + tax[C]; return tot_tax; } }
This is what you would need to change. At the top in the main, change Scanner NCR = new Scanner(input); to System.out.println("Enter 1. For new sale"); System.out.println("Enter 2. To quit"); Int NCR = new Scanner(keyboard); while(NCR != 2){ Done.
This is a whole homework I did, with a little outline and method breakdown before the actual program: A. Statement To create a program for an internet provider which calculates each customers bill based on their package, as well as the total of all the customer's bills. B. Data Analysis Name Type Content package string the customer's chosen package rate A real the rate of package A rate B real the rate of package B rate C real the rate of package C hours A interger amount of hours in package A hours B interger amount of hours in package B hours C interger amount of hours in package C hours interger the total amount of hours used overage interger the amount over the plan the customer used bill real the customers total bill total real the company's total revenue collected C. Problem Analysis repeat until end command get package get hours calculate customer bill print customer bill at end command calculate total of customer's bills print the results D. Algorithm function main() total = 0 print "Select a package, (A,B,or C): " get package while package does not equal X begin print "Enter the number of hours used: " get hours bill = Calc(package,hours) print "Internet Provided" print "-----------------" print " " print "Package: ", package print "Hours: ", hours print " " print "-----------------" print " " print "Bill: ", bill print " " total = total + bill end print "Total of all bills is $", total Trace Table count package rate hours overage charge bill 0 B 14.95 15 0 0 14.95 1 A 9.95 9 0 0 9.95 2 C 19.95 45 0 0 19.95 */ //import decimal formatter import java.text.DecimalFormat; //import scanner object import java.util.Scanner; public class prog3 { public static void main(String[] args) { int hours; //amount of hours used by the customer double bill, //customer's bill amount total; //total of all customer's bills String mypackage; //customer's chosen package Isp price; //calculation of price as Isp object //create a scanner object for keyboard input Scanner keyboard = new Scanner (System.in); //create decimal format object DecimalFormat formatter = new DecimalFormat ("00.00"); //set the total to zero total = 0.00; //get the customer's package System.out.print("Select a package, (A,B, or C) (Press X to exit):"); mypackage = keyboard.next(); //set sentinel loop stop condition while (mypackage != "X") { //get the customer's hours System.out.print("Enter the number of hours used:"); hours = keyboard.nextInt(); //Create an Isp object price = new Isp(mypackage,hours); //calculate the bill bill = price.CalcBill(); //print the results System.out.println(" Internet Provided "); System.out.println(" ----------------- "); System.out.println(" Package " + mypackage); System.out.println(" Hours " + hours); System.out.println(" ----------------- "); System.out.printf("\n Bill " + formatter.format (bill)); System.out.println(); //add to total total = total + bill; //continue the loop with next package System.out.print("Select a package, (A,B, or C) (Press x to exit):"); mypackage = keyboard.next(); } System.out.printf("\nTotal of all bills is $" + formatter.format (total)); } } /* Algorithm for Object function CalcBill(package,hours) package A rate = 9.95 package B rate = 14.95 package C rate = 19.95 package A hours = 10 package B hours = 20 get package get hours calculate the bill if package = A begin then overage = hours - 10 if overage < 0 begin overage = 0 end bill = overage * 2 + package A rate end else if package = B begin then overage = hours - 20 if overage < 0 begin overage = 0 end bill = overage + package B rate end else bill = package C rate return bill to main */ public class Isp { // declare package rates as constants public static final double PACKA = 9.95; //rate for package A public static final double PACKB = 14.95; //rate for package B public static final double PACKC = 19.95; //rate for package C // declare package hours as constants public static final int PHA = 10; //hours in package A public static final int PHB = 20; //hours in package B // class variables private int cust_hours; //customer's hours used private String cust_package; //customer's chosen package // constructor will assign customer's hours and package public Isp(String mypackage, int hours) //pass customer package and hours { cust_hours = hours; //convert hours for class cust_package = mypackage; //convert package for class } // accessor for GetCusthour public int GetCusthour() { return cust_hours; } // accessor for GetCustpack public String GetCustpack() { return cust_package; } //calculates the customer's bill public double CalcBill() { int overage; //set the amount of hours the customer goes over as int double bill; //set the customer's bill as double //set overage to zero overage = 0; //create formula for package A if(cust_package.equals("A")) { overage = cust_hours - PHA; if(overage <= 0) { overage = 0; } bill = (overage*2) + PACKA; } //create formula for package B else if(cust_package.equals("B")) { overage = cust_hours - PHB; if(overage <= 0) { overage = 0; } bill = overage + PACKB; } //create formula for package C else { bill = PACKC; } return bill; } }
Thanks a lot again for you help man, I really appreciate! :D
No problem. Keep at it. I had never programmed in my life before about a year and a half ago. I'm still in school for it and interning working on this software framework that calculates "numerical relativity that relates to black hole modeling."
Join our real-time social learning platform and learn together with your friends!