以下是我试图编程的大量代码,但还需要什么来完成这项工作我需要最终结果:
使用以下输入编译并运行程序(提供您的程序)a)第一个产品为"自行车喇叭",单价为"7.19",数量为"2",制造商为"Klout"。b) 第二产品为"Forerunner Watch",单价为"140.89",数量为"2",制造商为"Garmin"。
样本输出应该是这样的:
产品单价数量小计Klout自行车喇叭$7.19
2$14.38 Garmin Forerunner手表$140.89 2$281.78
//***************************************************************
// import statements
import java.util.ArrayList;
import java.text.NumberFormat;
import java.util.Scanner;
//Class header
public class ShoppingCart {
// Start of main method
public static <Item> void main(String[] args) {
// Declare and instantiate a variable that is an ArrayList that can hold
// Product objects
ArrayList<Product> item = new ArrayList<Product>();
// Declare necessary local variables here
String Name = null;
double Price = 0;
int Quantity = 0;
String Seller = null;
Scanner scan = new Scanner(System.in);
String Shop = "yes";
// Write a print statement that welcome's the customer to our shop
/**
*
* create a do while that will be keep looping as long as user wants to
* continue shopping
*/
Product item1 = new Product(Name, Price, Quantity, Seller);
// do while loop start
do {
// Ask user to enter product name and store it in appropriate local
// variable
System.out.print("Please Enter the Product Name: ");
Name = scan.next();
// Ask user to enter product price and store it in appropriate local
// variable
System.out.print("Please Enter the Price of the Product: ");
Price = scan.nextDouble();
// Ask user to enter quantity and store it in appropriate local
// variable
System.out.print("Please enter the Quantity: ");
Quantity = scan.nextInt();
// Ask user to enter product manufacturer name and store it in
// appropriate local variable
System.out.print("Please Enter the Manufacturer: ");
Seller = scan.next();
System.out.print("Would you like to continue shopping?");
Shop = scan.next();
// create a new Product object using above inputed values
Product newitem = new Product(Name, Price, Quantity, Seller);
// add above created Product to the ArrayList cart if Product has
// available stock
// if stock not available inform user requested product is out of
// stock
// Ask user whether wants to continue shopping
// set the do while loop to continue to loop if Yes option is
// selected
} while (Shop.equals(Shop));
{
if (Shop == null) {
break;
}
// do while loop end
// header for shopping cart contents
// print details of each product added to the ArrayList
// calculate total price of the shopping cart
// print the total price of the shopping cart
}
}// end of main method
}// end of Shop class
//***************************************************************
//Product.java
//Represents an item in a shopping cart.
//***************************************************************
import java.text.NumberFormat;
public class Product
{
private String name;
private double price;
private int quantity;
private double subtotal;
private String manufacturer;
private int inventory;
// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Product (String name, double price, int quantity, String manufacturer)
{
this.name = name;
this.price = price;
this.quantity = quantity;
this.manufacturer = manufacturer;
subtotal = price*quantity;
inventory = 10;
}
// -------------------------------------------------------
// Return a string with the information about the Product
// -------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
/*
String item;
if (name.length() >= 8)
item = name + "t";
else
item = name + "tt";
*/
return (manufacturer + " " + name + "tt " + fmt.format(price) + "t " + quantity
+ "tt" + fmt.format(subtotal));
}
// Returns the unit price of the Product
public double getPrice()
{
return price;
}
// Returns the name of the Product
public String getName()
{
return name;
}
// Returns the quantity of the Product
public int getQuantity()
{
return quantity;
}
// Returns the sub total of the Product
public double getSubTotal()
{
return subtotal;
}
// Returns product manufacturer
public String getManufacturer()
{
return manufacturer;
}
// Checks whether product is in stock
// if after inventory get below stock after processing order, then
// places order to replenish stock
public boolean checkInventory()
{
boolean flag = false;
if (inventory > quantity)
{
flag = true;
inventory = inventory - quantity;
if (inventory <= 0)
placeOrder(quantity);
}
return flag;
}
// Replenishes stock to 10 times the quantity of last order
private void placeOrder(int orderQuantity)
{
inventory = orderQuantity * 10;
}
}//end of class Item
您在循环的第一次迭代中覆盖了Shop。这意味着商店将永远等于商店,这将打破循环。我假设预期的逻辑是检查覆盖商店的扫描是否等于"是"。如果是,请循环检查Shop.equals("是")。为了将来参考,请在您的问题中包含更多信息,如您做了什么工作,实际问题是什么,并包含所需的类,以便其他人可以轻松运行程序。