java初学者提问,帮帮忙。 设计一个书籍类,描述书籍的书名,作者,出版社,定价,要求(1)提供
java初学者提问,帮帮忙。设计一个书籍类,描述书籍的书名,作者,出版社,定价,要求(1)提供全参的构造方法初始化这些信息(2)提供每个成员变量的getter,sette...
java初学者提问,帮帮忙。
设计一个书籍类,描述书籍的书名,作者,出版社,定价,要求(1)提供全参的构造方法初始化这些信息(2)提供每个成员变量的getter,setter方法(3)重写toString 方法输出书籍的信息。
设计一个购物车类,可以添加书籍,要求控制台最终显示出购物车里的信息和总价。 展开
设计一个书籍类,描述书籍的书名,作者,出版社,定价,要求(1)提供全参的构造方法初始化这些信息(2)提供每个成员变量的getter,setter方法(3)重写toString 方法输出书籍的信息。
设计一个购物车类,可以添加书籍,要求控制台最终显示出购物车里的信息和总价。 展开
展开全部
/**
* 书籍
*/
public class Book {
private String name; //书名
private String author; //作者
private String publisher; //出版社
private Integer price; //价格,分为单位,不使用浮点数防止精度丢失
public Book(String name, String author, String publisher, Integer price) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", price=" + price +
'}';
}
}
import java.util.ArrayList;
import java.util.List;
/**
* 购物车
* Created by admin on 2016/5/17.
*/
public class Cart {
List<Book> books = new ArrayList<>();
/**
* 添加书籍
*
* @param book
*/
public void addBook(Book book) {
books.add(book);
}
/**
* 获取book信息
*
* @return
*/
public String getBookInfo() {
return books.toString();
}
/**
* 获取book总价
*
* @return
*/
public Integer getTotalPrice() {
Integer totalPrice = 0;
if (null != books && books.size() > 0) {
for (Book book : books) {
totalPrice += book.getPrice();
}
}
return totalPrice;
}
public String getTotalInfo() {
return "书籍信息:\r\n" + getBookInfo() + "\r\n" + "总价格:" + getYuanPrice(getTotalPrice()) + "元";
}
/**
* 把分转换成元
*
* @param price
* @return
*/
public static String getYuanPrice(Integer price) {
String strPrice = String.valueOf(price);
while (strPrice.length() < 3) {
strPrice = "0" + strPrice;
}
strPrice = strPrice.substring(0, strPrice.length() - 2) + "." + strPrice.substring(strPrice.length() - 2);
return strPrice;
}
public static void main(String args[]) {
Cart cart = new Cart();
cart.addBook(new Book("书名", "作者", "出版社", 1245));
cart.addBook(new Book("书名1", "作者1", "出版社1", 3458));
cart.addBook(new Book("书名2", "作者2", "出版社2", 7859));
System.out.println(cart.getTotalInfo());
}
}
这是两个类,分别放到Book.java 跟Cart.java两个类文件中,直接执行Cart里的main方法就可以了
输出如下:
展开全部
其实这个代码还是很多缺陷 不严谨 但仅仅提供参考 目的就是让购物车在添加
书本的时候 能输出目前购物车的购书情况以及总价 目前代码并未实现
书的类
public class Book {
private String title; //书名
private String author; //作者
private String publisher; //出版社
private double price; //单价
private int num; //书数量
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Book(){}
public Book(String title,String author,String publisher,double price){
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return "书名:"+title+",作者:"+author+",出版社:"+publisher+",定价:"+price+"";
}
}
//购物车类
public class Cart {
private double totalprice;
private Book book;
private List<Book> books; //每买一本书则存一个book示例
public Cart(){}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public void addBook(String title,String author,String publisher,double price){
Book book=new Book(title,author,publisher,price);
book.setNum(book.getNum()==0?0:book.getNum()+1);
books.add(book);
totalprice+=price*(book.getNum());
}
public double getTotalprice() {
return totalprice;
}
public void setTotalprice(double totalprice) {
this.totalprice = totalprice;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
书本的时候 能输出目前购物车的购书情况以及总价 目前代码并未实现
书的类
public class Book {
private String title; //书名
private String author; //作者
private String publisher; //出版社
private double price; //单价
private int num; //书数量
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Book(){}
public Book(String title,String author,String publisher,double price){
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return "书名:"+title+",作者:"+author+",出版社:"+publisher+",定价:"+price+"";
}
}
//购物车类
public class Cart {
private double totalprice;
private Book book;
private List<Book> books; //每买一本书则存一个book示例
public Cart(){}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public void addBook(String title,String author,String publisher,double price){
Book book=new Book(title,author,publisher,price);
book.setNum(book.getNum()==0?0:book.getNum()+1);
books.add(book);
totalprice+=price*(book.getNum());
}
public double getTotalprice() {
return totalprice;
}
public void setTotalprice(double totalprice) {
this.totalprice = totalprice;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-05-17 · 知道合伙人软件行家
关注
展开全部
设计一个书籍类,描述书籍的书名,作者,出版社,定价,要求(1)提供全参的构造方法初始化这些信息(2)提供每个成员变量的getter,setter方法(3)重写toString 方法输出书籍的信息。设计一个购物车类,可以添加书籍,要求控制台最终显示出购物车里的信息和总价.
2227421573 只要设计一个类吗,还是要写逻辑,写界面呢?
2227421573 只要设计一个类吗,还是要写逻辑,写界面呢?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询