import java.util.Scanner;
public class Item {
private String name;
private double price;
private String code;
public Item(String name, String code) {
this.name = name;
this.code = code;
this.price = 0.0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void display() {
System.out.println("Name: " + name + "; code: " + code + "; Price: " + price + ";");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Item[] bill = new Item[5];
for (int i = 0; i < 5; i++) {
System.out.print("Enter item " + (i+1) + " (name, price, code) ");
String name = input.next();
double price = input.nextDouble();
String code = input.next();
bill[i] = new Item(name, code);
bill[i].setPrice(price);
}
double totalPayment = 0.0;
for (int i = 0; i < 5; i++) {
System.out.println("Item " + (i+1));
bill[i].display();
totalPayment += bill[i].getPrice();
}
System.out.println("Total Payment: " + totalPayment);
}
}
参考GPT:以下是Java的实现,符合题目要求:
import java.util.Scanner;
class Item {
private String name;
private double price;
private String code;
public Item(String name, String code) {
this.name = name;
this.code = code;
this.price = 0.0;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCode() {
return code;
}
public void display() {
System.out.println("Name: " + name + "; code: " + code + "; Price: " + price + ":");
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Item[] bill = new Item[5];
for (int i = 0; i < bill.length; i++) {
System.out.print("Enter item " + (i+1) + " (name, price, code): ");
String name = scanner.next();
double price = scanner.nextDouble();
String code = scanner.next();
bill[i] = new Item(name, code);
bill[i].setPrice(price);
}
double totalPayment = 0.0;
for (int i = 0; i < bill.length; i++) {
System.out.println("Item " + (i+1));
bill[i].display();
totalPayment += bill[i].getPrice();
}
System.out.println("Total Payment: " + totalPayment);
}
}
在命令行或控制台上运行该程序,就可以输入5个条目的名称、价格和代码。然后,程序会将它们添加到一个名为bill的Item数组中,并使用setPrice方法将价格设置为输入的价格。接下来,程序会打印出每个条目的详细信息,并计算总金额。最后,程序将打印出总金额。
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Scanner;
public class Item implements Serializable {
private static final long serialVersionUID = 7884132839647977345L;
private String name;
private BigDecimal price;
private String code;
public Item(String name, String code) {
this.name = name;
this.price = BigDecimal.ZERO;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void display() {
System.out.println(String.format("name: %s; code: %s; price: %.2f;", name, code, price) );
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Item[] bill = new Item[5];
for (int i = 0; i < bill.length; i++) {
System.out.print("Enter item " + (i + 1) + " (name, price, code) ");
String name = scanner.next();
double price = scanner.nextDouble();
String code = scanner.next();
bill[i] = new Item(name, code);
bill[i].setPrice(new BigDecimal(price).setScale(2));
}
for (int i = 0; i < bill.length; i++) {
System.out.println("Item " + (i + 1));
bill[i].display();
}
BigDecimal totalPayment = BigDecimal.ZERO;
for (int i = 0; i < bill.length; i++) {
totalPayment= totalPayment.add(bill[i].getPrice());
}
System.out.println("Total Payment: " + totalPayment.setScale(2));
}
}