定义数组存储3个商品对象。商品的属性:商品的id,名字,价格,库存。创建三个商品对象,并把商品对象存入到数组当中。

package train2;

public class store {
    public String id;
    public String name;
    public double price;
    public int count;


    public store() {
    }

    public store(String id, String name, double price, int count) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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 int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


}
package train2;

public class storeTest {
    public static void main(String[] args) {


        //1.创建一个数组
        store[] arr = new store[3];
        //2.创建三个商品对象
        store s1 = new store("001", "小米", 6999.0, 1500);
        store s2 = new store("002", "大米", 69.0, 50000);
        store s3 = new store("003", "水杯", 99.0, 66000);

        //3.把商品放到数组中
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        //4.遍历
        for (int i = 0; i < arr.length; i++) {
            store Store = arr[i];
            System.out.println(Store.getId() + Store.getName() + Store.getPrice() + Store.getCount()+"\t");

        }

    }


}

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 账户类(满分50 分) 版本1:满分10 分 设计 Account1 类,包含: ■ 一个名为id 的int 类型的私有数据域(默认值为0),长度为6 位。 ■ 一个名为balance 的double 类型的私有数据域(默认值为0)。 ■ 一个名为annualInterestRate 的double 类型的私有数据域存储当前利率(默认值为0)。 假设所有的账户都有相同的利率。 ■ 一个名为dateCreated 的Date 类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定id 和初始余额的构造方法,初始余额不能为负数。 ■ id、balance 和annualInterestRate 的访问器和修改器。 ■ dateCreated 的访问器。 ■ 一个名为getMonthlyInterestRate 的方法返回月利率。 ■ 一个名为withDraw 的方法从账户提取特定金额。 ■ 一个名为deposit 的方法向账户存人特定金额。 ■ double 类型的数据域保留2 位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类ATMMachine1: ■ 创建一个有100 个账户的数组,其id 为0,1,2,...99, 并初始化收支为1000 美元。 ■ 主菜单如下(可参考教材中文版P296 或英文版P367): Main menu 1: check balance 2: withdraw 3: deposit 4: exit 版本2:满分20 分 扩展 Account1 类为Account2 类: ■ Account2 类继承Account1 类。 ■ 为Account2 类新增一个名为password 的String 类型的私有数据域存储账号密码。 password 只能为字母或数字,长度不能小于6 且不能大于10。密码显示时为*******。 ■ 为Account2 类新增一个名为name 的String 类型的私有数据域存储客户名字。 ■ 为Account2 类新增一个名为transactions 的ArrayList 类型的新数据域,其为客户存 储交易记录。这要求新建一个名为Transaction 的类,类的定义请参照教材中文版P327 或英 文版P404。每笔交易都是Transaction 类的一个实例。 ■ 新增一个带初始余额的构造方法,其id 随机产生,但不能与当前系统的id 重复。 若初始余额的参数为负数,则抛出一个自定义异常并在当前构造方法中进行处理。 ■ 重写方法withDraw,要求支取的金额为100 的整数倍,并且当日支取金额不能超过 5000,支取金额不允许透支。每进行一次操作应向transactions 数组线性表添加一笔交易。 ■ 重写方法deposit,要求每进行一次操作应向transactions 数组线性表添加一笔交易。 ■ 新增一个方法changePassword,只有旧密码正确,新密码符合要求,且两次输入相 同的情况下才可以成功修改密码 设计测试类ATMMachine2,其主菜单如下(可参考教材中文版P296 或英文版P367): Main menu 0:create a account 1: check balance 2: withdraw 3: deposit 4:details of the transaction 5: change password 6:exit ■ 若用户选择新建一个账号, 则应提示用户输入账号password 、balance 和 annualInterestRate,其中id 随机产生。新产生的账户应序列化到名为accounts.dat 的文件中。 所有账户只能通过这种方式产生。 ■ 所有用户操作结果应同步到accounts.dat 文件中相应账户中。 ■ 所有用户操作应有友好、简介的提示语。 版本3:满分20 分 请参照银行的ATM 机界面,在Account2 类的基础上开发一个GUI 界面的ATM 系统。 要求界面应模拟小键盘,并且账户信息读、写于文件accounts.dat。
可以首先定义一个学生类,包含学号、姓名和年龄三个属性: ``` public class Student { private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } // 以下是getter和setter方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 然后在主程序中创建一个长度为3的数组,添加三个学生对象,并在添加第四个学生对象时进行学号唯一性判断: ``` public static void main(String[] args) { // 定义一个长度为3的数组,用于存储学生对象 Student[] students = new Student[3]; // 添加三个学生对象 students[0] = new Student(1, "张三", 18); students[1] = new Student(2, "李四", 19); students[2] = new Student(3, "王五", 20); // 添加第四个学生对象 Student s = new Student(4, "赵六", 21); boolean flag = true; // 标记是否已经存在相同学号的学生 for (int i = 0; i < students.length; i++) { if (students[i].getId() == s.getId()) { // 如果学号已经存在 flag = false; break; } } if (flag) { // 如果学号不存在,则添加该学生对象 students[2] = s; } else { // 如果学号已经存在,则输出错误信息 System.out.println("该学号已经存在,无法添加该学生对象!"); } // 遍历所有学生信息 for (int i = 0; i < students.length; i++) { System.out.println("学号:" + students[i].getId() + ",姓名:" + students[i].getName() + ",年龄:" + students[i].getAge()); } } ``` 以上代码可以创建一个长度为3的学生数组,并添加三个学生对象。然后再尝试添加第四个学生对象时,会进行学号唯一性判断,如果学号已经存在,则无法添加该学生对象;如果学号不存在,则添加该学生对象。最后遍历所有学生信息并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值