Can anyone teach me how to wirte this
Many Thanks
Supermarkets sometimes have discounts on multiple purchases. For example, the unitprice for “mango” is $1.89, but buying two of the product will cost $2.50. The discount information is stored with each item in the items array.
For those items that offer a discount for multiple purchases, the discounted price is shown when it applies.
An example is shown below:
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):
==========Bill= =========
mango/$1.89
mango/discounted (2@2.5) $0.61
mango/$1.89
mango/discounted (2@2.5) $0.61
Amount due: $5
Thanks for shopping with us!
--------------------------------------------------------------------------------
For the code i write so far the Bill i only can print the bill like this
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):
==========Bill= =========
mango/$1.89
mango/$1.89
mango/$1.89
mango/$1.89
Amount due: $5
Thanks for shopping with us!
Please Help me. And please give me more detail, cause this is my first year to learn java, i want to learn more and make me more understand~ A lot of thanks
Many Thanks
Supermarkets sometimes have discounts on multiple purchases. For example, the unitprice for “mango” is $1.89, but buying two of the product will cost $2.50. The discount information is stored with each item in the items array.
For those items that offer a discount for multiple purchases, the discounted price is shown when it applies.
An example is shown below:
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):
==========Bill= =========
mango/$1.89
mango/discounted (2@2.5) $0.61
mango/$1.89
mango/discounted (2@2.5) $0.61
Amount due: $5
Thanks for shopping with us!
--------------------------------------------------------------------------------
For the code i write so far the Bill i only can print the bill like this
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):1002
mango/$1.89
Type item code (press enter to finish):
==========Bill= =========
mango/$1.89
mango/$1.89
mango/$1.89
mango/$1.89
Amount due: $5
Thanks for shopping with us!
Please Help me. And please give me more detail, cause this is my first year to learn java, i want to learn more and make me more understand~ A lot of thanks
Code:
.CheckoutProgram.java.
import java.io.*;
import java.text.DecimalFormat;
public class CheckoutProgram {
public void start() {
SalesItem[] items = getStock();
System.out.print("Type item code (press enter to finish):");
String wordIn = Keyboard.readInput();
SalesItem[] goods = new SalesItem[1000];
int count = 0;
while (wordIn.length()>=4 && wordIn.length()<=4){
for (int i=0;i<items.length;i++) {
if (items[i] != null && wordIn.equals(items[i].getItemCode())){
System.out.println(items[i]);
goods[count] = items[i];
}
}
System.out.print("Type item code (press enter to finish):");
wordIn = Keyboard.readInput();
count++;
}
System.out.println();
System.out.println("==========Bill==========");
double amountDue = 0.0;
for (int i=0; i<count; i++){
System.out.println(goods[i]);
amountDue = amountDue + goods[i].getUnitPrice();
}
System.out.println();
System.out.println("Amount due: $" + new DecimalFormat().format(amountDue));
System.out.println("Thanks for shopping with us!");
}
// method to read in "stock.txt" and store the items for sale in an array of type SalesItem
private SalesItem[] getStock(){
SalesItem[] items = new SalesItem[1000];
try {
BufferedReader br = new BufferedReader(new FileReader("stock.txt"));
String theLine;
int count = 0;
while ((theLine = br.readLine()) != null) {
String[] parts = theLine.split(",");
items[count] = new SalesItem(parts[0],parts[1],Double.parseDouble(parts[2]));
if (parts.length==4){
String discount = parts[3];
String numPurchases = discount.substring(0, discount.indexOf("@"));
String price = discount.substring(discount.indexOf("@")+1);
items[count].setNumPurchases(Integer.parseInt(numPurchases));
items[count].setDiscountedPrice(Double.parseDouble(price));
}
count++;
}
}
catch (IOException e) {
System.err.println("Error: " + e);
}
return items;
}
}
..
Code:
..SalesItem.java
import java.text.DecimalFormat;
public class SalesItem {
private String itemCode; //the item code
private String description; // the item description
private double unitPrice; // the item unit price
// An item may offer a discount for multiple purchases
private int numPurchases; //the number of purchases required for receiving the discount
private double discountedPrice; // the discounted price of multiple purchases
// the constructor of the SalesItem class
public SalesItem (String itemCode, String description, double unitPrice){
this.itemCode = itemCode;
this.description = description;
this.unitPrice = unitPrice;
}
// accessor and mutator methods
public String getItemCode(){
return itemCode;
}
public void setItemCode(String itemCode){
this.itemCode = itemCode;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public double getUnitPrice(){
return unitPrice;
}
public void setUnitPrice(double unitPrice){
this.unitPrice = unitPrice;
}
public int getNumPurchases(){
return numPurchases;
}
public void setNumPurchases(int numPurchases){
this.numPurchases = numPurchases;
}
public double getDiscountedPrice(){
return discountedPrice;
}
public void setDiscountedPrice(double discountedPrice){
this.discountedPrice = discountedPrice;
}
// the string representation of a SalesItem object
public String toString(){
return description + "/$" + new DecimalFormat().format(unitPrice);
}
}
..
Code:
..stock.txt 1000,low-fat milk (1 litre),2.15 1001,good-morning cereal,5.60 1002,mango,1.89,2@2.50 1003,Coca-Cola (300 ml),2.5 ..
Comment