Project description:
Use this code:
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Locale;import java.util.Scanner;
/*Item class*/class Item { // Arrays to store data
public int[] itemCode; public String[] itemName; public float[] unitPrice;
Item(){ String[] data = new String[0]; /*Read file*/ data = this.readFile(data);
itemCode = newint[data.length]; itemName = newString[data.length]; unitPrice = new float[data.length];
for(int i = 0;i <data.length;i++){ String[] dataDivided =data.split(","); itemCode =Integer.parseInt(dataDivided[0]); itemName =dataDivided[1]; unitPrice =Float.parseFloat(dataDivided[2]); } }
public String[] readFile(String[] fileData){ try{ File eleFile = newFile("StoringData.txt"); int arraySize = 0 ; String line;
BufferedReader br =new BufferedReader(new FileReader(eleFile)); while((line =br.readLine())!=null){ arraySize++; } br.close();
fileData = newString[arraySize];
br = newBufferedReader(new FileReader(eleFile)); int i = 0; while((line =br.readLine())!=null){ fileData= line; i++; } br.close(); }catch(Exception e){ e.printStackTrace(); } return fileData; }
public String getName(int code){ return itemName[code-1]; }
/*** * * @param code returns price -1 * @return */ public double getPrice(int code){ return unitPrice[code-1]; }
}
/*Driver Class*/public class CashRegister { public static void main(String[] args){ // creating scanner class object Scanner sc = newScanner(System.in); // Variables to store data String[] data = new String[100]; int count = 0; int code; double totalDataCode = 0; double sales = 0;
// Print output System.out.println("Welcome to the cashregister system!"); System.out.println("Beginning a newsale (Y/N)"); String ans = sc.next();
/*User input */ while(ans.toLowerCase(Locale.ROOT).equals("y")) { if(ans.toLowerCase(Locale.ROOT).equals("y")) { do { Item n1 = new Item();
System.out.print("Enter product code: "); String value1 = sc.next();
// Check if input is correct while(value1.charAt(0) < 45 || value1.charAt(0) >57){ System.out.println("!!!Invalid productcode"); System.out.print("Enter product code:"); value1 = sc.next(); } code = Integer.parseInt(value1);
if (code == -1) { continue; }
System.out.println(" item name:" + n1.getName(code)); String itemName = n1.getName(code); double price = n1.getPrice(code);
System.out.print("Enter quantity: "); value1 = sc.next();
while(value1.charAt(0) < 48 || value1.charAt(0) >57){ System.out.println("!!!Invalid quantity:"); System.out.print("Enter quantity code:"); value1 = sc.next(); } int quantity = Integer.parseInt(value1);
double total = quantity * price; System.out.println("Item Total : $ " + total);
/*Add amount*/ totalDataCode = totalDataCode + total; data[count] = quantity + " " + itemName + " $" +String.format("%.2f", total); count++; } while(code != -1);
System.out.println("---------------------------"); System.out.println("Item List :"); for (Stringdatum : data) { if(datum!=null) { System.out.println(datum); } }
/*Printend*/ System.out.println("SubTotal $" + totalDataCode); doubletotalWithTax = totalDataCode + totalDataCode * 6 / 100; System.out.println("Total with tax " + String.format("%.2f",totalWithTax)); System.out.print("Tendered Amount : $ "); doubleamountPay = Double.parseDouble(sc.next());
//Iftendered < total while(true) { if (totalWithTax - amountPay > 0) { System.out.println("Enter tendered amountagain."); amountPay =Double.parseDouble(sc.next()); } else { break; } }
/*Changeamount*/ sales = sales + totalWithTax; doublechange = amountPay > totalWithTax ? amountPay - totalWithTax :0; System.out.println("Change : $" + String.format("%.2f",change)); System.out.println("---------------------------");
System.out.println(); System.out.println();
System.out.println("--------------------------"); System.out.println("Beginning a new sale (Y/N)"); ans =sc.next(); } }
if(ans.toLowerCase(Locale.ROOT).equals("n")){ System.out.println("Totalsales of the day is $ "+String.format("%.2f", sales)); System.out.println("Thanks for using POST System. Goodbye."); } }}
Text file:
1,bottled water,1.502,candy,1.003,chocolate,2.504,gum,1.005,soda,2.506,juice,3.007,popcorn,2.508,donut,1.509,pretzel,2.0010,caramel,1.50
In this program, you are improving the cash register system that you worked with in hw1. You still need to work on the same item data in hw1, but now the item code is changed as shown: item code A001 A002 A003 A004 A005 A006 B001 B002 B003 A007 item name bottled water candy chocolate gum soda juice popcorn donut pretzel caramel A001, bottled water, 1.50 A002, candy, 1.00 unit price 1.50 1.00 2.50 1.00 2.50 3.00 The item code type is String. Item codes that start with B does not have 6% sales tax. The input file is now provided CSV format as shown: 2.50 1.50 2.00 1.50 Provide the item data in a file and read them into your program. Extra features to support in this program are as follows: • Change the item code type to String Provide the input in CSV format. Ask the user to enter the input file name. • Implement exception handling for o File input o Checking wrong input data type o Checking invalid data value o Tendered amount less than the total amount Use ArrayList for the items data. Each element of the array is of the Item type. You need to create a class called Item that has three attributes: item Code, itemName. unitPrice. • Support adding/deleting/modifying item data Name the main class as HW2_LastName.java. The main class should work as a front end driver class. It should not contain any codes used for processing the sales. $ jaxas HW2-LastName.java $ java HW2-LastName.java Welcome to POST system! Your program should work like hw1, with an option to change/add/delete item data. Here shows the working of the program.
Input file: items.txt Beginning a new sale (Y/N) y Enter product code: A002 item name: candy 2 Enter quantity: item total: $ 2.00 Enter product code: 0000 item code A001 A002 A003 A004 A005 A006 B001 B002 B003 A007 item name bottled water candy chocolate gum soda juice Items list: 2 candy 1 gum popcorn donut Enter Product code: A004 item name: gum 1 item total: $ Subtotal pretzel caramel Enter quantity: Enter product code: -1 55 5 $ 2.00 Total with Tax (68) $ Tendered amount $ !!! Not enough. Enter Tendered amount Change Enter quantity: 1.00 $ 1.00 $ 3.00 3.18 $ $ Beginning a new sale (Y/N) Y Enter product code: A003 item name: chocolate Enter quantity: 4 item total: $ 10.00 3 again. 5 1.82 Enter Product code: B002 item name: donut Enter quantity: 2 item total: $ 3.00 Enter product code: A003 item name: chocolate Enter quantity: 1 item total: $ 2.50 Enter product code: A001 item name: bottled water 1 item total: $ 1.50 ;both the upper and lower case should work ; the user input stays right to the message ; the output should be aligned properly ; you enter this value to show the items info unit price 1.50 1.00 2.50 1.00 2.50 3.00 2.50 1.50 2.00 1.50 ; Handle exception ; the output should be properly aligned ; if less amount is entered, ask again
Enter product code: 11 !!! Invalid data type Enter product code: A !!! Invalid product code Enter Product code: B002 item name: donut Enter quantity: 2 !!! Invalid data type Enter quantity: Enter product code: -1 Items list: 1 bottled water $ 1.50 5 chocolate 12.50 6.00 20.00 20.84 40 19.16 2 item total: $ 3.00 $ $ $ Total with Tax (60) $ Tendered amount Change 4 donut Subtotal Beginning a new sale (Y/N) N $ $ item name: energy bar item price: 2.00 Item add successful! The total sale for the day is $ 23.00 Do you want to update the items data? (A/D/M/Q) : A item code: A008 item code A001 A002 A003 A005 A006 Do you want to update the items data? (A/D/M/Q) : D item code: A004 Item delete successful! B001 B002 B003 A007 A008 ; should be handled in a catch block Do you want to update the items data? (A/D/M/Q): M item code: B002 ; should be handled in a catch block item name: donut item price: 2.00 Item modify successful! Do you want to update the items data? (A/D/M/Q) : Q ; should be handled in a catch block item name bottled water candy chocolate ; note that multiple items are listed once ; note that the listed items are sorted by the name soda juice popcorn donut pretzel caramel energy bar unit price 1.50 1.00 2.50 2.50 3.00 2.50 2.00 2.00 1.50 2.00
Thanks for using POST system. Goodbye. In the sample run, the bold face letters are output from the system and non-bold italicized letters are input from the user. The user enters -1' to terminate each current sale. The system is terminated if the user enters 'N' on Beginning a new sale (Y/N). Then, the system prints out the total sale amount of the day, and asks the user to modify the items data. Once the modification is done, the items data are written back to the input file. Your program should compile and run in the IntelliJ IDE, and you need to submit the whole project file. If you use different IDEs, submit all Java files with the package statement removed. To get a full point • Properly layout and indent your code with Javadoc comment format. Comment your code that conforms to the Javadoc convention - class, method, member data. Format your output properly. (You can use the printf function). • In your program header comment, specify whether all the features are fully implemented, as shown: Program features: Change the item code type to String: Y Provide the input in CSV format. Ask the user to enter the input file name: Y Implement exception handling for File input: Y Checking wrong input data type: Y Checking invalid data value: Y Tendered amount less than the total amount: N Use Axxaxhit for the items data: P Adding item data: N Deleting item data: Y Modifying item data: N Note that Y is for full implementation, N means no implementation, and P means partial implementation.
Project description: Use this code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; impor
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am