![LineItem LineItem]()
本文介绍了零售交易计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我正在完成一项任务,我必须编写一个模拟零售交易商店的程序。我需要三节课。一个LineItem类,在Transaction类和一个TransactionTest来测试程序,这是我到目前为止所做的,但它不工作,继续给出未知元素的错误或不按我的意愿运行。 LineItem类: public 类 LineItem { 私有 int 数量; private double 价格; private String itemName; public LineItem( String itemName, int 数量, double 价格){ this .itemName = itemName; 此 .quantity = quantity; 此 .price = price; } public String getItemName() { return itemName; } public void setItemName(字符串 itemName) { 此 .itemName = itemName; } public int getQuantity() { 返回数量; } public void setQuantity( int 数量){ 此 .quantity = quantity; } public double getPrice() { 返回价格; } public void setPrice(双倍价格) { 此 .price = price; } public double getTotalPrice() { 返回数量*价格; } @Override public String toString(){ return ( this .itemName + \t + qty + 此 .quantity + \t + @ + this .price + \t); } } 交易类: import java.util.ArrayList; public class 交易{ public final ArrayList< LineItem>了LineItem; private int customerID; private String customerName; private String LineItem; private int i; public 事务( int customerID,字符串 customerName){ 此 .customerID = customerID; 此 .customerName = customerName; this .lineItems = new ArrayList<>(); } public int getcustomerID(){ return customerID; } public void setcustomerID( int customerID){ this .customerID = customerID; } public String getcustomerName(){ return customerName; } public void setcustomerName(字符串 customerName){ 此 .customerName = customerName; } public String addLineItem(){ lineItems。 add ( new LineItem( 高露洁牙膏, 2 , 2 。 99 )); lineItems。 add ( new LineItem( Bounty Paper Towels, 1 , 1 。 49 )); lineItems。 add ( new LineItem( Kleenex Tissue, 1 , 2 . 49 )); // System.out.println(lineItems); return ( 找不到colgate); } public String updateItem() { return null ; } public double getTotalPrice(){ double totalPrice = 0 ; for ( int i = 0 ; i< lineItems.size(); i ++){ LineItem item = lineItems。 get (i); totalPrice = totalPrice + item.getTotalPrice(); } return totalPrice; } public String getLineItem(){ return LineItem; } @Override public String toString () { return ( 客户ID: + 此 .customerID + \ n + 客户名称 + 此 .customerName); } } 有什么想法吗? 这里是问题详情: 零售交易编程项目项目要求: 1 。开发一个程序来模拟零售商店的购买交易。这个程序将有两个类,一个是LineItem class ,一个是Transaction class 。 LineItem 类将代表商品的单个订单项,客户 购买。 Transaction class 将合并多个LineItem 对象,并计算的总价格 在交易中。 还有两个测试类,一个用于 LineItem class 和一个 交易类。 2 。设计和构建LineItem 类。此类将具有三个实例变量。 将有一个itemName变量,它将包含行项目的标识(例如 as , 高露洁牙膏);数量变量,用于保存购买物品的数量;和一个价格变量,它将保持项目的零售价格。 LineItem 类应该有一个构造函数,访问者 实例变量,一个计算方法行项目的总价格,更新数量的方法,以及转换 object 到 string 。 使用统一建模语言(UML),类图表类似于此: LineItem - itemName:字符串 - 数量: int - 价格: double + LineItem( String , int , double ) + getName( ):字符串 + getQuantity(): int + getPrice(): double + getTotalPrice(): double + setQuantity( int ) + setPrice( double ) + toString():字符串 a。构造函数将第一个参数分配给实例变量 itemName,第二个参数分配给实例变量quantity,分配给实例变量price。 b。 类将有三个访问器方法-getName(),getQuantity(),和getPrice() - 返回每个相应实例变量的值。 © 2014 Laureate Education,Inc。页面 1 4 c。 类将有两个mutator方法,setQuantity( int )和setPrice( double ),它将分别更新与交易行关联的商品的数量和价格。 d。方法getTotalPrice()处理数量和价格转换为总价 订单项。 e。方法toString()允许访问 object 的状态 a 可打印或可读形成。它将变量转换为单个字符串,其中 格式整齐。 注意:有关转义序列的讨论,请参阅教科书 。这些是可以插入到字符串中的字符, 打印时,将是整齐地格式化显示。用于标签字符的转义序列可以将插入获取表格形式 打印时。此选项卡字符是 \t。 LineItem 类将有一个toString()方法,用于连接 itemName,数量,价格和总价格 - 由tab characters-并返回此 new string 。打印对象时,将隐式调用 toString()方法, 此 case ,将打印 字符串看起来像: 高露洁牙膏数量 2 @ $ 2. 99 $ 5. 98 3 。构建一个事务 class ,它将在单个事务中存储有关购买的商品信息 。它应该包含customerID和 customerName。它还应包含一个ArrayList,用于保存客户 购买 的每个项目的信息交易的一部分。 注意:您必须使用ArrayList,而不是数组。 4 。构建TransactionTest 类以测试应用程序。测试类不应该需要与用户进行任何交互。它应该验证构造函数和的所有方法在事务类中的正确操作。 特定要求 交易等级 1 。 Transaction class 应该有一个带有两个参数的构造函数。第一个 是一个包含客户ID的整数,第二个是 a String 包含客户的名字。 2 。应该有一种方法允许在成绩单中添加一个行项目。 对于 addLineItem方法,的三个参数将是( 1 )项目名称,( 2 )数量,( 3 )单项价格。 3 。应该有一种方法允许在 交易中更新已经的订单项。请注意,更新项目意味着更改数量或价格(或两者)。对于 updateItem方法的参数也是( 1 )项目名称,( 2 )数量,( 3 )单项价格。请注意,更新 2014 Laureate Education,Inc。Page 2 of 4 特定行项目需要搜索ArrayList才能找到所需的项目。无论何时搜索 ,搜索将是不成功的可能性。它 通常很难决定采取什么行动当这样的和 exception。由于异常处理 直到 code-keyword> span> 教科书,为 此项目做出一些任意决定。如果找不到要更新的项目 ,请采取最简单的操作,执行 。不要在屏幕上打印错误消息。只需保持交易不变。 4 。事务 class 需要一个名为getTotalPrice的方法,以 return 该事务的总价格。 5 。还应该有一种方法来返回有关特定订单项的信息。 应返回单个字符串 object 描述的相同格式 LineItem 类: 高露洁牙膏数量 2 @ $ 2. 99 $ 5. 98 同样,搜索的可能性为特定订单项将失败。在此 实例中,您应返回 a 字符串包含类似于此的消息: 未找到高露洁牙膏。 6 。所需的最终方法是一个toString方法。它应返回事务信息 单个字符串 对象。它应使用以下格式: 客户ID: 12345 客户名称:John Doe 高露洁牙膏数量 2 @ $ 2. 99 $ 5. 98 Bounty纸巾数量 1 @ $ 1. 49 $ 1. 49 Kleenex纸巾数量 1 @ $ 2. 49 $ 2. 49 交易总额$ 9. 96 请注意,可以插入换行符 \ n 进入 string 的中间位置。 Ex。 int age = 30 ; 字符串 temp = John Doe \ n是 + age + \ n + 年 old; 输出为: John Doe 30 岁 © 2014 Laureate Education,Inc。页面 3 4 另请注意 \ n 一个字符,实际上可以进入单个字符或 double 引号,具体取决于具体情况。 这里 一个UML图 事务上面描述的 。请注意,可以添加 私有实例变量和方法, as 。对于所有 public 方法,请使用下面给出的名称。 交易 - lineItems:ArrayList< LineItem> - customerID: int - customerName: String + Transaction( int , String ) + addLineItem( String , int , double ) + updateItem( String , int , double ) + getTotalPrice(): double + getLineItem( String ): String + toString(): String © 2014 Laureate Education,Inc。页面 4 4 解决方案 2. 99 5. 98 3 。构建一个事务 class ,它将在单个事务中存储有关购买的商品信息 。它应该包含customerID和 customerName。它还应包含一个ArrayList,用于保存客户 购买 的每个项目的信息交易的一部分。 注意:您必须使用ArrayList,而不是数组。 4 。构建TransactionTest 类以测试应用程序。测试类不应该需要与用户进行任何交互。它应该验证构造函数和的所有方法在事务类中的正确操作。 特定要求 交易等级 1 。 Transaction class 应该有一个带有两个参数的构造函数。第一个 是一个包含客户ID的整数,第二个是 a String 包含客户的名字。 2 。应该有一种方法允许在成绩单中添加一个行项目。 对于 addLineItem方法,的三个参数将是( 1 )项目名称,( 2 )数量,( 3 )单项价格。 3 。应该有一种方法允许在 交易中更新已经的订单项。请注意,更新项目意味着更改数量或价格(或两者)。对于 updateItem方法的参数也是( 1 )项目名称,( 2 )数量,( 3 )单项价格。请注意,更新 2014 Laureate Education,Inc。Page 2 of 4 特定行项目需要搜索ArrayList才能找到所需的项目。无论何时搜索 ,搜索将是不成功的可能性。它 通常很难决定采取什么行动当这样的和 exception。由于异常处理 直到 code-keyword> span> 教科书,为 此项目做出一些任意决定。如果找不到要更新的项目 ,请采取最简单的操作,执行 。不要在屏幕上打印错误消息。只需保持交易不变。 4 。事务 class 需要一个名为getTotalPrice的方法,以 return 该事务的总价格。 5 。还应该有一种方法来返回有关特定订单项的信息。 应返回单个字符串 object 描述的相同格式 LineItem 类: 高露洁牙膏数量 2 @ 2. 99 Hi All,I am working on an assignment, where I have to write a program that emulates a retail transaction store. I need to have three classes. One LineItem class, on Transaction class and one TransactionTest to test the program, here is what I have done so far, but it is not working, keep giving errors of unknown elements or not running as I wish.LineItem Class:public class LineItem { private int quantity; private double price; private String itemName; public LineItem(String itemName, int quantity, double price) { this.itemName = itemName; this.quantity = quantity; this.price = price; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public int getQuantity() { return quantity; } public void setQuantity(int quantity){ this.quantity = quantity; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getTotalPrice() { return quantity * price; } @Override public String toString(){ return(this.itemName + "\t" + "qty" + this.quantity + "\t" + "@" + this.price + "\t" ); } }Transaction Class:import java.util.ArrayList;public class Transaction { public final ArrayList<LineItem> lineItems; private int customerID; private String customerName; private String LineItem; private int i; public Transaction (int customerID, String customerName){ this.customerID = customerID; this.customerName = customerName; this.lineItems = new ArrayList<>(); } public int getcustomerID(){ return customerID; } public void setcustomerID(int customerID){ this.customerID = customerID; } public String getcustomerName(){ return customerName; } public void setcustomerName(String customerName){ this.customerName = customerName; } public String addLineItem(){ lineItems.add(new LineItem("Colgate Toothpaste", 2,2.99)); lineItems.add(new LineItem("Bounty Paper Towels", 1, 1.49)); lineItems.add(new LineItem("Kleenex Tissue", 1, 2.49)); //System.out.println(lineItems); return ("colgate not found"); } public String updateItem(){ return null; } public double getTotalPrice(){ double totalPrice = 0; for (int i =0;i<lineItems.size(); i++){ LineItem item = lineItems.get(i); totalPrice = totalPrice + item.getTotalPrice(); } return totalPrice; } public String getLineItem(){ return LineItem; } @Overridepublic String toString() {return ("Customer ID:" + this.customerID + "\n" + "Customer Name" + this.customerName );}}Any idea? here is the question details:Retail Transaction Programming Project Project Requirements: 1. Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that acustomer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction.There will also be two test classes, one for the LineItem class and one for the Transaction class. 2. Design and build a LineItem class. This class will have three instance variables.There will be an itemName variable that will hold the identification of the line item(such as, "Colgate Toothpaste"); a quantity variable that will hold the quantity ofthe item being purchased; and a price variable that will hold the retail price of theitem. The LineItem class should have a constructor, accessors for the instancevariables, a method to compute the total price for the line item, a method toupdate the quantity, and a method to convert the state of the object to a string.Using Unified Modeling Language (UML), the class diagram looks like this: LineItem - itemName : String - quantity : int - price : double + LineItem( String, int, double ) + getName( ) : String + getQuantity( ) : int + getPrice( ) : double + getTotalPrice( ) : double + setQuantity( int ) + setPrice( double ) + toString( ) : String a. The constructor will assign the first parameter to the instance variableitemName, the second parameter to the instance variable quantity, andthe third parameter to the instance variable price. b. The class will have three accessor methods—getName( ), getQuantity( ),and getPrice( )—that will return the value of each respective instance variable. © 2014 Laureate Education, Inc. Page 1 of 4 c. The class will have two mutator methods, setQuantity( int ) and setPrice(double ), that will update the quantity and price, respectively, of the item associated with the line of the transaction.d. The method getTotalPrice( ) handles the conversion of the quantity and price into a total price for the line item. e. The method toString( ) allows access to the state of the object in a printable or readable form. It converts the variables to a single string thatis neatly formatted. Note: Refer to the textbook for a discussion of escape sequences. These are characters that can be inserted into strings and, when printed, willformat the display neatly. An escape sequence for the tab character canbe inserted to get a tabular form when printing. This tab character is "\t". The LineItem class will have a toString( ) method that concatenatesitemName, quantity, price, and total price—separated by tab characters—and returns this new string. When printing an object, thetoString( ) method will be implicitly called, which in this case, will print a string that will look something like: Colgate Toothpaste qty 2 @ $2.99 $5.98 3. Build a Transaction class that will store information about the items beingpurchased in a single transaction. It should include a customerID andcustomerName. It should also include an ArrayList to hold information abouteach item that the customer is purchasing as part of the transaction. Note: You must use an ArrayList, not an array. 4. Build a TransactionTest class to test the application. The test class should notrequire any interaction with the user. It should verify the correct operation of theconstructor and all methods in the Transaction class. Specific Requirements for the Transaction Class 1. The Transaction class should have a constructor with two parameters. The firstis an integer containing the customer’s ID and the second is a String containingthe customer’s name. 2. There should be a method to allow the addition of a line item to the transcript.The three parameters for the addLineItem method will be (1) the item name, (2)the quantity, and (3) the single item price. 3. There should be a method to allow the updating of a line item already in thetransaction. Notice that updating an item means changing the quantity or price(or both). The parameters for the updateItem method are also (1) the item name,(2) the quantity, and (3) the single item price. Notice that the updating of a © 2014 Laureate Education, Inc. Page 2 of 4 specific line item requires a search through the ArrayList to find the desireditem. Anytime a search is done, the possibility exists that the search will beunsuccessful. It is often difficult to decide what action should be taken when suchan "exception" occurs. Since exception handling is not covered until later in thistextbook, make some arbitrary decisions for this project. If the item to be updated is not found, take the simplest action possible and do nothing. Do not print anerror message to the screen. Simply leave the transaction unchanged. 4. The transaction class needs a method called getTotalPrice to return the totalprice of the transaction. 5. There should also be a method to return information about a specific line item. Itshould return a single String object in the same format described for the LineItem class: Colgate Toothpaste qty 2 @ $2.99 $5.98Again, the possibility exists that the search for a specific line item will fail. In thisinstance, you should return a string containing a message similar to this: Colgate Toothpaste not found. 6. The final method needed is a toString method. It should return the transactioninformation in a single String object. It should use the following format: Customer ID : 12345 Customer Name : John Doe Colgate Toothpaste qty 2 @ $2.99 $5.98 Bounty Paper Towels qty 1 @ $1.49 $1.49 Kleenex Tissue qty 1 @ $2.49 $2.49 Transaction Total $9.96 Notice that a newline character "\n" can be inserted into the middle of a string. Ex. int age = 30; String temp = "John Doe \n is " + age + "\n" + " years old"; The output would be: John Doe is 30 years old © 2014 Laureate Education, Inc. Page 3 of 4 Notice also that "\n" is a single character and could actually go inside single ordouble quotes, depending on the circumstances. Here is a UML diagram for the Transaction class as described above. Notice thatprivate instance variables and methods may be added, as needed. For all public methods use exactly the name given below. Transaction - lineItems : ArrayList<LineItem> - customerID : int - customerName : String + Transaction( int, String ) + addLineItem( String, int, double ) + updateItem( String, int, double ) + getTotalPrice( ) : double + getLineItem( String ) : String + toString( ) : String © 2014 Laureate Education, Inc. Page 4 of 4 解决方案 2.995.98 3. Build a Transaction class that will store information about the items beingpurchased in a single transaction. It should include a customerID andcustomerName. It should also include an ArrayList to hold information abouteach item that the customer is purchasing as part of the transaction. Note: You must use an ArrayList, not an array. 4. Build a TransactionTest class to test the application. The test class should notrequire any interaction with the user. It should verify the correct operation of theconstructor and all methods in the Transaction class. Specific Requirements for the Transaction Class 1. The Transaction class should have a constructor with two parameters. The firstis an integer containing the customer’s ID and the second is a String containingthe customer’s name. 2. There should be a method to allow the addition of a line item to the transcript.The three parameters for the addLineItem method will be (1) the item name, (2)the quantity, and (3) the single item price. 3. There should be a method to allow the updating of a line item already in thetransaction. Notice that updating an item means changing the quantity or price(or both). The parameters for the updateItem method are also (1) the item name,(2) the quantity, and (3) the single item price. Notice that the updating of a © 2014 Laureate Education, Inc. Page 2 of 4 specific line item requires a search through the ArrayList to find the desireditem. Anytime a search is done, the possibility exists that the search will beunsuccessful. It is often difficult to decide what action should be taken when suchan "exception" occurs. Since exception handling is not covered until later in thistextbook, make some arbitrary decisions for this project. If the item to be updated is not found, take the simplest action possible and do nothing. Do not print anerror message to the screen. Simply leave the transaction unchanged. 4. The transaction class needs a method called getTotalPrice to return the totalprice of the transaction. 5. There should also be a method to return information about a specific line item. Itshould return a single String object in the same format described for the LineItem class: Colgate Toothpaste qty 2 @2.99 这篇关于零售交易计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 06:28